< 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
0%
Covered lines: 0
Uncovered lines: 97
Coverable lines: 97
Total lines: 146
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 22
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)0%620%
GetProgressesAsync()0%620%
GetProgressAsync()0%620%
CreateProgressAsync()100%210%
UpdateProgressAsync()0%2040%
TestDeleteProgressAsync()0%4260%
DeleteProgressAsync()0%2040%
SearchProgressesAsync()0%620%

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
 022public class ProgressService(LgdxContext context) : IProgressService
 23{
 024  private readonly LgdxContext _context = context ?? throw new ArgumentNullException(nameof(context));
 25
 26  public async Task<(IEnumerable<ProgressBusinessModel>, PaginationHelper)> GetProgressesAsync(string? name, int pageNum
 027  {
 028    var query = _context.Progresses as IQueryable<Progress>;
 029    if (!string.IsNullOrWhiteSpace(name))
 030    {
 031      name = name.Trim();
 032      query = query.Where(t => t.Name.Contains(name));
 033    }
 034    query = query.Where(t => t.System == system);
 035    var itemCount = await query.CountAsync();
 036    var PaginationHelper = new PaginationHelper(itemCount, pageNumber, pageSize);
 037    var progresses = await query.AsNoTracking()
 038      .OrderBy(p => p.Id)
 039      .Skip(pageSize * (pageNumber - 1))
 040      .Take(pageSize)
 041      .Select(p => new ProgressBusinessModel{
 042        Id = p.Id,
 043        Name = p.Name,
 044        System = p.System,
 045        Reserved = p.Reserved,
 046      })
 047      .ToListAsync();
 048    return (progresses, PaginationHelper);
 049  }
 50
 51  public async Task<ProgressBusinessModel> GetProgressAsync(int progressId)
 052  {
 053    return await _context.Progresses.AsNoTracking()
 054      .Where(p => p.Id == progressId)
 055      .Select(p => new ProgressBusinessModel {
 056        Id = p.Id,
 057        Name = p.Name,
 058        System = p.System,
 059        Reserved = p.Reserved,
 060      })
 061      .FirstOrDefaultAsync()
 062        ?? throw new LgdxNotFound404Exception();
 063  }
 64
 65  public async Task<ProgressBusinessModel> CreateProgressAsync(ProgressCreateBusinessModel progressCreateBusinessModel)
 066  {
 067    var progress = new Progress {
 068      Name = progressCreateBusinessModel.Name,
 069    };
 070    await _context.Progresses.AddAsync(progress);
 071    await _context.SaveChangesAsync();
 072    return new ProgressBusinessModel {
 073      Id = progress.Id,
 074      Name = progress.Name,
 075      System = progress.System,
 076      Reserved = progress.Reserved,
 077    };
 078  }
 79
 80  public async Task<bool> UpdateProgressAsync(int progressId, ProgressUpdateBusinessModel progressUpdateBusinessModel)
 081  {
 082    var progress = await _context.Progresses.Where(p => p.Id == progressId)
 083      .FirstOrDefaultAsync()
 084        ?? throw new LgdxNotFound404Exception();
 85
 086    if (progress.System)
 087    {
 088      throw new LgdxValidation400Expection(nameof(progressId), $"Cannot update system progress.");
 89    }
 090    progress.Name = progressUpdateBusinessModel.Name;
 091    return await _context.SaveChangesAsync() >= 1;
 092  }
 93
 94  public async Task<bool> TestDeleteProgressAsync(int progressId)
 095  {
 096    var progress = await _context.Progresses.AsNoTracking()
 097      .Where(p => p.Id == progressId)
 098      .FirstOrDefaultAsync()
 099        ?? throw new LgdxNotFound404Exception();
 100
 0101    if (progress.System)
 0102    {
 0103      throw new LgdxValidation400Expection(nameof(progressId), $"Cannot delete system progress.");
 104    }
 105
 0106    var depeendencies = await _context.FlowDetails.Where(t => t.ProgressId == progressId).CountAsync();
 0107    if (depeendencies > 0)
 0108    {
 0109      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
 0113    return true;
 0114  }
 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)
 0134  {
 0135    var n = name ?? string.Empty;
 0136    return await _context.Progresses.AsNoTracking()
 0137      .Where(t => t.Name.ToLower().Contains(n.ToLower()))
 0138      .Where(t => t.Reserved == reserved)
 0139      .Take(10)
 0140      .Select(t => new ProgressSearchBusinessModel {
 0141        Id = t.Id,
 0142        Name = t.Name,
 0143      })
 0144      .ToListAsync();
 0145  }
 146}