< Summary

Information
Class: OzonOrdersWeb.Areas.Studio2.Controllers.Directories.InterpartsSutCurrencyController
Assembly: OzonOrdersWeb
File(s): /home/runner/work/Studio2Prod/Studio2Prod/OzonOrdersWeb/Areas/Studio2/Controllers/Directories/InterpartsSutCurrencyController.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 45
Coverable lines: 45
Total lines: 88
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 14
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%210%
Index()100%210%
Save()0%210140%

File(s)

/home/runner/work/Studio2Prod/Studio2Prod/OzonOrdersWeb/Areas/Studio2/Controllers/Directories/InterpartsSutCurrencyController.cs

#LineLine coverage
 1using Microsoft.AspNetCore.Authorization;
 2using Microsoft.AspNetCore.Mvc;
 3using Microsoft.EntityFrameworkCore;
 4using OzonOrdersWeb.Areas.Studio2.ViewModels.Warehouse;
 5using OzonRepositories.Context;
 6using Services.ApiServices.InterpartsPriceApi;
 7
 8namespace OzonOrdersWeb.Areas.Studio2.Controllers.Directories;
 9
 10[Area("Studio2")]
 11[Authorize(Roles = "User,Admin")]
 12public class InterpartsSutCurrencyController : Controller
 13{
 14    private readonly ApplicationDbContext _context;
 15    private readonly InterpartsCurrencyRateService _currencyRateService;
 16
 017    public InterpartsSutCurrencyController(
 018        ApplicationDbContext context,
 019        InterpartsCurrencyRateService currencyRateService)
 20    {
 021        _context = context;
 022        _currencyRateService = currencyRateService;
 023    }
 24
 25    [HttpGet]
 26    public async Task<IActionResult> Index()
 27    {
 028        var rows = await _context.InterpartsSutCurrencies
 029            .AsNoTracking()
 030            .OrderBy(c => c.Id)
 031            .Select(c => new InterpartsSutCurrencyRowViewModel
 032            {
 033                Id = c.Id,
 034                Name = c.Name,
 035                LongName = c.LongName,
 036                RateToRub = c.RateToRub
 037            })
 038            .ToListAsync()
 039            .ConfigureAwait(false);
 40
 041        return View(new InterpartsSutCurrencyPageViewModel { Rows = rows });
 042    }
 43
 44    [HttpPost]
 45    [ValidateAntiForgeryToken]
 46    public async Task<IActionResult> Save(InterpartsSutCurrencyPageViewModel model)
 47    {
 048        if (model?.Rows == null || model.Rows.Count == 0)
 49        {
 050            TempData["ErrorMessage"] = "Нет данных для сохранения.";
 051            return RedirectToAction(nameof(Index));
 52        }
 53
 054        var ids = model.Rows
 055            .Where(r => r.Id > 0)
 056            .Select(r => r.Id)
 057            .Distinct()
 058            .ToList();
 59
 060        var entities = await _context.InterpartsSutCurrencies
 061            .Where(x => ids.Contains(x.Id))
 062            .ToDictionaryAsync(x => x.Id)
 063            .ConfigureAwait(false);
 64
 065        foreach (var row in model.Rows)
 66        {
 067            if (row.Id <= 0)
 68                continue;
 69
 070            if (!entities.TryGetValue(row.Id, out var entity))
 71                continue;
 72
 073            if (row.RateToRub <= 0)
 74            {
 075                TempData["ErrorMessage"] = $"Курс для id={row.Id} должен быть больше нуля.";
 076                return RedirectToAction(nameof(Index));
 77            }
 78
 079            entity.RateToRub = row.RateToRub;
 080            _context.InterpartsSutCurrencies.Update(entity);
 81        }
 82
 083        await _context.SaveChangesAsync().ConfigureAwait(false);
 084        _currencyRateService.InvalidateRatesCache();
 085        TempData["SuccessMessage"] = "Курсы Interparts сохранены.";
 086        return RedirectToAction(nameof(Index));
 087    }
 88}