TripsController.cs
4,404 bytes
| 1 | using App.BLL.DTO; |
|---|---|
| 2 | using App.BLL.Services.Admin; |
| 3 | using App.Domain; |
| 4 | using Microsoft.AspNetCore.Authorization; |
| 5 | using Microsoft.AspNetCore.Mvc; |
| 6 | using Microsoft.AspNetCore.Mvc.Rendering; |
| 7 | using Microsoft.EntityFrameworkCore; |
| 8 | using Microsoft.Extensions.Localization; |
| 9 | using WebApp.Areas.Admin.Models; |
| 10 | |
| 11 | namespace WebApp.Areas.Admin.Controllers |
| 12 | { |
| 13 | [Area("Admin")] |
| 14 | [Authorize(Roles = "admin")] |
| 15 | public class TripsController : Controller |
| 16 | { |
| 17 | private readonly ITripAdminService _service; |
| 18 | private readonly IStringLocalizer<App.Resources.Views.Shared> _l; |
| 19 | |
| 20 | public TripsController(ITripAdminService service, IStringLocalizer<App.Resources.Views.Shared> l) |
| 21 | { |
| 22 | _service = service; |
| 23 | _l = l; |
| 24 | } |
| 25 | |
| 26 | public async Task<IActionResult> Index(string? search) |
| 27 | { |
| 28 | var trips = await _service.GetAllAsync(search); |
| 29 | |
| 30 | return View(new AdminTripIndexViewModel |
| 31 | { |
| 32 | Title = _l["Trips"].Value, |
| 33 | Items = trips.OrderByDescending(t => t.CreatedAt).ToList(), |
| 34 | CurrentSearch = search |
| 35 | }); |
| 36 | } |
| 37 | |
| 38 | public async Task<IActionResult> Details(Guid? id) |
| 39 | { |
| 40 | if (id == null) return NotFound(); |
| 41 | var trip = await _service.GetByIdAsync(id.Value); |
| 42 | if (trip == null) return NotFound(); |
| 43 | |
| 44 | return View(new AdminDetailsViewModel<TripBllDto> |
| 45 | { |
| 46 | Title = _l["Trip details"].Value, |
| 47 | Item = trip |
| 48 | }); |
| 49 | } |
| 50 | |
| 51 | public async Task<IActionResult> Create() |
| 52 | { |
| 53 | return View(new AdminTripFormViewModel |
| 54 | { |
| 55 | Title = _l["New trip"].Value, |
| 56 | CurrencyList = new SelectList(await _service.GetAllCurrenciesAsync(), "Id", "Code") |
| 57 | }); |
| 58 | } |
| 59 | |
| 60 | [HttpPost] |
| 61 | [ValidateAntiForgeryToken] |
| 62 | public async Task<IActionResult> Create(AdminTripFormViewModel vm) |
| 63 | { |
| 64 | if (ModelState.IsValid) |
| 65 | { |
| 66 | await _service.CreateAsync(vm.Trip); |
| 67 | return RedirectToAction(nameof(Index)); |
| 68 | } |
| 69 | |
| 70 | vm.Title = _l["New trip"].Value; |
| 71 | vm.CurrencyList = new SelectList(await _service.GetAllCurrenciesAsync(), "Id", "Code", vm.Trip.DefaultCurrencyId); |
| 72 | return View(vm); |
| 73 | } |
| 74 | |
| 75 | public async Task<IActionResult> Edit(Guid? id) |
| 76 | { |
| 77 | if (id == null) return NotFound(); |
| 78 | var trip = await _service.GetByIdAsync(id.Value); |
| 79 | if (trip == null) return NotFound(); |
| 80 | |
| 81 | return View(new AdminTripFormViewModel |
| 82 | { |
| 83 | Title = _l["Edit trip"].Value, |
| 84 | Trip = trip, |
| 85 | CurrencyList = new SelectList(await _service.GetAllCurrenciesAsync(), "Id", "Code", trip.DefaultCurrencyId) |
| 86 | }); |
| 87 | } |
| 88 | |
| 89 | [HttpPost] |
| 90 | [ValidateAntiForgeryToken] |
| 91 | public async Task<IActionResult> Edit(Guid id, AdminTripFormViewModel vm) |
| 92 | { |
| 93 | if (id != vm.Trip.Id) return NotFound(); |
| 94 | |
| 95 | if (ModelState.IsValid) |
| 96 | { |
| 97 | try |
| 98 | { |
| 99 | await _service.UpdateAsync(vm.Trip); |
| 100 | } |
| 101 | catch (DbUpdateConcurrencyException) |
| 102 | { |
| 103 | if (!await _service.ExistsAsync(vm.Trip.Id)) return NotFound(); |
| 104 | throw; |
| 105 | } |
| 106 | |
| 107 | return RedirectToAction(nameof(Index)); |
| 108 | } |
| 109 | |
| 110 | vm.Title = _l["Edit trip"].Value; |
| 111 | vm.CurrencyList = new SelectList(await _service.GetAllCurrenciesAsync(), "Id", "Code", vm.Trip.DefaultCurrencyId); |
| 112 | return View(vm); |
| 113 | } |
| 114 | |
| 115 | public async Task<IActionResult> Delete(Guid? id) |
| 116 | { |
| 117 | if (id == null) return NotFound(); |
| 118 | var trip = await _service.GetByIdAsync(id.Value); |
| 119 | if (trip == null) return NotFound(); |
| 120 | |
| 121 | return View(new AdminDeleteViewModel<TripBllDto> |
| 122 | { |
| 123 | Title = _l["Delete trip"].Value, |
| 124 | Item = trip |
| 125 | }); |
| 126 | } |
| 127 | |
| 128 | [HttpPost, ActionName("Delete")] |
| 129 | [ValidateAntiForgeryToken] |
| 130 | public async Task<IActionResult> DeleteConfirmed(Guid id) |
| 131 | { |
| 132 | await _service.DeleteAsync(id); |
| 133 | return RedirectToAction(nameof(Index)); |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | |