roadie/Roadie.Api.Services/GenreService.cs

93 lines
3.8 KiB
C#
Raw Normal View History

2018-11-15 04:25:40 +00:00
using Microsoft.Extensions.Logging;
using Roadie.Library.Caching;
using Roadie.Library.Configuration;
using Roadie.Library.Encoding;
using Roadie.Library.Models;
using Roadie.Library.Models.Pagination;
using Roadie.Library.Models.Users;
using Roadie.Library.Utility;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;
using data = Roadie.Library.Data;
namespace Roadie.Api.Services
{
public class GenreService : ServiceBase, IGenreService
{
public GenreService(IRoadieSettings configuration,
2019-06-30 22:14:36 +00:00
IHttpEncoder httpEncoder,
IHttpContext httpContext,
data.IRoadieDbContext dbContext,
ICacheManager cacheManager,
ILogger<GenreService> logger)
2018-11-15 04:25:40 +00:00
: base(configuration, httpEncoder, dbContext, cacheManager, logger, httpContext)
{
}
2019-06-30 22:14:36 +00:00
public Task<Library.Models.Pagination.PagedResult<GenreList>> List(User roadieUser, PagedRequest request,
bool? doRandomize = false)
2018-11-15 04:25:40 +00:00
{
var sw = new Stopwatch();
sw.Start();
if (!string.IsNullOrEmpty(request.Sort))
{
request.Sort = request.Sort.Replace("createdDate", "createdDateTime");
request.Sort = request.Sort.Replace("lastUpdated", "lastUpdatedDateTime");
}
2019-06-30 22:14:36 +00:00
var result = from g in DbContext.Genres
let releaseCount = (from rg in DbContext.ReleaseGenres
2019-01-08 22:40:26 +00:00
where rg.GenreId == g.Id
select rg.Id).Count()
2019-06-30 22:14:36 +00:00
let artistCount = (from rg in DbContext.ArtistGenres
where rg.GenreId == g.Id
select rg.Id).Count()
where request.FilterValue.Length == 0 || g.Name.Contains(request.FilterValue)
select new GenreList
{
DatabaseId = g.Id,
Id = g.RoadieId,
Genre = new DataToken
{
Text = g.Name,
Value = g.RoadieId.ToString()
},
ReleaseCount = releaseCount,
ArtistCount = artistCount,
CreatedDate = g.CreatedDate,
LastUpdated = g.LastUpdated
};
2018-11-15 04:25:40 +00:00
2019-06-30 22:14:36 +00:00
GenreList[] rows;
2018-11-15 04:25:40 +00:00
var rowCount = result.Count();
if (doRandomize ?? false)
{
var randomLimit = roadieUser?.RandomReleaseLimit ?? 100;
request.Limit = request.LimitValue > randomLimit ? randomLimit : request.LimitValue;
2019-03-06 01:18:21 +00:00
rows = result.OrderBy(x => x.RandomSortId).Skip(request.SkipValue).Take(request.LimitValue).ToArray();
}
else
{
2019-06-30 22:14:36 +00:00
var sortBy = string.IsNullOrEmpty(request.Sort)
? request.OrderValue(new Dictionary<string, string> { { "Genre.Text", "ASC" } })
: request.OrderValue();
rows = result.OrderBy(sortBy).Skip(request.SkipValue).Take(request.LimitValue).ToArray();
}
2019-06-30 22:14:36 +00:00
2018-11-15 04:25:40 +00:00
sw.Stop();
2018-12-24 19:40:49 +00:00
return Task.FromResult(new Library.Models.Pagination.PagedResult<GenreList>
2018-11-15 04:25:40 +00:00
{
TotalCount = rowCount,
CurrentPage = request.PageValue,
TotalPages = (int)Math.Ceiling((double)rowCount / request.LimitValue),
OperationTime = sw.ElapsedMilliseconds,
Rows = rows
2018-12-24 19:40:49 +00:00
});
2018-11-15 04:25:40 +00:00
}
}
2018-11-16 03:37:00 +00:00
}