mirror of
https://github.com/azaion/flights.git
synced 2026-04-22 07:06:32 +00:00
Initial commit
Made-with: Cursor
This commit is contained in:
@@ -0,0 +1,102 @@
|
||||
using Azaion.Flights.Database;
|
||||
using Azaion.Flights.Database.Entities;
|
||||
using Azaion.Flights.DTOs;
|
||||
|
||||
namespace Azaion.Flights.Services;
|
||||
|
||||
public class AircraftService(AppDataConnection db)
|
||||
{
|
||||
public async Task<Aircraft> CreateAircraft(CreateAircraftRequest request)
|
||||
{
|
||||
if (request.IsDefault)
|
||||
await db.Aircrafts.Where(a => a.IsDefault).Set(a => a.IsDefault, false).UpdateAsync();
|
||||
|
||||
var aircraft = new Aircraft
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
Type = request.Type,
|
||||
Model = request.Model,
|
||||
Name = request.Name,
|
||||
FuelType = request.FuelType,
|
||||
BatteryCapacity = request.BatteryCapacity,
|
||||
EngineConsumption = request.EngineConsumption,
|
||||
EngineConsumptionIdle = request.EngineConsumptionIdle,
|
||||
IsDefault = request.IsDefault
|
||||
};
|
||||
await db.InsertAsync(aircraft);
|
||||
return aircraft;
|
||||
}
|
||||
|
||||
public async Task<Aircraft> UpdateAircraft(Guid id, UpdateAircraftRequest request)
|
||||
{
|
||||
var aircraft = await db.Aircrafts.FirstOrDefaultAsync(a => a.Id == id)
|
||||
?? throw new KeyNotFoundException($"Aircraft {id} not found");
|
||||
|
||||
if (request.Type.HasValue)
|
||||
aircraft.Type = request.Type.Value;
|
||||
if (request.Model != null)
|
||||
aircraft.Model = request.Model;
|
||||
if (request.Name != null)
|
||||
aircraft.Name = request.Name;
|
||||
if (request.FuelType.HasValue)
|
||||
aircraft.FuelType = request.FuelType.Value;
|
||||
if (request.BatteryCapacity.HasValue)
|
||||
aircraft.BatteryCapacity = request.BatteryCapacity.Value;
|
||||
if (request.EngineConsumption.HasValue)
|
||||
aircraft.EngineConsumption = request.EngineConsumption.Value;
|
||||
if (request.EngineConsumptionIdle.HasValue)
|
||||
aircraft.EngineConsumptionIdle = request.EngineConsumptionIdle.Value;
|
||||
if (request.IsDefault.HasValue)
|
||||
{
|
||||
if (request.IsDefault.Value)
|
||||
await db.Aircrafts.Where(a => a.IsDefault).Set(a => a.IsDefault, false).UpdateAsync();
|
||||
aircraft.IsDefault = request.IsDefault.Value;
|
||||
}
|
||||
|
||||
await db.UpdateAsync(aircraft);
|
||||
return aircraft;
|
||||
}
|
||||
|
||||
public async Task<Aircraft> GetAircraft(Guid id)
|
||||
{
|
||||
var aircraft = await db.Aircrafts.FirstOrDefaultAsync(a => a.Id == id)
|
||||
?? throw new KeyNotFoundException($"Aircraft {id} not found");
|
||||
return aircraft;
|
||||
}
|
||||
|
||||
public async Task<List<Aircraft>> GetAircrafts(GetAircraftsQuery query)
|
||||
{
|
||||
var q = db.Aircrafts.AsQueryable();
|
||||
|
||||
if (!string.IsNullOrEmpty(query.Name))
|
||||
q = q.Where(a => a.Name.ToLower().Contains(query.Name.ToLower()));
|
||||
if (query.IsDefault.HasValue)
|
||||
q = q.Where(a => a.IsDefault == query.IsDefault.Value);
|
||||
|
||||
return await q.OrderBy(a => a.Name).ToListAsync();
|
||||
}
|
||||
|
||||
public async Task DeleteAircraft(Guid id)
|
||||
{
|
||||
var hasFlights = await db.Flights.AnyAsync(f => f.AircraftId == id);
|
||||
if (hasFlights)
|
||||
throw new InvalidOperationException($"Aircraft {id} is referenced by flights");
|
||||
|
||||
var aircraft = await db.Aircrafts.FirstOrDefaultAsync(a => a.Id == id)
|
||||
?? throw new KeyNotFoundException($"Aircraft {id} not found");
|
||||
|
||||
await db.Aircrafts.DeleteAsync(a => a.Id == id);
|
||||
}
|
||||
|
||||
public async Task SetDefault(Guid id, SetDefaultRequest request)
|
||||
{
|
||||
var aircraft = await db.Aircrafts.FirstOrDefaultAsync(a => a.Id == id)
|
||||
?? throw new KeyNotFoundException($"Aircraft {id} not found");
|
||||
|
||||
if (request.IsDefault)
|
||||
await db.Aircrafts.Where(a => a.IsDefault).Set(a => a.IsDefault, false).UpdateAsync();
|
||||
|
||||
aircraft.IsDefault = request.IsDefault;
|
||||
await db.UpdateAsync(aircraft);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
using LinqToDB;
|
||||
using Azaion.Flights.Database;
|
||||
using Azaion.Flights.Database.Entities;
|
||||
using Azaion.Flights.DTOs;
|
||||
|
||||
namespace Azaion.Flights.Services;
|
||||
|
||||
public class FlightService(AppDataConnection db)
|
||||
{
|
||||
public async Task<Flight> CreateFlight(CreateFlightRequest request)
|
||||
{
|
||||
var aircraftExists = await db.Aircrafts.AnyAsync(a => a.Id == request.AircraftId);
|
||||
if (!aircraftExists)
|
||||
throw new ArgumentException($"Aircraft {request.AircraftId} not found");
|
||||
|
||||
var flight = new Flight
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
CreatedDate = request.CreatedDate ?? DateTime.UtcNow,
|
||||
Name = request.Name,
|
||||
AircraftId = request.AircraftId
|
||||
};
|
||||
await db.InsertAsync(flight);
|
||||
return flight;
|
||||
}
|
||||
|
||||
public async Task<Flight> UpdateFlight(Guid id, UpdateFlightRequest request)
|
||||
{
|
||||
var flight = await db.Flights.FirstOrDefaultAsync(f => f.Id == id)
|
||||
?? throw new KeyNotFoundException($"Flight {id} not found");
|
||||
|
||||
if (request.Name != null)
|
||||
flight.Name = request.Name;
|
||||
if (request.AircraftId.HasValue)
|
||||
{
|
||||
var aircraftExists = await db.Aircrafts.AnyAsync(a => a.Id == request.AircraftId.Value);
|
||||
if (!aircraftExists)
|
||||
throw new ArgumentException($"Aircraft {request.AircraftId} not found");
|
||||
flight.AircraftId = request.AircraftId.Value;
|
||||
}
|
||||
|
||||
await db.UpdateAsync(flight);
|
||||
return flight;
|
||||
}
|
||||
|
||||
public async Task<Flight> GetFlight(Guid id)
|
||||
{
|
||||
var flight = await db.Flights.FirstOrDefaultAsync(f => f.Id == id)
|
||||
?? throw new KeyNotFoundException($"Flight {id} not found");
|
||||
return flight;
|
||||
}
|
||||
|
||||
public async Task<PaginatedResponse<Flight>> GetFlights(GetFlightsQuery query)
|
||||
{
|
||||
var q = db.Flights.AsQueryable();
|
||||
|
||||
if (!string.IsNullOrEmpty(query.Name))
|
||||
q = q.Where(f => f.Name.ToLower().Contains(query.Name.ToLower()));
|
||||
if (query.FromDate.HasValue)
|
||||
q = q.Where(f => f.CreatedDate >= query.FromDate.Value);
|
||||
if (query.ToDate.HasValue)
|
||||
q = q.Where(f => f.CreatedDate <= query.ToDate.Value);
|
||||
|
||||
var totalCount = await q.CountAsync();
|
||||
|
||||
var items = await q
|
||||
.OrderByDescending(f => f.CreatedDate)
|
||||
.Skip((query.Page - 1) * query.PageSize)
|
||||
.Take(query.PageSize)
|
||||
.ToListAsync();
|
||||
|
||||
return new PaginatedResponse<Flight>
|
||||
{
|
||||
Items = items,
|
||||
TotalCount = totalCount,
|
||||
Page = query.Page,
|
||||
PageSize = query.PageSize
|
||||
};
|
||||
}
|
||||
|
||||
public async Task DeleteFlight(Guid id)
|
||||
{
|
||||
var flight = await db.Flights.FirstOrDefaultAsync(f => f.Id == id)
|
||||
?? throw new KeyNotFoundException($"Flight {id} not found");
|
||||
|
||||
await db.MapObjects.DeleteAsync(m => m.FlightId == id);
|
||||
await db.GpsCorrections.DeleteAsync(g => g.FlightId == id);
|
||||
await db.Orthophotos.DeleteAsync(o => o.FlightId == id);
|
||||
|
||||
var waypointIds = await db.Waypoints.Where(w => w.FlightId == id).Select(w => w.Id).ToListAsync();
|
||||
if (waypointIds.Count > 0)
|
||||
{
|
||||
var mediaIds = await db.Media.Where(m => m.WaypointId != null && waypointIds.Contains(m.WaypointId!.Value))
|
||||
.Select(m => m.Id).ToListAsync();
|
||||
if (mediaIds.Count > 0)
|
||||
{
|
||||
var annotationIds = await db.Annotations.Where(a => mediaIds.Contains(a.MediaId))
|
||||
.Select(a => a.Id).ToListAsync();
|
||||
if (annotationIds.Count > 0)
|
||||
await db.Detections.DeleteAsync(d => annotationIds.Contains(d.AnnotationId));
|
||||
await db.Annotations.DeleteAsync(a => mediaIds.Contains(a.MediaId));
|
||||
}
|
||||
await db.Media.DeleteAsync(m => m.WaypointId != null && waypointIds.Contains(m.WaypointId!.Value));
|
||||
}
|
||||
|
||||
await db.Waypoints.DeleteAsync(w => w.FlightId == id);
|
||||
await db.Flights.DeleteAsync(f => f.Id == id);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
using Azaion.Flights.Database;
|
||||
using Azaion.Flights.Database.Entities;
|
||||
using Azaion.Flights.DTOs;
|
||||
using Azaion.Flights.Enums;
|
||||
|
||||
namespace Azaion.Flights.Services;
|
||||
|
||||
public class WaypointService(AppDataConnection db)
|
||||
{
|
||||
public async Task<Waypoint> CreateWaypoint(Guid flightId, CreateWaypointRequest request)
|
||||
{
|
||||
var flightExists = await db.Flights.AnyAsync(f => f.Id == flightId);
|
||||
if (!flightExists)
|
||||
throw new KeyNotFoundException($"Flight {flightId} not found");
|
||||
|
||||
var waypoint = new Waypoint
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
FlightId = flightId,
|
||||
Lat = request.GeoPoint?.Lat,
|
||||
Lon = request.GeoPoint?.Lon,
|
||||
Mgrs = request.GeoPoint?.Mgrs,
|
||||
WaypointSource = request.WaypointSource,
|
||||
WaypointObjective = request.WaypointObjective,
|
||||
OrderNum = request.OrderNum,
|
||||
Height = request.Height
|
||||
};
|
||||
await db.InsertAsync(waypoint);
|
||||
return waypoint;
|
||||
}
|
||||
|
||||
public async Task<Waypoint> UpdateWaypoint(Guid flightId, Guid waypointId, UpdateWaypointRequest request)
|
||||
{
|
||||
var waypoint = await db.Waypoints.FirstOrDefaultAsync(w => w.FlightId == flightId && w.Id == waypointId)
|
||||
?? throw new KeyNotFoundException($"Waypoint {waypointId} not found");
|
||||
|
||||
waypoint.Lat = request.GeoPoint?.Lat;
|
||||
waypoint.Lon = request.GeoPoint?.Lon;
|
||||
waypoint.Mgrs = request.GeoPoint?.Mgrs;
|
||||
waypoint.WaypointSource = request.WaypointSource;
|
||||
waypoint.WaypointObjective = request.WaypointObjective;
|
||||
waypoint.OrderNum = request.OrderNum;
|
||||
waypoint.Height = request.Height;
|
||||
|
||||
await db.UpdateAsync(waypoint);
|
||||
return waypoint;
|
||||
}
|
||||
|
||||
public async Task<List<Waypoint>> GetWaypoints(Guid flightId)
|
||||
{
|
||||
return await db.Waypoints
|
||||
.Where(w => w.FlightId == flightId)
|
||||
.OrderBy(w => w.OrderNum)
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
public async Task DeleteWaypoint(Guid flightId, Guid waypointId)
|
||||
{
|
||||
var waypoint = await db.Waypoints.FirstOrDefaultAsync(w => w.FlightId == flightId && w.Id == waypointId)
|
||||
?? throw new KeyNotFoundException($"Waypoint {waypointId} not found");
|
||||
|
||||
var mediaIds = await db.Media.Where(m => m.WaypointId == waypointId).Select(m => m.Id).ToListAsync();
|
||||
if (mediaIds.Count > 0)
|
||||
{
|
||||
var annotationIds = await db.Annotations.Where(a => mediaIds.Contains(a.MediaId))
|
||||
.Select(a => a.Id).ToListAsync();
|
||||
if (annotationIds.Count > 0)
|
||||
await db.Detections.DeleteAsync(d => annotationIds.Contains(d.AnnotationId));
|
||||
await db.Annotations.DeleteAsync(a => mediaIds.Contains(a.MediaId));
|
||||
}
|
||||
await db.Media.DeleteAsync(m => m.WaypointId == waypointId);
|
||||
await db.GpsCorrections.DeleteAsync(g => g.WaypointId == waypointId);
|
||||
await db.Waypoints.DeleteAsync(w => w.Id == waypointId);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user