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