| | | 1 | | using System.Security.Cryptography; |
| | | 2 | | using System.Text.Json; |
| | | 3 | | using Microsoft.AspNetCore.Authorization; |
| | | 4 | | using Microsoft.AspNetCore.Mvc; |
| | | 5 | | using Microsoft.EntityFrameworkCore; |
| | | 6 | | using OzonDomains.Models; |
| | | 7 | | using OzonRepositories.Context; |
| | | 8 | | using Services.DataServices; |
| | | 9 | | |
| | | 10 | | namespace OzonOrdersWeb.Areas.Studio2.Controllers; |
| | | 11 | | |
| | | 12 | | [Authorize(Roles = "Admin")] |
| | | 13 | | [Area("Studio2")] |
| | | 14 | | public class AssemblyListAccessKeysController : Controller |
| | | 15 | | { |
| | | 16 | | private readonly ApplicationDbContext _db; |
| | | 17 | | private readonly OzonClientServices _ozonClientServices; |
| | | 18 | | |
| | 0 | 19 | | public AssemblyListAccessKeysController(ApplicationDbContext db, OzonClientServices ozonClientServices) |
| | | 20 | | { |
| | 0 | 21 | | _db = db; |
| | 0 | 22 | | _ozonClientServices = ozonClientServices; |
| | 0 | 23 | | } |
| | | 24 | | |
| | | 25 | | [HttpGet] |
| | | 26 | | public async Task<IActionResult> Index() |
| | | 27 | | { |
| | 0 | 28 | | var keys = await _db.AssemblyListAccessKeys |
| | 0 | 29 | | .OrderByDescending(k => k.CreatedAt) |
| | 0 | 30 | | .ToListAsync(); |
| | 0 | 31 | | var clients = await _ozonClientServices.GetOzonClients(); |
| | 0 | 32 | | ViewBag.Clients = clients; |
| | 0 | 33 | | return View(keys); |
| | 0 | 34 | | } |
| | | 35 | | |
| | | 36 | | [HttpGet] |
| | | 37 | | public async Task<IActionResult> Create() |
| | | 38 | | { |
| | 0 | 39 | | var clients = await _ozonClientServices.GetOzonClients(); |
| | 0 | 40 | | var warehouses = await _db.WarehouseMappings |
| | 0 | 41 | | .Where(w => w.OzonWarehouseName != null && w.OzonWarehouseName != "") |
| | 0 | 42 | | .Select(w => w.OzonWarehouseName) |
| | 0 | 43 | | .Distinct() |
| | 0 | 44 | | .OrderBy(w => w) |
| | 0 | 45 | | .ToListAsync(); |
| | 0 | 46 | | ViewBag.Warehouses = warehouses.OfType<string>().ToList(); |
| | 0 | 47 | | ViewBag.Clients = clients; |
| | 0 | 48 | | return View(new AssemblyListAccessKey()); |
| | 0 | 49 | | } |
| | | 50 | | |
| | | 51 | | [HttpPost] |
| | | 52 | | [ValidateAntiForgeryToken] |
| | | 53 | | public async Task<IActionResult> Create( |
| | | 54 | | [Bind("Description,ExpiresAt,PeriodFromOffsetDays,PeriodToOffsetDays")] AssemblyListAccessKey model, |
| | | 55 | | [FromForm] List<int> allowedClientIds, |
| | | 56 | | [FromForm] List<string> allowedStatuses, |
| | | 57 | | [FromForm] List<string> allowedWarehouses) |
| | | 58 | | { |
| | | 59 | | // AccessKey — non-nullable string вне [Bind]-списка. Model binding валидирует ВСЮ модель |
| | | 60 | | // (включая неявный required для non-nullable reference type, т.к. |
| | | 61 | | // SuppressImplicitRequiredAttributeForNonNullableReferenceTypes не включён) ДО входа в тело |
| | | 62 | | // экшена, поэтому ModelState уже содержит ошибку "AccessKey field is required" на этом этапе — |
| | | 63 | | // простое присвоение значения ниже её не убирает, нужно явно снять запись из ModelState. |
| | 0 | 64 | | ModelState.Remove(nameof(AssemblyListAccessKey.AccessKey)); |
| | | 65 | | |
| | 0 | 66 | | model.AccessKey = Convert.ToHexString(RandomNumberGenerator.GetBytes(16)).ToLower(); |
| | 0 | 67 | | model.IsActive = true; |
| | 0 | 68 | | model.CreatedAt = DateTime.UtcNow; |
| | 0 | 69 | | model.AllowedOzonClientIds = JsonSerializer.Serialize(allowedClientIds ?? new List<int>()); |
| | 0 | 70 | | model.AllowedStatuses = JsonSerializer.Serialize(allowedStatuses ?? new List<string>()); |
| | 0 | 71 | | model.AllowedWarehouses = JsonSerializer.Serialize(allowedWarehouses ?? new List<string>()); |
| | | 72 | | |
| | 0 | 73 | | if (allowedClientIds == null || allowedClientIds.Count == 0) |
| | 0 | 74 | | ModelState.AddModelError(string.Empty, "Выберите хотя бы одного клиента Ozon."); |
| | | 75 | | |
| | 0 | 76 | | if (model.PeriodFromOffsetDays > model.PeriodToOffsetDays) |
| | 0 | 77 | | ModelState.AddModelError(nameof(model.PeriodFromOffsetDays), "Дата «С» не может быть позже даты «По»."); |
| | | 78 | | |
| | 0 | 79 | | if (model.PeriodFromOffsetDays < -365 || model.PeriodToOffsetDays > 365) |
| | 0 | 80 | | ModelState.AddModelError(nameof(model.PeriodFromOffsetDays), "Смещение должно быть в пределах −365…+365 дней |
| | | 81 | | |
| | 0 | 82 | | if (!ModelState.IsValid) |
| | | 83 | | { |
| | | 84 | | // Дублируем все field-level ошибки в общую сводку (ModelOnly показывает только |
| | | 85 | | // string.Empty-ключи) — иначе пользователь не видит, почему форма не сохранилась. |
| | 0 | 86 | | foreach (var entry in ModelState) |
| | | 87 | | { |
| | 0 | 88 | | if (string.IsNullOrEmpty(entry.Key)) continue; |
| | 0 | 89 | | foreach (var error in entry.Value.Errors) |
| | 0 | 90 | | ModelState.AddModelError(string.Empty, $"{entry.Key}: {error.ErrorMessage}"); |
| | | 91 | | } |
| | | 92 | | |
| | 0 | 93 | | ViewBag.Clients = await _ozonClientServices.GetOzonClients(); |
| | 0 | 94 | | ViewBag.Warehouses = await _db.WarehouseMappings |
| | 0 | 95 | | .Where(w => w.OzonWarehouseName != null && w.OzonWarehouseName != "") |
| | 0 | 96 | | .Select(w => w.OzonWarehouseName) |
| | 0 | 97 | | .Distinct() |
| | 0 | 98 | | .OrderBy(w => w) |
| | 0 | 99 | | .ToListAsync(); |
| | 0 | 100 | | return View(model); |
| | | 101 | | } |
| | | 102 | | |
| | 0 | 103 | | _db.AssemblyListAccessKeys.Add(model); |
| | 0 | 104 | | await _db.SaveChangesAsync(); |
| | 0 | 105 | | return RedirectToAction(nameof(Index)); |
| | 0 | 106 | | } |
| | | 107 | | |
| | | 108 | | [HttpPost] |
| | | 109 | | [ValidateAntiForgeryToken] |
| | | 110 | | public async Task<IActionResult> Deactivate(long id) |
| | | 111 | | { |
| | 0 | 112 | | await _db.AssemblyListAccessKeys |
| | 0 | 113 | | .Where(k => k.Id == id) |
| | 0 | 114 | | .ExecuteUpdateAsync(s => s.SetProperty(k => k.IsActive, false)); |
| | 0 | 115 | | return RedirectToAction(nameof(Index)); |
| | 0 | 116 | | } |
| | | 117 | | |
| | | 118 | | [HttpPost] |
| | | 119 | | [ValidateAntiForgeryToken] |
| | | 120 | | public async Task<IActionResult> Activate(long id) |
| | | 121 | | { |
| | 0 | 122 | | await _db.AssemblyListAccessKeys |
| | 0 | 123 | | .Where(k => k.Id == id) |
| | 0 | 124 | | .ExecuteUpdateAsync(s => s.SetProperty(k => k.IsActive, true)); |
| | 0 | 125 | | return RedirectToAction(nameof(Index)); |
| | 0 | 126 | | } |
| | | 127 | | } |