Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix bugs with car #260

Merged
merged 6 commits into from
Mar 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions CheckDrive.Web/CheckDrive.Web/Controllers/CarsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public async Task<IActionResult> DetailsForMechanicHandover(int id)

public async Task<IActionResult> Create()
{
var oilMarks = await GetOilMarkAsync();
var oilMarks = await GetOilMarksAsync();
ViewBag.OilMarks = oilMarks;

return View();
Expand All @@ -58,7 +58,7 @@ public async Task<IActionResult> Create([FromForm] CreateCarRequest request)
{
if (ModelState.IsValid)
{
var newCar = await carStore.CreateAsync(request);
await carStore.CreateAsync(request);
return RedirectToAction(nameof(Index));
}
return BadRequest(ModelState);
Expand All @@ -67,7 +67,7 @@ public async Task<IActionResult> Create([FromForm] CreateCarRequest request)
public async Task<IActionResult> Edit(int id)
{
var car = await carStore.GetByIdAsync(id);
var oilMarks = await GetOilMarkAsync();
var oilMarks = await GetOilMarksAsync(car.OilMarkId);

ViewBag.OilMarks = oilMarks;
var carRequest = car.ToUpdateViewModel();
Expand Down Expand Up @@ -108,16 +108,17 @@ public async Task<IActionResult> DeleteConfirmed(int id)
return RedirectToAction(nameof(Index));
}

private async Task<List<SelectListItem>> GetOilMarkAsync()
private async Task<List<SelectListItem>> GetOilMarksAsync(int? oilMarkId = default)
{
var oilMarks = await oilMarkStore.GetAsync();

return oilMarks
.Select(e => new SelectListItem()
{
Value = e.Id.ToString(),
Text = e.Name.ToString()
Text = e.Name.ToString(),
Selected = e.Id == oilMarkId
})
.ToList();
}
}
}
4 changes: 0 additions & 4 deletions CheckDrive.Web/CheckDrive.Web/Mappings/CarMappings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,8 @@ public static UpdateCarRequest ToUpdateViewModel(this CarViewModel car) =>
Number = car.Number,
ManufacturedYear = car.ManufacturedYear,
Mileage = car.Mileage,
CurrentMonthMileage = car.CurrentMonthMileage,
CurrentYearMileage = car.CurrentYearMileage,
MonthlyDistanceLimit = car.MonthlyDistanceLimit,
YearlyDistanceLimit = car.YearlyDistanceLimit,
CurrentMonthFuelConsumption = car.CurrentMonthFuelConsumption,
CurrentYearFuelConsumption = car.CurrentYearFuelConsumption,
MonthlyFuelConsumptionLimit = car.MonthlyFuelConsumptionLimit,
YearlyFuelConsumptionLimit = car.YearlyFuelConsumptionLimit,
AverageFuelConsumption = car.AverageFuelConsumption,
Expand Down
44 changes: 38 additions & 6 deletions CheckDrive.Web/CheckDrive.Web/Requests/Cars/CreateCarRequest.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,56 @@
using CheckDrive.Web.Models.Enums;
using System.ComponentModel.DataAnnotations;

namespace CheckDrive.Web.Requests.Cars;

public class CreateCarRequest
{
public int OilMarkId { get; set; }
[Required(ErrorMessage = "Avtomobil modeli majburiy")]
[StringLength(25, MinimumLength = 2, ErrorMessage = "Belgilar soni 2 va 25 orasida bo'lishi kerak")]
public required string Model { get; set; }

[Required(ErrorMessage = "Davlat raqami majburiy")]
[StringLength(10, MinimumLength = 8, ErrorMessage = "Belgilar soni 8 va 10 orasida bo'lishi kerak")]
public required string Number { get; set; }

[Required(ErrorMessage = "Ishlab chiqarilgan yili majburiy")]
[Range(1, int.MaxValue, ErrorMessage = "Ishlab chiqarilgan yil noto‘g‘ri")]
public int ManufacturedYear { get; set; }

[Required(ErrorMessage = "Bosib o'tilgan masofasi [km]")]
[Range(0, double.MaxValue, ErrorMessage = "Yurgan masofa manfiy bo‘lishi mumkin emas")]
public decimal Mileage { get; set; }
public decimal CurrentMonthMileage { get; set; }
public decimal CurrentYearMileage { get; set; }

[Required(ErrorMessage = "1 yillik bosib o'tish me'yori [km] majburiy")]
[Range(0, double.MaxValue, ErrorMessage = "Yillik masofa chegarasi manfiy bo‘lishi mumkin emas")]
public decimal YearlyDistanceLimit { get; set; }

[Required(ErrorMessage = "1 oylik bosib o'tish me'yori [km] majburiy")]
[Range(0, double.MaxValue, ErrorMessage = "Oylik masofa chegarasi manfiy bo‘lishi mumkin emas")]
public decimal MonthlyDistanceLimit { get; set; }
public decimal CurrentMonthFuelConsumption { get; set; }
public decimal CurrentYearFuelConsumption { get; set; }

[Required(ErrorMessage = "1 oylik yoqilg'i me'yori [litr] majburiy")]
[Range(0, double.MaxValue, ErrorMessage = "Oylik yoqilg‘i sarfi chegarasi manfiy bo‘lishi mumkin emas")]
public decimal MonthlyFuelConsumptionLimit { get; set; }

[Required(ErrorMessage = "1 yillik yoqilg'i me'yori [litr] majburiy")]
[Range(0, double.MaxValue, ErrorMessage = "Yillik yoqilg‘i sarfi chegarasi manfiy bo‘lishi mumkin emas")]
public decimal YearlyFuelConsumptionLimit { get; set; }

[Required(ErrorMessage = "100 km uchun yoqilg'i me'yori [litr]")]
[Range(0, double.MaxValue, ErrorMessage = "100 km uchun yoqilg'i me'yori [litr] manfiy bo‘lishi mumkin emas")]
public decimal AverageFuelConsumption { get; set; }

[Required(ErrorMessage = "Yoqilg'i baki sig'imi majburiy")]
[Range(0, double.MaxValue, ErrorMessage = "Yoqilg'i baki sig'imi manfiy bo‘lishi mumkin emas")]
public decimal FuelCapacity { get; set; }

[Required(ErrorMessage = "Avtomobildagi yoqilg'i qoldig'i [litr] majburiy")]
[Range(0, double.MaxValue, ErrorMessage = "Qolgan yoqilg‘i miqdori manfiy bo‘lishi mumkin emas")]
public decimal RemainingFuel { get; set; }
public CarStatus Status { get; set; }

public CarStatus Status { get; set; } = CarStatus.Free;

[Range(1, int.MaxValue, ErrorMessage = "Moy markasi noto‘g‘ri")]
public int? OilMarkId { get; set; }
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
namespace CheckDrive.Web.Requests.OilMark;
using System.ComponentModel.DataAnnotations;

namespace CheckDrive.Web.Requests.OilMark;

public class CreateOilMarkRequest
{
[Required(ErrorMessage = "Yoqilg'i nomi majburiy.")]
[StringLength(25, MinimumLength = 2, ErrorMessage = "Belgilar soni 2 va 25 orasida bo'lishi kerak")]
public required string Name { get; set; }
}
3 changes: 2 additions & 1 deletion CheckDrive.Web/CheckDrive.Web/ViewModels/Car/CarViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public class CarViewModel
{
public int Id { get; set; }
public int OilMarkId { get; set; }
public required string OilMarkName { get; set; }
public required string Model { get; set; }
public required string Number { get; set; }
public int ManufacturedYear { get; set; }
Expand All @@ -22,4 +23,4 @@ public class CarViewModel
public decimal FuelCapacity { get; set; }
public decimal RemainingFuel { get; set; }
public CarStatus Status { get; set; }
}
}
12 changes: 12 additions & 0 deletions CheckDrive.Web/CheckDrive.Web/Views/Cars/Details.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@
<div class="col text-center">
Bosib o'tgan masofasi
</div>
@if (Model.OilMarkName is not null)
{
<div class="col text-center">
Yoqilg'i turi
</div>
}
<div class="col text-center">
Yoqilg'i qoldig'i
</div>
Expand All @@ -41,6 +47,12 @@
<div class="col text-center">
@Model.Mileage
</div>
@if (Model.OilMarkName is not null)
{
<div class="col text-center">
@Model.OilMarkName
</div>
}
<div class="col text-center">
@Model.RemainingFuel
</div>
Expand Down
4 changes: 2 additions & 2 deletions CheckDrive.Web/CheckDrive.Web/Views/OilMarks/Create.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
<form asp-action="Create">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="">
<label asp-for="Name" class="control-label"> Yoqilg'i turini kiriting</label>
<input asp-for="Name" class="form-control" required />
<label asp-for="Name" class="control-label"> Yoqilg'i nomini kiriting</label>
<input asp-for="Name" class="form-control" />
<span asp-validation-for="Name" class="text-danger"></span>
</div>
<div class="form-group mt-3 d-flex justify-content-center ">
Expand Down
1 change: 1 addition & 0 deletions CheckDrive.Web/CheckDrive.Web/Views/OilMarks/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

@{
ViewData["Title"] = "Yoqilg'ilar";
Layout = "~/Views/Shared/Layouts/_Layout.cshtml";
}

@section Scripts {
Expand Down