| | 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.Helpers; |
| | 6 | | using Microsoft.EntityFrameworkCore; |
| | 7 | |
|
| | 8 | | namespace LGDXRobotCloud.API.Services.Automation; |
| | 9 | |
|
| | 10 | | public interface IProgressService |
| | 11 | | { |
| | 12 | | Task<(IEnumerable<ProgressBusinessModel>, PaginationHelper)> GetProgressesAsync(string? name, int pageNumber, int page |
| | 13 | | Task<ProgressBusinessModel> GetProgressAsync(int progressId); |
| | 14 | | Task<ProgressBusinessModel> CreateProgressAsync(ProgressCreateBusinessModel progressCreateBusinessModel); |
| | 15 | | Task<bool> UpdateProgressAsync(int progressId, ProgressUpdateBusinessModel progressUpdateBusinessModel); |
| | 16 | | Task<bool> TestDeleteProgressAsync(int progressId); |
| | 17 | | Task<bool> DeleteProgressAsync(int progressId); |
| | 18 | |
|
| | 19 | | Task<IEnumerable<ProgressSearchBusinessModel>> SearchProgressesAsync(string? name, bool reserved = false); |
| | 20 | | } |
| | 21 | |
|
| 0 | 22 | | public class ProgressService(LgdxContext context) : IProgressService |
| | 23 | | { |
| 0 | 24 | | private readonly LgdxContext _context = context ?? throw new ArgumentNullException(nameof(context)); |
| | 25 | |
|
| | 26 | | public async Task<(IEnumerable<ProgressBusinessModel>, PaginationHelper)> GetProgressesAsync(string? name, int pageNum |
| 0 | 27 | | { |
| 0 | 28 | | var query = _context.Progresses as IQueryable<Progress>; |
| 0 | 29 | | if (!string.IsNullOrWhiteSpace(name)) |
| 0 | 30 | | { |
| 0 | 31 | | name = name.Trim(); |
| 0 | 32 | | query = query.Where(t => t.Name.Contains(name)); |
| 0 | 33 | | } |
| 0 | 34 | | query = query.Where(t => t.System == system); |
| 0 | 35 | | var itemCount = await query.CountAsync(); |
| 0 | 36 | | var PaginationHelper = new PaginationHelper(itemCount, pageNumber, pageSize); |
| 0 | 37 | | var progresses = await query.AsNoTracking() |
| 0 | 38 | | .OrderBy(p => p.Id) |
| 0 | 39 | | .Skip(pageSize * (pageNumber - 1)) |
| 0 | 40 | | .Take(pageSize) |
| 0 | 41 | | .Select(p => new ProgressBusinessModel{ |
| 0 | 42 | | Id = p.Id, |
| 0 | 43 | | Name = p.Name, |
| 0 | 44 | | System = p.System, |
| 0 | 45 | | Reserved = p.Reserved, |
| 0 | 46 | | }) |
| 0 | 47 | | .ToListAsync(); |
| 0 | 48 | | return (progresses, PaginationHelper); |
| 0 | 49 | | } |
| | 50 | |
|
| | 51 | | public async Task<ProgressBusinessModel> GetProgressAsync(int progressId) |
| 0 | 52 | | { |
| 0 | 53 | | return await _context.Progresses.AsNoTracking() |
| 0 | 54 | | .Where(p => p.Id == progressId) |
| 0 | 55 | | .Select(p => new ProgressBusinessModel { |
| 0 | 56 | | Id = p.Id, |
| 0 | 57 | | Name = p.Name, |
| 0 | 58 | | System = p.System, |
| 0 | 59 | | Reserved = p.Reserved, |
| 0 | 60 | | }) |
| 0 | 61 | | .FirstOrDefaultAsync() |
| 0 | 62 | | ?? throw new LgdxNotFound404Exception(); |
| 0 | 63 | | } |
| | 64 | |
|
| | 65 | | public async Task<ProgressBusinessModel> CreateProgressAsync(ProgressCreateBusinessModel progressCreateBusinessModel) |
| 0 | 66 | | { |
| 0 | 67 | | var progress = new Progress { |
| 0 | 68 | | Name = progressCreateBusinessModel.Name, |
| 0 | 69 | | }; |
| 0 | 70 | | await _context.Progresses.AddAsync(progress); |
| 0 | 71 | | await _context.SaveChangesAsync(); |
| 0 | 72 | | return new ProgressBusinessModel { |
| 0 | 73 | | Id = progress.Id, |
| 0 | 74 | | Name = progress.Name, |
| 0 | 75 | | System = progress.System, |
| 0 | 76 | | Reserved = progress.Reserved, |
| 0 | 77 | | }; |
| 0 | 78 | | } |
| | 79 | |
|
| | 80 | | public async Task<bool> UpdateProgressAsync(int progressId, ProgressUpdateBusinessModel progressUpdateBusinessModel) |
| 0 | 81 | | { |
| 0 | 82 | | var progress = await _context.Progresses.Where(p => p.Id == progressId) |
| 0 | 83 | | .FirstOrDefaultAsync() |
| 0 | 84 | | ?? throw new LgdxNotFound404Exception(); |
| | 85 | |
|
| 0 | 86 | | if (progress.System) |
| 0 | 87 | | { |
| 0 | 88 | | throw new LgdxValidation400Expection(nameof(progressId), $"Cannot update system progress."); |
| | 89 | | } |
| 0 | 90 | | progress.Name = progressUpdateBusinessModel.Name; |
| 0 | 91 | | return await _context.SaveChangesAsync() >= 1; |
| 0 | 92 | | } |
| | 93 | |
|
| | 94 | | public async Task<bool> TestDeleteProgressAsync(int progressId) |
| 0 | 95 | | { |
| 0 | 96 | | var progress = await _context.Progresses.AsNoTracking() |
| 0 | 97 | | .Where(p => p.Id == progressId) |
| 0 | 98 | | .FirstOrDefaultAsync() |
| 0 | 99 | | ?? throw new LgdxNotFound404Exception(); |
| | 100 | |
|
| 0 | 101 | | if (progress.System) |
| 0 | 102 | | { |
| 0 | 103 | | throw new LgdxValidation400Expection(nameof(progressId), $"Cannot delete system progress."); |
| | 104 | | } |
| | 105 | |
|
| 0 | 106 | | var depeendencies = await _context.FlowDetails.Where(t => t.ProgressId == progressId).CountAsync(); |
| 0 | 107 | | if (depeendencies > 0) |
| 0 | 108 | | { |
| 0 | 109 | | throw new LgdxValidation400Expection(nameof(progressId), $"This progress has been used by {depeendencies} details |
| | 110 | | } |
| | 111 | | // Don't check AutoTasks because it dependes on the Flow/FlowDetails |
| | 112 | |
|
| 0 | 113 | | return true; |
| 0 | 114 | | } |
| | 115 | |
|
| | 116 | |
|
| | 117 | | public async Task<bool> DeleteProgressAsync(int progressId) |
| 0 | 118 | | { |
| 0 | 119 | | var progress = await _context.Progresses.AsNoTracking() |
| 0 | 120 | | .Where(p => p.Id == progressId) |
| 0 | 121 | | .FirstOrDefaultAsync() |
| 0 | 122 | | ?? throw new LgdxNotFound404Exception(); |
| | 123 | |
|
| 0 | 124 | | if (progress.System) |
| 0 | 125 | | { |
| 0 | 126 | | throw new LgdxValidation400Expection(nameof(progressId), $"Cannot delete system progress."); |
| | 127 | | } |
| | 128 | |
|
| 0 | 129 | | return await _context.Progresses.Where(p => p.Id == progressId) |
| 0 | 130 | | .ExecuteDeleteAsync() >= 1; |
| 0 | 131 | | } |
| | 132 | |
|
| | 133 | | public async Task<IEnumerable<ProgressSearchBusinessModel>> SearchProgressesAsync(string? name, bool reserved) |
| 0 | 134 | | { |
| 0 | 135 | | var n = name ?? string.Empty; |
| 0 | 136 | | return await _context.Progresses.AsNoTracking() |
| 0 | 137 | | .Where(t => t.Name.ToLower().Contains(n.ToLower())) |
| 0 | 138 | | .Where(t => t.Reserved == reserved) |
| 0 | 139 | | .Take(10) |
| 0 | 140 | | .Select(t => new ProgressSearchBusinessModel { |
| 0 | 141 | | Id = t.Id, |
| 0 | 142 | | Name = t.Name, |
| 0 | 143 | | }) |
| 0 | 144 | | .ToListAsync(); |
| 0 | 145 | | } |
| | 146 | | } |