roadie/Roadie.Api/Controllers/CollectionController.cs

157 lines
No EOL
5.4 KiB
C#

using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Roadie.Api.Services;
using Roadie.Library.Caching;
using Roadie.Library.Configuration;
using Roadie.Library.Identity;
using Roadie.Library.Models.Collections;
using Roadie.Library.Models.Pagination;
using System;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
namespace Roadie.Api.Controllers
{
[Produces("application/json")]
[Route("collections")]
[ApiController]
[Authorize]
public class CollectionController : EntityControllerBase
{
private ICollectionService CollectionService { get; }
public CollectionController(ICollectionService collectionService, ILogger<CollectionController> logger,
ICacheManager cacheManager,
UserManager<ApplicationUser> userManager, IRoadieSettings roadieSettings)
: base(cacheManager, roadieSettings, userManager)
{
Logger = logger;
CollectionService = collectionService;
}
[HttpGet("add")]
[ProducesResponseType(200)]
[ProducesResponseType(404)]
public async Task<IActionResult> Add()
{
var result = CollectionService.Add(await CurrentUserModel());
if (!result.IsSuccess)
{
if (result.IsAccessDeniedResult)
{
return StatusCode((int)HttpStatusCode.Forbidden);
}
if (result.Messages?.Any() ?? false)
{
return StatusCode((int)HttpStatusCode.BadRequest, result.Messages);
}
return StatusCode((int)HttpStatusCode.InternalServerError);
}
return Ok(result);
}
[HttpPost("delete/{id}")]
[ProducesResponseType(200)]
[ProducesResponseType(404)]
[Authorize(Policy = "Editor")]
public async Task<IActionResult> DeleteCollection(Guid id)
{
var result = await CollectionService.DeleteCollection(await CurrentUserModel(), id);
if (result == null || result.IsNotFoundResult)
{
return NotFound();
}
if (!result.IsSuccess)
{
if (result.IsAccessDeniedResult)
{
return StatusCode((int)HttpStatusCode.Forbidden);
}
if (result.Messages?.Any() ?? false)
{
return StatusCode((int)HttpStatusCode.BadRequest, result.Messages);
}
return StatusCode((int)HttpStatusCode.InternalServerError);
}
return Ok(result);
}
[HttpGet("{id}")]
[ProducesResponseType(200)]
[ProducesResponseType(404)]
public async Task<IActionResult> Get(Guid id, string inc = null)
{
var result = await CollectionService.ById(await CurrentUserModel(), id,
(inc ?? Collection.DefaultIncludes).ToLower().Split(","));
if (result == null || result.IsNotFoundResult) return NotFound();
if (!result.IsSuccess)
{
if (result.IsAccessDeniedResult)
{
return StatusCode((int)HttpStatusCode.Forbidden);
}
if (result.Messages?.Any() ?? false)
{
return StatusCode((int)HttpStatusCode.BadRequest, result.Messages);
}
return StatusCode((int)HttpStatusCode.InternalServerError);
}
return Ok(result);
}
[HttpGet]
[ProducesResponseType(200)]
public async Task<IActionResult> List([FromQuery] PagedRequest request)
{
try
{
var result = await CollectionService.List(await CurrentUserModel(), request);
if (!result.IsSuccess) return StatusCode((int)HttpStatusCode.InternalServerError);
return Ok(result);
}
catch (UnauthorizedAccessException)
{
return StatusCode((int)HttpStatusCode.Unauthorized);
}
catch (Exception ex)
{
Logger.LogError(ex);
}
return StatusCode((int)HttpStatusCode.InternalServerError);
}
[HttpPost("edit")]
[ProducesResponseType(200)]
[ProducesResponseType(404)]
[Authorize(Policy = "Editor")]
public async Task<IActionResult> Update(Collection collection)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
var result = await CollectionService.UpdateCollection(await CurrentUserModel(), collection);
if (result == null || result.IsNotFoundResult)
{
return NotFound();
}
if (!result.IsSuccess)
{
if (result.IsAccessDeniedResult)
{
return StatusCode((int)HttpStatusCode.Forbidden);
}
if (result.Messages?.Any() ?? false)
{
return StatusCode((int)HttpStatusCode.BadRequest, result.Messages);
}
return StatusCode((int)HttpStatusCode.InternalServerError);
}
return Ok(result);
}
}
}