diff --git a/Controllers/AdminController.cs b/Controllers/AdminController.cs index 2e7c29e..ff10876 100644 --- a/Controllers/AdminController.cs +++ b/Controllers/AdminController.cs @@ -34,15 +34,23 @@ namespace CarCareTracker.Controllers var result = _loginLogic.GenerateUserToken(emailAddress, autoNotify); return Json(result); } + [HttpPost] public IActionResult DeleteToken(int tokenId) { var result = _loginLogic.DeleteUserToken(tokenId); return Json(result); } + [HttpPost] public IActionResult DeleteUser(int userId) { var result =_userLogic.DeleteAllAccessToUser(userId) && _configHelper.DeleteUserConfig(userId) && _loginLogic.DeleteUser(userId); return Json(result); } + [HttpPost] + public IActionResult UpdateUserAdminStatus(int userId, bool isAdmin) + { + var result = _loginLogic.MakeUserAdmin(userId, isAdmin); + return Json(result); + } } } diff --git a/Controllers/VehicleController.cs b/Controllers/VehicleController.cs index 600c825..fb773c0 100644 --- a/Controllers/VehicleController.cs +++ b/Controllers/VehicleController.cs @@ -678,6 +678,16 @@ namespace CarCareTracker.Controllers //get collaborators var collaborators = _userLogic.GetCollaboratorsForVehicle(vehicleId); viewModel.Collaborators = collaborators; + //get MPG per month. + var userConfig = _config.GetUserConfig(User); + var mileageData = _gasHelper.GetGasRecordViewModels(gasRecords, userConfig.UseMPG, userConfig.UseUKMPG); + mileageData.RemoveAll(x => x.MilesPerGallon == default); + var monthlyMileageData = mileageData.GroupBy(x=>x.MonthId).OrderBy(x => x.Key).Select(x => new CostForVehicleByMonth + { + MonthName = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(x.Key), + Cost = x.Average(y=>y.MilesPerGallon) + }).ToList(); + viewModel.FuelMileageForVehicleByMonth = monthlyMileageData; return PartialView("_Report", viewModel); } [TypeFilter(typeof(CollaboratorFilter))] @@ -807,6 +817,25 @@ namespace CarCareTracker.Controllers } [TypeFilter(typeof(CollaboratorFilter))] [HttpPost] + public IActionResult GetMonthMPGByVehicle(int vehicleId, int year = 0) + { + var gasRecords = _gasRecordDataAccess.GetGasRecordsByVehicleId(vehicleId); + var userConfig = _config.GetUserConfig(User); + var mileageData = _gasHelper.GetGasRecordViewModels(gasRecords, userConfig.UseMPG, userConfig.UseUKMPG); + if (year != 0) + { + mileageData.RemoveAll(x => DateTime.Parse(x.Date).Year != year); + } + mileageData.RemoveAll(x => x.MilesPerGallon == default); + var monthlyMileageData = mileageData.GroupBy(x => x.MonthId).OrderBy(x => x.Key).Select(x => new CostForVehicleByMonth + { + MonthName = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(x.Key), + Cost = x.Average(y => y.MilesPerGallon) + }).ToList(); + return PartialView("_MPGByMonthReport", monthlyMileageData); + } + [TypeFilter(typeof(CollaboratorFilter))] + [HttpPost] public IActionResult GetCostByMonthByVehicle(int vehicleId, List selectedMetrics, int year = 0) { List allCosts = new List(); diff --git a/Helper/GasHelper.cs b/Helper/GasHelper.cs index 23d78b9..9172552 100644 --- a/Helper/GasHelper.cs +++ b/Helper/GasHelper.cs @@ -36,6 +36,7 @@ namespace CarCareTracker.Helper { Id = currentObject.Id, VehicleId = currentObject.VehicleId, + MonthId = currentObject.Date.Month, Date = currentObject.Date.ToShortDateString(), Mileage = currentObject.Mileage, Gallons = convertedConsumption, @@ -73,6 +74,7 @@ namespace CarCareTracker.Helper { Id = currentObject.Id, VehicleId = currentObject.VehicleId, + MonthId = currentObject.Date.Month, Date = currentObject.Date.ToShortDateString(), Mileage = currentObject.Mileage, Gallons = convertedConsumption, diff --git a/Logic/LoginLogic.cs b/Logic/LoginLogic.cs index ac444c8..8dda856 100644 --- a/Logic/LoginLogic.cs +++ b/Logic/LoginLogic.cs @@ -12,6 +12,7 @@ namespace CarCareTracker.Logic { public interface ILoginLogic { + bool MakeUserAdmin(int userId, bool isAdmin); OperationResponse GenerateUserToken(string emailAddress, bool autoNotify); bool DeleteUserToken(int tokenId); bool DeleteUser(int userId); @@ -193,6 +194,17 @@ namespace CarCareTracker.Logic } } #region "Admin Functions" + public bool MakeUserAdmin(int userId, bool isAdmin) + { + var user = _userData.GetUserRecordById(userId); + if (user == default) + { + return false; + } + user.IsAdmin = isAdmin; + var result = _userData.SaveUserRecord(user); + return result; + } public List GetAllUsers() { var result = _userData.GetUsers(); diff --git a/MapProfile/FuellyMappers.cs b/MapProfile/FuellyMappers.cs index 5c3d849..b90d948 100644 --- a/MapProfile/FuellyMappers.cs +++ b/MapProfile/FuellyMappers.cs @@ -9,7 +9,7 @@ namespace CarCareTracker.MapProfile { Map(m => m.Date).Name(["date", "fuelup_date"]); Map(m => m.Odometer).Name(["odometer"]); - Map(m => m.FuelConsumed).Name(["gallons", "liters", "litres", "consumption", "quantity", "fueleconomy"]); + Map(m => m.FuelConsumed).Name(["gallons", "liters", "litres", "consumption", "quantity", "fueleconomy", "fuelconsumed"]); Map(m => m.Cost).Name(["cost", "total cost", "totalcost", "total price"]); Map(m => m.Notes).Name("notes", "note"); Map(m => m.Price).Name(["price"]); diff --git a/Models/GasRecord/GasRecordViewModel.cs b/Models/GasRecord/GasRecordViewModel.cs index 0131d7d..e790af0 100644 --- a/Models/GasRecord/GasRecordViewModel.cs +++ b/Models/GasRecord/GasRecordViewModel.cs @@ -4,6 +4,7 @@ { public int Id { get; set; } public int VehicleId { get; set; } + public int MonthId { get; set; } public string Date { get; set; } /// /// American moment diff --git a/Models/Report/ReportViewModel.cs b/Models/Report/ReportViewModel.cs index de2b2c8..2c2e6a4 100644 --- a/Models/Report/ReportViewModel.cs +++ b/Models/Report/ReportViewModel.cs @@ -3,6 +3,7 @@ public class ReportViewModel { public List CostForVehicleByMonth { get; set; } = new List(); + public List FuelMileageForVehicleByMonth { get; set; } = new List(); public CostMakeUpForVehicle CostMakeUpForVehicle { get; set; } = new CostMakeUpForVehicle(); public ReminderMakeUpForVehicle ReminderMakeUpForVehicle { get; set; } = new ReminderMakeUpForVehicle(); public List Years { get; set; } = new List(); diff --git a/Views/Admin/Index.cshtml b/Views/Admin/Index.cshtml index 81bba62..f101972 100644 --- a/Views/Admin/Index.cshtml +++ b/Views/Admin/Index.cshtml @@ -76,7 +76,7 @@ @userData.UserName @userData.EmailAddress - @userData.Id + } @@ -86,7 +86,16 @@ +} else +{ +
+

No data found, insert/select some data to see visualizations here.

+
+} \ No newline at end of file diff --git a/Views/Vehicle/_Report.cshtml b/Views/Vehicle/_Report.cshtml index 7d0d651..1b36458 100644 --- a/Views/Vehicle/_Report.cshtml +++ b/Views/Vehicle/_Report.cshtml @@ -69,13 +69,15 @@
-
+
@await Html.PartialAsync("_Collaborators", Model.Collaborators)
-
- +
+
+ @await Html.PartialAsync("_MPGByMonthReport", Model.FuelMileageForVehicleByMonth) +
-
+
diff --git a/Views/Vehicle/_VehicleModal.cshtml b/Views/Vehicle/_VehicleModal.cshtml index 25a172e..f9d209c 100644 --- a/Views/Vehicle/_VehicleModal.cshtml +++ b/Views/Vehicle/_VehicleModal.cshtml @@ -8,6 +8,15 @@ }