| | | 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 | | |
| | | 8 | | namespace OzonOrdersWeb.Areas.Studio2.Controllers.Directories |
| | | 9 | | { |
| | | 10 | | [Authorize(Roles = "Admin")] |
| | | 11 | | [Area("Studio2")] |
| | | 12 | | public class ManufacturersController : Controller |
| | | 13 | | { |
| | | 14 | | private readonly ManufacturerDataService _manufacturerDataService; |
| | | 15 | | private readonly OrdersDataServices _ordersDataServices; |
| | | 16 | | private readonly OrderCache _orderCache; |
| | | 17 | | |
| | 0 | 18 | | public ManufacturersController(ManufacturerDataService manufacturerDataService, |
| | 0 | 19 | | OrdersDataServices ordersDataServices, |
| | 0 | 20 | | OrderCache orderCache) |
| | | 21 | | { |
| | 0 | 22 | | _manufacturerDataService = manufacturerDataService; |
| | 0 | 23 | | _ordersDataServices = ordersDataServices; |
| | 0 | 24 | | _orderCache = orderCache; |
| | 0 | 25 | | } |
| | | 26 | | |
| | | 27 | | /// <summary>Весь справочник (сотни записей) для грида AG Grid: фильтры, сортировка и правка ячеек — на клиенте. |
| | | 28 | | public async Task<IActionResult> Index() |
| | | 29 | | { |
| | 0 | 30 | | var manufacturers = (await _manufacturerDataService.GetManufacturers()).OrderBy(m => m.Name).ToList(); |
| | 0 | 31 | | return View(manufacturers); |
| | 0 | 32 | | } |
| | | 33 | | |
| | | 34 | | public IActionResult Create() |
| | | 35 | | { |
| | 0 | 36 | | return View(); |
| | | 37 | | } |
| | | 38 | | |
| | | 39 | | [HttpPost] |
| | | 40 | | [ValidateAntiForgeryToken] |
| | | 41 | | public async Task<IActionResult> Create([Bind("Id,Code,Name")] Manufacturer manufacturer) |
| | | 42 | | { |
| | 0 | 43 | | Manufacturer? isOrig = (await _manufacturerDataService.GetManufacturers()).FirstOrDefault(m => m.Name?.ToLow |
| | 0 | 44 | | m.Code?.ToLo |
| | 0 | 45 | | if (isOrig == null) |
| | | 46 | | { |
| | 0 | 47 | | if (ModelState.IsValid) |
| | | 48 | | { |
| | 0 | 49 | | await _manufacturerDataService.AddManufacturer(manufacturer); |
| | 0 | 50 | | return RedirectToAction(nameof(Index)); |
| | | 51 | | } |
| | 0 | 52 | | return View(manufacturer); |
| | | 53 | | } |
| | | 54 | | else |
| | | 55 | | { |
| | 0 | 56 | | throw new Exception(); |
| | | 57 | | } |
| | 0 | 58 | | } |
| | | 59 | | |
| | | 60 | | public async Task<IActionResult> Edit(int id) |
| | | 61 | | { |
| | 0 | 62 | | var manufacturer = await _manufacturerDataService.GetManufacturerAsync(id); |
| | 0 | 63 | | if (manufacturer == null) |
| | | 64 | | { |
| | 0 | 65 | | return NotFound(); |
| | | 66 | | } |
| | 0 | 67 | | return View(manufacturer); |
| | 0 | 68 | | } |
| | | 69 | | |
| | | 70 | | [HttpPost] |
| | | 71 | | [ValidateAntiForgeryToken] |
| | | 72 | | public async Task<IActionResult> Edit(int id, [Bind("Id,Code,Name,EtProduserId")] Manufacturer manufacturer) |
| | | 73 | | { |
| | 0 | 74 | | if (id != manufacturer.Id) |
| | | 75 | | { |
| | 0 | 76 | | return NotFound(); |
| | | 77 | | } |
| | | 78 | | |
| | 0 | 79 | | if (ModelState.IsValid) |
| | | 80 | | { |
| | | 81 | | // Дубль — другая запись с тем же кодом и названием (сама правимая запись дублем не считается: |
| | | 82 | | // иначе нельзя поменять только ET ID). |
| | 0 | 83 | | Manufacturer? isOrig = (await _manufacturerDataService.GetManufacturers()).FirstOrDefault(m => m.Id != m |
| | 0 | 84 | | m.Name?.ToLo |
| | 0 | 85 | | m.Code?.ToLo |
| | | 86 | | |
| | | 87 | | try |
| | | 88 | | { |
| | 0 | 89 | | if (isOrig == null) |
| | | 90 | | { |
| | 0 | 91 | | await _manufacturerDataService.UpdateManufacturer(manufacturer); |
| | 0 | 92 | | List<Order> ordersToUpdate = (await _ordersDataServices.GetOrdersByManufacturerCode(manufacturer |
| | 0 | 93 | | await _orderCache.Update(); |
| | | 94 | | } |
| | | 95 | | else |
| | | 96 | | { |
| | 0 | 97 | | TempData["ErrorMessage"] = $"Производитель «{manufacturer.Name}» с кодом «{manufacturer.Code}» у |
| | | 98 | | } |
| | 0 | 99 | | } |
| | 0 | 100 | | catch (DbUpdateConcurrencyException) |
| | | 101 | | { |
| | 0 | 102 | | if (await _manufacturerDataService.GetManufacturerAsync(id) == null) |
| | | 103 | | { |
| | 0 | 104 | | return NotFound(); |
| | | 105 | | } |
| | | 106 | | else |
| | | 107 | | { |
| | 0 | 108 | | throw; |
| | | 109 | | } |
| | | 110 | | } |
| | 0 | 111 | | return RedirectToAction(nameof(Index)); |
| | | 112 | | } |
| | 0 | 113 | | return View(manufacturer); |
| | 0 | 114 | | } |
| | | 115 | | } |
| | | 116 | | } |