Skip to content

Commit

Permalink
Added maintenance record controller to the API
Browse files Browse the repository at this point in the history
  • Loading branch information
davewalker5 committed Mar 12, 2024
1 parent 3b01a3f commit 76e1b68
Show file tree
Hide file tree
Showing 9 changed files with 204 additions and 22 deletions.
10 changes: 5 additions & 5 deletions src/DroneFlightLog.Api/Controllers/FlightsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public async Task<ActionResult<List<Flight>>> GetFlightsAsync(int page, int page
{
List<Flight> flights = await _factory.Flights.FindFlightsAsync(null, null, null, null, null, page, pageSize).ToListAsync();

if (!flights.Any())
if (flights.Count == 0)
{
return NoContent();
}
Expand All @@ -47,7 +47,7 @@ public async Task<ActionResult<List<Flight>>> FindFlightsByOperatorAsync(int ope
{
List<Flight> flights = await _factory.Flights.FindFlightsAsync(operatorId, null, null, null, null, page, pageSize).ToListAsync();

if (!flights.Any())
if (flights.Count == 0)
{
return NoContent();
}
Expand All @@ -61,7 +61,7 @@ public async Task<ActionResult<List<Flight>>> FindFlightsByDroneAsync(int droneI
{
List<Flight> flights = await _factory.Flights.FindFlightsAsync(null, droneId, null, null, null, page, pageSize).ToListAsync();

if (!flights.Any())
if (flights.Count == 0)
{
return NoContent();
}
Expand All @@ -75,7 +75,7 @@ public async Task<ActionResult<List<Flight>>> FindFlightsByLocationAsync(int loc
{
List<Flight> flights = await _factory.Flights.FindFlightsAsync(null, null, locationId, null, null, page, pageSize).ToListAsync();

if (!flights.Any())
if (flights.Count == 0)
{
return NoContent();
}
Expand All @@ -92,7 +92,7 @@ public async Task<ActionResult<List<Flight>>> FindFlightsByDateAsync(string star

List<Flight> flights = await _factory.Flights.FindFlightsAsync(null, null, null, flightStart, flightEnd, page, pageSize).ToListAsync();

if (!flights.Any())
if (flights.Count == 0)
{
return NoContent();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public async Task<ActionResult<List<Maintainer>>> GetMaintainersAsync()
{
List<Maintainer> maintainers = await _factory.Maintainers.GetMaintainersAsync().ToListAsync();

if (!maintainers.Any())
if (maintainers.Count == 0)
{
return NoContent();
}
Expand Down
134 changes: 134 additions & 0 deletions src/DroneFlightLog.Api/Controllers/MaintenanceRecordsController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
using DroneFlightLog.Data.Entities;
using DroneFlightLog.Data.Exceptions;
using DroneFlightLog.Data.Interfaces;
using DroneFlightLog.Data.Sqlite;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;

namespace DroneFlightLog.Api.Controllers
{
[ApiController]
[ApiConventionType(typeof(DefaultApiConventions))]
[Route("[controller]")]
public class MaintenanceRecordsController : Controller
{
private const string DateTimeFormat = "yyyy-MM-dd H:mm:ss";

private readonly IDroneFlightLogFactory<DroneFlightLogDbContext> _factory;

public MaintenanceRecordsController(IDroneFlightLogFactory<DroneFlightLogDbContext> factory)
{
_factory = factory;
}

[HttpGet]
[Route("{id}")]
public async Task<ActionResult<MaintenanceRecord>> GetMaintenanceRecordAsync(int id)
{
MaintenanceRecord maintenanceRecord;

try
{
maintenanceRecord = await _factory.MaintenanceRecords.GetMaintenanceRecordAsync(id);
}
catch (MaintenanceRecordNotFoundException)
{
return NotFound();
}

return maintenanceRecord;
}

[HttpGet]
[Route("{droneId}/{start}/{end}/{page}/{pageSize}")]
public async Task<ActionResult<List<MaintenanceRecord>>> FindMaintenanceRecordsAsync(int droneId, string start, string end, int page, int pageSize)
{
DateTime recordsFrom = DateTime.ParseExact(HttpUtility.UrlDecode(start), DateTimeFormat, null);
DateTime recordsTo = DateTime.ParseExact(HttpUtility.UrlDecode(end), DateTimeFormat, null);

List<MaintenanceRecord> maintenanceRecords = await _factory.MaintenanceRecords
.FindMaintenanceRecordsAsync(
null,
droneId,
recordsFrom,
recordsTo,
page,
pageSize)
.ToListAsync();

if (maintenanceRecords.Count == 0)
{
return NoContent();
}

return maintenanceRecords;
}

[HttpPut]
[Route("")]
public async Task<ActionResult<MaintenanceRecord>> UpdateMaintenanceRecordAsync([FromBody] MaintenanceRecord template)
{
MaintenanceRecord maintenanceRecord;

try
{
maintenanceRecord = await _factory.MaintenanceRecords.UpdateMaintenanceRecordAsync(
template.Id,
template.MaintainerId,
template.DroneId,
template.RecordType,
template.DateCompleted,
template.Description,
template.Notes);
await _factory.Context.SaveChangesAsync();
}
catch (MaintainerNotFoundException)
{
return BadRequest();
}
catch (DroneNotFoundException)
{
return BadRequest();
}
catch (MaintenanceRecordNotFoundException)
{
return NotFound();
}

return maintenanceRecord;
}

[HttpPost]
[Route("")]
public async Task<ActionResult<MaintenanceRecord>> CreateMaintenanceRecordAsync([FromBody] MaintenanceRecord template)
{
MaintenanceRecord maintainer;

try
{
maintainer = await _factory.MaintenanceRecords.AddMaintenanceRecordAsync(
template.MaintainerId,
template.DroneId,
template.RecordType,
template.DateCompleted,
template.Description,
template.Notes);
await _factory.Context.SaveChangesAsync();
}
catch (MaintainerNotFoundException)
{
return BadRequest();
}
catch (DroneNotFoundException)
{
return BadRequest();
}

return maintainer;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using DroneFlightLog.Data.Entities;
using System;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace DroneFlightLog.Data.Binders
{
public class MaintenanceRecordTypeJsonConverter : JsonConverter<MaintenanceRecordType>
{
public override MaintenanceRecordType Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
string value = reader.GetString();

if (Enum.TryParse(typeToConvert, value, true, out object result))
{
return (MaintenanceRecordType)result;
}

throw new JsonException($"Unable to convert \"{value}\" to enum {typeToConvert}.");
}

public override void Write(Utf8JsonWriter writer, MaintenanceRecordType value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.ToString());
}
}
}
6 changes: 5 additions & 1 deletion src/DroneFlightLog.Data/Entities/MaintenanceRecord.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using DroneFlightLog.Data.Binders;
using System;
using System.ComponentModel.DataAnnotations;
using System.Diagnostics.CodeAnalysis;
using System.Text.Json.Serialization;

namespace DroneFlightLog.Data.Entities
{
Expand All @@ -12,6 +14,8 @@ public class MaintenanceRecord
public int MaintainerId { get; set; }
public int DroneId { get; set; }
public DateTime DateCompleted { get; set; }

[JsonConverter(typeof(MaintenanceRecordTypeJsonConverter))]
public MaintenanceRecordType RecordType { get; set; }
public string Description { get; set; }
public string Notes { get; set; }
Expand Down
7 changes: 5 additions & 2 deletions src/DroneFlightLog.Data/Entities/MaintenanceRecordType.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
namespace DroneFlightLog.Data.Entities
using System.Runtime.Serialization;

namespace DroneFlightLog.Data.Entities
{
public enum MaintenanceRecordType
{
Maintenance,
Modification
Modification,
Repair
}
}
22 changes: 11 additions & 11 deletions src/DroneFlightLog.Data/Factory/DroneFlightLogFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ namespace DroneFlightLog.Data.Factory
{
public class DroneFlightLogFactory<T> : IDroneFlightLogFactory<T> where T : DbContext, IDroneFlightLogDbContext
{
private Lazy<IFlightPropertyManager> _properties;
private Lazy<IAddressManager> _addresses;
private Lazy<IOperatorManager> _operators;
private Lazy<IManufacturerManager> _manufacturers;
private Lazy<IModelManager> _models;
private Lazy<IDroneManager> _drones;
private Lazy<ILocationManager> _locations;
private Lazy<IFlightManager> _flights;
private Lazy<IUserManager> _users;
private Lazy<IMaintainerManager> _maintainers;
private Lazy<IMaintenanceRecordManager> _maintenanceRecords;
private readonly Lazy<IFlightPropertyManager> _properties;
private readonly Lazy<IAddressManager> _addresses;
private readonly Lazy<IOperatorManager> _operators;
private readonly Lazy<IManufacturerManager> _manufacturers;
private readonly Lazy<IModelManager> _models;
private readonly Lazy<IDroneManager> _drones;
private readonly Lazy<ILocationManager> _locations;
private readonly Lazy<IFlightManager> _flights;
private readonly Lazy<IUserManager> _users;
private readonly Lazy<IMaintainerManager> _maintainers;
private readonly Lazy<IMaintenanceRecordManager> _maintenanceRecords;

public DroneFlightLogFactory(T context)
{
Expand Down
12 changes: 10 additions & 2 deletions src/DroneFlightLog.Data/Logic/MaintenanceRecordManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,10 @@ public async Task<MaintenanceRecord> AddMaintenanceRecordAsync(int maintainerId,
/// <returns></returns>
public MaintenanceRecord GetMaintenanceRecord(int id)
{
MaintenanceRecord maintenanceRecord = _factory.Context.MaintenanceRecords.FirstOrDefault(m => m.Id == id);
MaintenanceRecord maintenanceRecord = _factory.Context
.MaintenanceRecords
.Include(m => m.Maintainer)
.FirstOrDefault(m => m.Id == id);
ThrowIfMaintenanceRecordNotFound(maintenanceRecord, id);
return maintenanceRecord;
}
Expand All @@ -98,7 +101,10 @@ public MaintenanceRecord GetMaintenanceRecord(int id)
/// <returns></returns>
public async Task<MaintenanceRecord> GetMaintenanceRecordAsync(int id)
{
MaintenanceRecord maintenanceRecord = await _factory.Context.MaintenanceRecords.FirstOrDefaultAsync(m => m.Id == id);
MaintenanceRecord maintenanceRecord = await _factory.Context
.MaintenanceRecords
.Include(m => m.Maintainer)
.FirstOrDefaultAsync(m => m.Id == id);
ThrowIfMaintenanceRecordNotFound(maintenanceRecord, id);
return maintenanceRecord;
}
Expand Down Expand Up @@ -178,6 +184,7 @@ public IEnumerable<MaintenanceRecord> FindMaintenanceRecords(int? maintainerId,
((droneId == null) || (droneId == m.DroneId)) &&
((start == null) || (m.DateCompleted >= start)) &&
((end == null) || (m.DateCompleted <= end)))
.OrderBy(m => m.DateCompleted)
.Skip((pageNumber - 1) * pageSize)
.Take(pageSize);

Expand All @@ -199,6 +206,7 @@ public IAsyncEnumerable<MaintenanceRecord> FindMaintenanceRecordsAsync(int? main
((droneId == null) || (droneId == m.DroneId)) &&
((start == null) || (m.DateCompleted >= start)) &&
((end == null) || (m.DateCompleted <= end)))
.OrderBy(m => m.DateCompleted)
.Skip((pageNumber - 1) * pageSize)
.Take(pageSize)
.AsAsyncEnumerable();
Expand Down
6 changes: 6 additions & 0 deletions src/DroneFlightLog.Mvc/Api/MaintenanceRecordClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace DroneFlightLog.Mvc.Api
{
public class MaintenanceRecordClient
{
}
}

0 comments on commit 76e1b68

Please sign in to comment.