| | | 1 | | using Microsoft.AspNetCore.Authorization; |
| | | 2 | | using Microsoft.AspNetCore.Mvc; |
| | | 3 | | using Microsoft.EntityFrameworkCore; |
| | | 4 | | using OzonDomains.Models; |
| | | 5 | | using Services.CacheServices.Cache.OzonOrdersCache; |
| | | 6 | | using Services.DataServices; |
| | | 7 | | using Services.CacheServices.Cache; |
| | | 8 | | |
| | | 9 | | namespace OzonOrdersWeb.Controllers |
| | | 10 | | { |
| | | 11 | | [Authorize(Roles = "Admin")] |
| | | 12 | | [Area("Studio2")] |
| | | 13 | | public class AppStatusController : Controller |
| | | 14 | | { |
| | | 15 | | private readonly AppStatusDataServices _servcies; |
| | | 16 | | private readonly OrdersDataServices _ordersDataServices; |
| | | 17 | | private readonly OrderCache _cache; |
| | | 18 | | private readonly CacheUpdater<Order> _cacheUpdater; |
| | 0 | 19 | | public AppStatusController(AppStatusDataServices servcies, |
| | 0 | 20 | | OrdersDataServices ordersDataServices, |
| | 0 | 21 | | OrderCache orderCache, |
| | 0 | 22 | | CacheUpdater<Order> cacheUpdater) |
| | | 23 | | { |
| | 0 | 24 | | _servcies = servcies; |
| | 0 | 25 | | _ordersDataServices = ordersDataServices; |
| | 0 | 26 | | _cache = orderCache; |
| | 0 | 27 | | _cacheUpdater = cacheUpdater; |
| | 0 | 28 | | } |
| | | 29 | | |
| | | 30 | | /// <summary>Справочник статусов — весь список: сортировка, фильтры и поиск в гриде AG Grid на клиенте.</summary |
| | | 31 | | public async Task<IActionResult> Index() |
| | | 32 | | { |
| | 0 | 33 | | var appStatuses = (await _servcies.GetAppStatuses()).OrderBy(status => status.Name).ToList(); |
| | 0 | 34 | | return View(appStatuses); |
| | 0 | 35 | | } |
| | | 36 | | |
| | | 37 | | public async Task<IActionResult> Create() |
| | | 38 | | { |
| | 0 | 39 | | string? result = TempData["StatusResult"] as string; |
| | 0 | 40 | | ViewData["StatusResult"] = result; |
| | | 41 | | |
| | 0 | 42 | | var appStatusList = await _servcies.GetAppStatuses(); |
| | 0 | 43 | | ViewData["AppStatusList"] = appStatusList; |
| | | 44 | | |
| | 0 | 45 | | return View(); |
| | 0 | 46 | | } |
| | | 47 | | |
| | | 48 | | [HttpPost] |
| | | 49 | | public async Task<IActionResult> Create([Bind("Id,Name,IsFinal")] AppStatus appStatus) |
| | | 50 | | { |
| | 0 | 51 | | if (ModelState.IsValid) |
| | | 52 | | { |
| | | 53 | | |
| | | 54 | | |
| | 0 | 55 | | var existingStatus = await _servcies.GetAppStatusAsync(appStatus); |
| | 0 | 56 | | if (existingStatus != null) |
| | | 57 | | { |
| | 0 | 58 | | TempData["StatusResult"] = $"Не удалось добавить статус '{appStatus.Name}'"; |
| | 0 | 59 | | return RedirectToAction(nameof(Create)); |
| | | 60 | | } |
| | | 61 | | |
| | 0 | 62 | | await _servcies.AddAppStatus(appStatus); |
| | | 63 | | |
| | 0 | 64 | | return RedirectToAction("Index"); |
| | | 65 | | |
| | | 66 | | } |
| | 0 | 67 | | return View(appStatus); |
| | 0 | 68 | | } |
| | | 69 | | |
| | | 70 | | |
| | | 71 | | public async Task<IActionResult> Delete(int? id) |
| | | 72 | | { |
| | 0 | 73 | | if (!id.HasValue) |
| | 0 | 74 | | return RedirectToAction("Index"); |
| | | 75 | | |
| | 0 | 76 | | var appStatus = await _servcies.GetAppStatusAsync(id.Value); |
| | 0 | 77 | | if (appStatus != null) |
| | | 78 | | { |
| | 0 | 79 | | var defaultStatus = await _servcies.GetAppStatusAsync(new AppStatus(){Name = "Не указан"}); |
| | | 80 | | |
| | 0 | 81 | | var ordersWithDeletedStatus = (await _ordersDataServices.GetOrders()).ToList(); |
| | 0 | 82 | | ordersWithDeletedStatus = ordersWithDeletedStatus.Where(o => o.AppStatusId == id).ToList(); |
| | | 83 | | |
| | 0 | 84 | | foreach (var order in ordersWithDeletedStatus) |
| | | 85 | | { |
| | 0 | 86 | | order.AppStatus = defaultStatus; |
| | | 87 | | } |
| | 0 | 88 | | await _servcies.DeleteAppStatus(appStatus); |
| | 0 | 89 | | await _cacheUpdater.Update(_cache); |
| | 0 | 90 | | } |
| | | 91 | | |
| | 0 | 92 | | return RedirectToAction("Index"); |
| | 0 | 93 | | } |
| | | 94 | | |
| | | 95 | | public async Task<IActionResult> Edit(int id) |
| | | 96 | | { |
| | 0 | 97 | | var appStatus = await _servcies.GetAppStatusAsync(id); |
| | 0 | 98 | | if (appStatus == null) |
| | | 99 | | { |
| | 0 | 100 | | return NotFound(); |
| | | 101 | | } |
| | | 102 | | |
| | 0 | 103 | | return View(appStatus); |
| | 0 | 104 | | } |
| | | 105 | | |
| | | 106 | | |
| | | 107 | | [HttpPost] |
| | | 108 | | [ValidateAntiForgeryToken] |
| | | 109 | | public async Task<IActionResult> Edit(int id, AppStatus appStatus) |
| | | 110 | | { |
| | 0 | 111 | | if (id != appStatus.Id) |
| | | 112 | | { |
| | 0 | 113 | | return NotFound(); |
| | | 114 | | } |
| | | 115 | | |
| | 0 | 116 | | if (ModelState.IsValid) |
| | | 117 | | { |
| | | 118 | | try |
| | | 119 | | { |
| | 0 | 120 | | await _servcies.UpdateAppStatus(appStatus); |
| | 0 | 121 | | await _cacheUpdater.Update(_cache); |
| | 0 | 122 | | } |
| | 0 | 123 | | catch (DbUpdateConcurrencyException) |
| | | 124 | | { |
| | 0 | 125 | | if (await _servcies.GetAppStatusAsync(id) == null) |
| | | 126 | | { |
| | 0 | 127 | | return NotFound(); |
| | | 128 | | } |
| | | 129 | | else |
| | | 130 | | { |
| | 0 | 131 | | throw; |
| | | 132 | | } |
| | | 133 | | } |
| | 0 | 134 | | return RedirectToAction(nameof(Index)); |
| | | 135 | | } |
| | 0 | 136 | | return View(appStatus); |
| | 0 | 137 | | } |
| | | 138 | | |
| | | 139 | | [HttpPost] |
| | | 140 | | public async Task<IActionResult> UpdateStatusColor(int statusId, string colorCode) |
| | | 141 | | { |
| | | 142 | | try |
| | | 143 | | { |
| | 0 | 144 | | await _servcies.UpdateStatusColor(statusId, colorCode); |
| | 0 | 145 | | await _cacheUpdater.Update(_cache); |
| | 0 | 146 | | return Json(new { success = true }); |
| | | 147 | | } |
| | 0 | 148 | | catch (Exception ex) |
| | | 149 | | { |
| | 0 | 150 | | return Json(new { success = false, error = ex.Message }); |
| | | 151 | | } |
| | 0 | 152 | | } |
| | | 153 | | |
| | | 154 | | [HttpPost] |
| | | 155 | | public async Task<IActionResult> ResetStatusColor(int statusId) |
| | | 156 | | { |
| | | 157 | | try |
| | | 158 | | { |
| | 0 | 159 | | await _servcies.ResetStatusColor(statusId); |
| | 0 | 160 | | await _cacheUpdater.Update(_cache); |
| | 0 | 161 | | return Json(new { success = true }); |
| | | 162 | | } |
| | 0 | 163 | | catch (Exception ex) |
| | | 164 | | { |
| | 0 | 165 | | return Json(new { success = false, error = ex.Message }); |
| | | 166 | | } |
| | 0 | 167 | | } |
| | | 168 | | } |
| | | 169 | | } |