| | 1 | | using LGDXRobotCloud.API.Exceptions; |
| | 2 | | using LGDXRobotCloud.Data.DbContexts; |
| | 3 | | using LGDXRobotCloud.Data.Entities; |
| | 4 | | using LGDXRobotCloud.Data.Models.Business.Navigation; |
| | 5 | | using LGDXRobotCloud.Utilities.Enums; |
| | 6 | | using LGDXRobotCloud.Utilities.Helpers; |
| | 7 | | using Microsoft.EntityFrameworkCore; |
| | 8 | |
|
| | 9 | | namespace LGDXRobotCloud.API.Services.Navigation; |
| | 10 | |
|
| | 11 | | public 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 | |
|
| 0 | 23 | | public class WaypointService(LgdxContext context) : IWaypointService |
| | 24 | | { |
| 0 | 25 | | private readonly LgdxContext _context = context ?? throw new ArgumentNullException(nameof(context)); |
| | 26 | |
|
| | 27 | | public async Task<(IEnumerable<WaypointListBusinessModel>, PaginationHelper)> GetWaypointsAsync(int? realmId, string? |
| 0 | 28 | | { |
| 0 | 29 | | var query = _context.Waypoints as IQueryable<Waypoint>; |
| 0 | 30 | | if(!string.IsNullOrWhiteSpace(name)) |
| 0 | 31 | | { |
| 0 | 32 | | name = name.Trim(); |
| 0 | 33 | | query = query.Where(t => t.Name.Contains(name)); |
| 0 | 34 | | } |
| 0 | 35 | | if(realmId != null) |
| 0 | 36 | | { |
| 0 | 37 | | query = query.Where(t => t.RealmId == realmId); |
| 0 | 38 | | } |
| 0 | 39 | | var itemCount = await query.CountAsync(); |
| 0 | 40 | | var PaginationHelper = new PaginationHelper(itemCount, pageNumber, pageSize); |
| 0 | 41 | | var waypoints = await query.AsNoTracking() |
| 0 | 42 | | .Include(w => w.Realm) |
| 0 | 43 | | .OrderBy(a => a.Id) |
| 0 | 44 | | .Skip(pageSize * (pageNumber - 1)) |
| 0 | 45 | | .Take(pageSize) |
| 0 | 46 | | .Select(w => new WaypointListBusinessModel { |
| 0 | 47 | | Id = w.Id, |
| 0 | 48 | | Name = w.Name, |
| 0 | 49 | | RealmId = w.RealmId, |
| 0 | 50 | | RealmName = w.Realm.Name, |
| 0 | 51 | | X = w.X, |
| 0 | 52 | | Y = w.Y, |
| 0 | 53 | | Rotation = w.Rotation, |
| 0 | 54 | | IsParking = w.IsParking, |
| 0 | 55 | | HasCharger = w.HasCharger, |
| 0 | 56 | | IsReserved = w.IsReserved, |
| 0 | 57 | | }) |
| 0 | 58 | | .AsSplitQuery() |
| 0 | 59 | | .ToListAsync(); |
| 0 | 60 | | return (waypoints, PaginationHelper); |
| 0 | 61 | | } |
| | 62 | |
|
| | 63 | | public async Task<WaypointBusinessModel> GetWaypointAsync(int waypointId) |
| 0 | 64 | | { |
| 0 | 65 | | return await _context.Waypoints.AsNoTracking() |
| 0 | 66 | | .Where(w => w.Id == waypointId) |
| 0 | 67 | | .Include(w => w.Realm) |
| 0 | 68 | | .Select(w => new WaypointBusinessModel { |
| 0 | 69 | | Id = w.Id, |
| 0 | 70 | | Name = w.Name, |
| 0 | 71 | | RealmId = w.RealmId, |
| 0 | 72 | | RealmName = w.Realm.Name, |
| 0 | 73 | | X = w.X, |
| 0 | 74 | | Y = w.Y, |
| 0 | 75 | | Rotation = w.Rotation, |
| 0 | 76 | | IsParking = w.IsParking, |
| 0 | 77 | | HasCharger = w.HasCharger, |
| 0 | 78 | | IsReserved = w.IsReserved, |
| 0 | 79 | | }) |
| 0 | 80 | | .FirstOrDefaultAsync() |
| 0 | 81 | | ?? throw new LgdxNotFound404Exception(); |
| 0 | 82 | | } |
| | 83 | |
|
| | 84 | | public async Task<WaypointBusinessModel> CreateWaypointAsync(WaypointCreateBusinessModel waypointCreateBusinessModel) |
| 0 | 85 | | { |
| 0 | 86 | | var realm = await _context.Realms.AsNoTracking() |
| 0 | 87 | | .Where(r => r.Id == waypointCreateBusinessModel.RealmId) |
| 0 | 88 | | .FirstOrDefaultAsync() |
| 0 | 89 | | ?? throw new LgdxValidation400Expection(nameof(waypointCreateBusinessModel.RealmId), "Realm does not exist."); |
| | 90 | |
|
| 0 | 91 | | var waypoint = new Waypoint { |
| 0 | 92 | | Name = waypointCreateBusinessModel.Name, |
| 0 | 93 | | RealmId = waypointCreateBusinessModel.RealmId, |
| 0 | 94 | | X = waypointCreateBusinessModel.X, |
| 0 | 95 | | Y = waypointCreateBusinessModel.Y, |
| 0 | 96 | | Rotation = waypointCreateBusinessModel.Rotation, |
| 0 | 97 | | IsParking = waypointCreateBusinessModel.IsParking, |
| 0 | 98 | | HasCharger = waypointCreateBusinessModel.HasCharger, |
| 0 | 99 | | IsReserved = waypointCreateBusinessModel.IsReserved, |
| 0 | 100 | | }; |
| | 101 | |
|
| 0 | 102 | | await _context.Waypoints.AddAsync(waypoint); |
| 0 | 103 | | await _context.SaveChangesAsync(); |
| 0 | 104 | | return new WaypointBusinessModel { |
| 0 | 105 | | Id = waypoint.Id, |
| 0 | 106 | | Name = waypoint.Name, |
| 0 | 107 | | RealmId = waypoint.RealmId, |
| 0 | 108 | | RealmName = realm.Name, |
| 0 | 109 | | X = waypoint.X, |
| 0 | 110 | | Y = waypoint.Y, |
| 0 | 111 | | Rotation = waypoint.Rotation, |
| 0 | 112 | | IsParking = waypoint.IsParking, |
| 0 | 113 | | HasCharger = waypoint.HasCharger, |
| 0 | 114 | | IsReserved = waypoint.IsReserved, |
| 0 | 115 | | }; |
| 0 | 116 | | } |
| | 117 | |
|
| | 118 | | public async Task<bool> UpdateWaypointAsync(int waypointId, WaypointUpdateBusinessModel waypointUpdateBusinessModel) |
| 0 | 119 | | { |
| 0 | 120 | | return await _context.Waypoints |
| 0 | 121 | | .Where(w => w.Id == waypointId) |
| 0 | 122 | | .ExecuteUpdateAsync(setters => setters |
| 0 | 123 | | .SetProperty(w => w.Name, waypointUpdateBusinessModel.Name) |
| 0 | 124 | | .SetProperty(w => w.X, waypointUpdateBusinessModel.X) |
| 0 | 125 | | .SetProperty(w => w.Y, waypointUpdateBusinessModel.Y) |
| 0 | 126 | | .SetProperty(w => w.Rotation, waypointUpdateBusinessModel.Rotation) |
| 0 | 127 | | .SetProperty(w => w.IsParking, waypointUpdateBusinessModel.IsParking) |
| 0 | 128 | | .SetProperty(w => w.HasCharger, waypointUpdateBusinessModel.HasCharger) |
| 0 | 129 | | .SetProperty(w => w.IsReserved, waypointUpdateBusinessModel.IsReserved) |
| 0 | 130 | | ) == 1; |
| 0 | 131 | | } |
| | 132 | |
|
| | 133 | | public async Task<bool> TestDeleteWaypointAsync(int waypointId) |
| 0 | 134 | | { |
| 0 | 135 | | var depeendencies = await _context.AutoTasksDetail |
| 0 | 136 | | .Include(t => t.AutoTask) |
| 0 | 137 | | .Where(t => t.WaypointId == waypointId) |
| 0 | 138 | | .Where(t => t.AutoTask.CurrentProgressId != (int)ProgressState.Completed && t.AutoTask.CurrentProgressId != (int)P |
| 0 | 139 | | .CountAsync(); |
| 0 | 140 | | if (depeendencies > 0) |
| 0 | 141 | | { |
| 0 | 142 | | throw new LgdxValidation400Expection(nameof(waypointId), $"This waypoint has been used by {depeendencies} running/ |
| | 143 | | } |
| 0 | 144 | | return true; |
| 0 | 145 | | } |
| | 146 | |
|
| | 147 | | public async Task<bool> DeleteWaypointAsync(int waypointId) |
| 0 | 148 | | { |
| 0 | 149 | | return await _context.Waypoints.Where(w => w.Id == waypointId) |
| 0 | 150 | | .ExecuteDeleteAsync() == 1; |
| 0 | 151 | | } |
| | 152 | |
|
| | 153 | | public async Task<IEnumerable<WaypointSearchBusinessModel>> SearchWaypointsAsync(int realmId, string? name) |
| 0 | 154 | | { |
| 0 | 155 | | var n = name ?? string.Empty; |
| 0 | 156 | | return await _context.Waypoints.AsNoTracking() |
| 0 | 157 | | .Where(w => w.RealmId == realmId) |
| 0 | 158 | | .Where(t => t.Name.ToLower().Contains(n.ToLower())) |
| 0 | 159 | | .Take(10) |
| 0 | 160 | | .Select(m => new WaypointSearchBusinessModel { |
| 0 | 161 | | Id = m.Id, |
| 0 | 162 | | Name = m.Name, |
| 0 | 163 | | }) |
| 0 | 164 | | .ToListAsync(); |
| 0 | 165 | | } |
| | 166 | | } |