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