roadie/RoadieApi/Controllers/ReleaseController.cs

64 lines
2.2 KiB
C#
Raw Normal View History

2018-11-02 21:04:49 +00:00
using Mapster;
using Microsoft.AspNet.OData;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
2018-11-03 21:21:36 +00:00
using Roadie.Library.Setttings;
2018-11-02 21:04:49 +00:00
using Roadie.Library.Caching;
using Roadie.Library.Data;
using System;
using System.Linq;
using models = Roadie.Api.Data.Models;
namespace Roadie.Api.Controllers
{
[Produces("application/json")]
[Route("release")]
[ApiController]
[Authorize]
public class ReleaseController : EntityControllerBase
{
2018-11-03 21:21:36 +00:00
public ReleaseController(IRoadieDbContext RoadieDbContext, ILoggerFactory logger, ICacheManager cacheManager, IConfiguration configuration, IRoadieSettings RoadieSettings)
: base(RoadieDbContext, cacheManager, configuration, RoadieSettings)
2018-11-02 21:04:49 +00:00
{
this._logger = logger.CreateLogger("RoadieApi.Controllers.ReleaseController"); ;
}
[EnableQuery]
public IActionResult Get()
{
2018-11-03 21:21:36 +00:00
return Ok(this._RoadieDbContext.Releases.ProjectToType<models.Release>());
2018-11-02 21:04:49 +00:00
}
[HttpGet("{id}")]
[ProducesResponseType(200)]
[ProducesResponseType(404)]
public IActionResult Get(Guid id)
{
var key = id.ToString();
var result = this._cacheManager.Get<models.Release>(key, () =>
{
2018-11-03 21:21:36 +00:00
var d = this._RoadieDbContext
2018-11-02 21:04:49 +00:00
.Releases
.Include(x => x.Artist)
.Include(x => x.Labels).Include("Labels.Label")
.Include(x => x.Medias).Include("Medias.Tracks")
.Include(x => x.Genres).Include("Genres.Genre")
.Include(x => x.Collections).Include("Collections.Collection")
.FirstOrDefault(x => x.RoadieId == id);
if (d != null)
{
return d.Adapt<models.Release>();
}
return null;
}, key);
if (result == null)
{
return NotFound();
}
return Ok(result);
}
}
}