< Summary

Information
Class: MRA.WebApi.Controllers.Art.CollectionController
Assembly: MRA.WebApi
File(s): D:\a\MiguelRomerART\MiguelRomerART\MRA.WebApi\Controllers\Art\CollectionController.cs
Line coverage
100%
Covered lines: 76
Uncovered lines: 0
Coverable lines: 76
Total lines: 175
Line coverage: 100%
Branch coverage
100%
Covered branches: 4
Total branches: 4
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
Details()100%11100%
FullDetails()100%11100%
FetchCollectionDetails()100%11100%
Exists()100%22100%
List()100%11100%
FullList()100%11100%
FetchCollectionList()100%44100%
Save()100%22100%
Delete()100%11100%

File(s)

D:\a\MiguelRomerART\MiguelRomerART\MRA.WebApi\Controllers\Art\CollectionController.cs

#LineLine coverage
 1using Microsoft.AspNetCore.Mvc;
 2using MRA.Services.Models.Collections;
 3using MRA.Services;
 4using Microsoft.AspNetCore.Authorization;
 5using MRA.WebApi.Models.Responses;
 6using MRA.WebApi.Models.Requests;
 7using MRA.WebApi.Models.Responses.Errors;
 8using MRA.DTO.Exceptions.Collections;
 9
 10namespace MRA.WebApi.Controllers.Art;
 11
 12[ApiController]
 13[Route("api/art/collection")]
 14public class CollectionController : ControllerBase
 15{
 16    private readonly IAppService _appService;
 17    private readonly ICollectionService _collectionService;
 18    private readonly ILogger<CollectionController> _logger;
 19
 2020    public CollectionController(
 2021        ILogger<CollectionController> logger,
 2022        IAppService appService,
 2023        ICollectionService collectionService
 2024        )
 25    {
 2026        _logger = logger;
 2027        _appService = appService;
 2028        _collectionService = collectionService;
 2029    }
 30
 31
 32    [HttpGet("details/{id}")]
 33    public async Task<ActionResult<CollectionResponse>> Details(string id)
 34    {
 335        return await FetchCollectionDetails(id, true);
 336    }
 37
 38
 39    [Authorize]
 40    [HttpGet("full-details/{id}")]
 41    public async Task<ActionResult<CollectionResponse>> FullDetails(string id)
 42    {
 343        return await FetchCollectionDetails(id, false);
 344    }
 45
 46    private async Task<ActionResult<CollectionResponse>> FetchCollectionDetails(string id, bool onlyIfVisible)
 47    {
 48        try
 49        {
 650            var collection = await _appService.FindCollectionByIdAsync(id, onlyIfVisible: onlyIfVisible, cache: true);
 251            return Ok(new CollectionResponse(collection));
 52        }
 253        catch (CollectionNotFoundException cnf)
 54        {
 255            _logger.LogWarning(cnf, cnf.Message);
 256            return NotFound(new ErrorResponse(cnf.Message));
 57        }
 258        catch (Exception ex)
 59        {
 260            _logger.LogError(ex, ErrorMessages.CollectionErrorMessages.FetchDetails.InternalServer(id));
 261            return StatusCode(StatusCodes.Status500InternalServerError,
 262                new ErrorResponse(ErrorMessages.CollectionErrorMessages.FetchDetails.InternalServer(id)));
 63        }
 664    }
 65
 66    [HttpGet("exist/{id}")]
 67    public async Task<ActionResult<bool>> Exists(string id)
 68    {
 69        try
 70        {
 371            var exists = await _collectionService.ExistsCollection(id);
 272            _logger.LogInformation("Existe colección '{Id}': {Exists}", id, (exists ? "Sí" : "No"));
 273            return Ok(exists);
 74        }
 175        catch (Exception ex)
 76        {
 177            _logger.LogError(ex, "Error al comprobar colección '{Id}'.", id);
 178            return StatusCode(StatusCodes.Status500InternalServerError,
 179                new ErrorResponse($"Error when checking collection \"{id}\""));
 80        }
 381    }
 82
 83    [HttpGet("list")]
 84    public async Task<ActionResult<IEnumerable<CollectionResponse>>> List()
 85    {
 386        return await FetchCollectionList(true);
 387    }
 88
 89    [HttpGet("full-list")]
 90    [Authorize]
 91    public async Task<ActionResult<IEnumerable<CollectionResponse>>> FullList()
 92    {
 293        return await FetchCollectionList(false);
 294    }
 95
 96    private async Task<ActionResult<IEnumerable<CollectionResponse>>> FetchCollectionList(bool onlyIfVisible)
 97    {
 98        try
 99        {
 5100            _logger.LogInformation("Fetching collections, only visible: {OnlyIfVisible}", (onlyIfVisible));
 5101            var collections = await _appService.GetAllCollectionsAsync(onlyIfVisible: onlyIfVisible, cache: true);
 102
 10103            if (onlyIfVisible && collections.Any(c => c.Drawings.Any(d => !d.Visible)))
 104            {
 1105                throw new VisibleCollectionRetrievedException();
 106            }
 107
 2108            _logger.LogInformation("{Count} collections found" , collections.Count());
 6109            return Ok(collections.Select(c => new CollectionResponse(c)).ToList());
 110        }
 1111        catch (VisibleCollectionRetrievedException vcr)
 112        {
 1113            _logger.LogError(vcr, vcr.Message);
 1114            return StatusCode(StatusCodes.Status503ServiceUnavailable,
 1115                new ErrorResponse(vcr.Message));
 116        }
 2117        catch (Exception ex)
 118        {
 2119            _logger.LogError(ex, ErrorMessages.CollectionErrorMessages.FetchList.InternalServer);
 2120            return StatusCode(StatusCodes.Status500InternalServerError,
 2121                new ErrorResponse(ErrorMessages.CollectionErrorMessages.FetchList.InternalServer));
 122        }
 5123    }
 124
 125    [Authorize]
 126    [HttpPost("save/{id}")]
 127    public async Task<ActionResult<CollectionResponse>> Save(string id, [FromBody] SaveCollectionRequest model)
 128    {
 129        try
 130        {
 3131            _logger.LogInformation("Saving collection '{Id}'", model.Id);
 3132            var collection = model.GetModel();
 3133            if (String.IsNullOrEmpty(collection.Id))
 134            {
 1135                _logger.LogWarning(ErrorMessages.CollectionErrorMessages.Save.IdNotProvided);
 1136                return BadRequest(new ErrorResponse(ErrorMessages.CollectionErrorMessages.Save.IdNotProvided));
 137            }
 138
 2139            await _collectionService.SaveCollectionAsync(id, collection);
 1140            _logger.LogInformation("Saved collection '{Id}'", model.Id);
 1141            _appService.Clear();
 1142            return Ok(new CollectionResponse(collection));
 143        }
 1144        catch (Exception ex)
 145        {
 1146            _logger.LogError(ex, ErrorMessages.CollectionErrorMessages.Save.InternalServer(model.Id));
 1147            return StatusCode(StatusCodes.Status500InternalServerError,
 1148                new ErrorResponse(ErrorMessages.CollectionErrorMessages.Save.InternalServer(model.Id)));
 149        }
 3150    }
 151
 152    [Authorize]
 153    [HttpPost("delete")]
 154    public async Task<ActionResult<bool>> Delete([FromBody] string id)
 155    {
 156        try
 157        {
 3158            await _collectionService.DeleteCollection(id);
 1159            _logger.LogInformation("Colección '{Id}' eliminada con éxito", id);
 1160            _appService.Clear();
 1161            return Ok(true);
 162        }
 1163        catch (CollectionNotFoundException cnf)
 164        {
 1165            _logger.LogWarning(cnf, cnf.Message);
 1166            return NotFound(new ErrorResponse(cnf.Message));
 167        }
 1168        catch (Exception ex)
 169        {
 1170            _logger.LogError(ex, ErrorMessages.CollectionErrorMessages.Delete.InternalServer(id));
 1171            return StatusCode(StatusCodes.Status500InternalServerError,
 1172                new ErrorResponse(ErrorMessages.CollectionErrorMessages.Delete.InternalServer(id)));
 173        }
 3174    }
 175}