diff --git a/RoadieApi/Controllers/LabelController.cs b/RoadieApi/Controllers/LabelController.cs index c495e42..fc666bc 100644 --- a/RoadieApi/Controllers/LabelController.cs +++ b/RoadieApi/Controllers/LabelController.cs @@ -7,8 +7,10 @@ using Roadie.Api.Services; using Roadie.Library.Caching; using Roadie.Library.Identity; using Roadie.Library.Models.Pagination; +using System; using System.Net; using System.Threading.Tasks; +using models = Roadie.Library.Models; namespace Roadie.Api.Controllers { @@ -27,33 +29,22 @@ namespace Roadie.Api.Controllers this.LabelService = labelService; } - //[EnableQuery] - //public IActionResult Get() - //{ - // return Ok(this._RoadieDbContext.Labels.ProjectToType()); - //} - - //[HttpGet("{id}")] - //[ProducesResponseType(200)] - //[ProducesResponseType(404)] - //public IActionResult Get(Guid id) - //{ - // var key = id.ToString(); - // var result = this._cacheManager.Get(key, () => - // { - // var d = this._RoadieDbContext.Labels.FirstOrDefault(x => x.RoadieId == id); - // if (d != null) - // { - // return d.Adapt(); - // } - // return null; - // }, key); - // if (result == null) - // { - // return NotFound(); - // } - // return Ok(result); - //} + [HttpGet("{id}")] + [ProducesResponseType(200)] + [ProducesResponseType(404)] + public async Task Get(Guid id, string inc = null) + { + var result = await this.LabelService.ById(await this.CurrentUserModel(), id, (inc ?? models.Label.DefaultIncludes).ToLower().Split(",")); + if (result == null || result.IsNotFoundResult) + { + return NotFound(); + } + if (!result.IsSuccess) + { + return StatusCode((int)HttpStatusCode.InternalServerError); + } + return Ok(result); + } [HttpGet] [ProducesResponseType(200)] diff --git a/RoadieApi/Services/ILabelService.cs b/RoadieApi/Services/ILabelService.cs index 9c7982a..4950352 100644 --- a/RoadieApi/Services/ILabelService.cs +++ b/RoadieApi/Services/ILabelService.cs @@ -1,12 +1,16 @@ -using Roadie.Library.Models; +using Roadie.Library; +using Roadie.Library.Models; using Roadie.Library.Models.Pagination; using Roadie.Library.Models.Users; +using System; +using System.Collections.Generic; using System.Threading.Tasks; namespace Roadie.Api.Services { public interface ILabelService { + Task> ById(User roadieUser, Guid id, IEnumerable includes = null); Task> List(User roadieUser, PagedRequest request, bool? doRandomize = false); } } \ No newline at end of file diff --git a/RoadieApi/Services/LabelService.cs b/RoadieApi/Services/LabelService.cs index 3497c50..10b364c 100644 --- a/RoadieApi/Services/LabelService.cs +++ b/RoadieApi/Services/LabelService.cs @@ -1,7 +1,11 @@ -using Microsoft.Extensions.Logging; +using Mapster; +using Microsoft.Extensions.Logging; +using Roadie.Library; using Roadie.Library.Caching; using Roadie.Library.Configuration; using Roadie.Library.Encoding; +using Roadie.Library.Enums; +using Roadie.Library.Extensions; using Roadie.Library.Models; using Roadie.Library.Models.Pagination; using Roadie.Library.Models.Users; @@ -18,6 +22,8 @@ namespace Roadie.Api.Services { public class LabelService : ServiceBase, ILabelService { + private IBookmarkService BookmarkService { get; } = null; + public LabelService(IRoadieSettings configuration, IHttpEncoder httpEncoder, IHttpContext httpContext, @@ -25,11 +31,99 @@ namespace Roadie.Api.Services ICacheManager cacheManager, ILogger logger, ICollectionService collectionService, - IPlaylistService playlistService) + IPlaylistService playlistService, + IBookmarkService bookmarkService) : base(configuration, httpEncoder, context, cacheManager, logger, httpContext) { + this.BookmarkService = bookmarkService; } + public async Task> ById(User roadieUser, Guid id, IEnumerable includes = null) + { + var sw = Stopwatch.StartNew(); + sw.Start(); + var cacheKey = string.Format("urn:label_by_id_operation:{0}:{1}", id, includes == null ? "0" : string.Join("|", includes)); + var result = await this.CacheManager.GetAsync>(cacheKey, async () => + { + return await this.LabelByIdAction(id, includes); + }, data.Artist.CacheRegionUrn(id)); + sw.Stop(); + if (result?.Data != null && roadieUser != null) + { + var userBookmarkResult = await this.BookmarkService.List(roadieUser, new PagedRequest(), false, BookmarkType.Label); + if (userBookmarkResult.IsSuccess) + { + result.Data.UserBookmarked = userBookmarkResult?.Rows?.FirstOrDefault(x => x.Bookmark.Text == result.Data.Id.ToString()) != null; + } + } + return new OperationResult