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