| | 1 | | using Microsoft.AspNetCore.Mvc; |
| | 2 | | using MRA.Services.Models.Collections; |
| | 3 | | using MRA.Services; |
| | 4 | | using Microsoft.AspNetCore.Authorization; |
| | 5 | | using MRA.WebApi.Models.Responses; |
| | 6 | | using MRA.WebApi.Models.Requests; |
| | 7 | | using MRA.WebApi.Models.Responses.Errors; |
| | 8 | | using MRA.DTO.Exceptions.Collections; |
| | 9 | |
|
| | 10 | | namespace MRA.WebApi.Controllers.Art; |
| | 11 | |
|
| | 12 | | [ApiController] |
| | 13 | | [Route("api/art/collection")] |
| | 14 | | public class CollectionController : ControllerBase |
| | 15 | | { |
| | 16 | | private readonly IAppService _appService; |
| | 17 | | private readonly ICollectionService _collectionService; |
| | 18 | | private readonly ILogger<CollectionController> _logger; |
| | 19 | |
|
| 20 | 20 | | public CollectionController( |
| 20 | 21 | | ILogger<CollectionController> logger, |
| 20 | 22 | | IAppService appService, |
| 20 | 23 | | ICollectionService collectionService |
| 20 | 24 | | ) |
| | 25 | | { |
| 20 | 26 | | _logger = logger; |
| 20 | 27 | | _appService = appService; |
| 20 | 28 | | _collectionService = collectionService; |
| 20 | 29 | | } |
| | 30 | |
|
| | 31 | |
|
| | 32 | | [HttpGet("details/{id}")] |
| | 33 | | public async Task<ActionResult<CollectionResponse>> Details(string id) |
| | 34 | | { |
| 3 | 35 | | return await FetchCollectionDetails(id, true); |
| 3 | 36 | | } |
| | 37 | |
|
| | 38 | |
|
| | 39 | | [Authorize] |
| | 40 | | [HttpGet("full-details/{id}")] |
| | 41 | | public async Task<ActionResult<CollectionResponse>> FullDetails(string id) |
| | 42 | | { |
| 3 | 43 | | return await FetchCollectionDetails(id, false); |
| 3 | 44 | | } |
| | 45 | |
|
| | 46 | | private async Task<ActionResult<CollectionResponse>> FetchCollectionDetails(string id, bool onlyIfVisible) |
| | 47 | | { |
| | 48 | | try |
| | 49 | | { |
| 6 | 50 | | var collection = await _appService.FindCollectionByIdAsync(id, onlyIfVisible: onlyIfVisible, cache: true); |
| 2 | 51 | | return Ok(new CollectionResponse(collection)); |
| | 52 | | } |
| 2 | 53 | | catch (CollectionNotFoundException cnf) |
| | 54 | | { |
| 2 | 55 | | _logger.LogWarning(cnf, cnf.Message); |
| 2 | 56 | | return NotFound(new ErrorResponse(cnf.Message)); |
| | 57 | | } |
| 2 | 58 | | catch (Exception ex) |
| | 59 | | { |
| 2 | 60 | | _logger.LogError(ex, ErrorMessages.CollectionErrorMessages.FetchDetails.InternalServer(id)); |
| 2 | 61 | | return StatusCode(StatusCodes.Status500InternalServerError, |
| 2 | 62 | | new ErrorResponse(ErrorMessages.CollectionErrorMessages.FetchDetails.InternalServer(id))); |
| | 63 | | } |
| 6 | 64 | | } |
| | 65 | |
|
| | 66 | | [HttpGet("exist/{id}")] |
| | 67 | | public async Task<ActionResult<bool>> Exists(string id) |
| | 68 | | { |
| | 69 | | try |
| | 70 | | { |
| 3 | 71 | | var exists = await _collectionService.ExistsCollection(id); |
| 2 | 72 | | _logger.LogInformation("Existe colección '{Id}': {Exists}", id, (exists ? "Sí" : "No")); |
| 2 | 73 | | return Ok(exists); |
| | 74 | | } |
| 1 | 75 | | catch (Exception ex) |
| | 76 | | { |
| 1 | 77 | | _logger.LogError(ex, "Error al comprobar colección '{Id}'.", id); |
| 1 | 78 | | return StatusCode(StatusCodes.Status500InternalServerError, |
| 1 | 79 | | new ErrorResponse($"Error when checking collection \"{id}\"")); |
| | 80 | | } |
| 3 | 81 | | } |
| | 82 | |
|
| | 83 | | [HttpGet("list")] |
| | 84 | | public async Task<ActionResult<IEnumerable<CollectionResponse>>> List() |
| | 85 | | { |
| 3 | 86 | | return await FetchCollectionList(true); |
| 3 | 87 | | } |
| | 88 | |
|
| | 89 | | [HttpGet("full-list")] |
| | 90 | | [Authorize] |
| | 91 | | public async Task<ActionResult<IEnumerable<CollectionResponse>>> FullList() |
| | 92 | | { |
| 2 | 93 | | return await FetchCollectionList(false); |
| 2 | 94 | | } |
| | 95 | |
|
| | 96 | | private async Task<ActionResult<IEnumerable<CollectionResponse>>> FetchCollectionList(bool onlyIfVisible) |
| | 97 | | { |
| | 98 | | try |
| | 99 | | { |
| 5 | 100 | | _logger.LogInformation("Fetching collections, only visible: {OnlyIfVisible}", (onlyIfVisible)); |
| 5 | 101 | | var collections = await _appService.GetAllCollectionsAsync(onlyIfVisible: onlyIfVisible, cache: true); |
| | 102 | |
|
| 10 | 103 | | if (onlyIfVisible && collections.Any(c => c.Drawings.Any(d => !d.Visible))) |
| | 104 | | { |
| 1 | 105 | | throw new VisibleCollectionRetrievedException(); |
| | 106 | | } |
| | 107 | |
|
| 2 | 108 | | _logger.LogInformation("{Count} collections found" , collections.Count()); |
| 6 | 109 | | return Ok(collections.Select(c => new CollectionResponse(c)).ToList()); |
| | 110 | | } |
| 1 | 111 | | catch (VisibleCollectionRetrievedException vcr) |
| | 112 | | { |
| 1 | 113 | | _logger.LogError(vcr, vcr.Message); |
| 1 | 114 | | return StatusCode(StatusCodes.Status503ServiceUnavailable, |
| 1 | 115 | | new ErrorResponse(vcr.Message)); |
| | 116 | | } |
| 2 | 117 | | catch (Exception ex) |
| | 118 | | { |
| 2 | 119 | | _logger.LogError(ex, ErrorMessages.CollectionErrorMessages.FetchList.InternalServer); |
| 2 | 120 | | return StatusCode(StatusCodes.Status500InternalServerError, |
| 2 | 121 | | new ErrorResponse(ErrorMessages.CollectionErrorMessages.FetchList.InternalServer)); |
| | 122 | | } |
| 5 | 123 | | } |
| | 124 | |
|
| | 125 | | [Authorize] |
| | 126 | | [HttpPost("save/{id}")] |
| | 127 | | public async Task<ActionResult<CollectionResponse>> Save(string id, [FromBody] SaveCollectionRequest model) |
| | 128 | | { |
| | 129 | | try |
| | 130 | | { |
| 3 | 131 | | _logger.LogInformation("Saving collection '{Id}'", model.Id); |
| 3 | 132 | | var collection = model.GetModel(); |
| 3 | 133 | | if (String.IsNullOrEmpty(collection.Id)) |
| | 134 | | { |
| 1 | 135 | | _logger.LogWarning(ErrorMessages.CollectionErrorMessages.Save.IdNotProvided); |
| 1 | 136 | | return BadRequest(new ErrorResponse(ErrorMessages.CollectionErrorMessages.Save.IdNotProvided)); |
| | 137 | | } |
| | 138 | |
|
| 2 | 139 | | await _collectionService.SaveCollectionAsync(id, collection); |
| 1 | 140 | | _logger.LogInformation("Saved collection '{Id}'", model.Id); |
| 1 | 141 | | _appService.Clear(); |
| 1 | 142 | | return Ok(new CollectionResponse(collection)); |
| | 143 | | } |
| 1 | 144 | | catch (Exception ex) |
| | 145 | | { |
| 1 | 146 | | _logger.LogError(ex, ErrorMessages.CollectionErrorMessages.Save.InternalServer(model.Id)); |
| 1 | 147 | | return StatusCode(StatusCodes.Status500InternalServerError, |
| 1 | 148 | | new ErrorResponse(ErrorMessages.CollectionErrorMessages.Save.InternalServer(model.Id))); |
| | 149 | | } |
| 3 | 150 | | } |
| | 151 | |
|
| | 152 | | [Authorize] |
| | 153 | | [HttpPost("delete")] |
| | 154 | | public async Task<ActionResult<bool>> Delete([FromBody] string id) |
| | 155 | | { |
| | 156 | | try |
| | 157 | | { |
| 3 | 158 | | await _collectionService.DeleteCollection(id); |
| 1 | 159 | | _logger.LogInformation("Colección '{Id}' eliminada con éxito", id); |
| 1 | 160 | | _appService.Clear(); |
| 1 | 161 | | return Ok(true); |
| | 162 | | } |
| 1 | 163 | | catch (CollectionNotFoundException cnf) |
| | 164 | | { |
| 1 | 165 | | _logger.LogWarning(cnf, cnf.Message); |
| 1 | 166 | | return NotFound(new ErrorResponse(cnf.Message)); |
| | 167 | | } |
| 1 | 168 | | catch (Exception ex) |
| | 169 | | { |
| 1 | 170 | | _logger.LogError(ex, ErrorMessages.CollectionErrorMessages.Delete.InternalServer(id)); |
| 1 | 171 | | return StatusCode(StatusCodes.Status500InternalServerError, |
| 1 | 172 | | new ErrorResponse(ErrorMessages.CollectionErrorMessages.Delete.InternalServer(id))); |
| | 173 | | } |
| 3 | 174 | | } |
| | 175 | | } |