From c3656625aeb8169f0eb4074c4705d2fa54cf7118 Mon Sep 17 00:00:00 2001 From: Steven Hildreth Date: Sun, 11 Nov 2018 14:45:44 -0600 Subject: [PATCH] WIP --- RoadieApi/Controllers/ArtistController.cs | 7 +--- RoadieApi/Controllers/EntityControllerBase.cs | 40 ++++++++++++++----- RoadieApi/Controllers/ImageController.cs | 22 +++++++++- RoadieApi/Controllers/LabelController.cs | 6 ++- RoadieApi/Controllers/ReleaseController.cs | 6 ++- RoadieApi/Controllers/TrackController.cs | 6 ++- RoadieApi/Services/ArtistService.cs | 6 +-- RoadieApi/Services/IImageService.cs | 3 ++ RoadieApi/Services/ImageService.cs | 33 +++++++++++++++ RoadieLibrary/FileOperationResult.cs | 5 +++ RoadieLibrary/Models/Users/User.cs | 4 ++ RoadieLibrary/OperationResult.cs | 16 ++++++++ 12 files changed, 126 insertions(+), 28 deletions(-) diff --git a/RoadieApi/Controllers/ArtistController.cs b/RoadieApi/Controllers/ArtistController.cs index 7bf0861..bf6e4b8 100644 --- a/RoadieApi/Controllers/ArtistController.cs +++ b/RoadieApi/Controllers/ArtistController.cs @@ -21,14 +21,11 @@ namespace Roadie.Api.Controllers { private IArtistService ArtistService { get; } - private UserManager UserManager { get; } - public ArtistController(IArtistService artistService, ILoggerFactory logger, ICacheManager cacheManager, IConfiguration configuration, UserManager userManager) - : base(cacheManager, configuration) + : base(cacheManager, configuration, userManager) { this._logger = logger.CreateLogger("RoadieApi.Controllers.ArtistController"); this.ArtistService = artistService; - this.UserManager = userManager; } //[EnableQuery] @@ -43,7 +40,7 @@ namespace Roadie.Api.Controllers public async Task> Get(Guid id, string inc = null) { var result = await this.ArtistService.ArtistById(null, id, (inc ?? Artist.DefaultIncludes).ToLower().Split(",")); - if (result == null) + if (result == null || result.IsNotFoundResult) { return NotFound(); } diff --git a/RoadieApi/Controllers/EntityControllerBase.cs b/RoadieApi/Controllers/EntityControllerBase.cs index b60c4a8..631a480 100644 --- a/RoadieApi/Controllers/EntityControllerBase.cs +++ b/RoadieApi/Controllers/EntityControllerBase.cs @@ -1,31 +1,51 @@ using Mapster; using Microsoft.AspNet.OData; +using Microsoft.AspNetCore.Identity; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Logging; using Roadie.Library.Caching; using Roadie.Library.Configuration; using Roadie.Library.Data; +using Roadie.Library.Identity; +using models = Roadie.Library.Models.Users; +using System.Threading.Tasks; namespace Roadie.Api.Controllers { public abstract class EntityControllerBase : ODataController { - protected readonly ICacheManager _cacheManager; - protected readonly IConfiguration _configuration; - protected readonly IRoadieSettings _roadieSettings; + protected ICacheManager CacheManager { get; } + protected IConfiguration Configuration { get; } + protected IRoadieSettings RoadieSettings { get; } + protected UserManager UserManager { get; } protected ILogger _logger; - protected IRoadieSettings RoadieSettings => this._roadieSettings; - - public EntityControllerBase(ICacheManager cacheManager, IConfiguration configuration) + public EntityControllerBase(ICacheManager cacheManager, IConfiguration configuration, UserManager userManager) { - this._cacheManager = cacheManager; - this._configuration = configuration; + this.CacheManager = cacheManager; + this.Configuration = configuration; - this._roadieSettings = new RoadieSettings(); - this._configuration.GetSection("RoadieSettings").Bind(this._roadieSettings); + this.RoadieSettings = new RoadieSettings(); + this.Configuration.GetSection("RoadieSettings").Bind(this.RoadieSettings); + this.UserManager = userManager; } + + private models.User _currentUser = null; + protected async Task CurrentUserModel() + { + if(this._currentUser == null) + { + if(this.User.Identity.IsAuthenticated) + { + var user = await this.UserManager.GetUserAsync(User); + this._currentUser = user.Adapt(); + this._currentUser.IsAdmin = User.IsInRole("Admin"); + this._currentUser.IsEditor = User.IsInRole("Editor"); + } + } + return this._currentUser; + } } } \ No newline at end of file diff --git a/RoadieApi/Controllers/ImageController.cs b/RoadieApi/Controllers/ImageController.cs index e2a5454..f6e0317 100644 --- a/RoadieApi/Controllers/ImageController.cs +++ b/RoadieApi/Controllers/ImageController.cs @@ -1,9 +1,11 @@ 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.Identity; using System; using System.Net; using System.Threading.Tasks; @@ -18,8 +20,8 @@ namespace Roadie.Api.Controllers { private IImageService ImageService { get; } - public ImageController(IImageService imageService, ILoggerFactory logger, ICacheManager cacheManager, IConfiguration configuration) - : base(cacheManager, configuration) + public ImageController(IImageService imageService, ILoggerFactory logger, ICacheManager cacheManager, IConfiguration configuration, UserManager userManager) + : base(cacheManager, configuration, userManager) { this._logger = logger.CreateLogger("RoadieApi.Controllers.ImageController"); this.ImageService = imageService; @@ -198,5 +200,21 @@ namespace Roadie.Api.Controllers lastModified: result.LastModified, entityTag: result.ETag); } + + [HttpPost("{id}")] + [Authorize(Policy ="Editor")] + public async Task Delete(Guid id) + { + var result = await this.ImageService.Delete(await this.CurrentUserModel(), id); + if (result == null || result.IsNotFoundResult) + { + return NotFound(); + } + if (!result.IsSuccess) + { + return StatusCode((int)HttpStatusCode.InternalServerError); + } + return Ok(result); + } } } \ No newline at end of file diff --git a/RoadieApi/Controllers/LabelController.cs b/RoadieApi/Controllers/LabelController.cs index 44733de..0b7c1c8 100644 --- a/RoadieApi/Controllers/LabelController.cs +++ b/RoadieApi/Controllers/LabelController.cs @@ -1,11 +1,13 @@ using Mapster; using Microsoft.AspNet.OData; using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Logging; using Roadie.Library.Caching; using Roadie.Library.Data; +using Roadie.Library.Identity; using System; using System.Linq; using models = Roadie.Library.Models; @@ -18,8 +20,8 @@ namespace Roadie.Api.Controllers [Authorize] public class LabelController : EntityControllerBase { - public LabelController(ILoggerFactory logger, ICacheManager cacheManager, IConfiguration configuration) - : base( cacheManager, configuration) + public LabelController(ILoggerFactory logger, ICacheManager cacheManager, IConfiguration configuration, UserManager userManager) + : base( cacheManager, configuration, userManager) { this._logger = logger.CreateLogger("RoadieApi.Controllers.LabelController"); ; } diff --git a/RoadieApi/Controllers/ReleaseController.cs b/RoadieApi/Controllers/ReleaseController.cs index a742334..509d773 100644 --- a/RoadieApi/Controllers/ReleaseController.cs +++ b/RoadieApi/Controllers/ReleaseController.cs @@ -1,12 +1,14 @@ using Mapster; using Microsoft.AspNet.OData; using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Logging; using Roadie.Library.Caching; using Roadie.Library.Data; +using Roadie.Library.Identity; using System; using System.Linq; using models = Roadie.Library.Models; @@ -19,8 +21,8 @@ namespace Roadie.Api.Controllers [Authorize] public class ReleaseController : EntityControllerBase { - public ReleaseController(ILoggerFactory logger, ICacheManager cacheManager, IConfiguration configuration) - : base(cacheManager, configuration) + public ReleaseController(ILoggerFactory logger, ICacheManager cacheManager, IConfiguration configuration, UserManager userManager) + : base(cacheManager, configuration, userManager) { this._logger = logger.CreateLogger("RoadieApi.Controllers.ReleaseController"); ; } diff --git a/RoadieApi/Controllers/TrackController.cs b/RoadieApi/Controllers/TrackController.cs index b1e0b83..dac165a 100644 --- a/RoadieApi/Controllers/TrackController.cs +++ b/RoadieApi/Controllers/TrackController.cs @@ -1,11 +1,13 @@ using Mapster; using Microsoft.AspNet.OData; using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Logging; using Roadie.Library.Caching; using Roadie.Library.Data; +using Roadie.Library.Identity; using System; using System.Linq; using models = Roadie.Library.Models; @@ -18,8 +20,8 @@ namespace Roadie.Api.Controllers [Authorize] public class TrackController : EntityControllerBase { - public TrackController(ILoggerFactory logger, ICacheManager cacheManager, IConfiguration configuration) - : base(cacheManager, configuration) + public TrackController(ILoggerFactory logger, ICacheManager cacheManager, IConfiguration configuration, UserManager userManager) + : base(cacheManager, configuration, userManager) { this._logger = logger.CreateLogger("RoadieApi.Controllers.TrackController"); ; } diff --git a/RoadieApi/Services/ArtistService.cs b/RoadieApi/Services/ArtistService.cs index 8ad9424..6ce1b01 100644 --- a/RoadieApi/Services/ArtistService.cs +++ b/RoadieApi/Services/ArtistService.cs @@ -76,11 +76,7 @@ namespace Roadie.Api.Services if (artist == null) { - sw.Stop(); - return new OperationResult(string.Format("Artist Not Found [{0}]", id)) - { - OperationTime = sw.ElapsedMilliseconds - }; + return new OperationResult(true, string.Format("Artist Not Found [{0}]", id)); } var result = artist.Adapt(); result.Thumbnail = base.MakeArtistThumbnailImage(id); diff --git a/RoadieApi/Services/IImageService.cs b/RoadieApi/Services/IImageService.cs index 67e1d33..48037a1 100644 --- a/RoadieApi/Services/IImageService.cs +++ b/RoadieApi/Services/IImageService.cs @@ -1,5 +1,6 @@ using Microsoft.Net.Http.Headers; using Roadie.Library; +using Roadie.Library.Models.Users; using System; using System.Threading.Tasks; @@ -11,6 +12,8 @@ namespace Roadie.Api.Services Task> CollectionThumbnail(Guid id, int? width, int? height, EntityTagHeaderValue etag = null); + Task> Delete(User roadieUser, Guid id); + Task> ImageById(Guid id, int? width, int? height, EntityTagHeaderValue etag = null); Task> LabelThumbnail(Guid id, int? width, int? height, EntityTagHeaderValue etag = null); diff --git a/RoadieApi/Services/ImageService.cs b/RoadieApi/Services/ImageService.cs index c6cf921..59713bd 100644 --- a/RoadieApi/Services/ImageService.cs +++ b/RoadieApi/Services/ImageService.cs @@ -9,6 +9,7 @@ using Roadie.Library.Encoding; using Roadie.Library.Identity; using Roadie.Library.Imaging; using Roadie.Library.Models; +using Roadie.Library.Models.Users; using Roadie.Library.Utility; using System; using System.Diagnostics; @@ -418,5 +419,37 @@ namespace Roadie.Api.Services } return new FileOperationResult(OperationMessages.ErrorOccured); } + + public async Task> Delete(User roadieUser, Guid id) + { + var sw = Stopwatch.StartNew(); + var image = this.DbContext.Images + .Include("Release") + .Include("Artist") + .FirstOrDefault(x => x.RoadieId == id); + if (image == null) + { + return new OperationResult(true, string.Format("Image Not Found [{0}]", id)); + } + if(image.ArtistId.HasValue) + { + this.CacheManager.ClearRegion(data.Artist.CacheRegionUrn(image.Artist.RoadieId)); + } + if(image.ReleaseId.HasValue) + { + this.CacheManager.ClearRegion(data.Release.CacheRegionUrn(image.Release.RoadieId)); + } + this.DbContext.Images.Remove(image); + await this.DbContext.SaveChangesAsync(); + this.CacheManager.ClearRegion(data.Image.CacheRegionUrn(id)); + this.Logger.LogInformation($"Deleted Image [{ id }], By User [{ roadieUser.ToString() }]"); + sw.Stop(); + return new OperationResult + { + Data = true, + IsSuccess = true, + OperationTime = sw.ElapsedMilliseconds + }; + } } } \ No newline at end of file diff --git a/RoadieLibrary/FileOperationResult.cs b/RoadieLibrary/FileOperationResult.cs index 9ee1aad..327de17 100644 --- a/RoadieLibrary/FileOperationResult.cs +++ b/RoadieLibrary/FileOperationResult.cs @@ -41,6 +41,11 @@ namespace Roadie.Library public FileOperationResult(bool isNotFoundResult, IEnumerable messages = null) { this.IsNotFoundResult = isNotFoundResult; + if (messages != null && messages.Any()) + { + this.AdditionalData = new Dictionary(); + messages.ToList().ForEach(x => this.AddMessage(x)); + } } } } \ No newline at end of file diff --git a/RoadieLibrary/Models/Users/User.cs b/RoadieLibrary/Models/Users/User.cs index b7212c2..e459ace 100644 --- a/RoadieLibrary/Models/Users/User.cs +++ b/RoadieLibrary/Models/Users/User.cs @@ -13,5 +13,9 @@ namespace Roadie.Library.Models.Users public Guid UserId { get; set; } public int Id { get; set; } + public override string ToString() + { + return $"Id [{ Id }], RoadieId [{ UserId }], UserName [{ UserName }]"; + } } } \ No newline at end of file diff --git a/RoadieLibrary/OperationResult.cs b/RoadieLibrary/OperationResult.cs index bacd807..c9af8c7 100644 --- a/RoadieLibrary/OperationResult.cs +++ b/RoadieLibrary/OperationResult.cs @@ -38,6 +38,22 @@ namespace Roadie.Library } } + public OperationResult(bool isNotFoundResult, IEnumerable messages = null) + { + this.IsNotFoundResult = isNotFoundResult; + if (messages != null && messages.Any()) + { + this.AdditionalData = new Dictionary(); + messages.ToList().ForEach(x => this.AddMessage(x)); + } + } + + public OperationResult(bool isNotFoundResult, string message) + { + this.IsNotFoundResult = isNotFoundResult; + this.AddMessage(message); + } + public OperationResult(string message = null) { this.AdditionalData = new Dictionary();