| | | 1 | | using Microsoft.AspNetCore.Authorization; |
| | | 2 | | using Microsoft.AspNetCore.Mvc; |
| | | 3 | | using Microsoft.EntityFrameworkCore; |
| | | 4 | | using OzonDomains.Models; |
| | | 5 | | using Services.DataServices; |
| | | 6 | | |
| | | 7 | | namespace OzonOrdersWeb.Areas.Studio2.Controllers.Directories |
| | | 8 | | { |
| | | 9 | | [Authorize(Roles = "Admin")] |
| | | 10 | | [Area("Studio2")] |
| | | 11 | | public class ExpenseTypeController : Controller |
| | | 12 | | { |
| | | 13 | | private readonly ExpenseTypeDataServices _servcies; |
| | | 14 | | |
| | 0 | 15 | | public ExpenseTypeController( |
| | 0 | 16 | | ExpenseTypeDataServices servcies) |
| | | 17 | | { |
| | 0 | 18 | | _servcies = servcies; |
| | 0 | 19 | | } |
| | | 20 | | |
| | | 21 | | /// <summary>Весь справочник для грида AG Grid: фильтры и сортировка — на клиенте.</summary> |
| | | 22 | | public async Task<IActionResult> Index() |
| | | 23 | | { |
| | 0 | 24 | | var expenseTypes = (await _servcies.GetExpenseTypes()).OrderBy(e => e.Name).ToList(); |
| | 0 | 25 | | return View(expenseTypes); |
| | 0 | 26 | | } |
| | | 27 | | |
| | | 28 | | public async Task<IActionResult> Create() |
| | | 29 | | { |
| | 0 | 30 | | string? result = TempData["StatusResult"] as string; |
| | 0 | 31 | | ViewData["StatusResult"] = result; |
| | | 32 | | |
| | 0 | 33 | | var expenseTypeList = await _servcies.GetExpenseTypes(); |
| | 0 | 34 | | ViewData["ExpenseTypeList"] = expenseTypeList; |
| | | 35 | | |
| | 0 | 36 | | return View(); |
| | 0 | 37 | | } |
| | | 38 | | |
| | | 39 | | [HttpPost] |
| | | 40 | | [ValidateAntiForgeryToken] |
| | | 41 | | public async Task<IActionResult> Create([Bind("Id,Name,Description,IsActive")] ExpenseType expenseType) |
| | | 42 | | { |
| | 0 | 43 | | if (ModelState.IsValid) |
| | | 44 | | { |
| | 0 | 45 | | var existingExpenseType = await _servcies.GetExpenseTypeAsync(expenseType); |
| | 0 | 46 | | if (existingExpenseType != null) |
| | | 47 | | { |
| | 0 | 48 | | TempData["StatusResult"] = $"Не удалось добавить тип расхода '{expenseType.Name}'"; |
| | 0 | 49 | | return RedirectToAction(nameof(Create)); |
| | | 50 | | } |
| | | 51 | | |
| | 0 | 52 | | await _servcies.AddExpenseType(expenseType); |
| | | 53 | | |
| | 0 | 54 | | return RedirectToAction("Index"); |
| | | 55 | | } |
| | | 56 | | |
| | 0 | 57 | | return View(expenseType); |
| | 0 | 58 | | } |
| | | 59 | | |
| | | 60 | | public async Task<IActionResult> Delete(int? id) |
| | | 61 | | { |
| | 0 | 62 | | if (!id.HasValue) |
| | 0 | 63 | | return RedirectToAction("Index"); |
| | | 64 | | |
| | 0 | 65 | | var expenseType = await _servcies.GetExpenseTypeAsync(id.Value); |
| | 0 | 66 | | if (expenseType != null) |
| | | 67 | | { |
| | 0 | 68 | | await _servcies.DeleteExpenseType(expenseType); |
| | | 69 | | } |
| | | 70 | | |
| | 0 | 71 | | return RedirectToAction("Index"); |
| | 0 | 72 | | } |
| | | 73 | | |
| | | 74 | | public async Task<IActionResult> Edit(int id) |
| | | 75 | | { |
| | 0 | 76 | | var expenseType = await _servcies.GetExpenseTypeAsync(id); |
| | 0 | 77 | | if (expenseType == null) |
| | | 78 | | { |
| | 0 | 79 | | return NotFound(); |
| | | 80 | | } |
| | | 81 | | |
| | 0 | 82 | | return View(expenseType); |
| | 0 | 83 | | } |
| | | 84 | | |
| | | 85 | | [HttpPost] |
| | | 86 | | [ValidateAntiForgeryToken] |
| | | 87 | | public async Task<IActionResult> Edit(int id, ExpenseType expenseType) |
| | | 88 | | { |
| | 0 | 89 | | if (id != expenseType.Id) |
| | | 90 | | { |
| | 0 | 91 | | return NotFound(); |
| | | 92 | | } |
| | | 93 | | |
| | 0 | 94 | | if (ModelState.IsValid) |
| | | 95 | | { |
| | | 96 | | try |
| | | 97 | | { |
| | 0 | 98 | | await _servcies.UpdateExpenseType(expenseType); |
| | 0 | 99 | | } |
| | 0 | 100 | | catch (DbUpdateConcurrencyException) |
| | | 101 | | { |
| | 0 | 102 | | if (await _servcies.GetExpenseTypeAsync(id) == null) |
| | | 103 | | { |
| | 0 | 104 | | return NotFound(); |
| | | 105 | | } |
| | | 106 | | else |
| | | 107 | | { |
| | 0 | 108 | | throw; |
| | | 109 | | } |
| | | 110 | | } |
| | | 111 | | |
| | 0 | 112 | | return RedirectToAction(nameof(Index)); |
| | | 113 | | } |
| | | 114 | | |
| | 0 | 115 | | return View(expenseType); |
| | 0 | 116 | | } |
| | | 117 | | } |
| | | 118 | | } |