roadie/Roadie.Api/Controllers/UserController.cs

274 lines
13 KiB
C#
Raw Normal View History

using Microsoft.AspNetCore.Authorization;
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.Configuration;
using Roadie.Library.Identity;
using Roadie.Library.Models.Pagination;
2018-12-16 23:37:19 +00:00
using Roadie.Library.Models.Users;
2019-01-12 00:27:49 +00:00
using Roadie.Library.Utility;
2018-12-01 03:22:35 +00:00
using System;
2019-07-03 16:21:29 +00:00
using System.Collections.Generic;
using System.Net;
using System.Threading.Tasks;
namespace Roadie.Api.Controllers
{
[Produces("application/json")]
[Route("users")]
[ApiController]
2018-12-01 03:22:35 +00:00
[Authorize]
public class UserController : EntityControllerBase
{
2018-12-16 23:37:19 +00:00
private readonly ITokenService TokenService;
2019-07-03 16:21:29 +00:00
2019-01-12 00:27:49 +00:00
private IHttpContext RoadieHttpContext { get; }
2019-07-03 16:21:29 +00:00
2019-05-29 22:25:40 +00:00
private IUserService UserService { get; }
2019-07-03 16:21:29 +00:00
public UserController(IUserService userService, ILoggerFactory logger, ICacheManager cacheManager,
IConfiguration configuration, ITokenService tokenService, UserManager<ApplicationUser> userManager,
IHttpContext httpContext, IRoadieSettings roadieSettings)
: base(cacheManager, roadieSettings, userManager)
{
2019-07-03 16:21:29 +00:00
Logger = logger.CreateLogger("RoadieApi.Controllers.UserController");
UserService = userService;
TokenService = tokenService;
RoadieHttpContext = httpContext;
}
2018-12-16 23:37:19 +00:00
[HttpGet("{id}")]
[ProducesResponseType(200)]
[ProducesResponseType(204)]
[ProducesResponseType(404)]
2019-01-10 23:40:04 +00:00
public async Task<IActionResult> Get(Guid id, string inc = null)
2018-12-16 23:37:19 +00:00
{
2019-07-03 16:21:29 +00:00
var user = await CurrentUserModel();
var result = await CacheManager.GetAsync($"urn:user_model_by_id:{id}",
async () =>
{
return await UserService.ById(user, id,
(inc ?? Library.Models.Users.User.DefaultIncludes).ToLower().Split(","));
}, ControllerCacheRegionUrn);
if (result == null || result.IsNotFoundResult) return NotFound();
if (!result.IsSuccess) return StatusCode((int)HttpStatusCode.InternalServerError);
result.AdditionalClientData = new Dictionary<string, object>();
if (RoadieSettings.Integrations.LastFmProviderEnabled)
{
2019-07-03 16:21:29 +00:00
var lastFmCallBackUrl =
$"{RoadieHttpContext.BaseUrl}/users/integration/grant?userId={user.UserId}&iname=lastfm";
result.AdditionalClientData.Add("lastFMIntegrationUrl",
$"http://www.last.fm/api/auth/?api_key={RoadieSettings.Integrations.LastFMApiKey}&cb={WebUtility.UrlEncode(lastFmCallBackUrl)}");
}
2019-07-03 16:21:29 +00:00
2018-12-16 23:37:19 +00:00
return Ok(result);
}
2019-07-03 16:21:29 +00:00
[AllowAnonymous]
[HttpGet("integration/grant")]
[ProducesResponseType(200)]
public async Task<IActionResult> IntegrationGrant(Guid userId, string iname, string token)
{
var result = await UserService.UpdateIntegrationGrant(userId, iname, token);
if (!result.IsSuccess) return Content("Error while attempting to enable integration");
return Content("Successfully enabled integration!");
}
2019-01-05 22:40:33 +00:00
[HttpGet]
2018-12-16 23:37:19 +00:00
[ProducesResponseType(200)]
2019-07-03 16:21:29 +00:00
public async Task<IActionResult> List([FromQuery] PagedRequest request)
2018-12-16 23:37:19 +00:00
{
2019-07-03 16:21:29 +00:00
var result = await UserService.List(request);
if (!result.IsSuccess) return StatusCode((int)HttpStatusCode.InternalServerError);
2019-01-05 22:40:33 +00:00
return Ok(result);
}
[HttpPost("setArtistBookmark/{artistId}/{isBookmarked}")]
[ProducesResponseType(200)]
public async Task<IActionResult> SetArtistBookmark(Guid artistId, bool isBookmarked)
{
2019-07-03 16:21:29 +00:00
var result = await UserService.SetArtistBookmark(artistId, await CurrentUserModel(), isBookmarked);
if (!result.IsSuccess) return StatusCode((int)HttpStatusCode.InternalServerError);
CacheManager.ClearRegion(ControllerCacheRegionUrn);
2019-01-05 22:40:33 +00:00
return Ok(result);
}
[HttpPost("setArtistDisliked/{artistId}/{isDisliked}")]
[ProducesResponseType(200)]
public async Task<IActionResult> SetArtistDisliked(Guid artistId, bool isDisliked)
{
2019-07-03 16:21:29 +00:00
var result = await UserService.SetArtistDisliked(artistId, await CurrentUserModel(), isDisliked);
if (!result.IsSuccess) return StatusCode((int)HttpStatusCode.InternalServerError);
CacheManager.ClearRegion(ControllerCacheRegionUrn);
2019-01-05 22:40:33 +00:00
return Ok(result);
2018-12-16 23:37:19 +00:00
}
2019-01-05 22:40:33 +00:00
[HttpPost("setArtistFavorite/{artistId}/{isFavorite}")]
2018-12-01 03:22:35 +00:00
[ProducesResponseType(200)]
2019-01-05 22:40:33 +00:00
public async Task<IActionResult> SetArtistFavorite(Guid artistId, bool isFavorite)
2018-12-01 03:22:35 +00:00
{
2019-07-03 16:21:29 +00:00
var result = await UserService.SetArtistFavorite(artistId, await CurrentUserModel(), isFavorite);
if (!result.IsSuccess) return StatusCode((int)HttpStatusCode.InternalServerError);
CacheManager.ClearRegion(ControllerCacheRegionUrn);
2018-12-01 03:22:35 +00:00
return Ok(result);
}
2019-01-05 22:40:33 +00:00
[HttpPost("setArtistRating/{releaseId}/{rating}")]
2018-12-01 03:22:35 +00:00
[ProducesResponseType(200)]
2019-01-05 22:40:33 +00:00
public async Task<IActionResult> SetArtistRating(Guid releaseId, short rating)
2018-12-01 03:22:35 +00:00
{
2019-07-03 16:21:29 +00:00
var result = await UserService.SetArtistRating(releaseId, await CurrentUserModel(), rating);
if (!result.IsSuccess) return StatusCode((int)HttpStatusCode.InternalServerError);
CacheManager.ClearRegion(ControllerCacheRegionUrn);
2018-12-01 03:22:35 +00:00
return Ok(result);
}
2019-01-05 22:40:33 +00:00
[HttpPost("setCollectionBookmark/{collectionId}/{isBookmarked}")]
2018-12-01 03:22:35 +00:00
[ProducesResponseType(200)]
2019-01-05 22:40:33 +00:00
public async Task<IActionResult> SetCollectionBookmark(Guid collectionId, bool isBookmarked)
2018-12-01 03:22:35 +00:00
{
2019-07-03 16:21:29 +00:00
var result = await UserService.SetCollectionBookmark(collectionId, await CurrentUserModel(), isBookmarked);
if (!result.IsSuccess) return StatusCode((int)HttpStatusCode.InternalServerError);
CacheManager.ClearRegion(ControllerCacheRegionUrn);
2018-12-01 03:22:35 +00:00
return Ok(result);
}
2019-01-05 22:40:33 +00:00
[HttpPost("setLabelBookmark/{labelId}/{isBookmarked}")]
2018-12-01 18:05:24 +00:00
[ProducesResponseType(200)]
2019-01-05 22:40:33 +00:00
public async Task<IActionResult> SetLabelBookmark(Guid labelId, bool isBookmarked)
2018-12-01 18:05:24 +00:00
{
2019-07-03 16:21:29 +00:00
var result = await UserService.SetLabelBookmark(labelId, await CurrentUserModel(), isBookmarked);
if (!result.IsSuccess) return StatusCode((int)HttpStatusCode.InternalServerError);
CacheManager.ClearRegion(ControllerCacheRegionUrn);
2018-12-01 18:05:24 +00:00
return Ok(result);
}
2019-01-05 22:40:33 +00:00
[HttpPost("setPlaylistBookmark/{playlistId}/{isBookmarked}")]
2018-12-24 22:35:59 +00:00
[ProducesResponseType(200)]
2019-01-05 22:40:33 +00:00
public async Task<IActionResult> SetPlaylistBookmark(Guid playlistId, bool isBookmarked)
2018-12-24 22:35:59 +00:00
{
2019-07-03 16:21:29 +00:00
var result = await UserService.SetPlaylistBookmark(playlistId, await CurrentUserModel(), isBookmarked);
if (!result.IsSuccess) return StatusCode((int)HttpStatusCode.InternalServerError);
CacheManager.ClearRegion(ControllerCacheRegionUrn);
2018-12-24 22:35:59 +00:00
return Ok(result);
}
2019-01-05 22:40:33 +00:00
[HttpPost("setReleaseBookmark/{releaseId}/{isBookmarked}")]
2018-12-01 18:05:24 +00:00
[ProducesResponseType(200)]
2019-01-05 22:40:33 +00:00
public async Task<IActionResult> SetReleaseBookmark(Guid releaseId, bool isBookmarked)
2018-12-01 18:05:24 +00:00
{
2019-07-03 16:21:29 +00:00
var result = await UserService.SetReleaseBookmark(releaseId, await CurrentUserModel(), isBookmarked);
if (!result.IsSuccess) return StatusCode((int)HttpStatusCode.InternalServerError);
CacheManager.ClearRegion(ControllerCacheRegionUrn);
2018-12-01 18:05:24 +00:00
return Ok(result);
}
2018-12-01 03:22:35 +00:00
2018-12-24 22:35:59 +00:00
[HttpPost("setReleaseDisliked/{releaseId}/{isDisliked}")]
[ProducesResponseType(200)]
public async Task<IActionResult> SetReleaseDisliked(Guid releaseId, bool isDisliked)
{
2019-07-03 16:21:29 +00:00
var result = await UserService.SetReleaseDisliked(releaseId, await CurrentUserModel(), isDisliked);
if (!result.IsSuccess) return StatusCode((int)HttpStatusCode.InternalServerError);
CacheManager.ClearRegion(ControllerCacheRegionUrn);
2018-12-24 22:35:59 +00:00
return Ok(result);
}
2019-01-05 22:40:33 +00:00
[HttpPost("setReleaseFavorite/{releaseId}/{isFavorite}")]
2018-12-11 23:09:52 +00:00
[ProducesResponseType(200)]
2019-01-05 22:40:33 +00:00
public async Task<IActionResult> SetReleaseFavorite(Guid releaseId, bool isFavorite)
2018-12-11 23:09:52 +00:00
{
2019-07-03 16:21:29 +00:00
var result = await UserService.SetReleaseFavorite(releaseId, await CurrentUserModel(), isFavorite);
if (!result.IsSuccess) return StatusCode((int)HttpStatusCode.InternalServerError);
CacheManager.ClearRegion(ControllerCacheRegionUrn);
2018-12-11 23:09:52 +00:00
return Ok(result);
}
2019-01-05 22:40:33 +00:00
[HttpPost("setReleaseRating/{releaseId}/{rating}")]
2018-12-12 14:31:39 +00:00
[ProducesResponseType(200)]
2019-01-05 22:40:33 +00:00
public async Task<IActionResult> SetReleaseRating(Guid releaseId, short rating)
2018-12-12 14:31:39 +00:00
{
2019-07-03 16:21:29 +00:00
var result = await UserService.SetReleaseRating(releaseId, await CurrentUserModel(), rating);
if (!result.IsSuccess) return StatusCode((int)HttpStatusCode.InternalServerError);
CacheManager.ClearRegion(ControllerCacheRegionUrn);
2018-12-12 14:31:39 +00:00
return Ok(result);
}
[HttpPost("setTrackBookmark/{trackId}/{isBookmarked}")]
[ProducesResponseType(200)]
public async Task<IActionResult> SetTrackBookmark(Guid trackId, bool isBookmarked)
{
2019-07-03 16:21:29 +00:00
var result = await UserService.SetTrackBookmark(trackId, await CurrentUserModel(), isBookmarked);
if (!result.IsSuccess) return StatusCode((int)HttpStatusCode.InternalServerError);
CacheManager.ClearRegion(ControllerCacheRegionUrn);
2018-12-12 14:31:39 +00:00
return Ok(result);
}
2019-01-05 22:40:33 +00:00
[HttpPost("setTrackDisliked/{trackId}/{isDisliked}")]
2018-12-12 14:31:39 +00:00
[ProducesResponseType(200)]
2019-01-05 22:40:33 +00:00
public async Task<IActionResult> SetTrackDisliked(Guid trackId, bool isDisliked)
2018-12-12 14:31:39 +00:00
{
2019-07-03 16:21:29 +00:00
var result = await UserService.SetTrackDisliked(trackId, await CurrentUserModel(), isDisliked);
if (!result.IsSuccess) return StatusCode((int)HttpStatusCode.InternalServerError);
CacheManager.ClearRegion(ControllerCacheRegionUrn);
2018-12-12 14:31:39 +00:00
return Ok(result);
}
2019-01-05 22:40:33 +00:00
[HttpPost("setTrackFavorite/{trackId}/{isFavorite}")]
2018-12-12 14:31:39 +00:00
[ProducesResponseType(200)]
2019-01-05 22:40:33 +00:00
public async Task<IActionResult> SetTrackFavorite(Guid trackId, bool isFavorite)
2018-12-12 14:31:39 +00:00
{
2019-07-03 16:21:29 +00:00
var result = await UserService.SetTrackFavorite(trackId, await CurrentUserModel(), isFavorite);
if (!result.IsSuccess) return StatusCode((int)HttpStatusCode.InternalServerError);
CacheManager.ClearRegion(ControllerCacheRegionUrn);
2018-12-12 14:31:39 +00:00
return Ok(result);
}
2019-01-05 22:40:33 +00:00
[HttpPost("setTrackRating/{trackId}/{rating}")]
2018-12-12 14:31:39 +00:00
[ProducesResponseType(200)]
2019-01-05 22:40:33 +00:00
public async Task<IActionResult> SetTrackRating(Guid trackId, short rating)
2018-12-12 14:31:39 +00:00
{
2019-07-03 16:21:29 +00:00
var result = await UserService.SetTrackRating(trackId, await CurrentUserModel(), rating);
if (!result.IsSuccess) return StatusCode((int)HttpStatusCode.InternalServerError);
CacheManager.ClearRegion(ControllerCacheRegionUrn);
2018-12-12 14:31:39 +00:00
return Ok(result);
}
2018-12-01 03:22:35 +00:00
2019-01-05 22:40:33 +00:00
[HttpPost("profile/edit")]
[ProducesResponseType(200)]
2019-01-05 22:40:33 +00:00
public async Task<IActionResult> UpdateProfile(User model)
{
2019-07-03 16:21:29 +00:00
if (!ModelState.IsValid) return BadRequest(ModelState);
var user = await CurrentUserModel();
var result = await UserService.UpdateProfile(user, model);
if (result == null || result.IsNotFoundResult) return NotFound();
if (!result.IsSuccess) return StatusCode((int)HttpStatusCode.InternalServerError);
CacheManager.ClearRegion(ControllerCacheRegionUrn);
2019-01-05 22:40:33 +00:00
var modelUser = await UserManager.FindByNameAsync(model.UserName);
2019-07-03 16:21:29 +00:00
var t = await TokenService.GenerateToken(modelUser, UserManager);
CacheManager.ClearRegion(ControllerCacheRegionUrn);
var avatarUrl =
$"{RoadieHttpContext.ImageBaseUrl}/user/{modelUser.RoadieId}/{RoadieSettings.ThumbnailImageSize.Width}/{RoadieSettings.ThumbnailImageSize.Height}";
2019-01-05 22:40:33 +00:00
return Ok(new
{
IsSuccess = true,
Username = modelUser.UserName,
2019-01-27 05:05:43 +00:00
modelUser.RemoveTrackFromQueAfterPlayed,
2019-01-05 22:40:33 +00:00
modelUser.Email,
modelUser.LastLogin,
avatarUrl,
Token = t,
modelUser.Timeformat,
modelUser.Timezone
});
}
public class PagingParams
{
public int Limit { get; set; } = 5;
2019-01-05 22:40:33 +00:00
public int Page { get; set; } = 1;
}
}
}