roadie/Roadie.Api/Controllers/PlaylistController.cs

100 lines
3.5 KiB
C#
Raw Normal View History

using Microsoft.AspNetCore.Authorization;
2018-11-15 00:16:25 +00:00
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Roadie.Api.Services;
using Roadie.Library.Caching;
using Roadie.Library.Identity;
2018-12-30 20:57:06 +00:00
using Roadie.Library.Models;
2018-12-08 03:10:21 +00:00
using Roadie.Library.Models.Pagination;
2018-12-30 20:57:06 +00:00
using Roadie.Library.Models.Playlists;
2018-12-08 03:10:21 +00:00
using System;
2018-12-30 20:57:06 +00:00
using System.Collections.Generic;
2018-12-08 03:10:21 +00:00
using System.Net;
using System.Threading.Tasks;
using models = Roadie.Library.Models;
2018-11-15 00:16:25 +00:00
namespace Roadie.Api.Controllers
{
[Produces("application/json")]
[Route("playlists")]
2018-11-15 00:16:25 +00:00
[ApiController]
[Authorize]
public class PlaylistController : EntityControllerBase
{
private IPlaylistService PlaylistService { get; }
public PlaylistController(IPlaylistService playlistService, ILoggerFactory logger, ICacheManager cacheManager, IConfiguration configuration, UserManager<ApplicationUser> userManager)
: base(cacheManager, configuration, userManager)
{
2018-11-21 06:34:53 +00:00
this.Logger = logger.CreateLogger("RoadieApi.Controllers.PlaylistController");
2018-11-15 00:16:25 +00:00
this.PlaylistService = playlistService;
}
2018-12-09 17:58:31 +00:00
[HttpGet("{id}")]
[ProducesResponseType(200)]
[ProducesResponseType(404)]
public async Task<IActionResult> Get(Guid id, string inc = null)
{
var result = await this.PlaylistService.ById(await this.CurrentUserModel(), id, (inc ?? models.Playlists.Playlist.DefaultIncludes).ToLower().Split(","));
if (result == null || result.IsNotFoundResult)
{
return NotFound();
}
if (!result.IsSuccess)
{
return StatusCode((int)HttpStatusCode.InternalServerError);
}
return Ok(result);
}
2018-12-08 03:10:21 +00:00
[HttpGet]
[ProducesResponseType(200)]
public async Task<IActionResult> List([FromQuery]PagedRequest request, string inc)
{
var result = await this.PlaylistService.List(roadieUser: await this.CurrentUserModel(),
request: request);
if (!result.IsSuccess)
{
return StatusCode((int)HttpStatusCode.InternalServerError);
}
return Ok(result);
}
2018-12-30 20:57:06 +00:00
[HttpPost("add")]
[ProducesResponseType(200)]
[ProducesResponseType(404)]
public async Task<IActionResult> AddNewPlaylist([FromBody]Playlist model)
{
var result = await this.PlaylistService.AddNewPlaylist(await this.CurrentUserModel(), model);
if (!result.IsSuccess)
{
return StatusCode((int)HttpStatusCode.InternalServerError);
}
return Ok(result);
}
2018-12-31 05:16:42 +00:00
[HttpPost("delete/{id}")]
[ProducesResponseType(200)]
[ProducesResponseType(404)]
public async Task<IActionResult> DeletePlaylist(Guid id)
{
var result = await this.PlaylistService.DeletePlaylist(await this.CurrentUserModel(), id);
if (result == null || result.IsNotFoundResult)
{
return NotFound();
}
if(result != null && result.IsAccessDeniedResult)
{
return StatusCode((int)HttpStatusCode.Forbidden);
}
if (!result.IsSuccess)
{
return StatusCode((int)HttpStatusCode.InternalServerError);
}
return Ok(result);
}
2018-11-15 00:16:25 +00:00
}
}