From 1eb6e2cedf5066bf90b77ab40bde8c709a394191 Mon Sep 17 00:00:00 2001 From: "DESKTOP-T0O5CDB\\DESK-555BD" Date: Tue, 9 Jul 2024 14:48:09 -0600 Subject: [PATCH] enable extra fields to be imported via CSV --- Controllers/VehicleController.cs | 24 ++++++++++++++++-------- MapProfile/ImportMappers.cs | 12 ++++++++++++ Models/Shared/ImportModel.cs | 1 + 3 files changed, 29 insertions(+), 8 deletions(-) diff --git a/Controllers/VehicleController.cs b/Controllers/VehicleController.cs index 039b8737..4b9f38e7 100644 --- a/Controllers/VehicleController.cs +++ b/Controllers/VehicleController.cs @@ -439,7 +439,8 @@ public IActionResult ImportToVehicleIdFromCsv(int vehicleId, ImportMode mode, st Mileage = decimal.ToInt32(decimal.Parse(importModel.Odometer, NumberStyles.Any)), Gallons = decimal.Parse(importModel.FuelConsumed, NumberStyles.Any), Notes = string.IsNullOrWhiteSpace(importModel.Notes) ? "" : importModel.Notes, - Tags = string.IsNullOrWhiteSpace(importModel.Tags) ? [] : importModel.Tags.Split(" ").ToList() + Tags = string.IsNullOrWhiteSpace(importModel.Tags) ? [] : importModel.Tags.Split(" ").ToList(), + ExtraFields = importModel.ExtraFields.Any() ? importModel.ExtraFields.Select(x => new ExtraField { Name = x.Key, Value = x.Value }).ToList() : new List() }; if (string.IsNullOrWhiteSpace(importModel.Cost) && !string.IsNullOrWhiteSpace(importModel.Price)) { @@ -495,7 +496,8 @@ public IActionResult ImportToVehicleIdFromCsv(int vehicleId, ImportMode mode, st Description = string.IsNullOrWhiteSpace(importModel.Description) ? $"Service Record on {importModel.Date}" : importModel.Description, Notes = string.IsNullOrWhiteSpace(importModel.Notes) ? "" : importModel.Notes, Cost = decimal.Parse(importModel.Cost, NumberStyles.Any), - Tags = string.IsNullOrWhiteSpace(importModel.Tags) ? [] : importModel.Tags.Split(" ").ToList() + Tags = string.IsNullOrWhiteSpace(importModel.Tags) ? [] : importModel.Tags.Split(" ").ToList(), + ExtraFields = importModel.ExtraFields.Any() ? importModel.ExtraFields.Select(x => new ExtraField { Name = x.Key, Value = x.Value }).ToList() : new List() }; _serviceRecordDataAccess.SaveServiceRecordToVehicle(convertedRecord); if (_config.GetUserConfig(User).EnableAutoOdometerInsert) @@ -518,7 +520,8 @@ public IActionResult ImportToVehicleIdFromCsv(int vehicleId, ImportMode mode, st InitialMileage = string.IsNullOrWhiteSpace(importModel.InitialOdometer) ? 0 : decimal.ToInt32(decimal.Parse(importModel.InitialOdometer, NumberStyles.Any)), Mileage = decimal.ToInt32(decimal.Parse(importModel.Odometer, NumberStyles.Any)), Notes = string.IsNullOrWhiteSpace(importModel.Notes) ? "" : importModel.Notes, - Tags = string.IsNullOrWhiteSpace(importModel.Tags) ? [] : importModel.Tags.Split(" ").ToList() + Tags = string.IsNullOrWhiteSpace(importModel.Tags) ? [] : importModel.Tags.Split(" ").ToList(), + ExtraFields = importModel.ExtraFields.Any() ? importModel.ExtraFields.Select(x => new ExtraField { Name = x.Key, Value = x.Value }).ToList() : new List() }; _odometerRecordDataAccess.SaveOdometerRecordToVehicle(convertedRecord); } @@ -537,7 +540,8 @@ public IActionResult ImportToVehicleIdFromCsv(int vehicleId, ImportMode mode, st Priority = parsedPriority, Description = string.IsNullOrWhiteSpace(importModel.Description) ? $"Plan Record on {importModel.DateCreated}" : importModel.Description, Notes = string.IsNullOrWhiteSpace(importModel.Notes) ? "" : importModel.Notes, - Cost = decimal.Parse(importModel.Cost, NumberStyles.Any) + Cost = decimal.Parse(importModel.Cost, NumberStyles.Any), + ExtraFields = importModel.ExtraFields.Any() ? importModel.ExtraFields.Select(x => new ExtraField { Name = x.Key, Value = x.Value }).ToList() : new List() }; _planRecordDataAccess.SavePlanRecordToVehicle(convertedRecord); } @@ -551,7 +555,8 @@ public IActionResult ImportToVehicleIdFromCsv(int vehicleId, ImportMode mode, st Description = string.IsNullOrWhiteSpace(importModel.Description) ? $"Repair Record on {importModel.Date}" : importModel.Description, Notes = string.IsNullOrWhiteSpace(importModel.Notes) ? "" : importModel.Notes, Cost = decimal.Parse(importModel.Cost, NumberStyles.Any), - Tags = string.IsNullOrWhiteSpace(importModel.Tags) ? [] : importModel.Tags.Split(" ").ToList() + Tags = string.IsNullOrWhiteSpace(importModel.Tags) ? [] : importModel.Tags.Split(" ").ToList(), + ExtraFields = importModel.ExtraFields.Any() ? importModel.ExtraFields.Select(x => new ExtraField { Name = x.Key, Value = x.Value }).ToList() : new List() }; _collisionRecordDataAccess.SaveCollisionRecordToVehicle(convertedRecord); if (_config.GetUserConfig(User).EnableAutoOdometerInsert) @@ -575,7 +580,8 @@ public IActionResult ImportToVehicleIdFromCsv(int vehicleId, ImportMode mode, st Description = string.IsNullOrWhiteSpace(importModel.Description) ? $"Upgrade Record on {importModel.Date}" : importModel.Description, Notes = string.IsNullOrWhiteSpace(importModel.Notes) ? "" : importModel.Notes, Cost = decimal.Parse(importModel.Cost, NumberStyles.Any), - Tags = string.IsNullOrWhiteSpace(importModel.Tags) ? [] : importModel.Tags.Split(" ").ToList() + Tags = string.IsNullOrWhiteSpace(importModel.Tags) ? [] : importModel.Tags.Split(" ").ToList(), + ExtraFields = importModel.ExtraFields.Any() ? importModel.ExtraFields.Select(x => new ExtraField { Name = x.Key, Value = x.Value }).ToList() : new List() }; _upgradeRecordDataAccess.SaveUpgradeRecordToVehicle(convertedRecord); if (_config.GetUserConfig(User).EnableAutoOdometerInsert) @@ -601,7 +607,8 @@ public IActionResult ImportToVehicleIdFromCsv(int vehicleId, ImportMode mode, st Description = importModel.Description, Cost = decimal.Parse(importModel.Cost, NumberStyles.Any), Notes = importModel.Notes, - Tags = string.IsNullOrWhiteSpace(importModel.Tags) ? [] : importModel.Tags.Split(" ").ToList() + Tags = string.IsNullOrWhiteSpace(importModel.Tags) ? [] : importModel.Tags.Split(" ").ToList(), + ExtraFields = importModel.ExtraFields.Any() ? importModel.ExtraFields.Select(x => new ExtraField { Name = x.Key, Value = x.Value }).ToList() : new List() }; _supplyRecordDataAccess.SaveSupplyRecordToVehicle(convertedRecord); } @@ -614,7 +621,8 @@ public IActionResult ImportToVehicleIdFromCsv(int vehicleId, ImportMode mode, st Description = string.IsNullOrWhiteSpace(importModel.Description) ? $"Tax Record on {importModel.Date}" : importModel.Description, Notes = string.IsNullOrWhiteSpace(importModel.Notes) ? "" : importModel.Notes, Cost = decimal.Parse(importModel.Cost, NumberStyles.Any), - Tags = string.IsNullOrWhiteSpace(importModel.Tags) ? [] : importModel.Tags.Split(" ").ToList() + Tags = string.IsNullOrWhiteSpace(importModel.Tags) ? [] : importModel.Tags.Split(" ").ToList(), + ExtraFields = importModel.ExtraFields.Any() ? importModel.ExtraFields.Select(x => new ExtraField { Name = x.Key, Value = x.Value }).ToList() : new List() }; _taxRecordDataAccess.SaveTaxRecordToVehicle(convertedRecord); } diff --git a/MapProfile/ImportMappers.cs b/MapProfile/ImportMappers.cs index f44203b2..2f9940bb 100644 --- a/MapProfile/ImportMappers.cs +++ b/MapProfile/ImportMappers.cs @@ -27,6 +27,18 @@ public ImportMapper() Map(m => m.Type).Name(["type"]); Map(m => m.Priority).Name(["priority"]); Map(m => m.Tags).Name(["tags"]); + Map(m => m.ExtraFields).Convert(row => + { + var attributes = new Dictionary(); + foreach (var header in row.Row.HeaderRecord) + { + if (header.ToLower().StartsWith("extrafield_")) + { + attributes.Add(header.Substring(11), row.Row.GetField(header)); + } + } + return attributes; + }); ; } } } diff --git a/Models/Shared/ImportModel.cs b/Models/Shared/ImportModel.cs index 73c4e7ba..ea1fdfe2 100644 --- a/Models/Shared/ImportModel.cs +++ b/Models/Shared/ImportModel.cs @@ -25,6 +25,7 @@ public class ImportModel public string PartSupplier { get; set; } public string PartQuantity { get; set; } public string Tags { get; set; } + public Dictionary ExtraFields {get;set;} } public class SupplyRecordExportModel