| | 1 | | using LGDXRobotCloud.API.Exceptions; |
| | 2 | | using LGDXRobotCloud.Data.DbContexts; |
| | 3 | | using LGDXRobotCloud.Data.Entities; |
| | 4 | | using LGDXRobotCloud.Data.Models.Business.Automation; |
| | 5 | | using LGDXRobotCloud.Utilities.Enums; |
| | 6 | | using LGDXRobotCloud.Utilities.Helpers; |
| | 7 | | using Microsoft.EntityFrameworkCore; |
| | 8 | |
|
| | 9 | | namespace LGDXRobotCloud.API.Services.Automation; |
| | 10 | |
|
| | 11 | | public interface IFlowService |
| | 12 | | { |
| | 13 | | Task<(IEnumerable<FlowListBusinessModel>, PaginationHelper)> GetFlowsAsync(string? name, int pageNumber, int pageSize) |
| | 14 | | Task<FlowBusinessModel> GetFlowAsync(int flowId); |
| | 15 | | Task<FlowBusinessModel> CreateFlowAsync(FlowCreateBusinessModel flowCreateBusinessModel); |
| | 16 | | Task<bool> UpdateFlowAsync(int flowId, FlowUpdateBusinessModel flowUpdateBusinessModel); |
| | 17 | | Task<bool> TestDeleteFlowAsync(int flowId); |
| | 18 | | Task<bool> DeleteFlowAsync(int flowId); |
| | 19 | |
|
| | 20 | | Task<IEnumerable<FlowSearchBusinessModel>> SearchFlowsAsync(string? name); |
| | 21 | | } |
| | 22 | |
|
| 0 | 23 | | public class FlowService(LgdxContext context) : IFlowService |
| | 24 | | { |
| 0 | 25 | | private readonly LgdxContext _context = context ?? throw new ArgumentNullException(nameof(context)); |
| | 26 | |
|
| | 27 | | public async Task<(IEnumerable<FlowListBusinessModel>, PaginationHelper)> GetFlowsAsync(string? name, int pageNumber, |
| 0 | 28 | | { |
| 0 | 29 | | var query = _context.Flows as IQueryable<Flow>; |
| 0 | 30 | | if(!string.IsNullOrWhiteSpace(name)) |
| 0 | 31 | | { |
| 0 | 32 | | name = name.Trim(); |
| 0 | 33 | | query = query.Where(f => f.Name.Contains(name)); |
| 0 | 34 | | } |
| 0 | 35 | | var itemCount = await query.CountAsync(); |
| 0 | 36 | | var PaginationHelper = new PaginationHelper(itemCount, pageNumber, pageSize); |
| 0 | 37 | | var flows = await query.AsNoTracking() |
| 0 | 38 | | .OrderBy(t => t.Id) |
| 0 | 39 | | .Skip(pageSize * (pageNumber - 1)) |
| 0 | 40 | | .Take(pageSize) |
| 0 | 41 | | .Select(t => new FlowListBusinessModel { |
| 0 | 42 | | Id = t.Id, |
| 0 | 43 | | Name = t.Name, |
| 0 | 44 | | }) |
| 0 | 45 | | .ToListAsync(); |
| 0 | 46 | | return (flows, PaginationHelper); |
| 0 | 47 | | } |
| | 48 | |
|
| | 49 | | public async Task<FlowBusinessModel> GetFlowAsync(int flowId) |
| 0 | 50 | | { |
| 0 | 51 | | return await _context.Flows.Where(f => f.Id == flowId) |
| 0 | 52 | | .Include(f => f.FlowDetails |
| 0 | 53 | | .OrderBy(fd => fd.Order)) |
| 0 | 54 | | .ThenInclude(fd => fd.Progress) |
| 0 | 55 | | .Include(f => f.FlowDetails) |
| 0 | 56 | | .ThenInclude(fd => fd.Trigger) |
| 0 | 57 | | .Select(f => new FlowBusinessModel { |
| 0 | 58 | | Id = f.Id, |
| 0 | 59 | | Name = f.Name, |
| 0 | 60 | | FlowDetails = f.FlowDetails.Select(fd => new FlowDetailBusinessModel { |
| 0 | 61 | | Id = fd.Id, |
| 0 | 62 | | Order = fd.Order, |
| 0 | 63 | | ProgressId = fd.Progress.Id, |
| 0 | 64 | | ProgressName = fd.Progress.Name, |
| 0 | 65 | | AutoTaskNextControllerId = fd.AutoTaskNextControllerId, |
| 0 | 66 | | TriggerId = fd.Trigger!.Id, |
| 0 | 67 | | TriggerName = fd.Trigger!.Name, |
| 0 | 68 | | }).OrderBy(fd => fd.Order).ToList(), |
| 0 | 69 | | }) |
| 0 | 70 | | .FirstOrDefaultAsync() |
| 0 | 71 | | ?? throw new LgdxNotFound404Exception(); |
| 0 | 72 | | } |
| | 73 | |
|
| | 74 | | private async Task ValidateFlow(HashSet<int> progressIds, HashSet<int> triggerIds) |
| 0 | 75 | | { |
| 0 | 76 | | var progresses = await _context.Progresses.Where(p => progressIds.Contains(p.Id)).ToDictionaryAsync(p => p.Id); |
| 0 | 77 | | foreach (var progressId in progressIds) |
| 0 | 78 | | { |
| 0 | 79 | | if (progresses.TryGetValue(progressId, out var progress)) |
| 0 | 80 | | { |
| 0 | 81 | | if (progress.Reserved) |
| 0 | 82 | | { |
| 0 | 83 | | throw new LgdxValidation400Expection("Progress", $"The Progress ID: {progressId} is reserved."); |
| | 84 | | } |
| 0 | 85 | | } |
| | 86 | | else |
| 0 | 87 | | { |
| 0 | 88 | | throw new LgdxValidation400Expection("Progress", $"The Progress Id: {progressId} is invalid."); |
| | 89 | | } |
| 0 | 90 | | } |
| 0 | 91 | | var triggers = await _context.Triggers.Where(t => triggerIds.Contains(t.Id)).ToDictionaryAsync(t => t.Id); |
| 0 | 92 | | foreach (var triggerId in triggerIds) |
| 0 | 93 | | { |
| 0 | 94 | | if (!triggers.TryGetValue(triggerId, out var trigger)) |
| 0 | 95 | | { |
| 0 | 96 | | throw new LgdxValidation400Expection("Trigger", $"The Trigger ID: {triggerId} is invalid."); |
| | 97 | | } |
| 0 | 98 | | } |
| 0 | 99 | | } |
| | 100 | |
|
| | 101 | | public async Task<FlowBusinessModel> CreateFlowAsync(FlowCreateBusinessModel flowCreateBusinessModel) |
| 0 | 102 | | { |
| 0 | 103 | | HashSet<int> progressIds = flowCreateBusinessModel.FlowDetails.Select(d => d.ProgressId).ToHashSet(); |
| 0 | 104 | | HashSet<int> triggerIds = flowCreateBusinessModel.FlowDetails.Where(d => d.TriggerId != null).Select(d => d.TriggerI |
| 0 | 105 | | await ValidateFlow(progressIds, triggerIds); |
| 0 | 106 | | var flow = new Flow { |
| 0 | 107 | | Name = flowCreateBusinessModel.Name, |
| 0 | 108 | | FlowDetails = flowCreateBusinessModel.FlowDetails.Select(fd => new FlowDetail { |
| 0 | 109 | | Order = fd.Order, |
| 0 | 110 | | ProgressId = fd.ProgressId, |
| 0 | 111 | | AutoTaskNextControllerId = fd.AutoTaskNextControllerId, |
| 0 | 112 | | TriggerId = fd.TriggerId, |
| 0 | 113 | | }).ToList(), |
| 0 | 114 | | }; |
| 0 | 115 | | await _context.Flows.AddAsync(flow); |
| 0 | 116 | | await _context.SaveChangesAsync(); |
| | 117 | |
|
| 0 | 118 | | return new FlowBusinessModel { |
| 0 | 119 | | Id = flow.Id, |
| 0 | 120 | | Name = flow.Name, |
| 0 | 121 | | FlowDetails = flow.FlowDetails.Select(fd => new FlowDetailBusinessModel { |
| 0 | 122 | | Id = fd.Id, |
| 0 | 123 | | Order = fd.Order, |
| 0 | 124 | | ProgressId = fd.ProgressId, |
| 0 | 125 | | ProgressName = fd.Progress.Name, |
| 0 | 126 | | AutoTaskNextControllerId = fd.AutoTaskNextControllerId, |
| 0 | 127 | | TriggerId = fd.TriggerId, |
| 0 | 128 | | TriggerName = fd.Trigger?.Name, |
| 0 | 129 | | }).ToList(), |
| 0 | 130 | | }; |
| 0 | 131 | | } |
| | 132 | |
|
| | 133 | | public async Task<bool> UpdateFlowAsync(int flowId, FlowUpdateBusinessModel flowUpdateBusinessModel) |
| 0 | 134 | | { |
| 0 | 135 | | var flow = await _context.Flows.Where(f => f.Id == flowId) |
| 0 | 136 | | .Include(f => f.FlowDetails |
| 0 | 137 | | .OrderBy(fd => fd.Order)) |
| 0 | 138 | | .ThenInclude(fd => fd.Progress) |
| 0 | 139 | | .Include(f => f.FlowDetails) |
| 0 | 140 | | .ThenInclude(fd => fd.Trigger) |
| 0 | 141 | | .FirstOrDefaultAsync() |
| 0 | 142 | | ?? throw new LgdxNotFound404Exception(); |
| | 143 | |
|
| 0 | 144 | | HashSet<int?> dbDetailIds = flowUpdateBusinessModel.FlowDetails.Where(d => d.Id != null).Select(d => d.Id).ToHashSet |
| 0 | 145 | | foreach(var dtoDetailId in flowUpdateBusinessModel.FlowDetails.Where(d => d.Id != null).Select(d => d.Id)) |
| 0 | 146 | | { |
| 0 | 147 | | if(!dbDetailIds.Contains((int)dtoDetailId!)) |
| 0 | 148 | | { |
| 0 | 149 | | throw new LgdxValidation400Expection("FlowDetails", $"The Flow Detail ID {(int)dtoDetailId} is belongs to other |
| | 150 | | } |
| 0 | 151 | | } |
| 0 | 152 | | HashSet<int> progressIds = flowUpdateBusinessModel.FlowDetails.Select(d => d.ProgressId).ToHashSet(); |
| 0 | 153 | | HashSet<int> triggerIds = flowUpdateBusinessModel.FlowDetails.Where(d => d.TriggerId != null).Select(d => d.TriggerI |
| 0 | 154 | | await ValidateFlow(progressIds, triggerIds); |
| | 155 | |
|
| 0 | 156 | | flow.Name = flowUpdateBusinessModel.Name; |
| 0 | 157 | | flow.FlowDetails = flowUpdateBusinessModel.FlowDetails.Select(fd => new FlowDetail { |
| 0 | 158 | | Id = (int)fd.Id!, |
| 0 | 159 | | Order = fd.Order, |
| 0 | 160 | | ProgressId = fd.ProgressId, |
| 0 | 161 | | AutoTaskNextControllerId = fd.AutoTaskNextControllerId, |
| 0 | 162 | | TriggerId = fd.TriggerId, |
| 0 | 163 | | }).ToList(); |
| 0 | 164 | | await _context.SaveChangesAsync(); |
| 0 | 165 | | return true; |
| 0 | 166 | | } |
| | 167 | |
|
| | 168 | | public async Task<bool> TestDeleteFlowAsync(int flowId) |
| 0 | 169 | | { |
| 0 | 170 | | var depeendencies = await _context.AutoTasks |
| 0 | 171 | | .Where(a => a.FlowId == flowId) |
| 0 | 172 | | .Where(a => a.CurrentProgressId != (int)ProgressState.Completed && a.CurrentProgressId != (int)ProgressState.Abort |
| 0 | 173 | | .CountAsync(); |
| 0 | 174 | | if (depeendencies > 0) |
| 0 | 175 | | { |
| 0 | 176 | | throw new LgdxValidation400Expection(nameof(flowId), $"This flow has been used by {depeendencies} running/waiting/ |
| | 177 | | } |
| 0 | 178 | | return true; |
| 0 | 179 | | } |
| | 180 | |
|
| | 181 | | public async Task<bool> DeleteFlowAsync(int flowId) |
| 0 | 182 | | { |
| 0 | 183 | | return await _context.Flows.Where(f => f.Id == flowId) |
| 0 | 184 | | .ExecuteDeleteAsync() >= 1; |
| 0 | 185 | | } |
| | 186 | |
|
| | 187 | | public async Task<IEnumerable<FlowSearchBusinessModel>> SearchFlowsAsync(string? name) |
| 0 | 188 | | { |
| 0 | 189 | | var n = name ?? string.Empty; |
| 0 | 190 | | return await _context.Flows.AsNoTracking() |
| 0 | 191 | | .Where(w => w.Name.ToLower().Contains(n.ToLower())) |
| 0 | 192 | | .Take(10) |
| 0 | 193 | | .Select(t => new FlowSearchBusinessModel { |
| 0 | 194 | | Id = t.Id, |
| 0 | 195 | | Name = t.Name, |
| 0 | 196 | | }) |
| 0 | 197 | | .ToListAsync(); |
| 0 | 198 | | } |
| | 199 | | } |