diff --git a/Controllers/APIController.cs b/Controllers/APIController.cs index ab61240..0ea823a 100644 --- a/Controllers/APIController.cs +++ b/Controllers/APIController.cs @@ -162,6 +162,11 @@ namespace CarCareTracker.Controllers [TypeFilter(typeof(CollaboratorFilter))] [HttpPost] [Route("/api/vehicle/servicerecords/add")] + [Consumes("application/json")] + public IActionResult AddServiceRecordJson(int vehicleId, [FromBody] GenericRecordExportModel input) => AddServiceRecord(vehicleId, input); + [TypeFilter(typeof(CollaboratorFilter))] + [HttpPost] + [Route("/api/vehicle/servicerecords/add")] public IActionResult AddServiceRecord(int vehicleId, GenericRecordExportModel input) { if (vehicleId == default) @@ -213,6 +218,10 @@ namespace CarCareTracker.Controllers } [HttpPut] [Route("/api/vehicle/servicerecords/update")] + [Consumes("application/json")] + public IActionResult UpdateServiceRecordJson([FromBody] GenericRecordExportModel input) => UpdateServiceRecord(input); + [HttpPut] + [Route("/api/vehicle/servicerecords/update")] public IActionResult UpdateServiceRecord(GenericRecordExportModel input) { if (string.IsNullOrWhiteSpace(input.Id) || @@ -278,6 +287,11 @@ namespace CarCareTracker.Controllers [TypeFilter(typeof(CollaboratorFilter))] [HttpPost] [Route("/api/vehicle/repairrecords/add")] + [Consumes("application/json")] + public IActionResult AddRepairRecordJson(int vehicleId, [FromBody] GenericRecordExportModel input) => AddRepairRecord(vehicleId, input); + [TypeFilter(typeof(CollaboratorFilter))] + [HttpPost] + [Route("/api/vehicle/repairrecords/add")] public IActionResult AddRepairRecord(int vehicleId, GenericRecordExportModel input) { if (vehicleId == default) @@ -329,6 +343,10 @@ namespace CarCareTracker.Controllers } [HttpPut] [Route("/api/vehicle/repairrecords/update")] + [Consumes("application/json")] + public IActionResult UpdateRepairRecordJson([FromBody] GenericRecordExportModel input) => UpdateRepairRecord(input); + [HttpPut] + [Route("/api/vehicle/repairrecords/update")] public IActionResult UpdateRepairRecord(GenericRecordExportModel input) { if (string.IsNullOrWhiteSpace(input.Id) || @@ -395,6 +413,11 @@ namespace CarCareTracker.Controllers [TypeFilter(typeof(CollaboratorFilter))] [HttpPost] [Route("/api/vehicle/upgraderecords/add")] + [Consumes("application/json")] + public IActionResult AddUpgradeRecordJson(int vehicleId, [FromBody] GenericRecordExportModel input) => AddUpgradeRecord(vehicleId, input); + [TypeFilter(typeof(CollaboratorFilter))] + [HttpPost] + [Route("/api/vehicle/upgraderecords/add")] public IActionResult AddUpgradeRecord(int vehicleId, GenericRecordExportModel input) { if (vehicleId == default) @@ -446,6 +469,10 @@ namespace CarCareTracker.Controllers } [HttpPut] [Route("/api/vehicle/upgraderecords/update")] + [Consumes("application/json")] + public IActionResult UpdateUpgradeRecordJson([FromBody] GenericRecordExportModel input) => UpdateUpgradeRecord(input); + [HttpPut] + [Route("/api/vehicle/upgraderecords/update")] public IActionResult UpdateUpgradeRecord(GenericRecordExportModel input) { if (string.IsNullOrWhiteSpace(input.Id) || @@ -511,6 +538,11 @@ namespace CarCareTracker.Controllers [TypeFilter(typeof(CollaboratorFilter))] [HttpPost] [Route("/api/vehicle/taxrecords/add")] + [Consumes("application/json")] + public IActionResult AddTaxRecordJson(int vehicleId, [FromBody] TaxRecordExportModel input) => AddTaxRecord(vehicleId, input); + [TypeFilter(typeof(CollaboratorFilter))] + [HttpPost] + [Route("/api/vehicle/taxrecords/add")] public IActionResult AddTaxRecord(int vehicleId, TaxRecordExportModel input) { if (vehicleId == default) @@ -550,6 +582,10 @@ namespace CarCareTracker.Controllers } [HttpPut] [Route("/api/vehicle/taxrecords/update")] + [Consumes("application/json")] + public IActionResult UpdateTaxRecordJson([FromBody] TaxRecordExportModel input) => UpdateTaxRecord(input); + [HttpPut] + [Route("/api/vehicle/taxrecords/update")] public IActionResult UpdateTaxRecord(TaxRecordExportModel input) { if (string.IsNullOrWhiteSpace(input.Id) || @@ -633,6 +669,11 @@ namespace CarCareTracker.Controllers [TypeFilter(typeof(CollaboratorFilter))] [HttpPost] [Route("/api/vehicle/odometerrecords/add")] + [Consumes("application/json")] + public IActionResult AddOdometerRecordJson(int vehicleId, [FromBody] OdometerRecordExportModel input) => AddOdometerRecord(vehicleId, input); + [TypeFilter(typeof(CollaboratorFilter))] + [HttpPost] + [Route("/api/vehicle/odometerrecords/add")] public IActionResult AddOdometerRecord(int vehicleId, OdometerRecordExportModel input) { if (vehicleId == default) @@ -669,6 +710,10 @@ namespace CarCareTracker.Controllers } [HttpPut] [Route("/api/vehicle/odometerrecords/update")] + [Consumes("application/json")] + public IActionResult UpdateOdometerRecordJson([FromBody] OdometerRecordExportModel input) => UpdateOdometerRecord(input); + [HttpPut] + [Route("/api/vehicle/odometerrecords/update")] public IActionResult UpdateOdometerRecord(OdometerRecordExportModel input) { if (string.IsNullOrWhiteSpace(input.Id) || @@ -745,6 +790,11 @@ namespace CarCareTracker.Controllers [TypeFilter(typeof(CollaboratorFilter))] [HttpPost] [Route("/api/vehicle/gasrecords/add")] + [Consumes("application/json")] + public IActionResult AddGasRecordJson(int vehicleId, [FromBody] GasRecordExportModel input) => AddGasRecord(vehicleId, input); + [TypeFilter(typeof(CollaboratorFilter))] + [HttpPost] + [Route("/api/vehicle/gasrecords/add")] public IActionResult AddGasRecord(int vehicleId, GasRecordExportModel input) { if (vehicleId == default) @@ -801,6 +851,10 @@ namespace CarCareTracker.Controllers } [HttpPut] [Route("/api/vehicle/gasrecords/update")] + [Consumes("application/json")] + public IActionResult UpdateGasRecordJson([FromBody] GasRecordExportModel input) => UpdateGasRecord(input); + [HttpPut] + [Route("/api/vehicle/gasrecords/update")] public IActionResult UpdateGasRecord(GasRecordExportModel input) { if (string.IsNullOrWhiteSpace(input.Id) || diff --git a/Models/API/TypeConverter.cs b/Models/API/TypeConverter.cs new file mode 100644 index 0000000..f0d8407 --- /dev/null +++ b/Models/API/TypeConverter.cs @@ -0,0 +1,96 @@ +using System.Text.Json; +using System.Text.Json.Serialization; + +namespace CarCareTracker.Models +{ + class FromDateOptional: JsonConverter + { + public override string? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + { + var tokenType = reader.TokenType; + if (tokenType == JsonTokenType.String) + { + return reader.GetString(); + } + else if (tokenType == JsonTokenType.Number) + { + if (reader.TryGetInt32(out int intInput)) + { + return DateTimeOffset.FromUnixTimeSeconds(intInput).Date.ToShortDateString(); + } + } + return reader.GetString(); + } + public override void Write(Utf8JsonWriter writer, string value, JsonSerializerOptions options) + { + writer.WriteStringValue(value); + } + } + class FromDecimalOptional : JsonConverter + { + public override string? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + { + var tokenType = reader.TokenType; + if (tokenType == JsonTokenType.String) + { + return reader.GetString(); + } + else if (tokenType == JsonTokenType.Number) { + if (reader.TryGetDecimal(out decimal decimalInput)) + { + return decimalInput.ToString(); + } + } + return reader.GetString(); + } + public override void Write(Utf8JsonWriter writer, string value, JsonSerializerOptions options) + { + writer.WriteStringValue(value); + } + } + class FromIntOptional : JsonConverter + { + public override string? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + { + var tokenType = reader.TokenType; + if (tokenType == JsonTokenType.String) + { + return reader.GetString(); + } + else if (tokenType == JsonTokenType.Number) + { + if (reader.TryGetInt32(out int intInput)) + { + return intInput.ToString(); + } + } + return reader.GetString(); + } + public override void Write(Utf8JsonWriter writer, string value, JsonSerializerOptions options) + { + writer.WriteStringValue(value); + } + } + class FromBoolOptional : JsonConverter + { + public override string? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + { + var tokenType = reader.TokenType; + switch (tokenType) + { + case JsonTokenType.String: + return reader.GetString(); + case JsonTokenType.True: + return "True"; + case JsonTokenType.False: + return "False"; + default: + return reader.GetString(); + } + } + public override void Write(Utf8JsonWriter writer, string value, JsonSerializerOptions options) + { + writer.WriteStringValue(value); + } + } +} diff --git a/Models/Shared/ImportModel.cs b/Models/Shared/ImportModel.cs index 791d7ae..ef57a0e 100644 --- a/Models/Shared/ImportModel.cs +++ b/Models/Shared/ImportModel.cs @@ -1,4 +1,6 @@ -namespace CarCareTracker.Models +using System.Text.Json.Serialization; + +namespace CarCareTracker.Models { /// /// Import model used for importing records via CSV. @@ -46,10 +48,13 @@ public class GenericRecordExportModel { public string Id { get; set; } + [JsonConverter(typeof(FromDateOptional))] public string Date { get; set; } + [JsonConverter(typeof(FromIntOptional))] public string Odometer { get; set; } public string Description { get; set; } public string Notes { get; set; } + [JsonConverter(typeof(FromDecimalOptional))] public string Cost { get; set; } public string Tags { get; set; } public List ExtraFields { get; set; } @@ -57,8 +62,11 @@ public class OdometerRecordExportModel { public string Id { get; set; } + [JsonConverter(typeof(FromDateOptional))] public string Date { get; set; } + [JsonConverter(typeof(FromIntOptional))] public string InitialOdometer { get; set; } + [JsonConverter(typeof(FromIntOptional))] public string Odometer { get; set; } public string Notes { get; set; } public string Tags { get; set; } @@ -67,9 +75,11 @@ public class TaxRecordExportModel { public string Id { get; set; } + [JsonConverter(typeof(FromDateOptional))] public string Date { get; set; } public string Description { get; set; } public string Notes { get; set; } + [JsonConverter(typeof(FromDecimalOptional))] public string Cost { get; set; } public string Tags { get; set; } public List ExtraFields { get; set; } @@ -77,12 +87,18 @@ public class GasRecordExportModel { public string Id { get; set; } + [JsonConverter(typeof(FromDateOptional))] public string Date { get; set; } + [JsonConverter(typeof(FromIntOptional))] public string Odometer { get; set; } + [JsonConverter(typeof(FromDecimalOptional))] public string FuelConsumed { get; set; } + [JsonConverter(typeof(FromDecimalOptional))] public string Cost { get; set; } public string FuelEconomy { get; set; } + [JsonConverter(typeof(FromBoolOptional))] public string IsFillToFull { get; set; } + [JsonConverter(typeof(FromBoolOptional))] public string MissedFuelUp { get; set; } public string Notes { get; set; } public string Tags { get; set; }