< 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
86%
Covered lines: 107
Uncovered lines: 17
Coverable lines: 124
Total lines: 165
Line coverage: 86.2%
Branch coverage
85%
Covered branches: 12
Total branches: 14
Branch coverage: 85.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%
GetWaypointsAsync()100%44100%
GetWaypointAsync()100%22100%
CreateWaypointAsync()100%22100%
UpdateWaypointAsync()100%210%
TestDeleteWaypointAsync()100%22100%
DeleteWaypointAsync()100%210%
SearchWaypointsAsync()50%22100%

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
 1623public class WaypointService(LgdxContext context) : IWaypointService
 24{
 1625  private readonly LgdxContext _context = context ?? throw new ArgumentNullException(nameof(context));
 26
 27  public async Task<(IEnumerable<WaypointListBusinessModel>, PaginationHelper)> GetWaypointsAsync(int? realmId, string? 
 628  {
 629    var query = _context.Waypoints as IQueryable<Waypoint>;
 630    if(!string.IsNullOrWhiteSpace(name))
 531    {
 532      name = name.Trim();
 533      query = query.Where(t => t.Name.ToLower().Contains(name.ToLower()));
 534    }
 635    if(realmId != null)
 536    {
 537      query = query.Where(t => t.RealmId == realmId);
 538    }
 639    var itemCount = await query.CountAsync();
 640    var PaginationHelper = new PaginationHelper(itemCount, pageNumber, pageSize);
 641    var waypoints = await query.AsNoTracking()
 642      .OrderBy(a => a.Id)
 643      .Skip(pageSize * (pageNumber - 1))
 644      .Take(pageSize)
 645      .Select(w => new WaypointListBusinessModel {
 646        Id = w.Id,
 647        Name = w.Name,
 648        RealmId = w.RealmId,
 649        RealmName = w.Realm.Name,
 650        X = w.X,
 651        Y = w.Y,
 652        Rotation = w.Rotation,
 653        IsParking = w.IsParking,
 654        HasCharger = w.HasCharger,
 655        IsReserved = w.IsReserved,
 656      })
 657      .AsSplitQuery()
 658      .ToListAsync();
 659    return (waypoints, PaginationHelper);
 660  }
 61
 62  public async Task<WaypointBusinessModel> GetWaypointAsync(int waypointId)
 263  {
 264    return await _context.Waypoints.AsNoTracking()
 265      .Where(w => w.Id == waypointId)
 266      .Include(w => w.Realm)
 267      .Select(w => new WaypointBusinessModel {
 268        Id = w.Id,
 269        Name = w.Name,
 270        RealmId = w.RealmId,
 271        RealmName = w.Realm.Name,
 272        X = w.X,
 273        Y = w.Y,
 274        Rotation = w.Rotation,
 275        IsParking = w.IsParking,
 276        HasCharger = w.HasCharger,
 277        IsReserved = w.IsReserved,
 278      })
 279      .FirstOrDefaultAsync()
 280        ?? throw new LgdxNotFound404Exception();
 181  }
 82
 83  public async Task<WaypointBusinessModel> CreateWaypointAsync(WaypointCreateBusinessModel waypointCreateBusinessModel)
 284  {
 285    var realm = await _context.Realms.AsNoTracking()
 286      .Where(r => r.Id == waypointCreateBusinessModel.RealmId)
 287      .FirstOrDefaultAsync()
 288        ?? throw new LgdxValidation400Expection(nameof(waypointCreateBusinessModel.RealmId), "Realm does not exist.");
 89
 190    var waypoint = new Waypoint {
 191      Name = waypointCreateBusinessModel.Name,
 192      RealmId = waypointCreateBusinessModel.RealmId,
 193      X = waypointCreateBusinessModel.X,
 194      Y = waypointCreateBusinessModel.Y,
 195      Rotation = waypointCreateBusinessModel.Rotation,
 196      IsParking = waypointCreateBusinessModel.IsParking,
 197      HasCharger = waypointCreateBusinessModel.HasCharger,
 198      IsReserved = waypointCreateBusinessModel.IsReserved,
 199    };
 100
 1101    await _context.Waypoints.AddAsync(waypoint);
 1102    await _context.SaveChangesAsync();
 1103    return new WaypointBusinessModel {
 1104      Id = waypoint.Id,
 1105      Name = waypoint.Name,
 1106      RealmId = waypoint.RealmId,
 1107      RealmName = realm.Name,
 1108      X = waypoint.X,
 1109      Y = waypoint.Y,
 1110      Rotation = waypoint.Rotation,
 1111      IsParking = waypoint.IsParking,
 1112      HasCharger = waypoint.HasCharger,
 1113      IsReserved = waypoint.IsReserved,
 1114    };
 1115  }
 116
 117  public async Task<bool> UpdateWaypointAsync(int waypointId, WaypointUpdateBusinessModel waypointUpdateBusinessModel)
 0118  {
 0119    return await _context.Waypoints
 0120      .Where(w => w.Id == waypointId)
 0121      .ExecuteUpdateAsync(setters => setters
 0122        .SetProperty(w => w.Name, waypointUpdateBusinessModel.Name)
 0123        .SetProperty(w => w.X, waypointUpdateBusinessModel.X)
 0124        .SetProperty(w => w.Y, waypointUpdateBusinessModel.Y)
 0125        .SetProperty(w => w.Rotation, waypointUpdateBusinessModel.Rotation)
 0126        .SetProperty(w => w.IsParking, waypointUpdateBusinessModel.IsParking)
 0127        .SetProperty(w => w.HasCharger, waypointUpdateBusinessModel.HasCharger)
 0128        .SetProperty(w => w.IsReserved, waypointUpdateBusinessModel.IsReserved)
 0129      ) == 1;
 0130  }
 131
 132  public async Task<bool> TestDeleteWaypointAsync(int waypointId)
 2133  {
 2134    var depeendencies = await _context.AutoTasksDetail
 2135      .Include(t => t.AutoTask)
 2136      .Where(t => t.WaypointId == waypointId)
 2137      .Where(t => t.AutoTask.CurrentProgressId != (int)ProgressState.Completed && t.AutoTask.CurrentProgressId != (int)P
 2138      .CountAsync();
 2139    if (depeendencies > 0)
 1140    {
 1141      throw new LgdxValidation400Expection(nameof(waypointId), $"This waypoint has been used by {depeendencies} running/
 142    }
 1143    return true;
 1144  }
 145
 146  public async Task<bool> DeleteWaypointAsync(int waypointId)
 0147  {
 0148    return await _context.Waypoints.Where(w => w.Id == waypointId)
 0149      .ExecuteDeleteAsync() == 1;
 0150  }
 151
 152  public async Task<IEnumerable<WaypointSearchBusinessModel>> SearchWaypointsAsync(int realmId, string? name)
 4153  {
 4154    var n = name ?? string.Empty;
 4155    return await _context.Waypoints.AsNoTracking()
 4156      .Where(w => w.RealmId == realmId)
 4157      .Where(t => t.Name.ToLower().Contains(n.ToLower()))
 4158      .Take(10)
 4159      .Select(m => new WaypointSearchBusinessModel {
 4160        Id = m.Id,
 4161        Name = m.Name,
 4162      })
 4163      .ToListAsync();
 4164  }
 165}