profileShare

rasmusjy / splitapp-backend-microservices

Read-only snapshot

No repository description.

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