< Summary

Information
Class: LGDXRobotCloud.API.Services.Automation.ProgressService
Assembly: LGDXRobotCloud.API
File(s): /builds/yukaitung/lgdxrobot2-cloud/LGDXRobotCloud.API/Services/Automation/ProgressService.cs
Line coverage
88%
Covered lines: 86
Uncovered lines: 11
Coverable lines: 97
Total lines: 146
Line coverage: 88.6%
Branch coverage
72%
Covered branches: 16
Total branches: 22
Branch coverage: 72.7%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)50%22100%
GetProgressesAsync()100%22100%
GetProgressAsync()100%22100%
CreateProgressAsync()100%11100%
UpdateProgressAsync()100%44100%
TestDeleteProgressAsync()100%66100%
DeleteProgressAsync()0%2040%
SearchProgressesAsync()50%22100%

File(s)

/builds/yukaitung/lgdxrobot2-cloud/LGDXRobotCloud.API/Services/Automation/ProgressService.cs

#LineLine coverage
 1using LGDXRobotCloud.API.Exceptions;
 2using LGDXRobotCloud.Data.DbContexts;
 3using LGDXRobotCloud.Data.Entities;
 4using LGDXRobotCloud.Data.Models.Business.Automation;
 5using LGDXRobotCloud.Utilities.Helpers;
 6using Microsoft.EntityFrameworkCore;
 7
 8namespace LGDXRobotCloud.API.Services.Automation;
 9
 10public 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
 3422public class ProgressService(LgdxContext context) : IProgressService
 23{
 3424  private readonly LgdxContext _context = context ?? throw new ArgumentNullException(nameof(context));
 25
 26  public async Task<(IEnumerable<ProgressBusinessModel>, PaginationHelper)> GetProgressesAsync(string? name, int pageNum
 1227  {
 1228    var query = _context.Progresses as IQueryable<Progress>;
 1229    if (!string.IsNullOrWhiteSpace(name))
 1030    {
 1031      name = name.Trim();
 1032      query = query.Where(t => t.Name.ToLower().Contains(name.ToLower()));
 1033    }
 1234    query = query.Where(t => t.System == system);
 1235    var itemCount = await query.CountAsync();
 1236    var PaginationHelper = new PaginationHelper(itemCount, pageNumber, pageSize);
 1237    var progresses = await query.AsNoTracking()
 1238      .OrderBy(p => p.Id)
 1239      .Skip(pageSize * (pageNumber - 1))
 1240      .Take(pageSize)
 1241      .Select(p => new ProgressBusinessModel{
 1242        Id = p.Id,
 1243        Name = p.Name,
 1244        System = p.System,
 1245        Reserved = p.Reserved,
 1246      })
 1247      .ToListAsync();
 1248    return (progresses, PaginationHelper);
 1249  }
 50
 51  public async Task<ProgressBusinessModel> GetProgressAsync(int progressId)
 252  {
 253    return await _context.Progresses.AsNoTracking()
 254      .Where(p => p.Id == progressId)
 255      .Select(p => new ProgressBusinessModel {
 256        Id = p.Id,
 257        Name = p.Name,
 258        System = p.System,
 259        Reserved = p.Reserved,
 260      })
 261      .FirstOrDefaultAsync()
 262        ?? throw new LgdxNotFound404Exception();
 163  }
 64
 65  public async Task<ProgressBusinessModel> CreateProgressAsync(ProgressCreateBusinessModel progressCreateBusinessModel)
 166  {
 167    var progress = new Progress {
 168      Name = progressCreateBusinessModel.Name,
 169    };
 170    await _context.Progresses.AddAsync(progress);
 171    await _context.SaveChangesAsync();
 172    return new ProgressBusinessModel {
 173      Id = progress.Id,
 174      Name = progress.Name,
 175      System = progress.System,
 176      Reserved = progress.Reserved,
 177    };
 178  }
 79
 80  public async Task<bool> UpdateProgressAsync(int progressId, ProgressUpdateBusinessModel progressUpdateBusinessModel)
 381  {
 382    var progress = await _context.Progresses.Where(p => p.Id == progressId)
 383      .FirstOrDefaultAsync()
 384        ?? throw new LgdxNotFound404Exception();
 85
 286    if (progress.System)
 187    {
 188      throw new LgdxValidation400Expection(nameof(progressId), "Cannot update system progress.");
 89    }
 190    progress.Name = progressUpdateBusinessModel.Name;
 191    return await _context.SaveChangesAsync() >= 1;
 192  }
 93
 94  public async Task<bool> TestDeleteProgressAsync(int progressId)
 495  {
 496    var progress = await _context.Progresses.AsNoTracking()
 497      .Where(p => p.Id == progressId)
 498      .FirstOrDefaultAsync()
 499        ?? throw new LgdxNotFound404Exception();
 100
 3101    if (progress.System)
 1102    {
 1103      throw new LgdxValidation400Expection(nameof(progressId), $"Cannot delete system progress.");
 104    }
 105
 2106    var depeendencies = await _context.FlowDetails.Where(t => t.ProgressId == progressId).CountAsync();
 2107    if (depeendencies > 0)
 1108    {
 1109      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
 1113    return true;
 1114  }
 115
 116
 117  public async Task<bool> DeleteProgressAsync(int progressId)
 0118  {
 0119    var progress = await _context.Progresses.AsNoTracking()
 0120      .Where(p => p.Id == progressId)
 0121      .FirstOrDefaultAsync()
 0122        ?? throw new LgdxNotFound404Exception();
 123
 0124    if (progress.System)
 0125    {
 0126      throw new LgdxValidation400Expection(nameof(progressId), $"Cannot delete system progress.");
 127    }
 128
 0129    return await _context.Progresses.Where(p => p.Id == progressId)
 0130      .ExecuteDeleteAsync() >= 1;
 0131  }
 132
 133  public async Task<IEnumerable<ProgressSearchBusinessModel>> SearchProgressesAsync(string? name, bool reserved)
 12134  {
 12135    var n = name ?? string.Empty;
 12136    return await _context.Progresses.AsNoTracking()
 12137      .Where(t => t.Name.ToLower().Contains(n.ToLower()))
 12138      .Where(t => t.Reserved == reserved)
 12139      .Take(10)
 12140      .Select(t => new ProgressSearchBusinessModel {
 12141        Id = t.Id,
 12142        Name = t.Name,
 12143      })
 12144      .ToListAsync();
 12145  }
 146}