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