< 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
83%
Covered lines: 107
Uncovered lines: 21
Coverable lines: 128
Total lines: 184
Line coverage: 83.5%
Branch coverage
67%
Covered branches: 19
Total branches: 28
Branch coverage: 67.8%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

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

File(s)

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

#LineLine coverage
 1using LGDXRobotCloud.API.Exceptions;
 2using LGDXRobotCloud.API.Services.Administration;
 3using LGDXRobotCloud.Data.DbContexts;
 4using LGDXRobotCloud.Data.Entities;
 5using LGDXRobotCloud.Data.Models.Business.Administration;
 6using LGDXRobotCloud.Data.Models.Business.Automation;
 7using LGDXRobotCloud.Utilities.Enums;
 8using LGDXRobotCloud.Utilities.Helpers;
 9using Microsoft.EntityFrameworkCore;
 10
 11namespace LGDXRobotCloud.API.Services.Automation;
 12
 13public 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
 3425public class ProgressService(
 3426    IActivityLogService activityLogService,
 3427    LgdxContext context
 3428  ) : IProgressService
 29{
 3430  private readonly IActivityLogService _activityLogService = activityLogService ?? throw new ArgumentNullException(nameo
 3431  private readonly LgdxContext _context = context ?? throw new ArgumentNullException(nameof(context));
 32
 33  public async Task<(IEnumerable<ProgressBusinessModel>, PaginationHelper)> GetProgressesAsync(string? name, int pageNum
 1234  {
 1235    var query = _context.Progresses as IQueryable<Progress>;
 1236    if (!string.IsNullOrWhiteSpace(name))
 1037    {
 1038      name = name.Trim();
 1039      query = query.Where(t => t.Name.ToLower().Contains(name.ToLower()));
 1040    }
 1241    query = query.Where(t => t.System == system);
 1242    var itemCount = await query.CountAsync();
 1243    var PaginationHelper = new PaginationHelper(itemCount, pageNumber, pageSize);
 1244    var progresses = await query.AsNoTracking()
 1245      .OrderBy(p => p.Id)
 1246      .Skip(pageSize * (pageNumber - 1))
 1247      .Take(pageSize)
 1248      .Select(p => new ProgressBusinessModel{
 1249        Id = p.Id,
 1250        Name = p.Name,
 1251        System = p.System,
 1252        Reserved = p.Reserved,
 1253      })
 1254      .ToListAsync();
 1255    return (progresses, PaginationHelper);
 1256  }
 57
 58  public async Task<ProgressBusinessModel> GetProgressAsync(int progressId)
 259  {
 260    return await _context.Progresses.AsNoTracking()
 261      .Where(p => p.Id == progressId)
 262      .Select(p => new ProgressBusinessModel {
 263        Id = p.Id,
 264        Name = p.Name,
 265        System = p.System,
 266        Reserved = p.Reserved,
 267      })
 268      .FirstOrDefaultAsync()
 269        ?? throw new LgdxNotFound404Exception();
 170  }
 71
 72  public async Task<ProgressBusinessModel> CreateProgressAsync(ProgressCreateBusinessModel progressCreateBusinessModel)
 173  {
 174    var progress = new Progress {
 175      Name = progressCreateBusinessModel.Name,
 176    };
 177    await _context.Progresses.AddAsync(progress);
 178    await _context.SaveChangesAsync();
 79
 180    await _activityLogService.CreateActivityLogAsync(new ActivityLogCreateBusinessModel
 181    {
 182      EntityName = nameof(Progress),
 183      EntityId = progress.Id.ToString(),
 184      Action = ActivityAction.Create,
 185    });
 86
 187    return new ProgressBusinessModel
 188    {
 189      Id = progress.Id,
 190      Name = progress.Name,
 191      System = progress.System,
 192      Reserved = progress.Reserved,
 193    };
 194  }
 95
 96  public async Task<bool> UpdateProgressAsync(int progressId, ProgressUpdateBusinessModel progressUpdateBusinessModel)
 397  {
 398    var progress = await _context.Progresses.Where(p => p.Id == progressId)
 399      .FirstOrDefaultAsync()
 3100        ?? throw new LgdxNotFound404Exception();
 101
 2102    if (progress.System)
 1103    {
 1104      throw new LgdxValidation400Expection(nameof(progressId), "Cannot update system progress.");
 105    }
 1106    progress.Name = progressUpdateBusinessModel.Name;
 1107    bool result = await _context.SaveChangesAsync() == 1;
 108
 1109    if (result)
 1110    {
 1111      await _activityLogService.CreateActivityLogAsync(new ActivityLogCreateBusinessModel
 1112      {
 1113        EntityName = nameof(Progress),
 1114        EntityId = progressId.ToString(),
 1115        Action = ActivityAction.Update,
 1116      });
 1117    }
 1118    return result;
 1119  }
 120
 121  public async Task<bool> TestDeleteProgressAsync(int progressId)
 4122  {
 4123    var progress = await _context.Progresses.AsNoTracking()
 4124      .Where(p => p.Id == progressId)
 4125      .FirstOrDefaultAsync()
 4126        ?? throw new LgdxNotFound404Exception();
 127
 3128    if (progress.System)
 1129    {
 1130      throw new LgdxValidation400Expection(nameof(progressId), $"Cannot delete system progress.");
 131    }
 132
 2133    var dependencies = await _context.FlowDetails.Where(t => t.ProgressId == progressId).CountAsync();
 2134    if (dependencies > 0)
 1135    {
 1136      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
 1140    return true;
 1141  }
 142
 143
 144  public async Task<bool> DeleteProgressAsync(int progressId)
 0145  {
 0146    var progress = await _context.Progresses.AsNoTracking()
 0147      .Where(p => p.Id == progressId)
 0148      .FirstOrDefaultAsync()
 0149        ?? throw new LgdxNotFound404Exception();
 150
 0151    if (progress.System)
 0152    {
 0153      throw new LgdxValidation400Expection(nameof(progressId), $"Cannot delete system progress.");
 154    }
 155
 0156    bool result = await _context.Progresses.Where(p => p.Id == progressId)
 0157      .ExecuteDeleteAsync() == 1;
 158
 0159    if (result)
 0160    {
 0161      await _activityLogService.CreateActivityLogAsync(new ActivityLogCreateBusinessModel
 0162      {
 0163        EntityName = nameof(Progress),
 0164        EntityId = progressId.ToString(),
 0165        Action = ActivityAction.Delete,
 0166      });
 0167    }
 0168    return result;
 0169  }
 170
 171  public async Task<IEnumerable<ProgressSearchBusinessModel>> SearchProgressesAsync(string? name, bool reserved)
 12172  {
 12173    var n = name ?? string.Empty;
 12174    return await _context.Progresses.AsNoTracking()
 12175      .Where(t => t.Name.ToLower().Contains(n.ToLower()))
 12176      .Where(t => t.Reserved == reserved)
 12177      .Take(10)
 12178      .Select(t => new ProgressSearchBusinessModel {
 12179        Id = t.Id,
 12180        Name = t.Name,
 12181      })
 12182      .ToListAsync();
 12183  }
 184}