CurrenciesController.cs
4,316 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.EntityFrameworkCore; |
| 10 | using Microsoft.Extensions.Localization; |
| 11 | using SplitApp.WebApp.Areas.Admin.Models; |
| 12 | |
| 13 | namespace SplitApp.WebApp.Areas.Admin.Controllers |
| 14 | { |
| 15 | [Area("Admin")] |
| 16 | [Authorize(Roles = "admin")] |
| 17 | public class CurrenciesController : Controller |
| 18 | { |
| 19 | private readonly ICurrencyAdminService _service; |
| 20 | private readonly IStringLocalizer<App.Resources.Views.Shared> _l; |
| 21 | |
| 22 | public CurrenciesController(ICurrencyAdminService service, |
| 23 | IStringLocalizer<App.Resources.Views.Shared> l) |
| 24 | { |
| 25 | _service = service; |
| 26 | _l = l; |
| 27 | } |
| 28 | |
| 29 | public async Task<IActionResult> Index(string? search) |
| 30 | { |
| 31 | return View(new AdminCurrencyIndexViewModel |
| 32 | { |
| 33 | Title = _l["Currencies"].Value, |
| 34 | Items = await _service.GetAllAsync(search), |
| 35 | CurrentSearch = search |
| 36 | }); |
| 37 | } |
| 38 | |
| 39 | public async Task<IActionResult> Details(Guid? id) |
| 40 | { |
| 41 | if (id == null) return NotFound(); |
| 42 | var currency = await _service.GetByIdAsync(id.Value); |
| 43 | if (currency == null) return NotFound(); |
| 44 | |
| 45 | return View(new AdminDetailsViewModel<CurrencyBllDto> |
| 46 | { |
| 47 | Title = _l["Currency details"].Value, |
| 48 | Item = currency |
| 49 | }); |
| 50 | } |
| 51 | |
| 52 | public IActionResult Create() |
| 53 | { |
| 54 | return View(new AdminCurrencyFormViewModel { Title = _l["New currency"].Value }); |
| 55 | } |
| 56 | |
| 57 | [HttpPost] |
| 58 | [ValidateAntiForgeryToken] |
| 59 | public async Task<IActionResult> Create(AdminCurrencyFormViewModel vm, string? nameEn, string? nameEt) |
| 60 | { |
| 61 | ModelState.Remove("Currency.Name"); |
| 62 | |
| 63 | if (ModelState.IsValid) |
| 64 | { |
| 65 | await _service.CreateAsync(vm.Currency, nameEn, nameEt); |
| 66 | return RedirectToAction(nameof(Index)); |
| 67 | } |
| 68 | |
| 69 | vm.Title = _l["New currency"].Value; |
| 70 | return View(vm); |
| 71 | } |
| 72 | |
| 73 | public async Task<IActionResult> Edit(Guid? id) |
| 74 | { |
| 75 | if (id == null) return NotFound(); |
| 76 | var currency = await _service.GetByIdAsync(id.Value); |
| 77 | if (currency == null) return NotFound(); |
| 78 | |
| 79 | return View(new AdminCurrencyFormViewModel |
| 80 | { |
| 81 | Title = _l["Edit currency"].Value, |
| 82 | Currency = currency |
| 83 | }); |
| 84 | } |
| 85 | |
| 86 | [HttpPost] |
| 87 | [ValidateAntiForgeryToken] |
| 88 | public async Task<IActionResult> Edit(Guid id, AdminCurrencyFormViewModel vm, string? nameEn, string? nameEt) |
| 89 | { |
| 90 | if (id != vm.Currency.Id) return NotFound(); |
| 91 | |
| 92 | ModelState.Remove("Currency.Name"); |
| 93 | |
| 94 | if (ModelState.IsValid) |
| 95 | { |
| 96 | try |
| 97 | { |
| 98 | await _service.UpdateAsync(vm.Currency, nameEn, nameEt); |
| 99 | } |
| 100 | catch (DbUpdateConcurrencyException) |
| 101 | { |
| 102 | if (!await _service.ExistsAsync(vm.Currency.Id)) return NotFound(); |
| 103 | throw; |
| 104 | } |
| 105 | return RedirectToAction(nameof(Index)); |
| 106 | } |
| 107 | |
| 108 | vm.Title = _l["Edit currency"].Value; |
| 109 | return View(vm); |
| 110 | } |
| 111 | |
| 112 | public async Task<IActionResult> Delete(Guid? id) |
| 113 | { |
| 114 | if (id == null) return NotFound(); |
| 115 | var currency = await _service.GetByIdAsync(id.Value); |
| 116 | if (currency == null) return NotFound(); |
| 117 | |
| 118 | return View(new AdminDeleteViewModel<CurrencyBllDto> |
| 119 | { |
| 120 | Title = _l["Delete currency"].Value, |
| 121 | Item = currency |
| 122 | }); |
| 123 | } |
| 124 | |
| 125 | [HttpPost, ActionName("Delete")] |
| 126 | [ValidateAntiForgeryToken] |
| 127 | public async Task<IActionResult> DeleteConfirmed(Guid id) |
| 128 | { |
| 129 | await _service.DeleteAsync(id); |
| 130 | return RedirectToAction(nameof(Index)); |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | |