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