WishlistController.cs
4,472 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.Extensions.Localization; |
| 11 | using SplitApp.WebApp.Areas.Admin.Models; |
| 12 | |
| 13 | namespace SplitApp.WebApp.Areas.Admin.Controllers; |
| 14 | |
| 15 | [Area("Admin")] |
| 16 | [Authorize(Roles = "admin")] |
| 17 | public class WishlistController : Controller |
| 18 | { |
| 19 | private readonly IWishlistAdminService _service; |
| 20 | private readonly IStringLocalizer<App.Resources.Views.Shared> _l; |
| 21 | |
| 22 | public WishlistController(IWishlistAdminService service, IStringLocalizer<App.Resources.Views.Shared> l) |
| 23 | { |
| 24 | _service = service; |
| 25 | _l = l; |
| 26 | } |
| 27 | |
| 28 | public async Task<IActionResult> Index(string? search) |
| 29 | { |
| 30 | var items = await _service.GetAllAsync(search); |
| 31 | |
| 32 | var vm = new AdminWishlistIndexViewModel |
| 33 | { |
| 34 | Title = _l["Wishlist"].Value, |
| 35 | Items = items.OrderByDescending(w => w.Id).ToList(), |
| 36 | CurrentSearch = search |
| 37 | }; |
| 38 | return View(vm); |
| 39 | } |
| 40 | |
| 41 | public async Task<IActionResult> Details(Guid id) |
| 42 | { |
| 43 | var item = await _service.GetByIdAsync(id); |
| 44 | if (item == null) return NotFound(); |
| 45 | |
| 46 | return View(new AdminDetailsViewModel<TripWishlistItemBllDto> |
| 47 | { |
| 48 | Title = _l["Wishlist item details"].Value, |
| 49 | Item = item |
| 50 | }); |
| 51 | } |
| 52 | |
| 53 | public async Task<IActionResult> Create() |
| 54 | { |
| 55 | var vm = new AdminWishlistFormViewModel |
| 56 | { |
| 57 | Title = _l["New wishlist item"].Value, |
| 58 | TripList = new SelectList(await _service.GetTripsAsync(), "Id", "Name"), |
| 59 | UserList = new SelectList((await _service.GetUsersAsync()).Select(u => new { u.Id, Name = u.FirstName + " " + u.LastName }).ToList(), "Id", "Name") |
| 60 | }; |
| 61 | return View(vm); |
| 62 | } |
| 63 | |
| 64 | [HttpPost] |
| 65 | [ValidateAntiForgeryToken] |
| 66 | public async Task<IActionResult> Create(AdminWishlistFormViewModel vm) |
| 67 | { |
| 68 | if (ModelState.IsValid) |
| 69 | { |
| 70 | await _service.CreateAsync(vm.Item); |
| 71 | return RedirectToAction(nameof(Index)); |
| 72 | } |
| 73 | vm.Title = _l["New wishlist item"].Value; |
| 74 | vm.TripList = new SelectList(await _service.GetTripsAsync(), "Id", "Name"); |
| 75 | vm.UserList = new SelectList((await _service.GetUsersAsync()).Select(u => new { u.Id, Name = u.FirstName + " " + u.LastName }).ToList(), "Id", "Name"); |
| 76 | return View(vm); |
| 77 | } |
| 78 | |
| 79 | public async Task<IActionResult> Edit(Guid id) |
| 80 | { |
| 81 | var item = await _service.GetByIdAsync(id); |
| 82 | if (item == null) return NotFound(); |
| 83 | var vm = new AdminWishlistFormViewModel |
| 84 | { |
| 85 | Title = _l["Edit wishlist item"].Value, |
| 86 | Item = item, |
| 87 | TripList = new SelectList(await _service.GetTripsAsync(), "Id", "Name"), |
| 88 | UserList = new SelectList((await _service.GetUsersAsync()).Select(u => new { u.Id, Name = u.FirstName + " " + u.LastName }).ToList(), "Id", "Name") |
| 89 | }; |
| 90 | return View(vm); |
| 91 | } |
| 92 | |
| 93 | [HttpPost] |
| 94 | [ValidateAntiForgeryToken] |
| 95 | public async Task<IActionResult> Edit(Guid id, AdminWishlistFormViewModel vm) |
| 96 | { |
| 97 | if (id != vm.Item.Id) return NotFound(); |
| 98 | if (ModelState.IsValid) |
| 99 | { |
| 100 | await _service.UpdateAsync(vm.Item); |
| 101 | return RedirectToAction(nameof(Index)); |
| 102 | } |
| 103 | vm.Title = _l["Edit wishlist item"].Value; |
| 104 | vm.TripList = new SelectList(await _service.GetTripsAsync(), "Id", "Name"); |
| 105 | vm.UserList = new SelectList((await _service.GetUsersAsync()).Select(u => new { u.Id, Name = u.FirstName + " " + u.LastName }).ToList(), "Id", "Name"); |
| 106 | return View(vm); |
| 107 | } |
| 108 | |
| 109 | public async Task<IActionResult> Delete(Guid id) |
| 110 | { |
| 111 | var item = await _service.GetByIdAsync(id); |
| 112 | if (item == null) return NotFound(); |
| 113 | |
| 114 | return View(new AdminDeleteViewModel<TripWishlistItemBllDto> |
| 115 | { |
| 116 | Title = _l["Delete wishlist item"].Value, |
| 117 | Item = item |
| 118 | }); |
| 119 | } |
| 120 | |
| 121 | [HttpPost, ActionName("Delete")] |
| 122 | [ValidateAntiForgeryToken] |
| 123 | public async Task<IActionResult> DeleteConfirmed(Guid id) |
| 124 | { |
| 125 | await _service.DeleteAsync(id); |
| 126 | return RedirectToAction(nameof(Index)); |
| 127 | } |
| 128 | } |
| 129 | |