< 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
75%
Covered lines: 119
Uncovered lines: 39
Coverable lines: 158
Total lines: 206
Line coverage: 75.3%
Branch coverage
63%
Covered branches: 14
Total branches: 22
Branch coverage: 63.6%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)50%44100%
GetWaypointsAsync()100%44100%
GetWaypointAsync()100%22100%
CreateWaypointAsync()100%22100%
UpdateWaypointAsync()0%620%
TestDeleteWaypointAsync()75%4488.23%
DeleteWaypointAsync()0%620%
SearchWaypointsAsync()50%22100%

File(s)

/builds/yukaitung/lgdxrobot2-cloud/LGDXRobotCloud.API/Services/Navigation/WaypointService.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.Navigation;
 7using LGDXRobotCloud.Utilities.Enums;
 8using LGDXRobotCloud.Utilities.Helpers;
 9using Microsoft.EntityFrameworkCore;
 10
 11namespace LGDXRobotCloud.API.Services.Navigation;
 12
 13public interface IWaypointService
 14{
 15  Task<(IEnumerable<WaypointListBusinessModel>, PaginationHelper)> GetWaypointsAsync(int? realmId, string? name, int pag
 16  Task<WaypointBusinessModel> GetWaypointAsync(int waypointId);
 17  Task<WaypointBusinessModel> CreateWaypointAsync(WaypointCreateBusinessModel waypointCreateBusinessModel);
 18  Task<bool> UpdateWaypointAsync(int waypointId, WaypointUpdateBusinessModel waypointUpdateBusinessModel);
 19  Task<bool> TestDeleteWaypointAsync(int waypointId);
 20  Task<bool> DeleteWaypointAsync(int waypointId);
 21
 22  Task<IEnumerable<WaypointSearchBusinessModel>> SearchWaypointsAsync(int realmId, string? name);
 23}
 24
 1625public class WaypointService(
 1626    IActivityLogService activityLogService,
 1627    LgdxContext context
 1628  ) : IWaypointService
 29{
 1630  private readonly IActivityLogService _activityLogService = activityLogService ?? throw new ArgumentNullException(nameo
 1631  private readonly LgdxContext _context = context ?? throw new ArgumentNullException(nameof(context));
 32
 33  public async Task<(IEnumerable<WaypointListBusinessModel>, PaginationHelper)> GetWaypointsAsync(int? realmId, string? 
 634  {
 635    var query = _context.Waypoints as IQueryable<Waypoint>;
 636    if(!string.IsNullOrWhiteSpace(name))
 537    {
 538      name = name.Trim();
 539      query = query.Where(t => t.Name.ToLower().Contains(name.ToLower()));
 540    }
 641    if(realmId != null)
 542    {
 543      query = query.Where(t => t.RealmId == realmId);
 544    }
 645    var itemCount = await query.CountAsync();
 646    var PaginationHelper = new PaginationHelper(itemCount, pageNumber, pageSize);
 647    var waypoints = await query.AsNoTracking()
 648      .OrderBy(a => a.Id)
 649      .Skip(pageSize * (pageNumber - 1))
 650      .Take(pageSize)
 651      .Select(w => new WaypointListBusinessModel {
 652        Id = w.Id,
 653        Name = w.Name,
 654        RealmId = w.RealmId,
 655        RealmName = w.Realm.Name,
 656        X = w.X,
 657        Y = w.Y,
 658        Rotation = w.Rotation,
 659      })
 660      .AsSplitQuery()
 661      .ToListAsync();
 662    return (waypoints, PaginationHelper);
 663  }
 64
 65  public async Task<WaypointBusinessModel> GetWaypointAsync(int waypointId)
 266  {
 267    return await _context.Waypoints.AsNoTracking()
 268      .Where(w => w.Id == waypointId)
 269      .Include(w => w.Realm)
 270      .Select(w => new WaypointBusinessModel {
 271        Id = w.Id,
 272        Name = w.Name,
 273        RealmId = w.RealmId,
 274        RealmName = w.Realm.Name,
 275        X = w.X,
 276        Y = w.Y,
 277        Rotation = w.Rotation,
 278        IsParking = w.IsParking,
 279        HasCharger = w.HasCharger,
 280        IsReserved = w.IsReserved,
 281      })
 282      .FirstOrDefaultAsync()
 283        ?? throw new LgdxNotFound404Exception();
 184  }
 85
 86  public async Task<WaypointBusinessModel> CreateWaypointAsync(WaypointCreateBusinessModel waypointCreateBusinessModel)
 287  {
 288    var realm = await _context.Realms.AsNoTracking()
 289      .Where(r => r.Id == waypointCreateBusinessModel.RealmId)
 290      .FirstOrDefaultAsync()
 291        ?? throw new LgdxValidation400Expection(nameof(waypointCreateBusinessModel.RealmId), "Realm does not exist.");
 92
 193    var waypoint = new Waypoint {
 194      Name = waypointCreateBusinessModel.Name,
 195      RealmId = waypointCreateBusinessModel.RealmId,
 196      X = waypointCreateBusinessModel.X,
 197      Y = waypointCreateBusinessModel.Y,
 198      Rotation = waypointCreateBusinessModel.Rotation,
 199      IsParking = waypointCreateBusinessModel.IsParking,
 1100      HasCharger = waypointCreateBusinessModel.HasCharger,
 1101      IsReserved = waypointCreateBusinessModel.IsReserved,
 1102    };
 103
 1104    await _context.Waypoints.AddAsync(waypoint);
 1105    await _context.SaveChangesAsync();
 106
 1107    await _activityLogService.CreateActivityLogAsync(new ActivityLogCreateBusinessModel
 1108    {
 1109      EntityName = nameof(Waypoint),
 1110      EntityId = waypoint.Id.ToString(),
 1111      Action = ActivityAction.Create,
 1112    });
 113
 1114    return new WaypointBusinessModel
 1115    {
 1116      Id = waypoint.Id,
 1117      Name = waypoint.Name,
 1118      RealmId = waypoint.RealmId,
 1119      RealmName = realm.Name,
 1120      X = waypoint.X,
 1121      Y = waypoint.Y,
 1122      Rotation = waypoint.Rotation,
 1123      IsParking = waypoint.IsParking,
 1124      HasCharger = waypoint.HasCharger,
 1125      IsReserved = waypoint.IsReserved,
 1126    };
 1127  }
 128
 129  public async Task<bool> UpdateWaypointAsync(int waypointId, WaypointUpdateBusinessModel waypointUpdateBusinessModel)
 0130  {
 0131    bool result = await _context.Waypoints
 0132      .Where(w => w.Id == waypointId)
 0133      .ExecuteUpdateAsync(setters => setters
 0134        .SetProperty(w => w.Name, waypointUpdateBusinessModel.Name)
 0135        .SetProperty(w => w.X, waypointUpdateBusinessModel.X)
 0136        .SetProperty(w => w.Y, waypointUpdateBusinessModel.Y)
 0137        .SetProperty(w => w.Rotation, waypointUpdateBusinessModel.Rotation)
 0138        .SetProperty(w => w.IsParking, waypointUpdateBusinessModel.IsParking)
 0139        .SetProperty(w => w.HasCharger, waypointUpdateBusinessModel.HasCharger)
 0140        .SetProperty(w => w.IsReserved, waypointUpdateBusinessModel.IsReserved)
 0141      ) == 1;
 142
 0143    if (result)
 0144    {
 0145      await _activityLogService.CreateActivityLogAsync(new ActivityLogCreateBusinessModel
 0146      {
 0147        EntityName = nameof(Waypoint),
 0148        EntityId = waypointId.ToString(),
 0149        Action = ActivityAction.Update,
 0150      });
 0151    }
 0152    return result;
 0153  }
 154
 155  public async Task<bool> TestDeleteWaypointAsync(int waypointId)
 2156  {
 2157    var dependencies = await _context.AutoTasksDetail
 2158      .Include(t => t.AutoTask)
 2159      .Where(t => t.WaypointId == waypointId)
 2160      .Where(t => t.AutoTask.CurrentProgressId != (int)ProgressState.Completed && t.AutoTask.CurrentProgressId != (int)P
 2161      .CountAsync();
 2162    if (dependencies > 0)
 1163    {
 1164      throw new LgdxValidation400Expection(nameof(waypointId), $"This waypoint has been used by {dependencies} running/w
 165    }
 1166    dependencies = await _context.WaypointTraffics
 1167      .Where(t => t.WaypointFromId == waypointId || t.WaypointToId == waypointId)
 1168      .CountAsync();
 1169    if (dependencies > 0)
 0170    {
 0171      throw new LgdxValidation400Expection(nameof(waypointId), $"This waypoint has {dependencies} traffics.");
 172    }
 1173    return true;
 1174  }
 175
 176  public async Task<bool> DeleteWaypointAsync(int waypointId)
 0177  {
 0178    bool result = await _context.Waypoints.Where(w => w.Id == waypointId)
 0179      .ExecuteDeleteAsync() == 1;
 180
 0181    if (result)
 0182    {
 0183      await _activityLogService.CreateActivityLogAsync(new ActivityLogCreateBusinessModel
 0184      {
 0185        EntityName = nameof(Waypoint),
 0186        EntityId = waypointId.ToString(),
 0187        Action = ActivityAction.Delete,
 0188      });
 0189    }
 0190    return result;
 0191  }
 192
 193  public async Task<IEnumerable<WaypointSearchBusinessModel>> SearchWaypointsAsync(int realmId, string? name)
 4194  {
 4195    var n = name ?? string.Empty;
 4196    return await _context.Waypoints.AsNoTracking()
 4197      .Where(w => w.RealmId == realmId)
 4198      .Where(t => t.Name.ToLower().Contains(n.ToLower()))
 4199      .Take(10)
 4200      .Select(m => new WaypointSearchBusinessModel {
 4201        Id = m.Id,
 4202        Name = m.Name,
 4203      })
 4204      .ToListAsync();
 4205  }
 206}