Better error handling and unique key constraint checks on updates.

This commit is contained in:
Steven Hildreth 2019-08-03 10:23:46 -05:00
parent 7710fef6b0
commit 872350763a
18 changed files with 658 additions and 119 deletions

View file

@ -619,7 +619,18 @@ namespace Roadie.Api.Services
.Include("Genres.Genre")
.FirstOrDefault(x => x.RoadieId == model.Id);
if (artist == null)
{
return new OperationResult<bool>(true, $"Artist Not Found [{model.Id}]");
}
// If artist is being renamed, see if artist already exists with new model supplied name
if(artist.Name.ToAlphanumericName() != model.Name.ToAlphanumericName())
{
var existingArtist = DbContext.Artists.FirstOrDefault(x => x.Name == model.Name);
if(existingArtist != null)
{
return new OperationResult<bool>($"Artist already exists with name [{ model.Name }].");
}
}
try
{
var now = DateTime.UtcNow;
@ -653,12 +664,17 @@ namespace Roadie.Api.Services
artist.URLs = model.URLsList.ToDelimitedList();
var newArtistFolder = artist.ArtistFileFolder(Configuration);
// Rename artist folder to reflect new artist name
if (!newArtistFolder.Equals(originalArtistFolder, StringComparison.OrdinalIgnoreCase))
{
// If folder already exists for new artist name that means another artist has that folder (usually sort name)
if (Directory.Exists(newArtistFolder))
{
return new OperationResult<bool>($"Artist Folder [{ newArtistFolder }] already exists.");
}
didRenameArtist = true;
if (Directory.Exists(originalArtistFolder))
{
// Rename artist folder to reflect new artist name
Logger.LogInformation("Moving Artist From Folder [{0}] -> [{1}]", originalArtistFolder, newArtistFolder);
Directory.Move(originalArtistFolder, newArtistFolder);
}

View file

@ -132,7 +132,9 @@ namespace Roadie.Api.Services
if (!user.IsEditor)
{
Logger.LogWarning($"DeleteCollection: Access Denied: `{collection}`, By User `{user}`");
return new OperationResult<bool>("Access Denied");
var r = new OperationResult<bool>("Access Denied");
r.IsAccessDeniedResult = true;
return r;
}
try
@ -237,8 +239,16 @@ namespace Roadie.Api.Services
{
return new OperationResult<bool>(true, string.Format("Collection Not Found [{0}]", model.Id));
}
// If collection is being renamed, see if collection already exists with new model supplied name
if (collection.Name.ToAlphanumericName() != model.Name.ToAlphanumericName())
{
var existingCollection = DbContext.Collections.FirstOrDefault(x => x.Name == model.Name);
if (existingCollection != null)
{
return new OperationResult<bool>($"Collection already exists with name [{ model.Name }].");
}
}
}
collection.IsLocked = model.IsLocked;
var oldPathToImage = collection.PathToImage(Configuration);
var didChangeName = collection.Name != model.Name;

View file

@ -254,7 +254,19 @@ namespace Roadie.Api.Services
sw.Start();
var errors = new List<Exception>();
var label = DbContext.Labels.FirstOrDefault(x => x.RoadieId == model.Id);
if (label == null) return new OperationResult<bool>(true, string.Format("Label Not Found [{0}]", model.Id));
if (label == null)
{
return new OperationResult<bool>(true, string.Format("Label Not Found [{0}]", model.Id));
}
// If label is being renamed, see if label already exists with new model supplied name
if (label.Name.ToAlphanumericName() != model.Name.ToAlphanumericName())
{
var existingLabel = DbContext.Labels.FirstOrDefault(x => x.Name == model.Name);
if (existingLabel != null)
{
return new OperationResult<bool>($"Label already exists with name [{ model.Name }].");
}
}
try
{
var now = DateTime.UtcNow;

View file

@ -166,7 +166,10 @@ namespace Roadie.Api.Services
var sw = new Stopwatch();
sw.Start();
var playlist = DbContext.Playlists.FirstOrDefault(x => x.RoadieId == id);
if (playlist == null) return new OperationResult<bool>(true, string.Format("Playlist Not Found [{0}]", id));
if (playlist == null)
{
return new OperationResult<bool>(true, string.Format("Playlist Not Found [{0}]", id));
}
if (!user.IsAdmin && user.Id != playlist.UserId)
{
Logger.LogWarning("User `{0}` attempted to delete Playlist `{1}`", user, playlist);
@ -288,7 +291,17 @@ namespace Roadie.Api.Services
var errors = new List<Exception>();
var playlist = DbContext.Playlists.FirstOrDefault(x => x.RoadieId == model.Id);
if (playlist == null)
return new OperationResult<bool>(true, string.Format("Label Not Found [{0}]", model.Id));
{
return new OperationResult<bool>(true, string.Format("Playlist Not Found [{0}]", model.Id));
}
if (!user.IsAdmin && user.Id != playlist.UserId)
{
Logger.LogWarning("User `{0}` attempted to update Playlist `{1}`", user, playlist);
return new OperationResult<bool>("Access Denied")
{
IsAccessDeniedResult = true
};
}
try
{
var now = DateTime.UtcNow;
@ -349,7 +362,17 @@ namespace Roadie.Api.Services
var errors = new List<Exception>();
var playlist = DbContext.Playlists.Include(x => x.Tracks).FirstOrDefault(x => x.RoadieId == request.Id);
if (playlist == null)
{
return new OperationResult<bool>(true, string.Format("Label Not Found [{0}]", request.Id));
}
if (!user.IsAdmin && user.Id != playlist.UserId)
{
Logger.LogWarning("User `{0}` attempted to update Playlist Tracks `{1}`", user, playlist);
return new OperationResult<bool>("Access Denied")
{
IsAccessDeniedResult = true
};
}
try
{
var now = DateTime.UtcNow;

View file

@ -1357,6 +1357,15 @@ namespace Roadie.Api.Services
{
return new OperationResult<bool>(true, string.Format("Release Not Found [{0}]", model.Id));
}
// If release is being renamed, see if release already exists for artist with new model supplied name
if (release.Title.ToAlphanumericName() != model.Title.ToAlphanumericName())
{
var existingRelease = DbContext.Releases.FirstOrDefault(x => x.Title == model.Title && x.ArtistId == release.ArtistId);
if (existingRelease != null)
{
return new OperationResult<bool>($"Release already exists for Artist with title [{ model.Title }].");
}
}
try
{
var now = DateTime.UtcNow;

View file

@ -58,7 +58,9 @@ namespace Roadie.Api.Services
{
if(user.UserId != id && !user.IsAdmin)
{
return new OperationResult<User>(new Exception("Access Denied"));
var r = new OperationResult<User>("Access Denied");
r.IsAccessDeniedResult = true;
return r;
}
}
var sw = Stopwatch.StartNew();
@ -371,18 +373,16 @@ namespace Roadie.Api.Services
}
if (user.Id != userPerformingUpdate.Id && !userPerformingUpdate.IsAdmin)
{
return new OperationResult<bool>
var r = new OperationResult<bool>("Access Denied")
{
Errors = new List<Exception> { new Exception("Access Denied") }
IsAccessDeniedResult = true
};
return r;
}
// Check concurrency stamp
if (user.ConcurrencyStamp != userBeingUpdatedModel.ConcurrencyStamp)
{
return new OperationResult<bool>
{
Errors = new List<Exception> { new Exception("User data is stale.") }
};
return new OperationResult<bool>("User data is stale.");
}
// Check that username (if changed) doesn't already exist
if (user.UserName != userBeingUpdatedModel.UserName)
@ -390,10 +390,7 @@ namespace Roadie.Api.Services
var userByUsername = DbContext.Users.FirstOrDefault(x => x.NormalizedUserName == userBeingUpdatedModel.UserName.ToUpper());
if (userByUsername != null)
{
return new OperationResult<bool>
{
Errors = new List<Exception> { new Exception("Username already in use") }
};
return new OperationResult<bool>("Username already in use");
}
}
@ -403,10 +400,7 @@ namespace Roadie.Api.Services
var userByEmail = DbContext.Users.FirstOrDefault(x => x.NormalizedEmail == userBeingUpdatedModel.Email.ToUpper());
if (userByEmail != null)
{
return new OperationResult<bool>
{
Errors = new List<Exception> { new Exception("Email already in use") }
};
return new OperationResult<bool>("Email already in use");
}
}
var oldPathToImage = user.PathToImage(Configuration);

View file

@ -8,6 +8,7 @@ using Roadie.Library.Configuration;
using Roadie.Library.Identity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
@ -42,7 +43,14 @@ namespace Roadie.Api.Controllers
public async Task<IActionResult> DeleteArtist(Guid id)
{
var result = await AdminService.DeleteArtist(await UserManager.GetUserAsync(User), id);
if (!result.IsSuccess) return StatusCode((int)HttpStatusCode.InternalServerError);
if (!result.IsSuccess)
{
if (result.Messages?.Any() ?? false)
{
return StatusCode((int)HttpStatusCode.BadRequest, result.Messages);
}
return StatusCode((int)HttpStatusCode.InternalServerError);
}
return Ok(result);
}
@ -51,7 +59,14 @@ namespace Roadie.Api.Controllers
public async Task<IActionResult> DeleteArtistReleases(Guid id)
{
var result = await AdminService.DeleteArtistReleases(await UserManager.GetUserAsync(User), id);
if (!result.IsSuccess) return StatusCode((int)HttpStatusCode.InternalServerError);
if (!result.IsSuccess)
{
if (result.Messages?.Any() ?? false)
{
return StatusCode((int)HttpStatusCode.BadRequest, result.Messages);
}
return StatusCode((int)HttpStatusCode.InternalServerError);
}
return Ok(result);
}
@ -60,7 +75,14 @@ namespace Roadie.Api.Controllers
public async Task<IActionResult> DeleteArtistSecondaryImage(Guid id, int index)
{
var result = await AdminService.DeleteArtistSecondaryImage(await UserManager.GetUserAsync(User), id, index);
if (!result.IsSuccess) return StatusCode((int)HttpStatusCode.InternalServerError);
if (!result.IsSuccess)
{
if (result.Messages?.Any() ?? false)
{
return StatusCode((int)HttpStatusCode.BadRequest, result.Messages);
}
return StatusCode((int)HttpStatusCode.InternalServerError);
}
return Ok(result);
}
@ -69,7 +91,14 @@ namespace Roadie.Api.Controllers
public async Task<IActionResult> DeleteRelease(Guid id, bool? doDeleteFiles)
{
var result = await AdminService.DeleteRelease(await UserManager.GetUserAsync(User), id, doDeleteFiles);
if (!result.IsSuccess) return StatusCode((int)HttpStatusCode.InternalServerError);
if (!result.IsSuccess)
{
if (result.Messages?.Any() ?? false)
{
return StatusCode((int)HttpStatusCode.BadRequest, result.Messages);
}
return StatusCode((int)HttpStatusCode.InternalServerError);
}
return Ok(result);
}
@ -78,7 +107,14 @@ namespace Roadie.Api.Controllers
public async Task<IActionResult> DeleteLabel(Guid id)
{
var result = await AdminService.DeleteLabel(await UserManager.GetUserAsync(User), id);
if (!result.IsSuccess) return StatusCode((int)HttpStatusCode.InternalServerError);
if (!result.IsSuccess)
{
if (result.Messages?.Any() ?? false)
{
return StatusCode((int)HttpStatusCode.BadRequest, result.Messages);
}
return StatusCode((int)HttpStatusCode.InternalServerError);
}
return Ok(result);
}
@ -87,7 +123,14 @@ namespace Roadie.Api.Controllers
public async Task<IActionResult> DeleteGenre(Guid id)
{
var result = await AdminService.DeleteGenre(await UserManager.GetUserAsync(User), id);
if (!result.IsSuccess) return StatusCode((int)HttpStatusCode.InternalServerError);
if (!result.IsSuccess)
{
if (result.Messages?.Any() ?? false)
{
return StatusCode((int)HttpStatusCode.BadRequest, result.Messages);
}
return StatusCode((int)HttpStatusCode.InternalServerError);
}
return Ok(result);
}
@ -106,7 +149,14 @@ namespace Roadie.Api.Controllers
public async Task<IActionResult> DeleteTracks([FromBody]IEnumerable<Guid> ids, bool? doDeleteFile)
{
var result = await AdminService.DeleteTracks(await UserManager.GetUserAsync(User), ids, doDeleteFile);
if (!result.IsSuccess) return StatusCode((int)HttpStatusCode.InternalServerError);
if (!result.IsSuccess)
{
if (result.Messages?.Any() ?? false)
{
return StatusCode((int)HttpStatusCode.BadRequest, result.Messages);
}
return StatusCode((int)HttpStatusCode.InternalServerError);
}
return Ok(result);
}
@ -116,7 +166,14 @@ namespace Roadie.Api.Controllers
public async Task<IActionResult> DeleteTrack(Guid id, bool? doDeleteFile)
{
var result = await AdminService.DeleteTracks(await UserManager.GetUserAsync(User), new Guid[1] { id }, doDeleteFile);
if (!result.IsSuccess) return StatusCode((int)HttpStatusCode.InternalServerError);
if (!result.IsSuccess)
{
if (result.Messages?.Any() ?? false)
{
return StatusCode((int)HttpStatusCode.BadRequest, result.Messages);
}
return StatusCode((int)HttpStatusCode.InternalServerError);
}
return Ok(result);
}
@ -125,7 +182,14 @@ namespace Roadie.Api.Controllers
public async Task<IActionResult> DeleteUser(Guid id)
{
var result = await AdminService.DeleteUser(await UserManager.GetUserAsync(User), id);
if (!result.IsSuccess) return StatusCode((int)HttpStatusCode.InternalServerError);
if (!result.IsSuccess)
{
if (result.Messages?.Any() ?? false)
{
return StatusCode((int)HttpStatusCode.BadRequest, result.Messages);
}
return StatusCode((int)HttpStatusCode.InternalServerError);
}
return Ok(result);
}
@ -134,7 +198,14 @@ namespace Roadie.Api.Controllers
public async Task<IActionResult> MissingCollectionReleases()
{
var result = await AdminService.MissingCollectionReleases(await UserManager.GetUserAsync(User));
if (!result.IsSuccess) return StatusCode((int)HttpStatusCode.InternalServerError);
if (!result.IsSuccess)
{
if (result.Messages?.Any() ?? false)
{
return StatusCode((int)HttpStatusCode.BadRequest, result.Messages);
}
return StatusCode((int)HttpStatusCode.InternalServerError);
}
return Ok(result);
}
@ -143,7 +214,14 @@ namespace Roadie.Api.Controllers
public async Task<IActionResult> ScanAllCollections()
{
var result = await AdminService.ScanAllCollections(await UserManager.GetUserAsync(User));
if (!result.IsSuccess) return StatusCode((int)HttpStatusCode.InternalServerError);
if (!result.IsSuccess)
{
if (result.Messages?.Any() ?? false)
{
return StatusCode((int)HttpStatusCode.BadRequest, result.Messages);
}
return StatusCode((int)HttpStatusCode.InternalServerError);
}
return Ok(result);
}
@ -152,7 +230,14 @@ namespace Roadie.Api.Controllers
public async Task<IActionResult> ScanArtist(Guid id)
{
var result = await AdminService.ScanArtist(await UserManager.GetUserAsync(User), id);
if (!result.IsSuccess) return StatusCode((int)HttpStatusCode.InternalServerError);
if (!result.IsSuccess)
{
if (result.Messages?.Any() ?? false)
{
return StatusCode((int)HttpStatusCode.BadRequest, result.Messages);
}
return StatusCode((int)HttpStatusCode.InternalServerError);
}
return Ok(result);
}
@ -161,7 +246,14 @@ namespace Roadie.Api.Controllers
public async Task<IActionResult> ScanArtists(IEnumerable<Guid> ids)
{
var result = await AdminService.ScanArtists(await UserManager.GetUserAsync(User), ids);
if (!result.IsSuccess) return StatusCode((int)HttpStatusCode.InternalServerError);
if (!result.IsSuccess)
{
if (result.Messages?.Any() ?? false)
{
return StatusCode((int)HttpStatusCode.BadRequest, result.Messages);
}
return StatusCode((int)HttpStatusCode.InternalServerError);
}
return Ok(result);
}
@ -171,7 +263,14 @@ namespace Roadie.Api.Controllers
public async Task<IActionResult> ScanCollection(Guid id)
{
var result = await AdminService.ScanCollection(await UserManager.GetUserAsync(User), id);
if (!result.IsSuccess) return StatusCode((int)HttpStatusCode.InternalServerError);
if (!result.IsSuccess)
{
if (result.Messages?.Any() ?? false)
{
return StatusCode((int)HttpStatusCode.BadRequest, result.Messages);
}
return StatusCode((int)HttpStatusCode.InternalServerError);
}
return Ok(result);
}
@ -180,7 +279,14 @@ namespace Roadie.Api.Controllers
public async Task<IActionResult> ScanInbound()
{
var result = await AdminService.ScanInboundFolder(await UserManager.GetUserAsync(User));
if (!result.IsSuccess) return StatusCode((int)HttpStatusCode.InternalServerError);
if (!result.IsSuccess)
{
if (result.Messages?.Any() ?? false)
{
return StatusCode((int)HttpStatusCode.BadRequest, result.Messages);
}
return StatusCode((int)HttpStatusCode.InternalServerError);
}
return Ok(result);
}
@ -189,7 +295,14 @@ namespace Roadie.Api.Controllers
public async Task<IActionResult> ScanLibrary()
{
var result = await AdminService.ScanLibraryFolder(await UserManager.GetUserAsync(User));
if (!result.IsSuccess) return StatusCode((int)HttpStatusCode.InternalServerError);
if (!result.IsSuccess)
{
if (result.Messages?.Any() ?? false)
{
return StatusCode((int)HttpStatusCode.BadRequest, result.Messages);
}
return StatusCode((int)HttpStatusCode.InternalServerError);
}
return Ok(result);
}
@ -198,7 +311,14 @@ namespace Roadie.Api.Controllers
public async Task<IActionResult> ScanRelease(Guid id)
{
var result = await AdminService.ScanRelease(await UserManager.GetUserAsync(User), id);
if (!result.IsSuccess) return StatusCode((int)HttpStatusCode.InternalServerError);
if (!result.IsSuccess)
{
if (result.Messages?.Any() ?? false)
{
return StatusCode((int)HttpStatusCode.BadRequest, result.Messages);
}
return StatusCode((int)HttpStatusCode.InternalServerError);
}
return Ok(result);
}
}

View file

@ -9,6 +9,7 @@ using Roadie.Library.Configuration;
using Roadie.Library.Identity;
using Roadie.Library.Models.Pagination;
using System;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using System.Web;
@ -80,7 +81,18 @@ namespace Roadie.Api.Controllers
var result =
await ArtistService.MergeArtists(await UserManager.GetUserAsync(User), artistToMergeId, artistToMergeIntoId);
if (result == null || result.IsNotFoundResult) return NotFound();
if (!result.IsSuccess) return StatusCode((int)HttpStatusCode.InternalServerError);
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);
}
@ -93,7 +105,18 @@ namespace Roadie.Api.Controllers
var result =
await ArtistService.SetReleaseImageByUrl(await UserManager.GetUserAsync(User), id, HttpUtility.UrlDecode(imageUrl));
if (result == null || result.IsNotFoundResult) return NotFound();
if (!result.IsSuccess) return StatusCode((int)HttpStatusCode.InternalServerError);
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);
}
@ -103,10 +126,27 @@ namespace Roadie.Api.Controllers
[Authorize(Policy = "Editor")]
public async Task<IActionResult> Update(models.Artist artist)
{
if (!ModelState.IsValid) return BadRequest(ModelState);
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
var result = await ArtistService.UpdateArtist(await UserManager.GetUserAsync(User), artist);
if (result == null || result.IsNotFoundResult) return NotFound();
if (!result.IsSuccess) return StatusCode((int)HttpStatusCode.InternalServerError);
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);
}
@ -118,7 +158,18 @@ namespace Roadie.Api.Controllers
{
var result = await ArtistService.UploadArtistImage(await UserManager.GetUserAsync(User), id, file);
if (result == null || result.IsNotFoundResult) return NotFound();
if (!result.IsSuccess) return StatusCode((int)HttpStatusCode.InternalServerError);
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);
}
}

View file

@ -29,34 +29,6 @@ namespace Roadie.Api.Controllers
BookmarkService = bookmarkService;
}
//[EnableQuery]
//public IActionResult Get()
//{
// return Ok(this._RoadieDbContext.Labels.ProjectToType<models.Label>());
//}
//[HttpGet("{id}")]
//[ProducesResponseType(200)]
//[ProducesResponseType(404)]
//public IActionResult Get(Guid id)
//{
// var key = id.ToString();
// var result = this._cacheManager.Get<models.Label>(key, () =>
// {
// var d = this._RoadieDbContext.Labels.FirstOrDefault(x => x.RoadieId == id);
// if (d != null)
// {
// return d.Adapt<models.Label>();
// }
// return null;
// }, key);
// if (result == null)
// {
// return NotFound();
// }
// return Ok(result);
//}
[HttpGet]
[ProducesResponseType(200)]
public async Task<IActionResult> List([FromQuery] PagedRequest request)

View file

@ -9,6 +9,7 @@ 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;
@ -37,7 +38,18 @@ namespace Roadie.Api.Controllers
public async Task<IActionResult> Add()
{
var result = CollectionService.Add(await CurrentUserModel());
if (!result.IsSuccess) return StatusCode((int)HttpStatusCode.InternalServerError);
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);
}
@ -48,7 +60,22 @@ namespace Roadie.Api.Controllers
public async Task<IActionResult> DeleteCollection(Guid id)
{
var result = await CollectionService.DeleteCollection(await CurrentUserModel(), id);
if (!result.IsSuccess) return StatusCode((int)HttpStatusCode.InternalServerError);
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);
}
@ -60,7 +87,18 @@ namespace Roadie.Api.Controllers
var result = await CollectionService.ById(await CurrentUserModel(), id,
(inc ?? Collection.DefaultIncludes).ToLower().Split(","));
if (result == null || result.IsNotFoundResult) return NotFound();
if (!result.IsSuccess) return StatusCode((int)HttpStatusCode.InternalServerError);
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);
}
@ -70,8 +108,7 @@ namespace Roadie.Api.Controllers
{
try
{
var result = await CollectionService.List(await CurrentUserModel(),
request);
var result = await CollectionService.List(await CurrentUserModel(), request);
if (!result.IsSuccess) return StatusCode((int)HttpStatusCode.InternalServerError);
return Ok(result);
}
@ -93,10 +130,27 @@ namespace Roadie.Api.Controllers
[Authorize(Policy = "Editor")]
public async Task<IActionResult> Update(Collection collection)
{
if (!ModelState.IsValid) return BadRequest(ModelState);
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
var result = await CollectionService.UpdateCollection(await CurrentUserModel(), collection);
if (result == null || result.IsNotFoundResult) return NotFound();
if (!result.IsSuccess) return StatusCode((int)HttpStatusCode.InternalServerError);
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);
}
}

View file

@ -8,6 +8,7 @@ using Roadie.Library.Configuration;
using Roadie.Library.Enums;
using Roadie.Library.Identity;
using System;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using models = Roadie.Library.Models;
@ -39,7 +40,18 @@ namespace Roadie.Api.Controllers
if (string.IsNullOrEmpty(model.Cmt)) return StatusCode((int)HttpStatusCode.BadRequest, "Invalid Comment");
var result = await CommentService.AddNewArtistComment(await CurrentUserModel(), id, model.Cmt);
if (result == null || result.IsNotFoundResult) return NotFound();
if (!result.IsSuccess) return StatusCode((int)HttpStatusCode.InternalServerError);
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);
}
@ -51,7 +63,18 @@ namespace Roadie.Api.Controllers
if (string.IsNullOrEmpty(model.Cmt)) return StatusCode((int)HttpStatusCode.BadRequest, "Invalid Comment");
var result = await CommentService.AddNewCollectionComment(await CurrentUserModel(), id, model.Cmt);
if (result == null || result.IsNotFoundResult) return NotFound();
if (!result.IsSuccess) return StatusCode((int)HttpStatusCode.InternalServerError);
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);
}
@ -63,7 +86,18 @@ namespace Roadie.Api.Controllers
if (string.IsNullOrEmpty(model.Cmt)) return StatusCode((int)HttpStatusCode.BadRequest, "Invalid Comment");
var result = await CommentService.AddNewGenreComment(await CurrentUserModel(), id, model.Cmt);
if (result == null || result.IsNotFoundResult) return NotFound();
if (!result.IsSuccess) return StatusCode((int)HttpStatusCode.InternalServerError);
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);
}
@ -75,7 +109,18 @@ namespace Roadie.Api.Controllers
if (string.IsNullOrEmpty(model.Cmt)) return StatusCode((int)HttpStatusCode.BadRequest, "Invalid Comment");
var result = await CommentService.AddNewLabelComment(await CurrentUserModel(), id, model.Cmt);
if (result == null || result.IsNotFoundResult) return NotFound();
if (!result.IsSuccess) return StatusCode((int)HttpStatusCode.InternalServerError);
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);
}
@ -87,7 +132,18 @@ namespace Roadie.Api.Controllers
if (string.IsNullOrEmpty(model.Cmt)) return StatusCode((int)HttpStatusCode.BadRequest, "Invalid Comment");
var result = await CommentService.AddNewPlaylistComment(await CurrentUserModel(), id, model.Cmt);
if (result == null || result.IsNotFoundResult) return NotFound();
if (!result.IsSuccess) return StatusCode((int)HttpStatusCode.InternalServerError);
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);
}
@ -99,7 +155,18 @@ namespace Roadie.Api.Controllers
if (string.IsNullOrEmpty(model.Cmt)) return StatusCode((int)HttpStatusCode.BadRequest, "Invalid Comment");
var result = await CommentService.AddNewReleaseComment(await CurrentUserModel(), id, model.Cmt);
if (result == null || result.IsNotFoundResult) return NotFound();
if (!result.IsSuccess) return StatusCode((int)HttpStatusCode.InternalServerError);
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);
}
@ -111,7 +178,18 @@ namespace Roadie.Api.Controllers
if (string.IsNullOrEmpty(model.Cmt)) return StatusCode((int)HttpStatusCode.BadRequest, "Invalid Comment");
var result = await CommentService.AddNewTrackComment(await CurrentUserModel(), id, model.Cmt);
if (result == null || result.IsNotFoundResult) return NotFound();
if (!result.IsSuccess) return StatusCode((int)HttpStatusCode.InternalServerError);
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);
}
@ -122,7 +200,18 @@ namespace Roadie.Api.Controllers
{
var result = await CommentService.DeleteComment(await CurrentUserModel(), id);
if (result == null || result.IsNotFoundResult) return NotFound();
if (!result.IsSuccess) return StatusCode((int)HttpStatusCode.InternalServerError);
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);
}
@ -133,7 +222,18 @@ namespace Roadie.Api.Controllers
{
var result = await CommentService.SetCommentReaction(await CurrentUserModel(), id, reaction);
if (result == null || result.IsNotFoundResult) return NotFound();
if (!result.IsSuccess) return StatusCode((int)HttpStatusCode.InternalServerError);
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);
}
}

View file

@ -44,11 +44,19 @@ namespace Roadie.Api.Controllers
protected async Task<models.User> CurrentUserModel()
{
if (_currentUser == null)
{
if (User.Identity.IsAuthenticated)
_currentUser = await CacheManager.GetAsync($"urn:controller_user:{User.Identity.Name}",
async () => { return UserModelForUser(await UserManager.GetUserAsync(User)); },
ControllerCacheRegionUrn);
if (_currentUser == null) throw new UnauthorizedAccessException("Access Denied");
{
_currentUser = await CacheManager.GetAsync($"urn:controller_user:{User.Identity.Name}", async () =>
{
return UserModelForUser(await UserManager.GetUserAsync(User));
}, ControllerCacheRegionUrn);
}
}
if (_currentUser == null)
{
throw new UnauthorizedAccessException("Access Denied");
}
return _currentUser;
}

View file

@ -9,6 +9,7 @@ using Roadie.Library.Configuration;
using Roadie.Library.Identity;
using Roadie.Library.Models.Pagination;
using System;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using System.Web;
@ -80,6 +81,14 @@ namespace Roadie.Api.Controllers
}
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);
@ -93,7 +102,18 @@ namespace Roadie.Api.Controllers
{
var result = await GenreService.UploadGenreImage(await CurrentUserModel(), id, file);
if (result == null || result.IsNotFoundResult) return NotFound();
if (!result.IsSuccess) return StatusCode((int)HttpStatusCode.InternalServerError);
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);
}
}

View file

@ -9,6 +9,7 @@ using Roadie.Library.Configuration;
using Roadie.Library.Identity;
using Roadie.Library.Models.Pagination;
using System;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using System.Web;
@ -110,10 +111,23 @@ namespace Roadie.Api.Controllers
[Authorize(Policy = "Editor")]
public async Task<IActionResult> Update(models.Label label)
{
if (!ModelState.IsValid) return BadRequest(ModelState);
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
var result = await LabelService.UpdateLabel(await CurrentUserModel(), label);
if (result == null || result.IsNotFoundResult) return NotFound();
if (!result.IsSuccess) return StatusCode((int)HttpStatusCode.InternalServerError);
if (result == null || result.IsNotFoundResult)
{
return NotFound();
}
if (!result.IsSuccess)
{
if (result.Messages?.Any() ?? false)
{
return StatusCode((int)HttpStatusCode.BadRequest, result.Messages);
}
return StatusCode((int)HttpStatusCode.InternalServerError);
}
return Ok(result);
}

View file

@ -9,6 +9,7 @@ using Roadie.Library.Identity;
using Roadie.Library.Models.Pagination;
using Roadie.Library.Models.Playlists;
using System;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
@ -36,7 +37,18 @@ namespace Roadie.Api.Controllers
public async Task<IActionResult> AddNewPlaylist([FromBody] Playlist model)
{
var result = await PlaylistService.AddNewPlaylist(await CurrentUserModel(), model);
if (!result.IsSuccess) return StatusCode((int)HttpStatusCode.InternalServerError);
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);
}
@ -46,9 +58,22 @@ namespace Roadie.Api.Controllers
public async Task<IActionResult> DeletePlaylist(Guid id)
{
var result = await PlaylistService.DeletePlaylist(await 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);
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);
}
@ -57,10 +82,23 @@ namespace Roadie.Api.Controllers
[ProducesResponseType(404)]
public async Task<IActionResult> Get(Guid id, string inc = null)
{
var result = await PlaylistService.ById(await CurrentUserModel(), id,
(inc ?? Playlist.DefaultIncludes).ToLower().Split(","));
if (result == null || result.IsNotFoundResult) return NotFound();
if (!result.IsSuccess) return StatusCode((int)HttpStatusCode.InternalServerError);
var result = await PlaylistService.ById(await CurrentUserModel(), id,(inc ?? Playlist.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);
}
@ -81,8 +119,22 @@ namespace Roadie.Api.Controllers
{
if (!ModelState.IsValid) return BadRequest(ModelState);
var result = await PlaylistService.UpdatePlaylist(await CurrentUserModel(), playlist);
if (result == null || result.IsNotFoundResult) return NotFound();
if (!result.IsSuccess) return StatusCode((int)HttpStatusCode.InternalServerError);
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);
}
@ -92,8 +144,22 @@ namespace Roadie.Api.Controllers
public async Task<IActionResult> UpdateTracks(PlaylistTrackModifyRequest request)
{
var result = await PlaylistService.UpdatePlaylistTracks(await CurrentUserModel(), request);
if (result == null || result.IsNotFoundResult) return NotFound();
if (!result.IsSuccess) return StatusCode((int)HttpStatusCode.InternalServerError);
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);
}
}

View file

@ -10,6 +10,7 @@ using Roadie.Library.Identity;
using Roadie.Library.Models.Pagination;
using Roadie.Library.Models.Releases;
using System;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using System.Web;
@ -78,7 +79,18 @@ namespace Roadie.Api.Controllers
{
var result = await ReleaseService.MergeReleases(await UserManager.GetUserAsync(User), releaseToMergeId, releaseToMergeIntoId, addAsMedia);
if (result == null || result.IsNotFoundResult) return NotFound();
if (!result.IsSuccess) return StatusCode((int)HttpStatusCode.InternalServerError);
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);
}
@ -90,7 +102,18 @@ namespace Roadie.Api.Controllers
{
var result = await ReleaseService.SetReleaseImageByUrl(await UserManager.GetUserAsync(User), id, HttpUtility.UrlDecode(imageUrl));
if (result == null || result.IsNotFoundResult) return NotFound();
if (!result.IsSuccess) return StatusCode((int)HttpStatusCode.InternalServerError);
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);
}
@ -100,10 +123,23 @@ namespace Roadie.Api.Controllers
[Authorize(Policy = "Editor")]
public async Task<IActionResult> Update(Release release)
{
if (!ModelState.IsValid) return BadRequest(ModelState);
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
var result = await ReleaseService.UpdateRelease(await UserManager.GetUserAsync(User), release);
if (result == null || result.IsNotFoundResult) return NotFound();
if (!result.IsSuccess) return StatusCode((int)HttpStatusCode.InternalServerError);
if (result == null || result.IsNotFoundResult)
{
return NotFound();
}
if (!result.IsSuccess)
{
if (result.Messages?.Any() ?? false)
{
return StatusCode((int)HttpStatusCode.BadRequest, result.Messages);
}
return StatusCode((int)HttpStatusCode.InternalServerError);
}
return Ok(result);
}
@ -115,7 +151,18 @@ namespace Roadie.Api.Controllers
{
var result = await ReleaseService.UploadReleaseImage(await UserManager.GetUserAsync(User), id, file);
if (result == null || result.IsNotFoundResult) return NotFound();
if (!result.IsSuccess) return StatusCode((int)HttpStatusCode.InternalServerError);
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);
}
}

View file

@ -8,6 +8,7 @@ using Roadie.Library.Configuration;
using Roadie.Library.Identity;
using Roadie.Library.Models.Pagination;
using System;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using models = Roadie.Library.Models;
@ -75,7 +76,18 @@ namespace Roadie.Api.Controllers
if (!ModelState.IsValid) return BadRequest(ModelState);
var result = await TrackService.UpdateTrack(await CurrentUserModel(), track);
if (result == null || result.IsNotFoundResult) return NotFound();
if (!result.IsSuccess) return StatusCode((int)HttpStatusCode.InternalServerError);
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);
}
}

View file

@ -12,6 +12,7 @@ using Roadie.Library.Models.Users;
using Roadie.Library.Utility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
@ -273,13 +274,23 @@ namespace Roadie.Api.Controllers
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);
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);
}
CacheManager.ClearRegion(ControllerCacheRegionUrn);
var modelUser = await UserManager.FindByNameAsync(model.UserName);
var t = await TokenService.GenerateToken(modelUser, UserManager);
CacheManager.ClearRegion(ControllerCacheRegionUrn);
var avatarUrl =
$"{RoadieHttpContext.ImageBaseUrl}/user/{modelUser.RoadieId}/{RoadieSettings.ThumbnailImageSize.Width}/{RoadieSettings.ThumbnailImageSize.Height}";
var avatarUrl = $"{RoadieHttpContext.ImageBaseUrl}/user/{modelUser.RoadieId}/{RoadieSettings.ThumbnailImageSize.Width}/{RoadieSettings.ThumbnailImageSize.Height}";
return Ok(new
{
IsSuccess = true,