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