< Summary

Information
Class: LGDXRobotCloud.API.Services.Navigation.WaypointService
Assembly: LGDXRobotCloud.API
File(s): /builds/yukaitung/lgdxrobot2-cloud/LGDXRobotCloud.API/Services/Navigation/WaypointService.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 125
Coverable lines: 125
Total lines: 166
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 14
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%
GetWaypointsAsync()0%2040%
GetWaypointAsync()0%620%
CreateWaypointAsync()0%620%
UpdateWaypointAsync()100%210%
TestDeleteWaypointAsync()0%620%
DeleteWaypointAsync()100%210%
SearchWaypointsAsync()0%620%

File(s)

/builds/yukaitung/lgdxrobot2-cloud/LGDXRobotCloud.API/Services/Navigation/WaypointService.cs

#LineLine coverage
 1using LGDXRobotCloud.API.Exceptions;
 2using LGDXRobotCloud.Data.DbContexts;
 3using LGDXRobotCloud.Data.Entities;
 4using LGDXRobotCloud.Data.Models.Business.Navigation;
 5using LGDXRobotCloud.Utilities.Enums;
 6using LGDXRobotCloud.Utilities.Helpers;
 7using Microsoft.EntityFrameworkCore;
 8
 9namespace LGDXRobotCloud.API.Services.Navigation;
 10
 11public interface IWaypointService
 12{
 13  Task<(IEnumerable<WaypointListBusinessModel>, PaginationHelper)> GetWaypointsAsync(int? realmId, string? name, int pag
 14  Task<WaypointBusinessModel> GetWaypointAsync(int waypointId);
 15  Task<WaypointBusinessModel> CreateWaypointAsync(WaypointCreateBusinessModel waypointCreateBusinessModel);
 16  Task<bool> UpdateWaypointAsync(int waypointId, WaypointUpdateBusinessModel waypointUpdateBusinessModel);
 17  Task<bool> TestDeleteWaypointAsync(int waypointId);
 18  Task<bool> DeleteWaypointAsync(int waypointId);
 19
 20  Task<IEnumerable<WaypointSearchBusinessModel>> SearchWaypointsAsync(int realmId, string? name);
 21}
 22
 023public class WaypointService(LgdxContext context) : IWaypointService
 24{
 025  private readonly LgdxContext _context = context ?? throw new ArgumentNullException(nameof(context));
 26
 27  public async Task<(IEnumerable<WaypointListBusinessModel>, PaginationHelper)> GetWaypointsAsync(int? realmId, string? 
 028  {
 029    var query = _context.Waypoints as IQueryable<Waypoint>;
 030    if(!string.IsNullOrWhiteSpace(name))
 031    {
 032      name = name.Trim();
 033      query = query.Where(t => t.Name.Contains(name));
 034    }
 035    if(realmId != null)
 036    {
 037      query = query.Where(t => t.RealmId == realmId);
 038    }
 039    var itemCount = await query.CountAsync();
 040    var PaginationHelper = new PaginationHelper(itemCount, pageNumber, pageSize);
 041    var waypoints = await query.AsNoTracking()
 042      .Include(w => w.Realm)
 043      .OrderBy(a => a.Id)
 044      .Skip(pageSize * (pageNumber - 1))
 045      .Take(pageSize)
 046      .Select(w => new WaypointListBusinessModel {
 047        Id = w.Id,
 048        Name = w.Name,
 049        RealmId = w.RealmId,
 050        RealmName = w.Realm.Name,
 051        X = w.X,
 052        Y = w.Y,
 053        Rotation = w.Rotation,
 054        IsParking = w.IsParking,
 055        HasCharger = w.HasCharger,
 056        IsReserved = w.IsReserved,
 057      })
 058      .AsSplitQuery()
 059      .ToListAsync();
 060    return (waypoints, PaginationHelper);
 061  }
 62
 63  public async Task<WaypointBusinessModel> GetWaypointAsync(int waypointId)
 064  {
 065    return await _context.Waypoints.AsNoTracking()
 066      .Where(w => w.Id == waypointId)
 067      .Include(w => w.Realm)
 068      .Select(w => new WaypointBusinessModel {
 069        Id = w.Id,
 070        Name = w.Name,
 071        RealmId = w.RealmId,
 072        RealmName = w.Realm.Name,
 073        X = w.X,
 074        Y = w.Y,
 075        Rotation = w.Rotation,
 076        IsParking = w.IsParking,
 077        HasCharger = w.HasCharger,
 078        IsReserved = w.IsReserved,
 079      })
 080      .FirstOrDefaultAsync()
 081        ?? throw new LgdxNotFound404Exception();
 082  }
 83
 84  public async Task<WaypointBusinessModel> CreateWaypointAsync(WaypointCreateBusinessModel waypointCreateBusinessModel)
 085  {
 086    var realm = await _context.Realms.AsNoTracking()
 087      .Where(r => r.Id == waypointCreateBusinessModel.RealmId)
 088      .FirstOrDefaultAsync()
 089        ?? throw new LgdxValidation400Expection(nameof(waypointCreateBusinessModel.RealmId), "Realm does not exist.");
 90
 091    var waypoint = new Waypoint {
 092      Name = waypointCreateBusinessModel.Name,
 093      RealmId = waypointCreateBusinessModel.RealmId,
 094      X = waypointCreateBusinessModel.X,
 095      Y = waypointCreateBusinessModel.Y,
 096      Rotation = waypointCreateBusinessModel.Rotation,
 097      IsParking = waypointCreateBusinessModel.IsParking,
 098      HasCharger = waypointCreateBusinessModel.HasCharger,
 099      IsReserved = waypointCreateBusinessModel.IsReserved,
 0100    };
 101
 0102    await _context.Waypoints.AddAsync(waypoint);
 0103    await _context.SaveChangesAsync();
 0104    return new WaypointBusinessModel {
 0105      Id = waypoint.Id,
 0106      Name = waypoint.Name,
 0107      RealmId = waypoint.RealmId,
 0108      RealmName = realm.Name,
 0109      X = waypoint.X,
 0110      Y = waypoint.Y,
 0111      Rotation = waypoint.Rotation,
 0112      IsParking = waypoint.IsParking,
 0113      HasCharger = waypoint.HasCharger,
 0114      IsReserved = waypoint.IsReserved,
 0115    };
 0116  }
 117
 118  public async Task<bool> UpdateWaypointAsync(int waypointId, WaypointUpdateBusinessModel waypointUpdateBusinessModel)
 0119  {
 0120    return await _context.Waypoints
 0121      .Where(w => w.Id == waypointId)
 0122      .ExecuteUpdateAsync(setters => setters
 0123        .SetProperty(w => w.Name, waypointUpdateBusinessModel.Name)
 0124        .SetProperty(w => w.X, waypointUpdateBusinessModel.X)
 0125        .SetProperty(w => w.Y, waypointUpdateBusinessModel.Y)
 0126        .SetProperty(w => w.Rotation, waypointUpdateBusinessModel.Rotation)
 0127        .SetProperty(w => w.IsParking, waypointUpdateBusinessModel.IsParking)
 0128        .SetProperty(w => w.HasCharger, waypointUpdateBusinessModel.HasCharger)
 0129        .SetProperty(w => w.IsReserved, waypointUpdateBusinessModel.IsReserved)
 0130      ) == 1;
 0131  }
 132
 133  public async Task<bool> TestDeleteWaypointAsync(int waypointId)
 0134  {
 0135    var depeendencies = await _context.AutoTasksDetail
 0136      .Include(t => t.AutoTask)
 0137      .Where(t => t.WaypointId == waypointId)
 0138      .Where(t => t.AutoTask.CurrentProgressId != (int)ProgressState.Completed && t.AutoTask.CurrentProgressId != (int)P
 0139      .CountAsync();
 0140    if (depeendencies > 0)
 0141    {
 0142      throw new LgdxValidation400Expection(nameof(waypointId), $"This waypoint has been used by {depeendencies} running/
 143    }
 0144    return true;
 0145  }
 146
 147  public async Task<bool> DeleteWaypointAsync(int waypointId)
 0148  {
 0149    return await _context.Waypoints.Where(w => w.Id == waypointId)
 0150      .ExecuteDeleteAsync() == 1;
 0151  }
 152
 153  public async Task<IEnumerable<WaypointSearchBusinessModel>> SearchWaypointsAsync(int realmId, string? name)
 0154  {
 0155    var n = name ?? string.Empty;
 0156    return await _context.Waypoints.AsNoTracking()
 0157      .Where(w => w.RealmId == realmId)
 0158      .Where(t => t.Name.ToLower().Contains(n.ToLower()))
 0159      .Take(10)
 0160      .Select(m => new WaypointSearchBusinessModel {
 0161        Id = m.Id,
 0162        Name = m.Name,
 0163      })
 0164      .ToListAsync();
 0165  }
 166}