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