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

employee and car pages updated #251

Merged
merged 7 commits into from
Feb 2, 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
131 changes: 49 additions & 82 deletions CheckDrive.Web/CheckDrive.Web/Controllers/CarsController.cs
Original file line number Diff line number Diff line change
@@ -1,85 +1,32 @@
using CheckDrive.ApiContracts;
using CheckDrive.ApiContracts.Car;
using CheckDrive.Web.Mappings;
using CheckDrive.Web.Requests.Cars;
using CheckDrive.Web.Stores.Cars;
using CheckDrive.Web.Stores.OilMarks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;

namespace CheckDrive.Web.Controllers;

public class CarsController : Controller
public class CarsController(ICarStore carStore, IOilMarkStore oilMarkStore) : Controller
{
private readonly ICarDataStore _carDataStore;

public CarsController(ICarDataStore carDataStore)
public async Task<IActionResult> Index()
{
_carDataStore = carDataStore;
}

public async Task<IActionResult> Index(string? searchString, int? pageNumber)
{
var cars = await _carDataStore.GetCarsAsync(searchString, pageNumber);

ViewBag.SearchString = searchString;
var cars = await carStore.GetAsync();

ViewBag.PageSize = cars.PageSize;
ViewBag.PageCount = cars.TotalPages;
ViewBag.TotalCount = cars.TotalCount;
ViewBag.CurrentPage = cars.PageNumber;
ViewBag.HasPreviousPage = cars.HasPreviousPage;
ViewBag.HasNextPage = cars.HasNextPage;

var _cars = cars.Data.Select(c => new
{
c.Id,
c.Model,
c.Number,
c.Mileage,
c.Color,
c.RemainingFuel,
c.MeduimFuelConsumption,
c.FuelTankCapacity,
c.ManufacturedYear,
Status = ((CarStatusDto)c.CarStatus) switch
{
CarStatusDto.Free => "Bo'sh",
CarStatusDto.Busy => "Band",
CarStatusDto.Limited => "Limit tugagan",
_ => "No`malum holat"
}
}).ToList();

ViewBag.Cars = _cars;
return View();
}

public async Task<IActionResult> CarHistoryIndex(string? searchString, int? pageNumber, int? year, int? month)
{
var cars = await _carDataStore.GetCarsHistoryAsync(searchString, pageNumber, year, month);

ViewBag.SearchString = searchString;
ViewBag.Cars = cars.Data;

ViewBag.PageSize = cars.PageSize;
ViewBag.PageCount = cars.TotalPages;
ViewBag.TotalCount = cars.TotalCount;
ViewBag.CurrentPage = cars.PageNumber;
ViewBag.HasPreviousPage = cars.HasPreviousPage;
ViewBag.HasNextPage = cars.HasNextPage;
ViewBag.Cars = cars;
return View();
}

public async Task<IActionResult> Details(int id)
{
var car = await _carDataStore.GetCarAsync(id);
if (car == null)
{
return NotFound();
}
var car = await carStore.GetByIdAsync(id);

return View(car);
}

public async Task<IActionResult> DetailsForMechanicAcceptance(int id)
{
var car = await _carDataStore.GetCarAsync(id);
var car = await carStore.GetByIdAsync(id);
if (car == null)
{
return NotFound();
Expand All @@ -89,56 +36,63 @@ public async Task<IActionResult> DetailsForMechanicAcceptance(int id)

public async Task<IActionResult> DetailsForMechanicHandover(int id)
{
var car = await _carDataStore.GetCarAsync(id);
var car = await carStore.GetByIdAsync(id);
if (car == null)
{
return NotFound();
}
return PartialView("_CarDetailsForMechanicHandover", car);
Comment on lines 40 to 44
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this also be optimized?

}

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

return View();
}

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("Model,Color,Number,RemainingFuel,Mileage, MeduimFuelConsumption,FuelTankCapacity,ManufacturedYear, OneYearMediumDistance")] CarForCreateDto car)
public async Task<IActionResult> Create([FromForm] CreateCarRequest request)
{
if (ModelState.IsValid)
{
var newCar = await _carDataStore.CreateCarAsync(car);
var newCar = await carStore.CreateAsync(request);
return RedirectToAction(nameof(Index));
}
return View(car);
return BadRequest(ModelState);
}

public async Task<IActionResult> Edit(int id)
{
var car = await _carDataStore.GetCarAsync(id);
if (car == null)
{
return NotFound();
}
return View(car);
var car = await carStore.GetByIdAsync(id);
var oilMarks = await GetOilMarkAsync();

ViewBag.OilMarks = oilMarks;
var carRequest = car.ToUpdateViewModel();

return View(carRequest);
}

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(int id, [Bind("Id,Model,Color,Number,RemainingFuel,Mileage, MeduimFuelConsumption,FuelTankCapacity,ManufacturedYear, OneYearMediumDistance")] CarForUpdateDto car)
public async Task<IActionResult> Edit([FromForm] UpdateCarRequest request)
{
if (ModelState.IsValid)
try
{
var newCar = await _carDataStore.UpdateCarAsync(id, car);
await carStore.UpdateAsync(request);
return RedirectToAction(nameof(Index));
}
return View(car);
catch(Exception ex)
{
return BadRequest();
}
}

public async Task<IActionResult> Delete(int id)
{
var car = await _carDataStore.GetCarAsync(id);
var car = await carStore.GetByIdAsync(id);
if (car == null)
{
return NotFound();
Expand All @@ -150,7 +104,20 @@ public async Task<IActionResult> Delete(int id)
[ValidateAntiForgeryToken]
public async Task<IActionResult> DeleteConfirmed(int id)
{
await _carDataStore.DeleteCarAsync(id);
await carStore.DeleteAsync(id);
return RedirectToAction(nameof(Index));
}
}

private async Task<List<SelectListItem>> GetOilMarkAsync()
{
var oilMarks = await oilMarkStore.GetAsync();

return oilMarks
.Select(e => new SelectListItem()
{
Value = e.Id.ToString(),
Text = e.Name.ToString()
})
.ToList();
}
}
32 changes: 11 additions & 21 deletions CheckDrive.Web/CheckDrive.Web/Controllers/EmployeesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,19 @@

namespace CheckDrive.Web.Controllers;

public class EmployeesController : Controller
public class EmployeesController(IEmployeeStore employeeStore, ICarStore carStore) : Controller
{
private readonly IEmployeeStore _employeeStore;
private readonly ICarDataStore _carStore;

public EmployeesController(IEmployeeStore employeeStore, ICarDataStore carStore)
{
_employeeStore = employeeStore ?? throw new ArgumentNullException(nameof(employeeStore));
_carStore = carStore ?? throw new ArgumentNullException(nameof(carStore));
}

public async Task<ActionResult> Index()
{
var employees = await _employeeStore.GetAsync();
var employees = await employeeStore.GetAsync();
ViewBag.Positions = GetPositions();

return View(employees);
}

public async Task<ActionResult> Details(int id)
{
var employee = await _employeeStore.GetByIdAsync(id);
var employee = await employeeStore.GetByIdAsync(id);

return View(employee);
}
Expand All @@ -50,9 +41,9 @@ public async Task<ActionResult> Create(CreateEmployeeRequest request)
{
try
{
var createdEmployee = await _employeeStore.CreateAsync(request);

return RedirectToAction(nameof(Details), new { id = createdEmployee.Id });
var createdEmployee = await employeeStore.CreateAsync(request);
return RedirectToAction(nameof(Index));
}
catch
{
Expand All @@ -62,7 +53,7 @@ public async Task<ActionResult> Create(CreateEmployeeRequest request)

public async Task<ActionResult> Edit(int id)
{
var employee = await _employeeStore.GetByIdAsync(id);
var employee = await employeeStore.GetByIdAsync(id);
ViewBag.Positions = GetPositions();
ViewBag.Cars = await GetCarsAsync();

Expand All @@ -77,8 +68,7 @@ public async Task<ActionResult> Edit(UpdateEmployeeRequest request)
{
try
{
await _employeeStore.UpdateAsync(request);

await employeeStore.UpdateAsync(request);
return RedirectToAction(nameof(Index));
}
catch
Expand All @@ -89,7 +79,7 @@ public async Task<ActionResult> Edit(UpdateEmployeeRequest request)

public async Task<ActionResult> Delete(int id)
{
var employee = await _employeeStore.GetByIdAsync(id);
var employee = await employeeStore.GetByIdAsync(id);

return View(employee);
}
Expand All @@ -100,7 +90,7 @@ public async Task<ActionResult> Delete(int id, IFormCollection _)
{
try
{
await _employeeStore.DeleteAsync(id);
await employeeStore.DeleteAsync(id);
return RedirectToAction(nameof(Index));
}
catch
Expand All @@ -127,7 +117,7 @@ private static List<SelectListItem> GetPositions()

private async Task<List<SelectListItem>> GetCarsAsync()
{
var cars = await _carStore.GetAsync();
var cars = await carStore.GetAsync();

return cars
.Select(e => new SelectListItem()
Expand Down
Loading