roadie/Roadie.Api/Controllers/UserController.cs

396 lines
16 KiB
C#
Raw Normal View History

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.Models.Pagination;
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.Linq;
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
{
2019-01-12 00:27:49 +00:00
private IHttpContext RoadieHttpContext { get; }
2022-01-18 22:52:02 +00:00
2019-05-29 22:25:40 +00:00
private IUserService UserService { get; }
private readonly ITokenService TokenService;
public UserController(
IUserService userService,
ILogger<UserController> logger,
ICacheManager cacheManager,
ITokenService tokenService,
UserManager<Library.Identity.User> userManager,
IHttpContext httpContext,
IRoadieSettings roadieSettings)
: base(cacheManager, roadieSettings, userManager)
{
Logger = logger;
2019-07-03 16:21:29 +00:00
UserService = userService;
TokenService = tokenService;
RoadieHttpContext = httpContext;
}
[HttpPost("deleteAllBookmarks")]
[ProducesResponseType(200)]
public async Task<IActionResult> DeleteAllBookmarks()
{
var result = await UserService.DeleteAllBookmarksAsync(await CurrentUserModel().ConfigureAwait(false)).ConfigureAwait(false);
if (!result.IsSuccess)
{
return StatusCode((int)HttpStatusCode.InternalServerError);
}
CacheManager.ClearRegion(ControllerCacheRegionUrn);
return Ok(result);
}
[HttpGet("{id}")]
2019-08-02 20:59:24 +00:00
[ProducesResponseType(200)]
[ProducesResponseType(204)]
[ProducesResponseType(404)]
public async Task<IActionResult> Get(Guid id, string inc = null)
2019-08-02 20:59:24 +00:00
{
var user = await CurrentUserModel().ConfigureAwait(false);
2020-06-21 20:39:14 +00:00
var result = await CacheManager.GetAsync($"urn:user_model_by_id:{id}", async () =>
{
return await UserService.ByIdAsync(user, id, (inc ?? Library.Models.Users.User.DefaultIncludes)
.ToLower()
.Split(","))
.ConfigureAwait(false);
},
ControllerCacheRegionUrn).ConfigureAwait(false);
if (result?.IsNotFoundResult != false)
2019-08-02 20:59:24 +00:00
{
return NotFound();
}
if (!result.IsSuccess)
{
return StatusCode((int)HttpStatusCode.InternalServerError);
}
return Ok(result);
}
[HttpGet("accountsettings/{id}")]
2018-12-16 23:37:19 +00:00
[ProducesResponseType(200)]
[ProducesResponseType(204)]
[ProducesResponseType(404)]
public async Task<IActionResult> GetAccountSettings(Guid id, string inc = null)
2018-12-16 23:37:19 +00:00
{
var user = await CurrentUserModel().ConfigureAwait(false);
var result = await CacheManager.GetAsync($"urn:user_edit_model_by_id:{id}", async () => await UserService.ByIdAsync(user, id, (inc ?? Library.Models.Users.User.DefaultIncludes).ToLower().Split(","), true).ConfigureAwait(false), ControllerCacheRegionUrn).ConfigureAwait(false);
if (result?.IsNotFoundResult != false)
{
2019-08-02 20:59:24 +00:00
return NotFound();
}
if (!result.IsSuccess)
{
return StatusCode((int)HttpStatusCode.InternalServerError);
}
result.AdditionalClientData = new Dictionary<string, object>();
if (RoadieSettings.Integrations.LastFmProviderEnabled)
{
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)}");
}
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.UpdateIntegrationGrantAsync(userId, iname, token).ConfigureAwait(false);
if (!result.IsSuccess)
{
return Content("Error while attempting to enable integration");
}
2019-07-03 16:21:29 +00:00
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
{
var result = await UserService.ListAsync(request).ConfigureAwait(false);
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)
{
var result = await UserService.SetArtistBookmarkAsync(artistId, await CurrentUserModel().ConfigureAwait(false), isBookmarked).ConfigureAwait(false);
if (!result.IsSuccess)
{
return StatusCode((int)HttpStatusCode.InternalServerError);
}
2019-07-03 16:21:29 +00:00
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)
{
var result = await UserService.SetArtistDislikedAsync(artistId, await CurrentUserModel().ConfigureAwait(false), isDisliked).ConfigureAwait(false);
if (!result.IsSuccess)
{
return StatusCode((int)HttpStatusCode.InternalServerError);
}
2019-07-03 16:21:29 +00:00
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
{
var result = await UserService.SetArtistFavoriteAsync(artistId, await CurrentUserModel().ConfigureAwait(false), isFavorite).ConfigureAwait(false);
if (!result.IsSuccess)
{
return StatusCode((int)HttpStatusCode.InternalServerError);
}
2019-07-03 16:21:29 +00:00
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
{
var result = await UserService.SetArtistRatingAsync(releaseId, await CurrentUserModel().ConfigureAwait(false), rating).ConfigureAwait(false);
if (!result.IsSuccess)
{
return StatusCode((int)HttpStatusCode.InternalServerError);
}
2018-12-01 03:22:35 +00:00
CacheManager.ClearRegion(ControllerCacheRegionUrn);
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
{
var result = await UserService.SetCollectionBookmarkAsync(collectionId, await CurrentUserModel().ConfigureAwait(false), isBookmarked).ConfigureAwait(false);
if (!result.IsSuccess)
{
return StatusCode((int)HttpStatusCode.InternalServerError);
}
2019-07-03 16:21:29 +00:00
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
{
var result = await UserService.SetLabelBookmarkAsync(labelId, await CurrentUserModel().ConfigureAwait(false), isBookmarked).ConfigureAwait(false);
if (!result.IsSuccess)
{
return StatusCode((int)HttpStatusCode.InternalServerError);
}
2019-07-03 16:21:29 +00:00
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
{
var result = await UserService.SetPlaylistBookmarkAsync(playlistId, await CurrentUserModel().ConfigureAwait(false), isBookmarked).ConfigureAwait(false);
if (!result.IsSuccess)
{
return StatusCode((int)HttpStatusCode.InternalServerError);
}
2019-07-03 16:21:29 +00:00
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
{
var result = await UserService.SetReleaseBookmarkAsync(releaseId, await CurrentUserModel().ConfigureAwait(false), isBookmarked).ConfigureAwait(false);
if (!result.IsSuccess)
{
return StatusCode((int)HttpStatusCode.InternalServerError);
}
2019-07-03 16:21:29 +00:00
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)
{
var result = await UserService.SetReleaseDislikedAsync(releaseId, await CurrentUserModel().ConfigureAwait(false), isDisliked).ConfigureAwait(false);
if (!result.IsSuccess)
{
return StatusCode((int)HttpStatusCode.InternalServerError);
}
2019-07-03 16:21:29 +00:00
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
{
var result = await UserService.SetReleaseFavoriteAsync(releaseId, await CurrentUserModel().ConfigureAwait(false), isFavorite).ConfigureAwait(false);
if (!result.IsSuccess)
{
return StatusCode((int)HttpStatusCode.InternalServerError);
}
2019-07-03 16:21:29 +00:00
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
{
var result = await UserService.SetReleaseRatingAsync(releaseId, await CurrentUserModel().ConfigureAwait(false), rating).ConfigureAwait(false);
if (!result.IsSuccess)
{
return StatusCode((int)HttpStatusCode.InternalServerError);
}
2019-07-03 16:21:29 +00:00
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)
{
var result = await UserService.SetTrackBookmarkAsync(trackId, await CurrentUserModel().ConfigureAwait(false), isBookmarked).ConfigureAwait(false);
if (!result.IsSuccess)
{
return StatusCode((int)HttpStatusCode.InternalServerError);
}
2019-07-03 16:21:29 +00:00
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
{
var result = await UserService.SetTrackDislikedAsync(trackId, await CurrentUserModel().ConfigureAwait(false), isDisliked).ConfigureAwait(false);
if (!result.IsSuccess)
{
return StatusCode((int)HttpStatusCode.InternalServerError);
}
2019-07-03 16:21:29 +00:00
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
{
var result = await UserService.SetTrackFavoriteAsync(trackId, await CurrentUserModel().ConfigureAwait(false), isFavorite).ConfigureAwait(false);
if (!result.IsSuccess)
{
return StatusCode((int)HttpStatusCode.InternalServerError);
}
2019-07-03 16:21:29 +00:00
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
{
var result = await UserService.SetTrackRatingAsync(trackId, await CurrentUserModel().ConfigureAwait(false), rating).ConfigureAwait(false);
if (!result.IsSuccess)
{
return StatusCode((int)HttpStatusCode.InternalServerError);
}
2019-07-03 16:21:29 +00:00
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-11-28 17:38:26 +00:00
public async Task<IActionResult> UpdateProfile(Library.Models.Users.User model)
{
2019-08-02 20:59:24 +00:00
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
var user = await CurrentUserModel().ConfigureAwait(false);
var result = await UserService.UpdateProfileAsync(user, model).ConfigureAwait(false);
if (result?.IsNotFoundResult != false)
{
return NotFound();
}
if (!result.IsSuccess)
{
2019-11-28 17:38:26 +00:00
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);
}
2019-07-03 16:21:29 +00:00
CacheManager.ClearRegion(ControllerCacheRegionUrn);
var modelUser = await UserManager.FindByNameAsync(model.UserName).ConfigureAwait(false);
var t = await TokenService.GenerateTokenAsync(modelUser, UserManager).ConfigureAwait(false);
2019-07-03 16:21:29 +00:00
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,
2019-08-02 20:59:24 +00:00
modelUser.Timezone,
DefaultRowsPerPage = modelUser.DefaultRowsPerPage ?? RoadieSettings.DefaultRowsPerPage
2019-01-05 22:40:33 +00:00
});
}
}
2022-01-18 22:52:02 +00:00
}