< 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
85%
Covered lines: 108
Uncovered lines: 19
Coverable lines: 127
Total lines: 169
Line coverage: 85%
Branch coverage
81%
Covered branches: 13
Total branches: 16
Branch coverage: 81.2%
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()75%4488.23%
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      })
 654      .AsSplitQuery()
 655      .ToListAsync();
 656    return (waypoints, PaginationHelper);
 657  }
 58
 59  public async Task<WaypointBusinessModel> GetWaypointAsync(int waypointId)
 260  {
 261    return await _context.Waypoints.AsNoTracking()
 262      .Where(w => w.Id == waypointId)
 263      .Include(w => w.Realm)
 264      .Select(w => new WaypointBusinessModel {
 265        Id = w.Id,
 266        Name = w.Name,
 267        RealmId = w.RealmId,
 268        RealmName = w.Realm.Name,
 269        X = w.X,
 270        Y = w.Y,
 271        Rotation = w.Rotation,
 272        IsParking = w.IsParking,
 273        HasCharger = w.HasCharger,
 274        IsReserved = w.IsReserved,
 275      })
 276      .FirstOrDefaultAsync()
 277        ?? throw new LgdxNotFound404Exception();
 178  }
 79
 80  public async Task<WaypointBusinessModel> CreateWaypointAsync(WaypointCreateBusinessModel waypointCreateBusinessModel)
 281  {
 282    var realm = await _context.Realms.AsNoTracking()
 283      .Where(r => r.Id == waypointCreateBusinessModel.RealmId)
 284      .FirstOrDefaultAsync()
 285        ?? throw new LgdxValidation400Expection(nameof(waypointCreateBusinessModel.RealmId), "Realm does not exist.");
 86
 187    var waypoint = new Waypoint {
 188      Name = waypointCreateBusinessModel.Name,
 189      RealmId = waypointCreateBusinessModel.RealmId,
 190      X = waypointCreateBusinessModel.X,
 191      Y = waypointCreateBusinessModel.Y,
 192      Rotation = waypointCreateBusinessModel.Rotation,
 193      IsParking = waypointCreateBusinessModel.IsParking,
 194      HasCharger = waypointCreateBusinessModel.HasCharger,
 195      IsReserved = waypointCreateBusinessModel.IsReserved,
 196    };
 97
 198    await _context.Waypoints.AddAsync(waypoint);
 199    await _context.SaveChangesAsync();
 1100    return new WaypointBusinessModel {
 1101      Id = waypoint.Id,
 1102      Name = waypoint.Name,
 1103      RealmId = waypoint.RealmId,
 1104      RealmName = realm.Name,
 1105      X = waypoint.X,
 1106      Y = waypoint.Y,
 1107      Rotation = waypoint.Rotation,
 1108      IsParking = waypoint.IsParking,
 1109      HasCharger = waypoint.HasCharger,
 1110      IsReserved = waypoint.IsReserved,
 1111    };
 1112  }
 113
 114  public async Task<bool> UpdateWaypointAsync(int waypointId, WaypointUpdateBusinessModel waypointUpdateBusinessModel)
 0115  {
 0116    return await _context.Waypoints
 0117      .Where(w => w.Id == waypointId)
 0118      .ExecuteUpdateAsync(setters => setters
 0119        .SetProperty(w => w.Name, waypointUpdateBusinessModel.Name)
 0120        .SetProperty(w => w.X, waypointUpdateBusinessModel.X)
 0121        .SetProperty(w => w.Y, waypointUpdateBusinessModel.Y)
 0122        .SetProperty(w => w.Rotation, waypointUpdateBusinessModel.Rotation)
 0123        .SetProperty(w => w.IsParking, waypointUpdateBusinessModel.IsParking)
 0124        .SetProperty(w => w.HasCharger, waypointUpdateBusinessModel.HasCharger)
 0125        .SetProperty(w => w.IsReserved, waypointUpdateBusinessModel.IsReserved)
 0126      ) == 1;
 0127  }
 128
 129  public async Task<bool> TestDeleteWaypointAsync(int waypointId)
 2130  {
 2131    var dependencies = await _context.AutoTasksDetail
 2132      .Include(t => t.AutoTask)
 2133      .Where(t => t.WaypointId == waypointId)
 2134      .Where(t => t.AutoTask.CurrentProgressId != (int)ProgressState.Completed && t.AutoTask.CurrentProgressId != (int)P
 2135      .CountAsync();
 2136    if (dependencies > 0)
 1137    {
 1138      throw new LgdxValidation400Expection(nameof(waypointId), $"This waypoint has been used by {dependencies} running/w
 139    }
 1140    dependencies = await _context.WaypointTraffics
 1141      .Where(t => t.WaypointFromId == waypointId || t.WaypointToId == waypointId)
 1142      .CountAsync();
 1143    if (dependencies > 0)
 0144    {
 0145      throw new LgdxValidation400Expection(nameof(waypointId), $"This waypoint has {dependencies} traffics.");
 146    }
 1147    return true;
 1148  }
 149
 150  public async Task<bool> DeleteWaypointAsync(int waypointId)
 0151  {
 0152    return await _context.Waypoints.Where(w => w.Id == waypointId)
 0153      .ExecuteDeleteAsync() == 1;
 0154  }
 155
 156  public async Task<IEnumerable<WaypointSearchBusinessModel>> SearchWaypointsAsync(int realmId, string? name)
 4157  {
 4158    var n = name ?? string.Empty;
 4159    return await _context.Waypoints.AsNoTracking()
 4160      .Where(w => w.RealmId == realmId)
 4161      .Where(t => t.Name.ToLower().Contains(n.ToLower()))
 4162      .Take(10)
 4163      .Select(m => new WaypointSearchBusinessModel {
 4164        Id = m.Id,
 4165        Name = m.Name,
 4166      })
 4167      .ToListAsync();
 4168  }
 169}