profileShare

rasmusjy / splitapp-backend-clean-onion

Read-only snapshot

No repository description.

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