| | | 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 ColumnMappingsController : Controller |
| | | 12 | | { |
| | | 13 | | private readonly ColumnMappingDataServices _columnMappingDataServices; |
| | | 14 | | |
| | 0 | 15 | | public ColumnMappingsController(ColumnMappingDataServices columnMappingDataServices) |
| | | 16 | | { |
| | 0 | 17 | | _columnMappingDataServices = columnMappingDataServices; |
| | 0 | 18 | | } |
| | | 19 | | |
| | | 20 | | /// <summary>Весь список сопоставлений для грида AG Grid: фильтры и сортировка — на клиенте.</summary> |
| | | 21 | | public async Task<IActionResult> Index() |
| | | 22 | | { |
| | 0 | 23 | | var columnMappings = (await _columnMappingDataServices.GetColumnMappings()).OrderBy(m => m.MappingName).ToLi |
| | 0 | 24 | | return View(columnMappings); |
| | 0 | 25 | | } |
| | | 26 | | |
| | | 27 | | public async Task<IActionResult> Delete(int id) |
| | | 28 | | { |
| | 0 | 29 | | var columnMapping = await _columnMappingDataServices.GetColumnMappingAsync(id); |
| | 0 | 30 | | if (columnMapping != null) |
| | | 31 | | { |
| | 0 | 32 | | await _columnMappingDataServices.DeleteColumnMapping(columnMapping); |
| | | 33 | | } |
| | 0 | 34 | | return RedirectToAction(nameof(Index)); |
| | 0 | 35 | | } |
| | | 36 | | |
| | | 37 | | /// <summary> |
| | | 38 | | /// Страница редактирования сопоставления и его полей. |
| | | 39 | | /// </summary> |
| | | 40 | | [HttpGet] |
| | | 41 | | public async Task<IActionResult> Edit(int id) |
| | | 42 | | { |
| | 0 | 43 | | var columnMapping = await _columnMappingDataServices.GetColumnMappingAsync(id); |
| | 0 | 44 | | if (columnMapping == null) |
| | | 45 | | { |
| | 0 | 46 | | return NotFound(); |
| | | 47 | | } |
| | | 48 | | |
| | 0 | 49 | | return View(columnMapping); |
| | 0 | 50 | | } |
| | | 51 | | |
| | | 52 | | /// <summary> |
| | | 53 | | /// Сохранение изменений сопоставления (как с отдельной страницы, так и из списка). |
| | | 54 | | /// </summary> |
| | | 55 | | [HttpPost] |
| | | 56 | | [ValidateAntiForgeryToken] |
| | | 57 | | public async Task<IActionResult> Edit(int id, ColumnMapping columnMapping) |
| | | 58 | | { |
| | 0 | 59 | | if (id != columnMapping.Id) |
| | | 60 | | { |
| | 0 | 61 | | return NotFound(); |
| | | 62 | | } |
| | | 63 | | |
| | 0 | 64 | | if (!ModelState.IsValid) |
| | | 65 | | { |
| | 0 | 66 | | return View(columnMapping); |
| | | 67 | | } |
| | | 68 | | |
| | | 69 | | try |
| | | 70 | | { |
| | 0 | 71 | | await _columnMappingDataServices.UpdateColumnMapping(columnMapping); |
| | 0 | 72 | | } |
| | 0 | 73 | | catch (DbUpdateConcurrencyException) |
| | | 74 | | { |
| | | 75 | | // Если запись была изменена/удалена конкурирующей транзакцией |
| | 0 | 76 | | var existing = await _columnMappingDataServices.GetColumnMappingAsync(id); |
| | 0 | 77 | | if (existing == null) |
| | | 78 | | { |
| | 0 | 79 | | return NotFound(); |
| | | 80 | | } |
| | | 81 | | |
| | 0 | 82 | | throw; |
| | | 83 | | } |
| | | 84 | | |
| | 0 | 85 | | TempData["SuccessMessage"] = "Сопоставление успешно сохранено."; |
| | 0 | 86 | | return RedirectToAction(nameof(Index)); |
| | 0 | 87 | | } |
| | | 88 | | } |
| | | 89 | | } |