| | 1 | | using LGDXRobotCloud.API.Exceptions; |
| | 2 | | using LGDXRobotCloud.Data.DbContexts; |
| | 3 | | using LGDXRobotCloud.Data.Models.Business.Automation; |
| | 4 | | using LGDXRobotCloud.Utilities.Helpers; |
| | 5 | | using Microsoft.EntityFrameworkCore; |
| | 6 | |
|
| | 7 | | namespace LGDXRobotCloud.API.Services.Automation; |
| | 8 | |
|
| | 9 | | public interface ITriggerRetryService |
| | 10 | | { |
| | 11 | | Task<(IEnumerable<TriggerRetryListBusinessModel>, PaginationHelper)> GetTriggerRetriesAsync(int pageNumber, int pageSi |
| | 12 | | Task<TriggerRetryBusinessModel> GetTriggerRetryAsync(int triggerRetryId); |
| | 13 | | Task<bool> DeleteTriggerRetryAsync(int triggerRetryId); |
| | 14 | | Task RetryTriggerRetryAsync(int triggerRetryId); |
| | 15 | | } |
| | 16 | |
|
| 0 | 17 | | public class TriggerRetryService ( |
| 0 | 18 | | ITriggerService triggerService, |
| 0 | 19 | | LgdxContext context |
| 0 | 20 | | ) : ITriggerRetryService |
| | 21 | | { |
| 0 | 22 | | private readonly ITriggerService _triggerService = triggerService ?? throw new ArgumentNullException(nameof(triggerSer |
| 0 | 23 | | private readonly LgdxContext _context = context ?? throw new ArgumentNullException(nameof(context)); |
| | 24 | |
|
| | 25 | | public async Task<(IEnumerable<TriggerRetryListBusinessModel>, PaginationHelper)> GetTriggerRetriesAsync(int pageNumbe |
| 0 | 26 | | { |
| 0 | 27 | | var triggerRetries = await _context.TriggerRetries.AsNoTracking() |
| 0 | 28 | | .Include(tr => tr.Trigger) |
| 0 | 29 | | .Include(tr => tr.AutoTask) |
| 0 | 30 | | .AsSplitQuery() |
| 0 | 31 | | .Select(tr => new TriggerRetryListBusinessModel { |
| 0 | 32 | | Id = tr.Id, |
| 0 | 33 | | TriggerId = tr.TriggerId, |
| 0 | 34 | | TriggerName = tr.Trigger.Name, |
| 0 | 35 | | AutoTaskId = tr.AutoTaskId, |
| 0 | 36 | | AutoTaskName = tr.AutoTask.Name, |
| 0 | 37 | | CreatedAt = tr.CreatedAt |
| 0 | 38 | | }) |
| 0 | 39 | | .OrderBy(t => t.Id) |
| 0 | 40 | | .Skip(pageSize * (pageNumber - 1)) |
| 0 | 41 | | .Take(pageSize) |
| 0 | 42 | | .ToListAsync(); |
| 0 | 43 | | var itemCount = triggerRetries.Count; |
| 0 | 44 | | var PaginationHelper = new PaginationHelper(itemCount, pageNumber, pageSize); |
| 0 | 45 | | return (triggerRetries, PaginationHelper); |
| 0 | 46 | | } |
| | 47 | |
|
| | 48 | | public async Task<TriggerRetryBusinessModel> GetTriggerRetryAsync(int triggerRetryId) |
| 0 | 49 | | { |
| 0 | 50 | | return await _context.TriggerRetries.AsNoTracking() |
| 0 | 51 | | .Where(tr => tr.Id == triggerRetryId) |
| 0 | 52 | | .Include(tr => tr.Trigger) |
| 0 | 53 | | .Include(tr => tr.AutoTask) |
| 0 | 54 | | .Select(tr => new TriggerRetryBusinessModel { |
| 0 | 55 | | Id = tr.Id, |
| 0 | 56 | | TriggerId = tr.TriggerId, |
| 0 | 57 | | TriggerName = tr.Trigger.Name, |
| 0 | 58 | | TriggerUrl = tr.Trigger.Url, |
| 0 | 59 | | TriggerHttpMethodId = tr.Trigger.HttpMethodId, |
| 0 | 60 | | AutoTaskId = tr.AutoTaskId, |
| 0 | 61 | | AutoTaskName = tr.AutoTask.Name, |
| 0 | 62 | | Body = tr.Body, |
| 0 | 63 | | CreatedAt = tr.CreatedAt |
| 0 | 64 | | }) |
| 0 | 65 | | .FirstOrDefaultAsync() |
| 0 | 66 | | ?? throw new LgdxNotFound404Exception(); |
| 0 | 67 | | } |
| | 68 | |
|
| | 69 | | public async Task<bool> DeleteTriggerRetryAsync(int triggerRetryId) |
| 0 | 70 | | { |
| 0 | 71 | | return await _context.TriggerRetries.Where(tr => tr.Id == triggerRetryId) |
| 0 | 72 | | .ExecuteDeleteAsync() >= 1; |
| 0 | 73 | | } |
| | 74 | |
|
| | 75 | | public async Task RetryTriggerRetryAsync(int triggerRetryId) |
| 0 | 76 | | { |
| 0 | 77 | | var triggerRetry = await _context.TriggerRetries.AsNoTracking() |
| 0 | 78 | | .Where(tr => tr.Id == triggerRetryId) |
| 0 | 79 | | .FirstOrDefaultAsync() ?? throw new LgdxNotFound404Exception(); |
| | 80 | |
|
| 0 | 81 | | var autoTask = await _context.AutoTasks.AsNoTracking() |
| 0 | 82 | | .Where(at => at.Id == triggerRetry.AutoTaskId) |
| 0 | 83 | | .FirstOrDefaultAsync() ?? throw new LgdxValidation400Expection(nameof(triggerRetry.AutoTaskId), "AutoTask ID is in |
| | 84 | |
|
| 0 | 85 | | var trigger = await _context.Triggers.AsNoTracking() |
| 0 | 86 | | .Where(t => t.Id == triggerRetry.TriggerId) |
| 0 | 87 | | .FirstOrDefaultAsync() ?? throw new LgdxValidation400Expection(nameof(triggerRetry.TriggerId), "Trigger ID is inva |
| | 88 | |
|
| 0 | 89 | | await _triggerService.RetryTriggerAsync(autoTask, trigger, triggerRetry.Body); |
| 0 | 90 | | } |
| | 91 | | } |