| | | 1 | | using Microsoft.AspNetCore.Authorization; |
| | | 2 | | using Microsoft.AspNetCore.Mvc; |
| | | 3 | | using Microsoft.EntityFrameworkCore; |
| | | 4 | | using OzonOrdersWeb.Areas.Studio2.ViewModels.Warehouse; |
| | | 5 | | using OzonRepositories.Context; |
| | | 6 | | using Services.ApiServices.InterpartsPriceApi; |
| | | 7 | | |
| | | 8 | | namespace OzonOrdersWeb.Areas.Studio2.Controllers.Directories; |
| | | 9 | | |
| | | 10 | | [Area("Studio2")] |
| | | 11 | | [Authorize(Roles = "User,Admin")] |
| | | 12 | | public class InterpartsSutCurrencyController : Controller |
| | | 13 | | { |
| | | 14 | | private readonly ApplicationDbContext _context; |
| | | 15 | | private readonly InterpartsCurrencyRateService _currencyRateService; |
| | | 16 | | |
| | 0 | 17 | | public InterpartsSutCurrencyController( |
| | 0 | 18 | | ApplicationDbContext context, |
| | 0 | 19 | | InterpartsCurrencyRateService currencyRateService) |
| | | 20 | | { |
| | 0 | 21 | | _context = context; |
| | 0 | 22 | | _currencyRateService = currencyRateService; |
| | 0 | 23 | | } |
| | | 24 | | |
| | | 25 | | [HttpGet] |
| | | 26 | | public async Task<IActionResult> Index() |
| | | 27 | | { |
| | 0 | 28 | | var rows = await _context.InterpartsSutCurrencies |
| | 0 | 29 | | .AsNoTracking() |
| | 0 | 30 | | .OrderBy(c => c.Id) |
| | 0 | 31 | | .Select(c => new InterpartsSutCurrencyRowViewModel |
| | 0 | 32 | | { |
| | 0 | 33 | | Id = c.Id, |
| | 0 | 34 | | Name = c.Name, |
| | 0 | 35 | | LongName = c.LongName, |
| | 0 | 36 | | RateToRub = c.RateToRub |
| | 0 | 37 | | }) |
| | 0 | 38 | | .ToListAsync() |
| | 0 | 39 | | .ConfigureAwait(false); |
| | | 40 | | |
| | 0 | 41 | | return View(new InterpartsSutCurrencyPageViewModel { Rows = rows }); |
| | 0 | 42 | | } |
| | | 43 | | |
| | | 44 | | [HttpPost] |
| | | 45 | | [ValidateAntiForgeryToken] |
| | | 46 | | public async Task<IActionResult> Save(InterpartsSutCurrencyPageViewModel model) |
| | | 47 | | { |
| | 0 | 48 | | if (model?.Rows == null || model.Rows.Count == 0) |
| | | 49 | | { |
| | 0 | 50 | | TempData["ErrorMessage"] = "Нет данных для сохранения."; |
| | 0 | 51 | | return RedirectToAction(nameof(Index)); |
| | | 52 | | } |
| | | 53 | | |
| | 0 | 54 | | var ids = model.Rows |
| | 0 | 55 | | .Where(r => r.Id > 0) |
| | 0 | 56 | | .Select(r => r.Id) |
| | 0 | 57 | | .Distinct() |
| | 0 | 58 | | .ToList(); |
| | | 59 | | |
| | 0 | 60 | | var entities = await _context.InterpartsSutCurrencies |
| | 0 | 61 | | .Where(x => ids.Contains(x.Id)) |
| | 0 | 62 | | .ToDictionaryAsync(x => x.Id) |
| | 0 | 63 | | .ConfigureAwait(false); |
| | | 64 | | |
| | 0 | 65 | | foreach (var row in model.Rows) |
| | | 66 | | { |
| | 0 | 67 | | if (row.Id <= 0) |
| | | 68 | | continue; |
| | | 69 | | |
| | 0 | 70 | | if (!entities.TryGetValue(row.Id, out var entity)) |
| | | 71 | | continue; |
| | | 72 | | |
| | 0 | 73 | | if (row.RateToRub <= 0) |
| | | 74 | | { |
| | 0 | 75 | | TempData["ErrorMessage"] = $"Курс для id={row.Id} должен быть больше нуля."; |
| | 0 | 76 | | return RedirectToAction(nameof(Index)); |
| | | 77 | | } |
| | | 78 | | |
| | 0 | 79 | | entity.RateToRub = row.RateToRub; |
| | 0 | 80 | | _context.InterpartsSutCurrencies.Update(entity); |
| | | 81 | | } |
| | | 82 | | |
| | 0 | 83 | | await _context.SaveChangesAsync().ConfigureAwait(false); |
| | 0 | 84 | | _currencyRateService.InvalidateRatesCache(); |
| | 0 | 85 | | TempData["SuccessMessage"] = "Курсы Interparts сохранены."; |
| | 0 | 86 | | return RedirectToAction(nameof(Index)); |
| | 0 | 87 | | } |
| | | 88 | | } |