| | 1 | | using LGDXRobotCloud.API.Exceptions; |
| | 2 | | using LGDXRobotCloud.API.Services.Administration; |
| | 3 | | using LGDXRobotCloud.Data.DbContexts; |
| | 4 | | using LGDXRobotCloud.Data.Entities; |
| | 5 | | using LGDXRobotCloud.Data.Models.Business.Administration; |
| | 6 | | using LGDXRobotCloud.Data.Models.Business.Automation; |
| | 7 | | using LGDXRobotCloud.Utilities.Enums; |
| | 8 | | using LGDXRobotCloud.Utilities.Helpers; |
| | 9 | | using Microsoft.EntityFrameworkCore; |
| | 10 | |
|
| | 11 | | namespace LGDXRobotCloud.API.Services.Automation; |
| | 12 | |
|
| | 13 | | public interface IProgressService |
| | 14 | | { |
| | 15 | | Task<(IEnumerable<ProgressBusinessModel>, PaginationHelper)> GetProgressesAsync(string? name, int pageNumber, int page |
| | 16 | | Task<ProgressBusinessModel> GetProgressAsync(int progressId); |
| | 17 | | Task<ProgressBusinessModel> CreateProgressAsync(ProgressCreateBusinessModel progressCreateBusinessModel); |
| | 18 | | Task<bool> UpdateProgressAsync(int progressId, ProgressUpdateBusinessModel progressUpdateBusinessModel); |
| | 19 | | Task<bool> TestDeleteProgressAsync(int progressId); |
| | 20 | | Task<bool> DeleteProgressAsync(int progressId); |
| | 21 | |
|
| | 22 | | Task<IEnumerable<ProgressSearchBusinessModel>> SearchProgressesAsync(string? name, bool reserved = false); |
| | 23 | | } |
| | 24 | |
|
| 34 | 25 | | public class ProgressService( |
| 34 | 26 | | IActivityLogService activityLogService, |
| 34 | 27 | | LgdxContext context |
| 34 | 28 | | ) : IProgressService |
| | 29 | | { |
| 34 | 30 | | private readonly IActivityLogService _activityLogService = activityLogService ?? throw new ArgumentNullException(nameo |
| 34 | 31 | | private readonly LgdxContext _context = context ?? throw new ArgumentNullException(nameof(context)); |
| | 32 | |
|
| | 33 | | public async Task<(IEnumerable<ProgressBusinessModel>, PaginationHelper)> GetProgressesAsync(string? name, int pageNum |
| 12 | 34 | | { |
| 12 | 35 | | var query = _context.Progresses as IQueryable<Progress>; |
| 12 | 36 | | if (!string.IsNullOrWhiteSpace(name)) |
| 10 | 37 | | { |
| 10 | 38 | | name = name.Trim(); |
| 10 | 39 | | query = query.Where(t => t.Name.ToLower().Contains(name.ToLower())); |
| 10 | 40 | | } |
| 12 | 41 | | query = query.Where(t => t.System == system); |
| 12 | 42 | | var itemCount = await query.CountAsync(); |
| 12 | 43 | | var PaginationHelper = new PaginationHelper(itemCount, pageNumber, pageSize); |
| 12 | 44 | | var progresses = await query.AsNoTracking() |
| 12 | 45 | | .OrderBy(p => p.Id) |
| 12 | 46 | | .Skip(pageSize * (pageNumber - 1)) |
| 12 | 47 | | .Take(pageSize) |
| 12 | 48 | | .Select(p => new ProgressBusinessModel{ |
| 12 | 49 | | Id = p.Id, |
| 12 | 50 | | Name = p.Name, |
| 12 | 51 | | System = p.System, |
| 12 | 52 | | Reserved = p.Reserved, |
| 12 | 53 | | }) |
| 12 | 54 | | .ToListAsync(); |
| 12 | 55 | | return (progresses, PaginationHelper); |
| 12 | 56 | | } |
| | 57 | |
|
| | 58 | | public async Task<ProgressBusinessModel> GetProgressAsync(int progressId) |
| 2 | 59 | | { |
| 2 | 60 | | return await _context.Progresses.AsNoTracking() |
| 2 | 61 | | .Where(p => p.Id == progressId) |
| 2 | 62 | | .Select(p => new ProgressBusinessModel { |
| 2 | 63 | | Id = p.Id, |
| 2 | 64 | | Name = p.Name, |
| 2 | 65 | | System = p.System, |
| 2 | 66 | | Reserved = p.Reserved, |
| 2 | 67 | | }) |
| 2 | 68 | | .FirstOrDefaultAsync() |
| 2 | 69 | | ?? throw new LgdxNotFound404Exception(); |
| 1 | 70 | | } |
| | 71 | |
|
| | 72 | | public async Task<ProgressBusinessModel> CreateProgressAsync(ProgressCreateBusinessModel progressCreateBusinessModel) |
| 1 | 73 | | { |
| 1 | 74 | | var progress = new Progress { |
| 1 | 75 | | Name = progressCreateBusinessModel.Name, |
| 1 | 76 | | }; |
| 1 | 77 | | await _context.Progresses.AddAsync(progress); |
| 1 | 78 | | await _context.SaveChangesAsync(); |
| | 79 | |
|
| 1 | 80 | | await _activityLogService.CreateActivityLogAsync(new ActivityLogCreateBusinessModel |
| 1 | 81 | | { |
| 1 | 82 | | EntityName = nameof(Progress), |
| 1 | 83 | | EntityId = progress.Id.ToString(), |
| 1 | 84 | | Action = ActivityAction.Create, |
| 1 | 85 | | }); |
| | 86 | |
|
| 1 | 87 | | return new ProgressBusinessModel |
| 1 | 88 | | { |
| 1 | 89 | | Id = progress.Id, |
| 1 | 90 | | Name = progress.Name, |
| 1 | 91 | | System = progress.System, |
| 1 | 92 | | Reserved = progress.Reserved, |
| 1 | 93 | | }; |
| 1 | 94 | | } |
| | 95 | |
|
| | 96 | | public async Task<bool> UpdateProgressAsync(int progressId, ProgressUpdateBusinessModel progressUpdateBusinessModel) |
| 3 | 97 | | { |
| 3 | 98 | | var progress = await _context.Progresses.Where(p => p.Id == progressId) |
| 3 | 99 | | .FirstOrDefaultAsync() |
| 3 | 100 | | ?? throw new LgdxNotFound404Exception(); |
| | 101 | |
|
| 2 | 102 | | if (progress.System) |
| 1 | 103 | | { |
| 1 | 104 | | throw new LgdxValidation400Expection(nameof(progressId), "Cannot update system progress."); |
| | 105 | | } |
| 1 | 106 | | progress.Name = progressUpdateBusinessModel.Name; |
| 1 | 107 | | bool result = await _context.SaveChangesAsync() == 1; |
| | 108 | |
|
| 1 | 109 | | if (result) |
| 1 | 110 | | { |
| 1 | 111 | | await _activityLogService.CreateActivityLogAsync(new ActivityLogCreateBusinessModel |
| 1 | 112 | | { |
| 1 | 113 | | EntityName = nameof(Progress), |
| 1 | 114 | | EntityId = progressId.ToString(), |
| 1 | 115 | | Action = ActivityAction.Update, |
| 1 | 116 | | }); |
| 1 | 117 | | } |
| 1 | 118 | | return result; |
| 1 | 119 | | } |
| | 120 | |
|
| | 121 | | public async Task<bool> TestDeleteProgressAsync(int progressId) |
| 4 | 122 | | { |
| 4 | 123 | | var progress = await _context.Progresses.AsNoTracking() |
| 4 | 124 | | .Where(p => p.Id == progressId) |
| 4 | 125 | | .FirstOrDefaultAsync() |
| 4 | 126 | | ?? throw new LgdxNotFound404Exception(); |
| | 127 | |
|
| 3 | 128 | | if (progress.System) |
| 1 | 129 | | { |
| 1 | 130 | | throw new LgdxValidation400Expection(nameof(progressId), $"Cannot delete system progress."); |
| | 131 | | } |
| | 132 | |
|
| 2 | 133 | | var dependencies = await _context.FlowDetails.Where(t => t.ProgressId == progressId).CountAsync(); |
| 2 | 134 | | if (dependencies > 0) |
| 1 | 135 | | { |
| 1 | 136 | | throw new LgdxValidation400Expection(nameof(progressId), $"This progress has been used by {dependencies} details i |
| | 137 | | } |
| | 138 | | // Don't check AutoTasks because it dependes on the Flow/FlowDetails |
| | 139 | |
|
| 1 | 140 | | return true; |
| 1 | 141 | | } |
| | 142 | |
|
| | 143 | |
|
| | 144 | | public async Task<bool> DeleteProgressAsync(int progressId) |
| 0 | 145 | | { |
| 0 | 146 | | var progress = await _context.Progresses.AsNoTracking() |
| 0 | 147 | | .Where(p => p.Id == progressId) |
| 0 | 148 | | .FirstOrDefaultAsync() |
| 0 | 149 | | ?? throw new LgdxNotFound404Exception(); |
| | 150 | |
|
| 0 | 151 | | if (progress.System) |
| 0 | 152 | | { |
| 0 | 153 | | throw new LgdxValidation400Expection(nameof(progressId), $"Cannot delete system progress."); |
| | 154 | | } |
| | 155 | |
|
| 0 | 156 | | bool result = await _context.Progresses.Where(p => p.Id == progressId) |
| 0 | 157 | | .ExecuteDeleteAsync() == 1; |
| | 158 | |
|
| 0 | 159 | | if (result) |
| 0 | 160 | | { |
| 0 | 161 | | await _activityLogService.CreateActivityLogAsync(new ActivityLogCreateBusinessModel |
| 0 | 162 | | { |
| 0 | 163 | | EntityName = nameof(Progress), |
| 0 | 164 | | EntityId = progressId.ToString(), |
| 0 | 165 | | Action = ActivityAction.Delete, |
| 0 | 166 | | }); |
| 0 | 167 | | } |
| 0 | 168 | | return result; |
| 0 | 169 | | } |
| | 170 | |
|
| | 171 | | public async Task<IEnumerable<ProgressSearchBusinessModel>> SearchProgressesAsync(string? name, bool reserved) |
| 12 | 172 | | { |
| 12 | 173 | | var n = name ?? string.Empty; |
| 12 | 174 | | return await _context.Progresses.AsNoTracking() |
| 12 | 175 | | .Where(t => t.Name.ToLower().Contains(n.ToLower())) |
| 12 | 176 | | .Where(t => t.Reserved == reserved) |
| 12 | 177 | | .Take(10) |
| 12 | 178 | | .Select(t => new ProgressSearchBusinessModel { |
| 12 | 179 | | Id = t.Id, |
| 12 | 180 | | Name = t.Name, |
| 12 | 181 | | }) |
| 12 | 182 | | .ToListAsync(); |
| 12 | 183 | | } |
| | 184 | | } |