|
3 | 3 | using CheckDrive.Web.Stores.Cars;
|
4 | 4 | using Microsoft.AspNetCore.Mvc;
|
5 | 5 |
|
6 |
| -namespace CheckDrive.Web.Controllers |
7 |
| -{ |
8 |
| - public class CarsController : Controller |
9 |
| - { |
10 |
| - private readonly ICarDataStore _carDataStore; |
| 6 | +namespace CheckDrive.Web.Controllers; |
11 | 7 |
|
12 |
| - public CarsController(ICarDataStore carDataStore) |
13 |
| - { |
14 |
| - _carDataStore = carDataStore; |
15 |
| - } |
16 |
| - |
17 |
| - public async Task<IActionResult> Index(string? searchString, int? pageNumber) |
18 |
| - { |
19 |
| - var cars = await _carDataStore.GetCarsAsync(searchString, pageNumber); |
| 8 | +public class CarsController : Controller |
| 9 | +{ |
| 10 | + private readonly ICarDataStore _carDataStore; |
20 | 11 |
|
21 |
| - ViewBag.SearchString = searchString; |
| 12 | + public CarsController(ICarDataStore carDataStore) |
| 13 | + { |
| 14 | + _carDataStore = carDataStore; |
| 15 | + } |
22 | 16 |
|
23 |
| - ViewBag.PageSize = cars.PageSize; |
24 |
| - ViewBag.PageCount = cars.TotalPages; |
25 |
| - ViewBag.TotalCount = cars.TotalCount; |
26 |
| - ViewBag.CurrentPage = cars.PageNumber; |
27 |
| - ViewBag.HasPreviousPage = cars.HasPreviousPage; |
28 |
| - ViewBag.HasNextPage = cars.HasNextPage; |
| 17 | + public async Task<IActionResult> Index(string? searchString, int? pageNumber) |
| 18 | + { |
| 19 | + var cars = await _carDataStore.GetCarsAsync(searchString, pageNumber); |
29 | 20 |
|
30 |
| - var _cars = cars.Data.Select(c => new |
31 |
| - { |
32 |
| - c.Id, |
33 |
| - c.Model, |
34 |
| - c.Number, |
35 |
| - c.Mileage, |
36 |
| - c.Color, |
37 |
| - c.RemainingFuel, |
38 |
| - c.MeduimFuelConsumption, |
39 |
| - c.FuelTankCapacity, |
40 |
| - c.ManufacturedYear, |
41 |
| - Status = ((CarStatusDto)c.CarStatus) switch |
42 |
| - { |
43 |
| - CarStatusDto.Free => "Bo'sh", |
44 |
| - CarStatusDto.Busy => "Band", |
45 |
| - CarStatusDto.Limited => "Limit tugagan", |
46 |
| - _ => "No`malum holat" |
47 |
| - } |
48 |
| - }).ToList(); |
49 |
| - |
50 |
| - ViewBag.Cars = _cars; |
51 |
| - return View(); |
52 |
| - } |
| 21 | + ViewBag.SearchString = searchString; |
53 | 22 |
|
54 |
| - public async Task<IActionResult> CarHistoryIndex(string? searchString, int? pageNumber, int? year, int? month) |
55 |
| - { |
56 |
| - var cars = await _carDataStore.GetCarsHistoryAsync(searchString, pageNumber, year, month); |
57 |
| - |
58 |
| - ViewBag.SearchString = searchString; |
59 |
| - ViewBag.Cars = cars.Data; |
60 |
| - |
61 |
| - ViewBag.PageSize = cars.PageSize; |
62 |
| - ViewBag.PageCount = cars.TotalPages; |
63 |
| - ViewBag.TotalCount = cars.TotalCount; |
64 |
| - ViewBag.CurrentPage = cars.PageNumber; |
65 |
| - ViewBag.HasPreviousPage = cars.HasPreviousPage; |
66 |
| - ViewBag.HasNextPage = cars.HasNextPage; |
67 |
| - return View(); |
68 |
| - } |
| 23 | + ViewBag.PageSize = cars.PageSize; |
| 24 | + ViewBag.PageCount = cars.TotalPages; |
| 25 | + ViewBag.TotalCount = cars.TotalCount; |
| 26 | + ViewBag.CurrentPage = cars.PageNumber; |
| 27 | + ViewBag.HasPreviousPage = cars.HasPreviousPage; |
| 28 | + ViewBag.HasNextPage = cars.HasNextPage; |
69 | 29 |
|
70 |
| - public async Task<IActionResult> Details(int id) |
| 30 | + var _cars = cars.Data.Select(c => new |
71 | 31 | {
|
72 |
| - var car = await _carDataStore.GetCarAsync(id); |
73 |
| - if (car == null) |
| 32 | + c.Id, |
| 33 | + c.Model, |
| 34 | + c.Number, |
| 35 | + c.Mileage, |
| 36 | + c.Color, |
| 37 | + c.RemainingFuel, |
| 38 | + c.MeduimFuelConsumption, |
| 39 | + c.FuelTankCapacity, |
| 40 | + c.ManufacturedYear, |
| 41 | + Status = ((CarStatusDto)c.CarStatus) switch |
74 | 42 | {
|
75 |
| - return NotFound(); |
| 43 | + CarStatusDto.Free => "Bo'sh", |
| 44 | + CarStatusDto.Busy => "Band", |
| 45 | + CarStatusDto.Limited => "Limit tugagan", |
| 46 | + _ => "No`malum holat" |
76 | 47 | }
|
77 |
| - return View(car); |
78 |
| - } |
| 48 | + }).ToList(); |
79 | 49 |
|
80 |
| - public async Task<IActionResult> DetailsForMechanicAcceptance(int id) |
81 |
| - { |
82 |
| - var car = await _carDataStore.GetCarAsync(id); |
83 |
| - if (car == null) |
84 |
| - { |
85 |
| - return NotFound(); |
86 |
| - } |
87 |
| - return PartialView("_CarDetailsForMechanicAcceptance", car); |
88 |
| - } |
| 50 | + ViewBag.Cars = _cars; |
| 51 | + return View(); |
| 52 | + } |
| 53 | + |
| 54 | + public async Task<IActionResult> CarHistoryIndex(string? searchString, int? pageNumber, int? year, int? month) |
| 55 | + { |
| 56 | + var cars = await _carDataStore.GetCarsHistoryAsync(searchString, pageNumber, year, month); |
| 57 | + |
| 58 | + ViewBag.SearchString = searchString; |
| 59 | + ViewBag.Cars = cars.Data; |
| 60 | + |
| 61 | + ViewBag.PageSize = cars.PageSize; |
| 62 | + ViewBag.PageCount = cars.TotalPages; |
| 63 | + ViewBag.TotalCount = cars.TotalCount; |
| 64 | + ViewBag.CurrentPage = cars.PageNumber; |
| 65 | + ViewBag.HasPreviousPage = cars.HasPreviousPage; |
| 66 | + ViewBag.HasNextPage = cars.HasNextPage; |
| 67 | + return View(); |
| 68 | + } |
89 | 69 |
|
90 |
| - public async Task<IActionResult> DetailsForMechanicHandover(int id) |
| 70 | + public async Task<IActionResult> Details(int id) |
| 71 | + { |
| 72 | + var car = await _carDataStore.GetCarAsync(id); |
| 73 | + if (car == null) |
91 | 74 | {
|
92 |
| - var car = await _carDataStore.GetCarAsync(id); |
93 |
| - if (car == null) |
94 |
| - { |
95 |
| - return NotFound(); |
96 |
| - } |
97 |
| - return PartialView("_CarDetailsForMechanicHandover", car); |
| 75 | + return NotFound(); |
98 | 76 | }
|
| 77 | + return View(car); |
| 78 | + } |
99 | 79 |
|
100 |
| - public IActionResult Create() |
| 80 | + public async Task<IActionResult> DetailsForMechanicAcceptance(int id) |
| 81 | + { |
| 82 | + var car = await _carDataStore.GetCarAsync(id); |
| 83 | + if (car == null) |
101 | 84 | {
|
102 |
| - return View(); |
| 85 | + return NotFound(); |
103 | 86 | }
|
| 87 | + return PartialView("_CarDetailsForMechanicAcceptance", car); |
| 88 | + } |
104 | 89 |
|
105 |
| - [HttpPost] |
106 |
| - [ValidateAntiForgeryToken] |
107 |
| - public async Task<IActionResult> Create([Bind("Model,Color,Number,RemainingFuel,Mileage, MeduimFuelConsumption,FuelTankCapacity,ManufacturedYear, OneYearMediumDistance")] CarForCreateDto car) |
| 90 | + public async Task<IActionResult> DetailsForMechanicHandover(int id) |
| 91 | + { |
| 92 | + var car = await _carDataStore.GetCarAsync(id); |
| 93 | + if (car == null) |
108 | 94 | {
|
109 |
| - if (ModelState.IsValid) |
110 |
| - { |
111 |
| - var newCar = await _carDataStore.CreateCarAsync(car); |
112 |
| - return RedirectToAction(nameof(Index)); |
113 |
| - } |
114 |
| - return View(car); |
| 95 | + return NotFound(); |
115 | 96 | }
|
| 97 | + return PartialView("_CarDetailsForMechanicHandover", car); |
| 98 | + } |
| 99 | + |
| 100 | + public IActionResult Create() |
| 101 | + { |
| 102 | + return View(); |
| 103 | + } |
116 | 104 |
|
117 |
| - public async Task<IActionResult> Edit(int id) |
| 105 | + [HttpPost] |
| 106 | + [ValidateAntiForgeryToken] |
| 107 | + public async Task<IActionResult> Create([Bind("Model,Color,Number,RemainingFuel,Mileage, MeduimFuelConsumption,FuelTankCapacity,ManufacturedYear, OneYearMediumDistance")] CarForCreateDto car) |
| 108 | + { |
| 109 | + if (ModelState.IsValid) |
118 | 110 | {
|
119 |
| - var car = await _carDataStore.GetCarAsync(id); |
120 |
| - if (car == null) |
121 |
| - { |
122 |
| - return NotFound(); |
123 |
| - } |
124 |
| - return View(car); |
| 111 | + var newCar = await _carDataStore.CreateCarAsync(car); |
| 112 | + return RedirectToAction(nameof(Index)); |
125 | 113 | }
|
| 114 | + return View(car); |
| 115 | + } |
126 | 116 |
|
127 |
| - [HttpPost] |
128 |
| - [ValidateAntiForgeryToken] |
129 |
| - public async Task<IActionResult> Edit(int id, [Bind("Id,Model,Color,Number,RemainingFuel,Mileage, MeduimFuelConsumption,FuelTankCapacity,ManufacturedYear, OneYearMediumDistance")] CarForUpdateDto car) |
| 117 | + public async Task<IActionResult> Edit(int id) |
| 118 | + { |
| 119 | + var car = await _carDataStore.GetCarAsync(id); |
| 120 | + if (car == null) |
130 | 121 | {
|
131 |
| - if (ModelState.IsValid) |
132 |
| - { |
133 |
| - var newCar = await _carDataStore.UpdateCarAsync(id, car); |
134 |
| - return RedirectToAction(nameof(Index)); |
135 |
| - } |
136 |
| - return View(car); |
| 122 | + return NotFound(); |
137 | 123 | }
|
| 124 | + return View(car); |
| 125 | + } |
138 | 126 |
|
139 |
| - public async Task<IActionResult> Delete(int id) |
| 127 | + [HttpPost] |
| 128 | + [ValidateAntiForgeryToken] |
| 129 | + public async Task<IActionResult> Edit(int id, [Bind("Id,Model,Color,Number,RemainingFuel,Mileage, MeduimFuelConsumption,FuelTankCapacity,ManufacturedYear, OneYearMediumDistance")] CarForUpdateDto car) |
| 130 | + { |
| 131 | + if (ModelState.IsValid) |
140 | 132 | {
|
141 |
| - var car = await _carDataStore.GetCarAsync(id); |
142 |
| - if (car == null) |
143 |
| - { |
144 |
| - return NotFound(); |
145 |
| - } |
146 |
| - return View(car); |
| 133 | + var newCar = await _carDataStore.UpdateCarAsync(id, car); |
| 134 | + return RedirectToAction(nameof(Index)); |
147 | 135 | }
|
| 136 | + return View(car); |
| 137 | + } |
148 | 138 |
|
149 |
| - [HttpPost, ActionName("Delete")] |
150 |
| - [ValidateAntiForgeryToken] |
151 |
| - public async Task<IActionResult> DeleteConfirmed(int id) |
| 139 | + public async Task<IActionResult> Delete(int id) |
| 140 | + { |
| 141 | + var car = await _carDataStore.GetCarAsync(id); |
| 142 | + if (car == null) |
152 | 143 | {
|
153 |
| - await _carDataStore.DeleteCarAsync(id); |
154 |
| - return RedirectToAction(nameof(Index)); |
| 144 | + return NotFound(); |
155 | 145 | }
|
| 146 | + return View(car); |
| 147 | + } |
| 148 | + |
| 149 | + [HttpPost, ActionName("Delete")] |
| 150 | + [ValidateAntiForgeryToken] |
| 151 | + public async Task<IActionResult> DeleteConfirmed(int id) |
| 152 | + { |
| 153 | + await _carDataStore.DeleteCarAsync(id); |
| 154 | + return RedirectToAction(nameof(Index)); |
156 | 155 | }
|
157 | 156 | }
|
0 commit comments