SplitPresetsController.cs
3,559 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.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 SplitPresetsController : Controller |
| 18 | { |
| 19 | private readonly ISplitPresetAdminService _service; |
| 20 | private readonly IStringLocalizer<App.Resources.Views.Shared> _l; |
| 21 | |
| 22 | public SplitPresetsController(ISplitPresetAdminService 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 | var presets = await _service.GetAllAsync(search); |
| 32 | |
| 33 | var vm = new AdminSplitPresetIndexViewModel |
| 34 | { |
| 35 | Title = _l["Split presets"].Value, |
| 36 | Items = presets.OrderByDescending(s => s.Id).ToList(), |
| 37 | CurrentSearch = search |
| 38 | }; |
| 39 | return View(vm); |
| 40 | } |
| 41 | |
| 42 | public async Task<IActionResult> Details(Guid? id) |
| 43 | { |
| 44 | if (id == null) |
| 45 | { |
| 46 | return NotFound(); |
| 47 | } |
| 48 | |
| 49 | var splitPreset = await _service.GetByIdAsync(id.Value); |
| 50 | if (splitPreset == null) |
| 51 | { |
| 52 | return NotFound(); |
| 53 | } |
| 54 | |
| 55 | return View(new AdminDetailsViewModel<SplitPresetBllDto> |
| 56 | { |
| 57 | Title = _l["Split preset details"].Value, |
| 58 | Item = splitPreset |
| 59 | }); |
| 60 | } |
| 61 | |
| 62 | public async Task<IActionResult> Create() |
| 63 | { |
| 64 | return View(new AdminSplitPresetFormViewModel |
| 65 | { |
| 66 | Title = _l["New split preset"].Value, |
| 67 | TripList = new Microsoft.AspNetCore.Mvc.Rendering.SelectList(await _service.GetTripsAsync(), "Id", "Name"), |
| 68 | UserList = new Microsoft.AspNetCore.Mvc.Rendering.SelectList(await _service.GetUsersAsync(), "Id", "Email") |
| 69 | }); |
| 70 | } |
| 71 | |
| 72 | [HttpPost] |
| 73 | [ValidateAntiForgeryToken] |
| 74 | public async Task<IActionResult> Create(AdminSplitPresetFormViewModel vm) |
| 75 | { |
| 76 | if (ModelState.IsValid) |
| 77 | { |
| 78 | await _service.CreateAsync(vm.SplitPreset); |
| 79 | return RedirectToAction(nameof(Index)); |
| 80 | } |
| 81 | |
| 82 | vm.Title = _l["New split preset"].Value; |
| 83 | vm.TripList = new Microsoft.AspNetCore.Mvc.Rendering.SelectList(await _service.GetTripsAsync(), "Id", "Name", vm.SplitPreset.TripId); |
| 84 | vm.UserList = new Microsoft.AspNetCore.Mvc.Rendering.SelectList(await _service.GetUsersAsync(), "Id", "Email", vm.SplitPreset.CreatedById); |
| 85 | return View(vm); |
| 86 | } |
| 87 | |
| 88 | public async Task<IActionResult> Delete(Guid? id) |
| 89 | { |
| 90 | if (id == null) |
| 91 | { |
| 92 | return NotFound(); |
| 93 | } |
| 94 | |
| 95 | var splitPreset = await _service.GetByIdAsync(id.Value); |
| 96 | if (splitPreset == null) |
| 97 | { |
| 98 | return NotFound(); |
| 99 | } |
| 100 | |
| 101 | return View(new AdminDeleteViewModel<SplitPresetBllDto> |
| 102 | { |
| 103 | Title = _l["Delete split preset"].Value, |
| 104 | Item = splitPreset |
| 105 | }); |
| 106 | } |
| 107 | |
| 108 | [HttpPost, ActionName("Delete")] |
| 109 | [ValidateAntiForgeryToken] |
| 110 | public async Task<IActionResult> DeleteConfirmed(Guid id) |
| 111 | { |
| 112 | await _service.DeleteAsync(id); |
| 113 | return RedirectToAction(nameof(Index)); |
| 114 | } |
| 115 | } |
| 116 | |