< Summary

Information
Class: OzonOrdersWeb.Areas.Studio2.Controllers.Directories.ManufacturersController
Assembly: OzonOrdersWeb
File(s): /home/runner/work/Studio2Prod/Studio2Prod/OzonOrdersWeb/Areas/Studio2/Controllers/Directories/ManufacturersController.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 44
Coverable lines: 44
Total lines: 116
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 42
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%
Create()100%210%
Create()0%2040%
Edit()0%620%
Edit()0%110100%

File(s)

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

#LineLine coverage
 1using Microsoft.AspNetCore.Authorization;
 2using Microsoft.AspNetCore.Mvc;
 3using Microsoft.EntityFrameworkCore;
 4using OzonDomains.Models;
 5using Services.CacheServices.Cache.OzonOrdersCache;
 6using Services.DataServices;
 7
 8namespace OzonOrdersWeb.Areas.Studio2.Controllers.Directories
 9{
 10    [Authorize(Roles = "Admin")]
 11    [Area("Studio2")]
 12    public class ManufacturersController : Controller
 13    {
 14        private readonly ManufacturerDataService _manufacturerDataService;
 15        private readonly OrdersDataServices _ordersDataServices;
 16        private readonly OrderCache _orderCache;
 17
 018        public ManufacturersController(ManufacturerDataService manufacturerDataService,
 019                                       OrdersDataServices ordersDataServices,
 020                                       OrderCache orderCache)
 21        {
 022            _manufacturerDataService = manufacturerDataService;
 023            _ordersDataServices = ordersDataServices;
 024            _orderCache = orderCache;
 025        }
 26
 27        /// <summary>Весь справочник (сотни записей) для грида AG Grid: фильтры, сортировка и правка ячеек — на клиенте.
 28        public async Task<IActionResult> Index()
 29        {
 030            var manufacturers = (await _manufacturerDataService.GetManufacturers()).OrderBy(m => m.Name).ToList();
 031            return View(manufacturers);
 032        }
 33
 34        public IActionResult Create()
 35        {
 036            return View();
 37        }
 38
 39        [HttpPost]
 40        [ValidateAntiForgeryToken]
 41        public async Task<IActionResult> Create([Bind("Id,Code,Name")] Manufacturer manufacturer)
 42        {
 043            Manufacturer? isOrig = (await _manufacturerDataService.GetManufacturers()).FirstOrDefault(m => m.Name?.ToLow
 044                                                                                                            m.Code?.ToLo
 045            if (isOrig == null)
 46            {
 047                if (ModelState.IsValid)
 48                {
 049                    await _manufacturerDataService.AddManufacturer(manufacturer);
 050                    return RedirectToAction(nameof(Index));
 51                }
 052                return View(manufacturer);
 53            }
 54            else
 55            {
 056                throw new Exception();
 57            }
 058        }
 59
 60        public async Task<IActionResult> Edit(int id)
 61        {
 062            var manufacturer = await _manufacturerDataService.GetManufacturerAsync(id);
 063            if (manufacturer == null)
 64            {
 065                return NotFound();
 66            }
 067            return View(manufacturer);
 068        }
 69
 70        [HttpPost]
 71        [ValidateAntiForgeryToken]
 72        public async Task<IActionResult> Edit(int id, [Bind("Id,Code,Name,EtProduserId")] Manufacturer manufacturer)
 73        {
 074            if (id != manufacturer.Id)
 75            {
 076                return NotFound();
 77            }
 78
 079            if (ModelState.IsValid)
 80            {
 81                // Дубль — другая запись с тем же кодом и названием (сама правимая запись дублем не считается:
 82                // иначе нельзя поменять только ET ID).
 083                Manufacturer? isOrig = (await _manufacturerDataService.GetManufacturers()).FirstOrDefault(m => m.Id != m
 084                                                                                                            m.Name?.ToLo
 085                                                                                                            m.Code?.ToLo
 86
 87                try
 88                {
 089                    if (isOrig == null)
 90                    {
 091                        await _manufacturerDataService.UpdateManufacturer(manufacturer);
 092                        List<Order> ordersToUpdate = (await _ordersDataServices.GetOrdersByManufacturerCode(manufacturer
 093                        await _orderCache.Update();
 94                    }
 95                    else
 96                    {
 097                        TempData["ErrorMessage"] = $"Производитель «{manufacturer.Name}» с кодом «{manufacturer.Code}» у
 98                    }
 099                }
 0100                catch (DbUpdateConcurrencyException)
 101                {
 0102                    if (await _manufacturerDataService.GetManufacturerAsync(id) == null)
 103                    {
 0104                        return NotFound();
 105                    }
 106                    else
 107                    {
 0108                        throw;
 109                    }
 110                }
 0111                return RedirectToAction(nameof(Index));
 112            }
 0113            return View(manufacturer);
 0114        }
 115    }
 116}