| | | 1 | | using Microsoft.AspNetCore.Authorization; |
| | | 2 | | using Microsoft.AspNetCore.Mvc; |
| | | 3 | | using OzonDomains.Models; |
| | | 4 | | using OzonOrdersWeb.Areas.Studio2.ViewModels.ExcludedArticles; |
| | | 5 | | using Services.DataServices; |
| | | 6 | | |
| | | 7 | | namespace OzonWebApp.Controllers |
| | | 8 | | { |
| | | 9 | | [Area("Studio2")] |
| | | 10 | | [Authorize(Roles = "User,Admin")] |
| | | 11 | | public class ExcludedArticlesController : Controller |
| | | 12 | | { |
| | | 13 | | private readonly ExcludedArticleDataServices _excludedArticleService; |
| | | 14 | | private readonly OzonClientServices _ozonClientService; |
| | | 15 | | private readonly OrdersDataServices _ordersService; |
| | | 16 | | |
| | 0 | 17 | | public ExcludedArticlesController( |
| | 0 | 18 | | ExcludedArticleDataServices excludedArticleService, |
| | 0 | 19 | | OzonClientServices ozonClientService, |
| | 0 | 20 | | OrdersDataServices ordersService) |
| | | 21 | | { |
| | 0 | 22 | | _excludedArticleService = excludedArticleService; |
| | 0 | 23 | | _ozonClientService = ozonClientService; |
| | 0 | 24 | | _ordersService = ordersService; |
| | 0 | 25 | | } |
| | | 26 | | |
| | | 27 | | /// <summary>Весь список исключений для грида AG Grid: фильтры, сортировка и поиск — на клиенте.</summary> |
| | | 28 | | public async Task<IActionResult> Index() |
| | | 29 | | { |
| | 0 | 30 | | return View(await _excludedArticleService.GetExcludedArticles()); |
| | 0 | 31 | | } |
| | | 32 | | |
| | | 33 | | [HttpGet] |
| | | 34 | | public async Task<IActionResult> Create(int orderId) |
| | | 35 | | { |
| | 0 | 36 | | var clients = await _ozonClientService.GetOzonClients(); |
| | 0 | 37 | | ViewBag.OzonClients = clients ?? new List<OzonClient>(); |
| | | 38 | | |
| | 0 | 39 | | if (orderId != 0) |
| | | 40 | | { |
| | 0 | 41 | | var order = await _ordersService.GetOrder(orderId); |
| | 0 | 42 | | if (order == null) |
| | 0 | 43 | | return View(new ExcludedArticleViewModel()); |
| | | 44 | | |
| | 0 | 45 | | return View(new ExcludedArticleViewModel |
| | 0 | 46 | | { |
| | 0 | 47 | | Article = order.Article ?? string.Empty, |
| | 0 | 48 | | OzonClientId = order.OzonClient?.Id ?? 0 |
| | 0 | 49 | | }); |
| | | 50 | | } |
| | 0 | 51 | | return View(new ExcludedArticleViewModel()); |
| | 0 | 52 | | } |
| | | 53 | | |
| | | 54 | | |
| | | 55 | | [HttpPost] |
| | | 56 | | [ValidateAntiForgeryToken] |
| | | 57 | | public async Task<IActionResult> Create(ExcludedArticleViewModel model) |
| | | 58 | | { |
| | 0 | 59 | | if (ModelState.IsValid) |
| | | 60 | | { |
| | 0 | 61 | | var article = new ExcludedArticle |
| | 0 | 62 | | { |
| | 0 | 63 | | Article = model.Article ?? string.Empty, |
| | 0 | 64 | | OzonClientId = model.OzonClientId |
| | 0 | 65 | | }; |
| | | 66 | | |
| | 0 | 67 | | await _excludedArticleService.AddExcludedArticle(article); |
| | 0 | 68 | | return RedirectToAction(nameof(Index)); |
| | | 69 | | } |
| | | 70 | | |
| | 0 | 71 | | ViewBag.OzonClients = await _ozonClientService.GetOzonClients(); |
| | 0 | 72 | | return View(model); |
| | 0 | 73 | | } |
| | | 74 | | |
| | | 75 | | public async Task<IActionResult> Edit(int id) |
| | | 76 | | { |
| | 0 | 77 | | var article = await _excludedArticleService.GetExcludedArticleAsync(id); |
| | 0 | 78 | | if (article == null) return NotFound(); |
| | | 79 | | |
| | 0 | 80 | | ViewBag.OzonClients = await _ozonClientService.GetOzonClients(); |
| | 0 | 81 | | return View(article); |
| | 0 | 82 | | } |
| | | 83 | | |
| | | 84 | | [HttpPost] |
| | | 85 | | [ValidateAntiForgeryToken] |
| | | 86 | | public async Task<IActionResult> Edit(int id, ExcludedArticle model) |
| | | 87 | | { |
| | 0 | 88 | | if (id != model.Id) return NotFound(); |
| | | 89 | | |
| | 0 | 90 | | if (ModelState.IsValid) |
| | | 91 | | { |
| | 0 | 92 | | await _excludedArticleService.UpdateExcludedArticle(model); |
| | 0 | 93 | | return RedirectToAction(nameof(Index)); |
| | | 94 | | } |
| | 0 | 95 | | ViewBag.OzonClients = await _ozonClientService.GetOzonClients(); |
| | 0 | 96 | | return View(model); |
| | 0 | 97 | | } |
| | | 98 | | |
| | | 99 | | public async Task<IActionResult> Delete(int id) |
| | | 100 | | { |
| | 0 | 101 | | var article = await _excludedArticleService.GetExcludedArticleAsync(id); |
| | 0 | 102 | | if (article == null) return NotFound(); |
| | | 103 | | |
| | 0 | 104 | | return View(article); |
| | 0 | 105 | | } |
| | | 106 | | |
| | | 107 | | [HttpPost, ActionName("Delete")] |
| | | 108 | | [ValidateAntiForgeryToken] |
| | | 109 | | public async Task<IActionResult> DeleteConfirmed(int id) |
| | | 110 | | { |
| | 0 | 111 | | var article = await _excludedArticleService.GetExcludedArticleAsync(id); |
| | 0 | 112 | | if (article != null) |
| | | 113 | | { |
| | 0 | 114 | | await _excludedArticleService.DeleteExcludedArticle(article); |
| | | 115 | | } |
| | 0 | 116 | | return RedirectToAction(nameof(Index)); |
| | 0 | 117 | | } |
| | | 118 | | } |
| | | 119 | | } |