< Summary

Information
Class: OzonOrdersWeb.Areas.Studio2.Controllers.Admin.AccessProfilesController
Assembly: OzonOrdersWeb
File(s): /home/runner/work/Studio2Prod/Studio2Prod/OzonOrdersWeb/Areas/Studio2/Controllers/Admin/AccessProfilesController.cs
Line coverage
18%
Covered lines: 12
Uncovered lines: 53
Coverable lines: 65
Total lines: 145
Line coverage: 18.4%
Branch coverage
12%
Covered branches: 4
Total branches: 32
Branch coverage: 12.5%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
Index()100%210%
Details()100%44100%
Create()100%210%
Create()0%620%
Edit()0%4260%
Edit()0%210140%
Delete()0%2040%
DeleteConfirmed()0%620%

File(s)

/home/runner/work/Studio2Prod/Studio2Prod/OzonOrdersWeb/Areas/Studio2/Controllers/Admin/AccessProfilesController.cs

#LineLine coverage
 1using Microsoft.AspNetCore.Authorization;
 2using Microsoft.AspNetCore.Mvc;
 3using Microsoft.EntityFrameworkCore;
 4using OzonDomains.Models.AccessControl;
 5using OzonRepositories.Context;
 6
 7namespace OzonOrdersWeb.Areas.Studio2.Controllers.Admin;
 8
 9[Authorize(Roles = "Admin")]
 10[Area("Studio2")]
 11public class AccessProfilesController : Controller
 12{
 13    private readonly ApplicationDbContext _context;
 14
 315    public AccessProfilesController(ApplicationDbContext context)
 16    {
 317        _context = context;
 318    }
 19
 20    // GET: AccessProfiles
 21    public async Task<IActionResult> Index()
 22    {
 023        var profiles = await _context.AccessProfiles
 024            .Include(ap => ap.DataScopeSets)
 025            .ToListAsync();
 026        return View(profiles);
 027    }
 28
 29    // GET: AccessProfiles/Details/5
 30    public async Task<IActionResult> Details(int? id)
 31    {
 432        if (id == null) return NotFound();
 33
 234        var profile = await _context.AccessProfiles
 235            .Include(ap => ap.DataScopeSets)
 236                .ThenInclude(s => s.Items)
 237            .AsSplitQuery()
 238            .FirstOrDefaultAsync(ap => ap.Id == id);
 39
 340        if (profile == null) return NotFound();
 41
 142        return View(profile);
 343    }
 44
 45    // GET: AccessProfiles/Create
 46    public IActionResult Create()
 47    {
 048        return View(new AccessProfile());
 49    }
 50
 51    // POST: AccessProfiles/Create
 52    [HttpPost]
 53    [ValidateAntiForgeryToken]
 54    public async Task<IActionResult> Create([Bind("Name,AvailableOrderColumns")] AccessProfile profile)
 55    {
 056        if (ModelState.IsValid)
 57        {
 058            _context.AccessProfiles.Add(profile);
 059            await _context.SaveChangesAsync();
 060            return RedirectToAction(nameof(Index));
 61        }
 062        return View(profile);
 063    }
 64
 65    // GET: AccessProfiles/Edit/5
 66    public async Task<IActionResult> Edit(int? id)
 67    {
 068        if (id == null) return NotFound();
 69
 070        var profile = await _context.AccessProfiles
 071            .Include(ap => ap.DataScopeSets)
 072            .FirstOrDefaultAsync(ap => ap.Id == id);
 73
 074        if (profile == null) return NotFound();
 75
 076        ViewBag.AllDataScopeSets = await _context.DataScopeSets.ToListAsync();
 077        return View(profile);
 078    }
 79
 80    // POST: AccessProfiles/Edit/5
 81    [HttpPost]
 82    [ValidateAntiForgeryToken]
 83    public async Task<IActionResult> Edit(int id, [Bind("Id,Name,AvailableOrderColumns")] AccessProfile profile,
 84        List<int>? selectedDataScopeSetIds)
 85    {
 086        if (id != profile.Id) return NotFound();
 87
 088        if (ModelState.IsValid)
 89        {
 090            var existing = await _context.AccessProfiles
 091                .Include(ap => ap.DataScopeSets)
 092                .FirstOrDefaultAsync(ap => ap.Id == id);
 93
 094            if (existing == null) return NotFound();
 95
 096            existing.Name = profile.Name;
 097            existing.AvailableOrderColumns = profile.AvailableOrderColumns;
 98
 99            // Update M:N relationship with DataScopeSets
 0100            existing.DataScopeSets.Clear();
 0101            if (selectedDataScopeSetIds?.Any() == true)
 102            {
 0103                var sets = await _context.DataScopeSets
 0104                    .Where(s => selectedDataScopeSetIds.Contains(s.Id))
 0105                    .ToListAsync();
 0106                foreach (var set in sets)
 0107                    existing.DataScopeSets.Add(set);
 108            }
 109
 0110            await _context.SaveChangesAsync();
 0111            return RedirectToAction(nameof(Index));
 112        }
 113
 0114        ViewBag.AllDataScopeSets = await _context.DataScopeSets.ToListAsync();
 0115        return View(profile);
 0116    }
 117
 118    // GET: AccessProfiles/Delete/5
 119    public async Task<IActionResult> Delete(int? id)
 120    {
 0121        if (id == null) return NotFound();
 122
 0123        var profile = await _context.AccessProfiles
 0124            .Include(ap => ap.DataScopeSets)
 0125            .FirstOrDefaultAsync(ap => ap.Id == id);
 126
 0127        if (profile == null) return NotFound();
 128
 0129        return View(profile);
 0130    }
 131
 132    // POST: AccessProfiles/Delete/5
 133    [HttpPost, ActionName("Delete")]
 134    [ValidateAntiForgeryToken]
 135    public async Task<IActionResult> DeleteConfirmed(int id)
 136    {
 0137        var profile = await _context.AccessProfiles.FindAsync(id);
 0138        if (profile != null)
 139        {
 0140            _context.AccessProfiles.Remove(profile);
 0141            await _context.SaveChangesAsync();
 142        }
 0143        return RedirectToAction(nameof(Index));
 0144    }
 145}