profileShare

rasmusjy / splitapp-backend-microservices

Read-only snapshot

No repository description.

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