< Summary

Information
Class: OzonOrdersWeb.Areas.Studio2.Controllers.Directories.ClientStatusController
Assembly: OzonOrdersWeb
File(s): /home/runner/work/Studio2Prod/Studio2Prod/OzonOrdersWeb/Areas/Studio2/Controllers/Directories/ClientStatusController.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 100
Coverable lines: 100
Total lines: 210
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 28
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()0%620%
Create()0%4260%
Delete()0%7280%
Edit()0%2040%
Edit()0%7280%
UpdateStatusColor()100%210%
ResetStatusColor()100%210%

File(s)

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

#LineLine coverage
 1using Microsoft.AspNetCore.Authorization;
 2using Microsoft.AspNetCore.Mvc;
 3using Microsoft.AspNetCore.Mvc.Rendering;
 4using Microsoft.EntityFrameworkCore;
 5using OzonDomains;
 6using OzonDomains.Models;
 7using Services.CacheServices.Cache.OzonOrdersCache;
 8using Services.DataServices;
 9using Services.CacheServices.Cache;
 10
 11namespace OzonOrdersWeb.Areas.Studio2.Controllers.Directories
 12{
 13    [Authorize(Roles = "Admin")]
 14    [Area("Studio2")]
 15    public class ClientStatusController : Controller
 16    {
 17        private readonly ClientStatusDataServices _servcies;
 18        private readonly OrdersDataServices _ordersDataServices;
 19        private readonly OrderCache _cache;
 20        private readonly CacheUpdater<Order> _cacheUpdater;
 21
 022        public ClientStatusController(ClientStatusDataServices servcies,
 023            OrdersDataServices ordersDataServices,
 024            OrderCache orderCache,
 025            CacheUpdater<Order> cacheUpdater)
 26        {
 027            _servcies = servcies;
 028            _ordersDataServices = ordersDataServices;
 029            _cache = orderCache;
 030            _cacheUpdater = cacheUpdater;
 031        }
 32
 33        /// <summary>Справочник статусов клиентов — весь список: сортировка, фильтры и поиск в гриде AG Grid на клиенте.
 34        public async Task<IActionResult> Index()
 35        {
 036            var clientStatuses = (await _servcies.GetClientStatuses()).OrderBy(status => status.Name).ToList();
 037            return View(clientStatuses);
 038        }
 39
 40        public async Task<IActionResult> Create()
 41        {
 042            string? result = TempData["StatusResult"] as string;
 043            ViewData["StatusResult"] = result;
 44
 045            var clientStatusList = await _servcies.GetClientStatuses();
 046            ViewData["ClientStatusList"] = clientStatusList;
 47
 48            // Подготовка списка типов клиентов
 049            ViewBag.ClientTypes = new SelectList(new[]
 050            {
 051                new { Value = (int?)null, Text = "Не указан" },
 052                new { Value = (int?)ClientType.OZON, Text = "Ozon" },
 053                new { Value = (int?)ClientType.YANDEX, Text = "Yandex" }
 054            }, "Value", "Text");
 55
 056            return View();
 057        }
 58
 59        [HttpPost]
 60        public async Task<IActionResult> Create([Bind("Id,Name,ClientTypeId,IsFinal")] ClientStatus clientStatus)
 61        {
 062            if (ModelState.IsValid)
 63            {
 064                var existingStatus = await _servcies.GetClientStatusAsync(clientStatus);
 065                if (existingStatus != null)
 66                {
 067                    TempData["StatusResult"] = $"Не удалось добавить статус '{clientStatus.Name}'";
 068                    return RedirectToAction(nameof(Create));
 69                }
 70
 071                await _servcies.AddClientStatus(clientStatus);
 72
 073                return RedirectToAction("Index");
 74            }
 75
 76            // Подготовка списка типов клиентов для повторного отображения
 077            ViewBag.ClientTypes = new SelectList(new[]
 078            {
 079                new { Value = (int?)null, Text = "Не указан" },
 080                new { Value = (int?)ClientType.OZON, Text = "Ozon" },
 081                new { Value = (int?)ClientType.YANDEX, Text = "Yandex" }
 082            }, "Value", "Text", clientStatus.ClientTypeId);
 83
 084            return View(clientStatus);
 085        }
 86
 87        public async Task<IActionResult> Delete(int? id)
 88        {
 089            if (!id.HasValue)
 090                return RedirectToAction("Index");
 91
 092            var clientStatus = await _servcies.GetClientStatusAsync(id.Value);
 093            if (clientStatus != null)
 94            {
 095                var defaultStatus = await _servcies.GetClientStatusByNameAsync("Не указан");
 096                if (defaultStatus == null)
 97                {
 098                    defaultStatus = new ClientStatus { Name = "Не указан" };
 099                    await _servcies.AddClientStatus(defaultStatus);
 0100                    defaultStatus = await _servcies.GetClientStatusByNameAsync("Не указан");
 101                }
 102
 0103                var ordersWithDeletedStatus = (await _ordersDataServices.GetOrders())
 0104                    .Where(o => o.ClientStatusId == id)
 0105                    .ToList();
 106
 0107                foreach (var order in ordersWithDeletedStatus)
 108                {
 0109                    order.ClientStatus = defaultStatus;
 110                }
 111
 0112                await _servcies.DeleteClientStatus(clientStatus);
 0113                await _cacheUpdater.Update(_cache);
 0114            }
 115
 0116            return RedirectToAction("Index");
 0117        }
 118
 119        public async Task<IActionResult> Edit(int id)
 120        {
 0121            var clientStatus = await _servcies.GetClientStatusAsync(id);
 0122            if (clientStatus == null)
 123            {
 0124                return NotFound();
 125            }
 126
 127            // Подготовка списка типов клиентов
 0128            ViewBag.ClientTypes = new SelectList(new[]
 0129            {
 0130                new { Value = (int?)null, Text = "Не указан" },
 0131                new { Value = (int?)ClientType.OZON, Text = "Ozon" },
 0132                new { Value = (int?)ClientType.YANDEX, Text = "Yandex" }
 0133            }, "Value", "Text", clientStatus.ClientTypeId);
 134
 0135            return View(clientStatus);
 0136        }
 137
 138        [HttpPost]
 139        [ValidateAntiForgeryToken]
 140        public async Task<IActionResult> Edit(int id, ClientStatus clientStatus)
 141        {
 0142            if (id != clientStatus.Id)
 143            {
 0144                return NotFound();
 145            }
 146
 0147            if (ModelState.IsValid)
 148            {
 149                try
 150                {
 0151                    await _servcies.UpdateClientStatus(clientStatus);
 0152                    await _cacheUpdater.Update(_cache);
 0153                }
 0154                catch (DbUpdateConcurrencyException)
 155                {
 0156                    if (await _servcies.GetClientStatusAsync(id) == null)
 157                    {
 0158                        return NotFound();
 159                    }
 160                    else
 161                    {
 0162                        throw;
 163                    }
 164                }
 165
 0166                return RedirectToAction(nameof(Index));
 167            }
 168
 169            // Подготовка списка типов клиентов для повторного отображения
 0170            ViewBag.ClientTypes = new SelectList(new[]
 0171            {
 0172                new { Value = (int?)null, Text = "Не указан" },
 0173                new { Value = (int?)ClientType.OZON, Text = "Ozon" },
 0174                new { Value = (int?)ClientType.YANDEX, Text = "Yandex" }
 0175            }, "Value", "Text", clientStatus.ClientTypeId);
 176
 0177            return View(clientStatus);
 0178        }
 179
 180        [HttpPost]
 181        public async Task<IActionResult> UpdateStatusColor(int statusId, string colorCode)
 182        {
 183            try
 184            {
 0185                await _servcies.UpdateStatusColor(statusId, colorCode);
 0186                await _cacheUpdater.Update(_cache);
 0187                return Json(new { success = true });
 188            }
 0189            catch (Exception ex)
 190            {
 0191                return Json(new { success = false, error = ex.Message });
 192            }
 0193        }
 194
 195        [HttpPost]
 196        public async Task<IActionResult> ResetStatusColor(int statusId)
 197        {
 198            try
 199            {
 0200                await _servcies.ResetStatusColor(statusId);
 0201                await _cacheUpdater.Update(_cache);
 0202                return Json(new { success = true });
 203            }
 0204            catch (Exception ex)
 205            {
 0206                return Json(new { success = false, error = ex.Message });
 207            }
 0208        }
 209    }
 210}