added invariant exports to get methods.

This commit is contained in:
DESKTOP-T0O5CDB\DESK-555BD 2024-12-03 13:40:57 -07:00
parent 0380382c47
commit 0e49497da1
5 changed files with 129 additions and 14 deletions

View File

@ -126,7 +126,14 @@ namespace CarCareTracker.Controllers
} }
var apiResult = _vehicleLogic.GetVehicleInfo(vehicles); var apiResult = _vehicleLogic.GetVehicleInfo(vehicles);
return Json(apiResult); if (_config.GetInvariantApi() || Request.Headers.ContainsKey("culture-invariant"))
{
return Json(apiResult, StaticHelper.GetInvariantOption());
}
else
{
return Json(apiResult);
}
} }
[TypeFilter(typeof(CollaboratorFilter))] [TypeFilter(typeof(CollaboratorFilter))]
[HttpGet] [HttpGet]
@ -157,7 +164,13 @@ namespace CarCareTracker.Controllers
} }
var vehicleRecords = _serviceRecordDataAccess.GetServiceRecordsByVehicleId(vehicleId); var vehicleRecords = _serviceRecordDataAccess.GetServiceRecordsByVehicleId(vehicleId);
var result = vehicleRecords.Select(x => new GenericRecordExportModel { Id = x.Id.ToString(), Date = x.Date.ToShortDateString(), Description = x.Description, Cost = x.Cost.ToString(), Notes = x.Notes, Odometer = x.Mileage.ToString(), ExtraFields = x.ExtraFields }); var result = vehicleRecords.Select(x => new GenericRecordExportModel { Id = x.Id.ToString(), Date = x.Date.ToShortDateString(), Description = x.Description, Cost = x.Cost.ToString(), Notes = x.Notes, Odometer = x.Mileage.ToString(), ExtraFields = x.ExtraFields });
return Json(result); if (_config.GetInvariantApi() || Request.Headers.ContainsKey("culture-invariant"))
{
return Json(result, StaticHelper.GetInvariantOption());
} else
{
return Json(result);
}
} }
[TypeFilter(typeof(CollaboratorFilter))] [TypeFilter(typeof(CollaboratorFilter))]
[HttpPost] [HttpPost]
@ -282,7 +295,14 @@ namespace CarCareTracker.Controllers
} }
var vehicleRecords = _collisionRecordDataAccess.GetCollisionRecordsByVehicleId(vehicleId); var vehicleRecords = _collisionRecordDataAccess.GetCollisionRecordsByVehicleId(vehicleId);
var result = vehicleRecords.Select(x => new GenericRecordExportModel { Id = x.Id.ToString(), Date = x.Date.ToShortDateString(), Description = x.Description, Cost = x.Cost.ToString(), Notes = x.Notes, Odometer = x.Mileage.ToString(), ExtraFields = x.ExtraFields }); var result = vehicleRecords.Select(x => new GenericRecordExportModel { Id = x.Id.ToString(), Date = x.Date.ToShortDateString(), Description = x.Description, Cost = x.Cost.ToString(), Notes = x.Notes, Odometer = x.Mileage.ToString(), ExtraFields = x.ExtraFields });
return Json(result); if (_config.GetInvariantApi() || Request.Headers.ContainsKey("culture-invariant"))
{
return Json(result, StaticHelper.GetInvariantOption());
}
else
{
return Json(result);
}
} }
[TypeFilter(typeof(CollaboratorFilter))] [TypeFilter(typeof(CollaboratorFilter))]
[HttpPost] [HttpPost]
@ -408,7 +428,14 @@ namespace CarCareTracker.Controllers
} }
var vehicleRecords = _upgradeRecordDataAccess.GetUpgradeRecordsByVehicleId(vehicleId); var vehicleRecords = _upgradeRecordDataAccess.GetUpgradeRecordsByVehicleId(vehicleId);
var result = vehicleRecords.Select(x => new GenericRecordExportModel { Id = x.Id.ToString(), Date = x.Date.ToShortDateString(), Description = x.Description, Cost = x.Cost.ToString(), Notes = x.Notes, Odometer = x.Mileage.ToString(), ExtraFields = x.ExtraFields }); var result = vehicleRecords.Select(x => new GenericRecordExportModel { Id = x.Id.ToString(), Date = x.Date.ToShortDateString(), Description = x.Description, Cost = x.Cost.ToString(), Notes = x.Notes, Odometer = x.Mileage.ToString(), ExtraFields = x.ExtraFields });
return Json(result); if (_config.GetInvariantApi() || Request.Headers.ContainsKey("culture-invariant"))
{
return Json(result, StaticHelper.GetInvariantOption());
}
else
{
return Json(result);
}
} }
[TypeFilter(typeof(CollaboratorFilter))] [TypeFilter(typeof(CollaboratorFilter))]
[HttpPost] [HttpPost]
@ -533,7 +560,14 @@ namespace CarCareTracker.Controllers
return Json(response); return Json(response);
} }
var result = _taxRecordDataAccess.GetTaxRecordsByVehicleId(vehicleId).Select(x => new TaxRecordExportModel { Id = x.Id.ToString(), Date = x.Date.ToShortDateString(), Description = x.Description, Cost = x.Cost.ToString(), Notes = x.Notes, ExtraFields = x.ExtraFields }); var result = _taxRecordDataAccess.GetTaxRecordsByVehicleId(vehicleId).Select(x => new TaxRecordExportModel { Id = x.Id.ToString(), Date = x.Date.ToShortDateString(), Description = x.Description, Cost = x.Cost.ToString(), Notes = x.Notes, ExtraFields = x.ExtraFields });
return Json(result); if (_config.GetInvariantApi() || Request.Headers.ContainsKey("culture-invariant"))
{
return Json(result, StaticHelper.GetInvariantOption());
}
else
{
return Json(result);
}
} }
[TypeFilter(typeof(CollaboratorFilter))] [TypeFilter(typeof(CollaboratorFilter))]
[HttpPost] [HttpPost]
@ -664,7 +698,14 @@ namespace CarCareTracker.Controllers
vehicleRecords = _odometerLogic.AutoConvertOdometerRecord(vehicleRecords); vehicleRecords = _odometerLogic.AutoConvertOdometerRecord(vehicleRecords);
} }
var result = vehicleRecords.Select(x => new OdometerRecordExportModel { Id = x.Id.ToString(), Date = x.Date.ToShortDateString(), InitialOdometer = x.InitialMileage.ToString(), Odometer = x.Mileage.ToString(), Notes = x.Notes, ExtraFields = x.ExtraFields }); var result = vehicleRecords.Select(x => new OdometerRecordExportModel { Id = x.Id.ToString(), Date = x.Date.ToShortDateString(), InitialOdometer = x.InitialMileage.ToString(), Odometer = x.Mileage.ToString(), Notes = x.Notes, ExtraFields = x.ExtraFields });
return Json(result); if (_config.GetInvariantApi() || Request.Headers.ContainsKey("culture-invariant"))
{
return Json(result, StaticHelper.GetInvariantOption());
}
else
{
return Json(result);
}
} }
[TypeFilter(typeof(CollaboratorFilter))] [TypeFilter(typeof(CollaboratorFilter))]
[HttpPost] [HttpPost]
@ -785,7 +826,14 @@ namespace CarCareTracker.Controllers
Notes = x.Notes, Notes = x.Notes,
ExtraFields = x.ExtraFields ExtraFields = x.ExtraFields
}); });
return Json(result); if (_config.GetInvariantApi() || Request.Headers.ContainsKey("culture-invariant"))
{
return Json(result, StaticHelper.GetInvariantOption());
}
else
{
return Json(result);
}
} }
[TypeFilter(typeof(CollaboratorFilter))] [TypeFilter(typeof(CollaboratorFilter))]
[HttpPost] [HttpPost]
@ -919,7 +967,14 @@ namespace CarCareTracker.Controllers
var currentMileage = _vehicleLogic.GetMaxMileage(vehicleId); var currentMileage = _vehicleLogic.GetMaxMileage(vehicleId);
var reminders = _reminderRecordDataAccess.GetReminderRecordsByVehicleId(vehicleId); var reminders = _reminderRecordDataAccess.GetReminderRecordsByVehicleId(vehicleId);
var results = _reminderHelper.GetReminderRecordViewModels(reminders, currentMileage, DateTime.Now).Select(x=> new ReminderExportModel { Description = x.Description, Urgency = x.Urgency.ToString(), Metric = x.Metric.ToString(), Notes = x.Notes, DueDate = x.Date.ToShortDateString(), DueOdometer = x.Mileage.ToString()}); var results = _reminderHelper.GetReminderRecordViewModels(reminders, currentMileage, DateTime.Now).Select(x=> new ReminderExportModel { Description = x.Description, Urgency = x.Urgency.ToString(), Metric = x.Metric.ToString(), Notes = x.Notes, DueDate = x.Date.ToShortDateString(), DueOdometer = x.Mileage.ToString()});
return Json(results); if (_config.GetInvariantApi() || Request.Headers.ContainsKey("culture-invariant"))
{
return Json(results, StaticHelper.GetInvariantOption());
}
else
{
return Json(results);
}
} }
[Authorize(Roles = nameof(UserData.IsRootUser))] [Authorize(Roles = nameof(UserData.IsRootUser))]
[HttpGet] [HttpGet]

View File

@ -24,7 +24,8 @@ namespace CarCareTracker.Helper
bool GetServerEnableShopSupplies(); bool GetServerEnableShopSupplies();
string GetServerPostgresConnection(); string GetServerPostgresConnection();
string GetAllowedFileUploadExtensions(); string GetAllowedFileUploadExtensions();
public bool DeleteUserConfig(int userId); bool DeleteUserConfig(int userId);
bool GetInvariantApi();
} }
public class ConfigHelper : IConfigHelper public class ConfigHelper : IConfigHelper
{ {
@ -51,6 +52,10 @@ namespace CarCareTracker.Helper
{ {
return CheckBool(CheckString("LUBELOGGER_CUSTOM_WIDGETS")); return CheckBool(CheckString("LUBELOGGER_CUSTOM_WIDGETS"));
} }
public bool GetInvariantApi()
{
return CheckBool(CheckString("LUBELOGGER_INVARIANT_API"));
}
public string GetMOTD() public string GetMOTD()
{ {
var motd = CheckString("LUBELOGGER_MOTD"); var motd = CheckString("LUBELOGGER_MOTD");

View File

@ -1,6 +1,7 @@
using CarCareTracker.Models; using CarCareTracker.Models;
using CsvHelper; using CsvHelper;
using System.Globalization; using System.Globalization;
using System.Text.Json;
namespace CarCareTracker.Helper namespace CarCareTracker.Helper
{ {
@ -622,6 +623,12 @@ namespace CarCareTracker.Helper
return string.IsNullOrWhiteSpace(decorations) ? input.ToString("C2") : $"{input.ToString("C2")}{decorations}"; return string.IsNullOrWhiteSpace(decorations) ? input.ToString("C2") : $"{input.ToString("C2")}{decorations}";
} }
} }
public static JsonSerializerOptions GetInvariantOption()
{
var serializerOption = new JsonSerializerOptions();
serializerOption.Converters.Add(new InvariantConverter());
return serializerOption;
}
public static void WriteGasRecordExportModel(CsvWriter _csv, IEnumerable<GasRecordExportModel> genericRecords) public static void WriteGasRecordExportModel(CsvWriter _csv, IEnumerable<GasRecordExportModel> genericRecords)
{ {
var extraHeaders = genericRecords.SelectMany(x => x.ExtraFields).Select(y => y.Name).Distinct(); var extraHeaders = genericRecords.SelectMany(x => x.ExtraFields).Select(y => y.Name).Distinct();

View File

@ -3,6 +3,21 @@ using System.Text.Json.Serialization;
namespace CarCareTracker.Models namespace CarCareTracker.Models
{ {
public class DummyType
{
}
class InvariantConverter : JsonConverter<DummyType>
{
public override void Write(Utf8JsonWriter writer, DummyType value, JsonSerializerOptions options)
{
throw new NotImplementedException();
}
public override DummyType? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
throw new NotImplementedException();
}
}
class FromDateOptional: JsonConverter<string> class FromDateOptional: JsonConverter<string>
{ {
public override string? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) public override string? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
@ -14,7 +29,7 @@ namespace CarCareTracker.Models
} }
else if (tokenType == JsonTokenType.Number) else if (tokenType == JsonTokenType.Number)
{ {
if (reader.TryGetInt32(out int intInput)) if (reader.TryGetInt64(out long intInput))
{ {
return DateTimeOffset.FromUnixTimeSeconds(intInput).Date.ToShortDateString(); return DateTimeOffset.FromUnixTimeSeconds(intInput).Date.ToShortDateString();
} }
@ -23,7 +38,14 @@ namespace CarCareTracker.Models
} }
public override void Write(Utf8JsonWriter writer, string value, JsonSerializerOptions options) public override void Write(Utf8JsonWriter writer, string value, JsonSerializerOptions options)
{ {
writer.WriteStringValue(value); if (options.Converters.Any(x => x.Type == typeof(DummyType)))
{
writer.WriteStringValue(DateTime.Parse(value).ToString("yyyy-MM-dd"));
}
else
{
writer.WriteStringValue(value);
}
} }
} }
class FromDecimalOptional : JsonConverter<string> class FromDecimalOptional : JsonConverter<string>
@ -45,7 +67,13 @@ namespace CarCareTracker.Models
} }
public override void Write(Utf8JsonWriter writer, string value, JsonSerializerOptions options) public override void Write(Utf8JsonWriter writer, string value, JsonSerializerOptions options)
{ {
writer.WriteStringValue(value); if (options.Converters.Any(x=>x.Type == typeof(DummyType)))
{
writer.WriteNumberValue(decimal.Parse(value));
} else
{
writer.WriteStringValue(value);
}
} }
} }
class FromIntOptional : JsonConverter<string> class FromIntOptional : JsonConverter<string>
@ -68,7 +96,14 @@ namespace CarCareTracker.Models
} }
public override void Write(Utf8JsonWriter writer, string value, JsonSerializerOptions options) public override void Write(Utf8JsonWriter writer, string value, JsonSerializerOptions options)
{ {
writer.WriteStringValue(value); if (options.Converters.Any(x => x.Type == typeof(DummyType)))
{
writer.WriteNumberValue(int.Parse(value));
}
else
{
writer.WriteStringValue(value);
}
} }
} }
class FromBoolOptional : JsonConverter<string> class FromBoolOptional : JsonConverter<string>
@ -90,7 +125,14 @@ namespace CarCareTracker.Models
} }
public override void Write(Utf8JsonWriter writer, string value, JsonSerializerOptions options) public override void Write(Utf8JsonWriter writer, string value, JsonSerializerOptions options)
{ {
writer.WriteStringValue(value); if (options.Converters.Any(x => x.Type == typeof(DummyType)))
{
writer.WriteBooleanValue(bool.Parse(value));
}
else
{
writer.WriteStringValue(value);
}
} }
} }
} }

View File

@ -47,6 +47,7 @@ namespace CarCareTracker.Models
} }
public class GenericRecordExportModel public class GenericRecordExportModel
{ {
[JsonConverter(typeof(FromIntOptional))]
public string Id { get; set; } public string Id { get; set; }
[JsonConverter(typeof(FromDateOptional))] [JsonConverter(typeof(FromDateOptional))]
public string Date { get; set; } public string Date { get; set; }
@ -61,6 +62,7 @@ namespace CarCareTracker.Models
} }
public class OdometerRecordExportModel public class OdometerRecordExportModel
{ {
[JsonConverter(typeof(FromIntOptional))]
public string Id { get; set; } public string Id { get; set; }
[JsonConverter(typeof(FromDateOptional))] [JsonConverter(typeof(FromDateOptional))]
public string Date { get; set; } public string Date { get; set; }
@ -74,6 +76,7 @@ namespace CarCareTracker.Models
} }
public class TaxRecordExportModel public class TaxRecordExportModel
{ {
[JsonConverter(typeof(FromIntOptional))]
public string Id { get; set; } public string Id { get; set; }
[JsonConverter(typeof(FromDateOptional))] [JsonConverter(typeof(FromDateOptional))]
public string Date { get; set; } public string Date { get; set; }
@ -86,6 +89,7 @@ namespace CarCareTracker.Models
} }
public class GasRecordExportModel public class GasRecordExportModel
{ {
[JsonConverter(typeof(FromIntOptional))]
public string Id { get; set; } public string Id { get; set; }
[JsonConverter(typeof(FromDateOptional))] [JsonConverter(typeof(FromDateOptional))]
public string Date { get; set; } public string Date { get; set; }
@ -110,7 +114,9 @@ namespace CarCareTracker.Models
public string Urgency { get; set; } public string Urgency { get; set; }
public string Metric { get; set; } public string Metric { get; set; }
public string Notes { get; set; } public string Notes { get; set; }
[JsonConverter(typeof(FromDateOptional))]
public string DueDate { get; set; } public string DueDate { get; set; }
[JsonConverter(typeof(FromIntOptional))]
public string DueOdometer { get; set; } public string DueOdometer { get; set; }
} }
public class PlanRecordExportModel public class PlanRecordExportModel