ExpensesController.cs
5,262 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 ExpensesController : Controller |
| 20 | { |
| 21 | private readonly IExpenseAdminService _service; |
| 22 | private readonly IStringLocalizer<App.Resources.Views.Shared> _l; |
| 23 | |
| 24 | public ExpensesController(IExpenseAdminService service, IStringLocalizer<App.Resources.Views.Shared> l) |
| 25 | { |
| 26 | _service = service; |
| 27 | _l = l; |
| 28 | } |
| 29 | |
| 30 | private async Task PopulateSelectListsAsync(AdminExpenseFormViewModel vm) |
| 31 | { |
| 32 | vm.TripList = new SelectList(await _service.GetTripsAsync(), "Id", "Name", vm.Expense.TripId); |
| 33 | vm.UserList = new SelectList(await _service.GetUsersAsync(), "Id", "Email", vm.Expense.PaidByUserId); |
| 34 | vm.CurrencyList = new SelectList(await _service.GetCurrenciesAsync(), "Id", "Code", vm.Expense.CurrencyId); |
| 35 | vm.BudgetCategoryList = new SelectList(await _service.GetBudgetCategoriesAsync(), "Id", "Name", vm.Expense.BudgetCategoryId); |
| 36 | } |
| 37 | |
| 38 | public async Task<IActionResult> Index(Guid? tripId, string? search) |
| 39 | { |
| 40 | var expenses = await _service.GetAllAsync(tripId, search); |
| 41 | var trips = (await _service.GetTripsAsync()).OrderBy(t => t.Name).ToList(); |
| 42 | |
| 43 | return View(new AdminExpenseIndexViewModel |
| 44 | { |
| 45 | Title = _l["Expenses"].Value, |
| 46 | Items = expenses.OrderByDescending(e => e.ExpenseDate).ToList(), |
| 47 | Trips = trips, |
| 48 | CurrentTripId = tripId, |
| 49 | CurrentSearch = search |
| 50 | }); |
| 51 | } |
| 52 | |
| 53 | public async Task<IActionResult> Details(Guid? id) |
| 54 | { |
| 55 | if (id == null) return NotFound(); |
| 56 | var expense = await _service.GetByIdAsync(id.Value); |
| 57 | if (expense == null) return NotFound(); |
| 58 | |
| 59 | return View(new AdminDetailsViewModel<ExpenseBllDto> |
| 60 | { |
| 61 | Title = _l["Expense details"].Value, |
| 62 | Item = expense |
| 63 | }); |
| 64 | } |
| 65 | |
| 66 | public async Task<IActionResult> Create() |
| 67 | { |
| 68 | var vm = new AdminExpenseFormViewModel { Title = _l["New expense"].Value }; |
| 69 | await PopulateSelectListsAsync(vm); |
| 70 | return View(vm); |
| 71 | } |
| 72 | |
| 73 | [HttpPost] |
| 74 | [ValidateAntiForgeryToken] |
| 75 | public async Task<IActionResult> Create(AdminExpenseFormViewModel vm) |
| 76 | { |
| 77 | if (ModelState.IsValid) |
| 78 | { |
| 79 | await _service.CreateAsync(vm.Expense); |
| 80 | return RedirectToAction(nameof(Index)); |
| 81 | } |
| 82 | |
| 83 | vm.Title = _l["New expense"].Value; |
| 84 | await PopulateSelectListsAsync(vm); |
| 85 | return View(vm); |
| 86 | } |
| 87 | |
| 88 | public async Task<IActionResult> Edit(Guid? id) |
| 89 | { |
| 90 | if (id == null) return NotFound(); |
| 91 | var expense = await _service.GetByIdAsync(id.Value); |
| 92 | if (expense == null) return NotFound(); |
| 93 | |
| 94 | var vm = new AdminExpenseFormViewModel |
| 95 | { |
| 96 | Title = _l["Edit expense"].Value, |
| 97 | Expense = expense |
| 98 | }; |
| 99 | await PopulateSelectListsAsync(vm); |
| 100 | return View(vm); |
| 101 | } |
| 102 | |
| 103 | [HttpPost] |
| 104 | [ValidateAntiForgeryToken] |
| 105 | public async Task<IActionResult> Edit(Guid id, AdminExpenseFormViewModel vm) |
| 106 | { |
| 107 | if (id != vm.Expense.Id) return NotFound(); |
| 108 | |
| 109 | if (ModelState.IsValid) |
| 110 | { |
| 111 | try |
| 112 | { |
| 113 | await _service.UpdateAsync(vm.Expense); |
| 114 | } |
| 115 | catch (DbUpdateConcurrencyException) |
| 116 | { |
| 117 | if (!await _service.ExistsAsync(vm.Expense.Id)) return NotFound(); |
| 118 | throw; |
| 119 | } |
| 120 | return RedirectToAction(nameof(Index)); |
| 121 | } |
| 122 | |
| 123 | vm.Title = _l["Edit expense"].Value; |
| 124 | await PopulateSelectListsAsync(vm); |
| 125 | return View(vm); |
| 126 | } |
| 127 | |
| 128 | public async Task<IActionResult> Delete(Guid? id) |
| 129 | { |
| 130 | if (id == null) return NotFound(); |
| 131 | var expense = await _service.GetByIdAsync(id.Value); |
| 132 | if (expense == null) return NotFound(); |
| 133 | |
| 134 | return View(new AdminDeleteViewModel<ExpenseBllDto> |
| 135 | { |
| 136 | Title = _l["Delete expense"].Value, |
| 137 | Item = expense |
| 138 | }); |
| 139 | } |
| 140 | |
| 141 | [HttpPost, ActionName("Delete")] |
| 142 | [ValidateAntiForgeryToken] |
| 143 | public async Task<IActionResult> DeleteConfirmed(Guid id) |
| 144 | { |
| 145 | await _service.DeleteAsync(id); |
| 146 | return RedirectToAction(nameof(Index)); |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | |