-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added maintenance record controller to the API
- Loading branch information
1 parent
3b01a3f
commit 76e1b68
Showing
9 changed files
with
204 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
134 changes: 134 additions & 0 deletions
134
src/DroneFlightLog.Api/Controllers/MaintenanceRecordsController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/DroneFlightLog.Data/Binders/MaintenanceRecordTypeJsonConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace DroneFlightLog.Mvc.Api | ||
{ | ||
public class MaintenanceRecordClient | ||
{ | ||
} | ||
} |