profileShare

rasmusjy / splitapp-backend-clean-onion

Read-only snapshot

No repository description.

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