profileShare

rasmusjy / splitapp-backend-microservices

Read-only snapshot

No repository description.

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