profileShare

rasmusjy / splitapp-backend-modular-monolith

Read-only snapshot

No repository description.

main default branch 418 files Expires Sep 13, 2026, 9:06 AM
TripParticipantsController.cs 6,496 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 TripParticipantsController : Controller
20 {
21 private readonly ITripParticipantAdminService _service;
22 private readonly IStringLocalizer<App.Resources.Views.Shared> _l;
23
24 public TripParticipantsController(ITripParticipantAdminService service,
25 IStringLocalizer<App.Resources.Views.Shared> l)
26 {
27 _service = service;
28 _l = l;
29 }
30
31 // GET: Admin/TripParticipants
32 public async Task<IActionResult> Index(Guid? tripId, string? search)
33 {
34 var participants = await _service.GetAllAsync(tripId, search);
35
36 var vm = new AdminTripParticipantIndexViewModel
37 {
38 Title = _l["Trip participants"].Value,
39 Items = participants.OrderByDescending(tp => tp.JoinedAt).ToList(),
40 Trips = (await _service.GetTripsAsync()).OrderBy(t => t.Name).ToList(),
41 CurrentTripId = tripId,
42 CurrentSearch = search
43 };
44 return View(vm);
45 }
46
47 // GET: Admin/TripParticipants/Details/5
48 public async Task<IActionResult> Details(Guid? id)
49 {
50 if (id == null)
51 {
52 return NotFound();
53 }
54
55 var tripParticipant = await _service.GetByIdAsync(id.Value);
56 if (tripParticipant == null)
57 {
58 return NotFound();
59 }
60
61 return View(new AdminDetailsViewModel<TripParticipantBllDto>
62 {
63 Title = _l["Trip participant details"].Value,
64 Item = tripParticipant
65 });
66 }
67
68 // GET: Admin/TripParticipants/Create
69 public async Task<IActionResult> Create()
70 {
71 var vm = new AdminTripParticipantFormViewModel
72 {
73 Title = _l["New trip participant"].Value,
74 TripList = new SelectList(await _service.GetTripsAsync(), "Id", "Name"),
75 UserList = new SelectList(await _service.GetUsersAsync(), "Id", "Email")
76 };
77 return View(vm);
78 }
79
80 // POST: Admin/TripParticipants/Create
81 [HttpPost]
82 [ValidateAntiForgeryToken]
83 public async Task<IActionResult> Create(AdminTripParticipantFormViewModel vm)
84 {
85 if (ModelState.IsValid)
86 {
87 await _service.CreateAsync(vm.TripParticipant);
88 return RedirectToAction(nameof(Index));
89 }
90
91 vm.Title = _l["New trip participant"].Value;
92 vm.TripList = new SelectList(await _service.GetTripsAsync(), "Id", "Name", vm.TripParticipant.TripId);
93 vm.UserList = new SelectList(await _service.GetUsersAsync(), "Id", "Email", vm.TripParticipant.UserId);
94 return View(vm);
95 }
96
97 // GET: Admin/TripParticipants/Edit/5
98 public async Task<IActionResult> Edit(Guid? id)
99 {
100 if (id == null)
101 {
102 return NotFound();
103 }
104
105 var tripParticipant = await _service.GetByIdAsync(id.Value);
106 if (tripParticipant == null)
107 {
108 return NotFound();
109 }
110
111 var vm = new AdminTripParticipantFormViewModel
112 {
113 Title = _l["Edit trip participant"].Value,
114 TripParticipant = tripParticipant,
115 TripList = new SelectList(await _service.GetTripsAsync(), "Id", "Name", tripParticipant.TripId),
116 UserList = new SelectList(await _service.GetUsersAsync(), "Id", "Email", tripParticipant.UserId)
117 };
118 return View(vm);
119 }
120
121 // POST: Admin/TripParticipants/Edit/5
122 [HttpPost]
123 [ValidateAntiForgeryToken]
124 public async Task<IActionResult> Edit(Guid id, AdminTripParticipantFormViewModel vm)
125 {
126 if (id != vm.TripParticipant.Id)
127 {
128 return NotFound();
129 }
130
131 if (ModelState.IsValid)
132 {
133 try
134 {
135 await _service.UpdateAsync(vm.TripParticipant);
136 }
137 catch (DbUpdateConcurrencyException)
138 {
139 if (!await _service.ExistsAsync(vm.TripParticipant.Id))
140 {
141 return NotFound();
142 }
143 else
144 {
145 throw;
146 }
147 }
148
149 return RedirectToAction(nameof(Index));
150 }
151
152 vm.Title = _l["Edit trip participant"].Value;
153 vm.TripList = new SelectList(await _service.GetTripsAsync(), "Id", "Name", vm.TripParticipant.TripId);
154 vm.UserList = new SelectList(await _service.GetUsersAsync(), "Id", "Email", vm.TripParticipant.UserId);
155 return View(vm);
156 }
157
158 // GET: Admin/TripParticipants/Delete/5
159 public async Task<IActionResult> Delete(Guid? id)
160 {
161 if (id == null)
162 {
163 return NotFound();
164 }
165
166 var tripParticipant = await _service.GetByIdAsync(id.Value);
167 if (tripParticipant == null)
168 {
169 return NotFound();
170 }
171
172 return View(new AdminDeleteViewModel<TripParticipantBllDto>
173 {
174 Title = _l["Delete trip participant"].Value,
175 Item = tripParticipant
176 });
177 }
178
179 // POST: Admin/TripParticipants/Delete/5
180 [HttpPost, ActionName("Delete")]
181 [ValidateAntiForgeryToken]
182 public async Task<IActionResult> DeleteConfirmed(Guid id)
183 {
184 await _service.DeleteAsync(id);
185 return RedirectToAction(nameof(Index));
186 }
187 }
188 }
189