< Summary

Information
Class: LGDXRobotCloud.API.Services.Navigation.RealmService
Assembly: LGDXRobotCloud.API
File(s): /builds/yukaitung/lgdxrobot2-cloud/LGDXRobotCloud.API/Services/Navigation/RealmService.cs
Line coverage
67%
Covered lines: 123
Uncovered lines: 59
Coverable lines: 182
Total lines: 237
Line coverage: 67.5%
Branch coverage
57%
Covered branches: 16
Total branches: 28
Branch coverage: 57.1%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)50%44100%
GetRealmsAsync()100%22100%
GetRealmAsync()100%22100%
GetDefaultRealmAsync()50%22100%
CreateRealmAsync()50%44100%
UpdateRealmAsync()0%620%
UpdateRealmMapAsync()0%620%
TestDeleteRealmAsync()100%66100%
DeleteRealmAsync()0%620%
SearchRealmsAsync()50%22100%

File(s)

/builds/yukaitung/lgdxrobot2-cloud/LGDXRobotCloud.API/Services/Navigation/RealmService.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 IRealmService
 14{
 15  Task<(IEnumerable<RealmListBusinessModel>, PaginationHelper)> GetRealmsAsync(string? name, int pageNumber, int pageSiz
 16  Task<RealmBusinessModel> GetRealmAsync(int id);
 17  Task<RealmBusinessModel> GetDefaultRealmAsync();
 18  Task<RealmBusinessModel> CreateRealmAsync(RealmCreateBusinessModel createModel);
 19  Task<bool> UpdateRealmAsync(int id, RealmUpdateBusinessModel updateModel);
 20  Task<bool> UpdateRealmMapAsync(int id, RealmMapUpdateBusinessModel updateModel);
 21  Task<bool> TestDeleteRealmAsync(int id);
 22  Task<bool> DeleteRealmAsync(int id);
 23
 24  Task<IEnumerable<RealmSearchBusinessModel>> SearchRealmsAsync(string? name);
 25}
 26
 1627public class RealmService(
 1628    IActivityLogService activityLogService,
 1629    LgdxContext context
 1630  ) : IRealmService
 31{
 1632  private readonly IActivityLogService _activityLogService = activityLogService ?? throw new ArgumentNullException(nameo
 1633  private readonly LgdxContext _context = context ?? throw new ArgumentNullException(nameof(context));
 34
 35  public async Task<(IEnumerable<RealmListBusinessModel>, PaginationHelper)> GetRealmsAsync(string? name, int pageNumber
 436  {
 437    var query = _context.Realms as IQueryable<Realm>;
 438    if (!string.IsNullOrWhiteSpace(name))
 339    {
 340      name = name.Trim();
 341      query = query.Where(m => m.Name.ToLower().Contains(name.ToLower()));
 342    }
 443    var itemCount = await query.CountAsync();
 444    var PaginationHelper = new PaginationHelper(itemCount, pageNumber, pageSize);
 445    var realms = await query.AsNoTracking()
 446      .OrderBy(m => m.Id)
 447      .Skip(pageSize * (pageNumber - 1))
 448      .Take(pageSize)
 449      .Select(m => new RealmListBusinessModel {
 450        Id = m.Id,
 451        Name = m.Name,
 452        Description = m.Description,
 453        Resolution = m.Resolution,
 454      })
 455      .ToListAsync();
 456    return (realms, PaginationHelper);
 457  }
 58
 59  public async Task<RealmBusinessModel> GetRealmAsync(int id)
 260  {
 261    return await _context.Realms.AsNoTracking()
 262      .Where(m => m.Id == id)
 263      .Select(m => new RealmBusinessModel {
 264        Id = m.Id,
 265        Name = m.Name,
 266        Description = m.Description,
 267        HasWaypointsTrafficControl = m.HasWaypointsTrafficControl,
 268        Image = Convert.ToBase64String(m.Image),
 269        Resolution = m.Resolution,
 270        OriginX = m.OriginX,
 271        OriginY = m.OriginY,
 272        OriginRotation = m.OriginRotation,
 273      })
 274      .FirstOrDefaultAsync()
 275        ?? throw new LgdxNotFound404Exception();
 176  }
 77
 78  public async Task<RealmBusinessModel> GetDefaultRealmAsync()
 179  {
 180    return await _context.Realms.AsNoTracking()
 181      .OrderBy(m => m.Id)
 182      .Select(m => new RealmBusinessModel {
 183        Id = m.Id,
 184        Name = m.Name,
 185        Description = m.Description,
 186        HasWaypointsTrafficControl = m.HasWaypointsTrafficControl,
 187        Image = Convert.ToBase64String(m.Image),
 188        Resolution = m.Resolution,
 189        OriginX = m.OriginX,
 190        OriginY = m.OriginY,
 191        OriginRotation = m.OriginRotation,
 192      })
 193      .FirstOrDefaultAsync()
 194        ?? throw new LgdxNotFound404Exception();
 195  }
 96
 97  public async Task<RealmBusinessModel> CreateRealmAsync(RealmCreateBusinessModel createModel)
 198  {
 199    var realm = new Realm {
 1100      Name = createModel.Name,
 1101      Description = createModel.Description,
 1102      HasWaypointsTrafficControl = createModel.HasWaypointsTrafficControl,
 1103      Image = Convert.FromBase64String(createModel.Image ?? string.Empty),
 1104      Resolution = createModel.Resolution,
 1105      OriginX = createModel.OriginX,
 1106      OriginY = createModel.OriginY,
 1107      OriginRotation = createModel.OriginRotation,
 1108    };
 109
 1110    await _context.Realms.AddAsync(realm);
 1111    await _context.SaveChangesAsync();
 112
 1113    await _activityLogService.CreateActivityLogAsync(new ActivityLogCreateBusinessModel
 1114    {
 1115      EntityName = nameof(Realm),
 1116      EntityId = realm.Id.ToString(),
 1117      Action = ActivityAction.Create,
 1118    });
 119
 1120    return new RealmBusinessModel
 1121    {
 1122      Id = realm.Id,
 1123      Name = realm.Name,
 1124      Description = realm.Description,
 1125      HasWaypointsTrafficControl = realm.HasWaypointsTrafficControl,
 1126      Image = createModel.Image ?? string.Empty,
 1127      Resolution = realm.Resolution,
 1128      OriginX = realm.OriginX,
 1129      OriginY = realm.OriginY,
 1130      OriginRotation = realm.OriginRotation,
 1131    };
 1132  }
 133
 134  public async Task<bool> UpdateRealmAsync(int id, RealmUpdateBusinessModel updateModel)
 0135  {
 0136    bool result = await _context.Realms
 0137      .Where(m => m.Id == id)
 0138      .ExecuteUpdateAsync(setters => setters
 0139        .SetProperty(m => m.Name, updateModel.Name)
 0140        .SetProperty(m => m.Description, updateModel.Description)
 0141        .SetProperty(m => m.HasWaypointsTrafficControl, updateModel.HasWaypointsTrafficControl)
 0142        .SetProperty(m => m.Image, Convert.FromBase64String(updateModel.Image ?? string.Empty))
 0143        .SetProperty(m => m.Resolution, updateModel.Resolution)
 0144        .SetProperty(m => m.OriginX, updateModel.OriginX)
 0145        .SetProperty(m => m.OriginY, updateModel.OriginY)
 0146        .SetProperty(m => m.OriginRotation, updateModel.OriginRotation)
 0147      ) == 1;
 148
 0149    if (result)
 0150    {
 0151      await _activityLogService.CreateActivityLogAsync(new ActivityLogCreateBusinessModel
 0152      {
 0153        EntityName = nameof(Realm),
 0154        EntityId = id.ToString(),
 0155        Action = ActivityAction.Update,
 0156      });
 0157    }
 0158    return result;
 0159  }
 160
 161  public async Task<bool> UpdateRealmMapAsync(int id, RealmMapUpdateBusinessModel updateModel)
 0162  {
 0163    bool result = await _context.Realms
 0164      .Where(m => m.Id == id)
 0165      .ExecuteUpdateAsync(setters => setters
 0166        .SetProperty(m => m.Image, Convert.FromBase64String(updateModel.Image))
 0167        .SetProperty(m => m.Resolution, updateModel.Resolution)
 0168        .SetProperty(m => m.OriginX, updateModel.OriginX)
 0169        .SetProperty(m => m.OriginY, updateModel.OriginY)
 0170        .SetProperty(m => m.OriginRotation, updateModel.OriginRotation)
 0171      ) == 1;
 172
 0173    if (result)
 0174    {
 0175      await _activityLogService.CreateActivityLogAsync(new ActivityLogCreateBusinessModel
 0176      {
 0177        EntityName = nameof(Realm),
 0178        EntityId = id.ToString(),
 0179        Action = ActivityAction.Update,
 0180      });
 0181    }
 0182    return result;
 0183  }
 184
 185  public async Task<bool> TestDeleteRealmAsync(int id)
 4186  {
 4187    var dependencies = await _context.Robots.Where(m => m.RealmId == id).CountAsync();
 4188    if (dependencies > 0)
 1189    {
 1190      throw new LgdxValidation400Expection(nameof(id), $"This realm has been used by {dependencies} robots.");
 191    }
 3192    dependencies = await _context.Waypoints.Where(m => m.RealmId == id).CountAsync();
 3193    if (dependencies > 0)
 1194    {
 1195      throw new LgdxValidation400Expection(nameof(id), $"This realm has been used by {dependencies} waypoints.");
 196    }
 2197    dependencies = await _context.AutoTasks
 2198      .Where(m => m.RealmId == id)
 2199      .Where(m => m.CurrentProgressId != (int)ProgressState.Completed && m.CurrentProgressId != (int)ProgressState.Abort
 2200      .CountAsync();
 2201    if (dependencies > 0)
 1202    {
 1203      throw new LgdxValidation400Expection(nameof(id), $"This realm has been used by {dependencies} running/waiting/temp
 204    }
 1205    return true;
 1206  }
 207
 208  public async Task<bool> DeleteRealmAsync(int id)
 0209  {
 0210    bool result = await _context.Realms.Where(m => m.Id == id)
 0211      .ExecuteDeleteAsync() == 1;
 212
 0213    if (result)
 0214    {
 0215      await _activityLogService.CreateActivityLogAsync(new ActivityLogCreateBusinessModel
 0216      {
 0217        EntityName = nameof(Realm),
 0218        EntityId = id.ToString(),
 0219        Action = ActivityAction.Delete,
 0220      });
 0221    }
 0222    return result;
 0223  }
 224
 225  public async Task<IEnumerable<RealmSearchBusinessModel>> SearchRealmsAsync(string? name)
 4226  {
 4227    var n = name ?? string.Empty;
 4228    return await _context.Realms.AsNoTracking()
 4229      .Where(w => w.Name.ToLower().Contains(n.ToLower()))
 4230      .Take(10)
 4231      .Select(m => new RealmSearchBusinessModel {
 4232        Id = m.Id,
 4233        Name = m.Name,
 4234      })
 4235      .ToListAsync();
 4236  }
 237}