InvitationsController.cs
5,765 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.Extensions.Localization; |
| 10 | using SplitApp.WebApp.Areas.Admin.Models; |
| 11 | |
| 12 | namespace SplitApp.WebApp.Areas.Admin.Controllers |
| 13 | { |
| 14 | [Area("Admin")] |
| 15 | [Authorize(Roles = "admin")] |
| 16 | public class InvitationsController : Controller |
| 17 | { |
| 18 | private readonly IInvitationAdminService _service; |
| 19 | private readonly IStringLocalizer<App.Resources.Views.Shared> _l; |
| 20 | |
| 21 | public InvitationsController(IInvitationAdminService service, |
| 22 | IStringLocalizer<App.Resources.Views.Shared> l) |
| 23 | { |
| 24 | _service = service; |
| 25 | _l = l; |
| 26 | } |
| 27 | |
| 28 | // GET: Admin/Invitations |
| 29 | public async Task<IActionResult> Index(string? search) |
| 30 | { |
| 31 | var invitations = await _service.GetAllAsync(search); |
| 32 | |
| 33 | var vm = new AdminInvitationIndexViewModel |
| 34 | { |
| 35 | Title = _l["Invitations"].Value, |
| 36 | Items = invitations.OrderByDescending(i => i.Id).ToList(), |
| 37 | CurrentSearch = search |
| 38 | }; |
| 39 | return View(vm); |
| 40 | } |
| 41 | |
| 42 | // GET: Admin/Invitations/Details/5 |
| 43 | public async Task<IActionResult> Details(Guid? id) |
| 44 | { |
| 45 | if (id == null) |
| 46 | { |
| 47 | return NotFound(); |
| 48 | } |
| 49 | |
| 50 | var tripInvitation = await _service.GetByIdAsync(id.Value); |
| 51 | if (tripInvitation == null) |
| 52 | { |
| 53 | return NotFound(); |
| 54 | } |
| 55 | |
| 56 | return View(new AdminDetailsViewModel<TripInvitationBllDto> |
| 57 | { |
| 58 | Title = _l["Invitation details"].Value, |
| 59 | Item = tripInvitation |
| 60 | }); |
| 61 | } |
| 62 | |
| 63 | public async Task<IActionResult> Create() |
| 64 | { |
| 65 | return View(new AdminInvitationFormViewModel |
| 66 | { |
| 67 | Title = _l["New invitation"].Value, |
| 68 | Invitation = new TripInvitationBllDto |
| 69 | { |
| 70 | ExpiresAt = DateTime.UtcNow.AddDays(7), |
| 71 | Status = EInvitationStatus.Pending |
| 72 | }, |
| 73 | TripList = new Microsoft.AspNetCore.Mvc.Rendering.SelectList(await _service.GetTripsAsync(), "Id", "Name"), |
| 74 | UserList = new Microsoft.AspNetCore.Mvc.Rendering.SelectList(await _service.GetUsersAsync(), "Id", "Email") |
| 75 | }); |
| 76 | } |
| 77 | |
| 78 | [HttpPost] |
| 79 | [ValidateAntiForgeryToken] |
| 80 | public async Task<IActionResult> Create(AdminInvitationFormViewModel vm) |
| 81 | { |
| 82 | if (ModelState.IsValid) |
| 83 | { |
| 84 | await _service.CreateAsync(vm.Invitation); |
| 85 | return RedirectToAction(nameof(Index)); |
| 86 | } |
| 87 | |
| 88 | vm.Title = _l["New invitation"].Value; |
| 89 | vm.TripList = new Microsoft.AspNetCore.Mvc.Rendering.SelectList(await _service.GetTripsAsync(), "Id", "Name", vm.Invitation.TripId); |
| 90 | vm.UserList = new Microsoft.AspNetCore.Mvc.Rendering.SelectList(await _service.GetUsersAsync(), "Id", "Email", vm.Invitation.InvitedByUserId); |
| 91 | return View(vm); |
| 92 | } |
| 93 | |
| 94 | public async Task<IActionResult> Edit(Guid? id) |
| 95 | { |
| 96 | if (id == null) return NotFound(); |
| 97 | var invitation = await _service.GetByIdAsync(id.Value); |
| 98 | if (invitation == null) return NotFound(); |
| 99 | |
| 100 | return View(new AdminInvitationFormViewModel |
| 101 | { |
| 102 | Title = _l["Edit invitation"].Value, |
| 103 | Invitation = invitation, |
| 104 | TripList = new Microsoft.AspNetCore.Mvc.Rendering.SelectList(await _service.GetTripsAsync(), "Id", "Name", invitation.TripId), |
| 105 | UserList = new Microsoft.AspNetCore.Mvc.Rendering.SelectList(await _service.GetUsersAsync(), "Id", "Email", invitation.InvitedByUserId) |
| 106 | }); |
| 107 | } |
| 108 | |
| 109 | [HttpPost] |
| 110 | [ValidateAntiForgeryToken] |
| 111 | public async Task<IActionResult> Edit(Guid id, AdminInvitationFormViewModel vm) |
| 112 | { |
| 113 | if (id != vm.Invitation.Id) return NotFound(); |
| 114 | |
| 115 | if (ModelState.IsValid) |
| 116 | { |
| 117 | await _service.UpdateAsync(vm.Invitation); |
| 118 | return RedirectToAction(nameof(Index)); |
| 119 | } |
| 120 | |
| 121 | vm.Title = _l["Edit invitation"].Value; |
| 122 | vm.TripList = new Microsoft.AspNetCore.Mvc.Rendering.SelectList(await _service.GetTripsAsync(), "Id", "Name", vm.Invitation.TripId); |
| 123 | vm.UserList = new Microsoft.AspNetCore.Mvc.Rendering.SelectList(await _service.GetUsersAsync(), "Id", "Email", vm.Invitation.InvitedByUserId); |
| 124 | return View(vm); |
| 125 | } |
| 126 | |
| 127 | // GET: Admin/Invitations/Delete/5 |
| 128 | public async Task<IActionResult> Delete(Guid? id) |
| 129 | { |
| 130 | if (id == null) |
| 131 | { |
| 132 | return NotFound(); |
| 133 | } |
| 134 | |
| 135 | var tripInvitation = await _service.GetByIdAsync(id.Value); |
| 136 | if (tripInvitation == null) |
| 137 | { |
| 138 | return NotFound(); |
| 139 | } |
| 140 | |
| 141 | return View(new AdminDeleteViewModel<TripInvitationBllDto> |
| 142 | { |
| 143 | Title = _l["Delete invitation"].Value, |
| 144 | Item = tripInvitation |
| 145 | }); |
| 146 | } |
| 147 | |
| 148 | // POST: Admin/Invitations/Delete/5 |
| 149 | [HttpPost, ActionName("Delete")] |
| 150 | [ValidateAntiForgeryToken] |
| 151 | public async Task<IActionResult> DeleteConfirmed(Guid id) |
| 152 | { |
| 153 | await _service.DeleteAsync(id); |
| 154 | return RedirectToAction(nameof(Index)); |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | |