profileShare

rasmusjy / splitapp-backend-clean-onion

Read-only snapshot

No repository description.

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