profileShare

rasmusjy / splitapp-backend-modular-monolith

Read-only snapshot

No repository description.

main default branch 418 files Expires Sep 13, 2026, 9:06 AM
BudgetCategoriesController.cs 5,221 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 BudgetCategoriesController : Controller
20 {
21 private readonly IBudgetCategoryAdminService _service;
22 private readonly IStringLocalizer<App.Resources.Views.Shared> _l;
23
24 public BudgetCategoriesController(IBudgetCategoryAdminService service,
25 IStringLocalizer<App.Resources.Views.Shared> l)
26 {
27 _service = service;
28 _l = l;
29 }
30
31 public async Task<IActionResult> Index(Guid? tripId, string? search)
32 {
33 var vm = new AdminBudgetCategoryIndexViewModel
34 {
35 Title = _l["Budget Categories"].Value,
36 Items = await _service.GetAllAsync(tripId, search),
37 Trips = await _service.GetAllTripsAsync(),
38 CurrentTripId = tripId,
39 CurrentSearch = search
40 };
41 return View(vm);
42 }
43
44 public async Task<IActionResult> Details(Guid? id)
45 {
46 if (id == null) return NotFound();
47 var entity = await _service.GetByIdAsync(id.Value);
48 if (entity == null) return NotFound();
49
50 return View(new AdminDetailsViewModel<BudgetCategoryBllDto>
51 {
52 Title = _l["Budget Category details"].Value,
53 Item = entity
54 });
55 }
56
57 public async Task<IActionResult> Create()
58 {
59 var vm = new AdminBudgetCategoryFormViewModel
60 {
61 Title = _l["New budget category"].Value,
62 TripList = new SelectList(await _service.GetAllTripsAsync(), "Id", "Name")
63 };
64 return View(vm);
65 }
66
67 [HttpPost]
68 [ValidateAntiForgeryToken]
69 public async Task<IActionResult> Create(AdminBudgetCategoryFormViewModel vm, string? nameEn, string? nameEt)
70 {
71 ModelState.Remove("BudgetCategory.Name");
72
73 if (ModelState.IsValid)
74 {
75 await _service.CreateAsync(vm.BudgetCategory, nameEn, nameEt);
76 return RedirectToAction(nameof(Index));
77 }
78
79 vm.Title = _l["New budget category"].Value;
80 vm.TripList = new SelectList(await _service.GetAllTripsAsync(), "Id", "Name", vm.BudgetCategory.TripId);
81 return View(vm);
82 }
83
84 public async Task<IActionResult> Edit(Guid? id)
85 {
86 if (id == null) return NotFound();
87 var entity = await _service.GetByIdAsync(id.Value);
88 if (entity == null) return NotFound();
89
90 var vm = new AdminBudgetCategoryFormViewModel
91 {
92 Title = _l["Edit budget category"].Value,
93 BudgetCategory = entity,
94 TripList = new SelectList(await _service.GetAllTripsAsync(), "Id", "Name", entity.TripId)
95 };
96 return View(vm);
97 }
98
99 [HttpPost]
100 [ValidateAntiForgeryToken]
101 public async Task<IActionResult> Edit(Guid id, AdminBudgetCategoryFormViewModel vm, string? nameEn, string? nameEt)
102 {
103 if (id != vm.BudgetCategory.Id) return NotFound();
104
105 ModelState.Remove("BudgetCategory.Name");
106
107 if (ModelState.IsValid)
108 {
109 try
110 {
111 await _service.UpdateAsync(vm.BudgetCategory, nameEn, nameEt);
112 }
113 catch (DbUpdateConcurrencyException)
114 {
115 if (!await _service.ExistsAsync(vm.BudgetCategory.Id)) return NotFound();
116 throw;
117 }
118 return RedirectToAction(nameof(Index));
119 }
120
121 vm.Title = _l["Edit budget category"].Value;
122 vm.TripList = new SelectList(await _service.GetAllTripsAsync(), "Id", "Name", vm.BudgetCategory.TripId);
123 return View(vm);
124 }
125
126 public async Task<IActionResult> Delete(Guid? id)
127 {
128 if (id == null) return NotFound();
129 var entity = await _service.GetByIdAsync(id.Value);
130 if (entity == null) return NotFound();
131
132 return View(new AdminDeleteViewModel<BudgetCategoryBllDto>
133 {
134 Title = _l["Delete budget category"].Value,
135 Item = entity
136 });
137 }
138
139 [HttpPost, ActionName("Delete")]
140 [ValidateAntiForgeryToken]
141 public async Task<IActionResult> DeleteConfirmed(Guid id)
142 {
143 await _service.DeleteAsync(id);
144 return RedirectToAction(nameof(Index));
145 }
146 }
147 }
148