roadie/RoadieApi/Services/ArtistService.cs

476 lines
26 KiB
C#
Raw Normal View History

2018-11-06 22:59:48 +00:00
using Mapster;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
2018-12-05 16:04:21 +00:00
using Newtonsoft.Json;
2018-11-06 04:31:25 +00:00
using Roadie.Library;
using Roadie.Library.Caching;
using Roadie.Library.Configuration;
using Roadie.Library.Encoding;
2018-11-06 22:59:48 +00:00
using Roadie.Library.Enums;
using Roadie.Library.Extensions;
2018-11-06 04:31:25 +00:00
using Roadie.Library.Models;
using Roadie.Library.Models.Pagination;
2018-11-06 22:59:48 +00:00
using Roadie.Library.Models.Releases;
using Roadie.Library.Models.Statistics;
using Roadie.Library.Models.Users;
using Roadie.Library.Utility;
2018-11-06 04:31:25 +00:00
using System;
using System.Collections.Generic;
2018-11-06 21:55:31 +00:00
using System.Diagnostics;
2018-11-06 22:59:48 +00:00
using System.Linq;
2018-11-14 14:00:08 +00:00
using System.Linq.Dynamic.Core;
2018-11-06 04:31:25 +00:00
using System.Threading.Tasks;
using data = Roadie.Library.Data;
namespace Roadie.Api.Services
{
#pragma warning disable 1998
2018-11-06 04:31:25 +00:00
public class ArtistService : ServiceBase, IArtistService
{
2018-11-07 04:33:22 +00:00
private ICollectionService CollectionService { get; } = null;
private IPlaylistService PlaylistService { get; } = null;
private IBookmarkService BookmarkService { get; } = null;
2018-11-07 04:33:22 +00:00
2018-11-06 22:59:48 +00:00
public ArtistService(IRoadieSettings configuration,
2018-11-06 21:55:31 +00:00
IHttpEncoder httpEncoder,
IHttpContext httpContext,
data.IRoadieDbContext context,
ICacheManager cacheManager,
2018-11-07 04:33:22 +00:00
ILogger<ArtistService> logger,
ICollectionService collectionService,
IPlaylistService playlistService,
IBookmarkService bookmarkService
)
2018-11-06 21:55:31 +00:00
: base(configuration, httpEncoder, context, cacheManager, logger, httpContext)
2018-11-06 04:31:25 +00:00
{
2018-11-07 04:33:22 +00:00
this.CollectionService = collectionService;
this.PlaylistService = playlistService;
this.BookmarkService = bookmarkService;
2018-11-06 04:31:25 +00:00
}
2018-11-14 03:19:28 +00:00
public async Task<OperationResult<Artist>> ById(User roadieUser, Guid id, IEnumerable<string> includes)
2018-11-06 04:31:25 +00:00
{
2018-12-05 16:04:21 +00:00
var timings = new Dictionary<string, long>();
var tsw = new Stopwatch();
2018-11-06 21:55:31 +00:00
var sw = Stopwatch.StartNew();
sw.Start();
var cacheKey = string.Format("urn:artist_by_id_operation:{0}:{1}", id, includes == null ? "0" : string.Join("|", includes));
var result = await this.CacheManager.GetAsync<OperationResult<Artist>>(cacheKey, async () =>
{
2018-12-05 16:04:21 +00:00
tsw.Restart();
var rr = await this.ArtistByIdAction(id, includes);
tsw.Stop();
timings.Add("ArtistByIdAction", tsw.ElapsedMilliseconds);
return rr;
2018-11-11 16:20:33 +00:00
}, data.Artist.CacheRegionUrn(id));
if (result?.Data != null && roadieUser != null)
2018-11-14 03:19:28 +00:00
{
2018-12-05 16:04:21 +00:00
tsw.Restart();
2018-11-14 03:19:28 +00:00
var artist = this.GetArtist(id);
2018-12-05 16:04:21 +00:00
tsw.Stop();
timings.Add("GetArtist", tsw.ElapsedMilliseconds);
tsw.Restart();
var userBookmarkResult = await this.BookmarkService.List(roadieUser, new PagedRequest(), false, BookmarkType.Artist);
2018-12-05 16:04:21 +00:00
if (userBookmarkResult.IsSuccess)
{
2018-12-04 23:26:27 +00:00
result.Data.UserBookmarked = userBookmarkResult?.Rows?.FirstOrDefault(x => x.Bookmark.Value == artist.RoadieId.ToString()) != null;
}
2018-12-05 16:04:21 +00:00
tsw.Stop();
timings.Add("userBookmarkResult", tsw.ElapsedMilliseconds);
tsw.Restart();
2018-11-14 03:19:28 +00:00
var userArtist = this.DbContext.UserArtists.FirstOrDefault(x => x.ArtistId == artist.Id && x.UserId == roadieUser.Id);
if (userArtist != null)
{
2018-11-14 14:00:08 +00:00
result.Data.UserRating = new UserArtist
{
IsDisliked = userArtist.IsDisliked ?? false,
IsFavorite = userArtist.IsFavorite ?? false,
Rating = userArtist.Rating
};
}
2018-12-05 16:04:21 +00:00
tsw.Stop();
timings.Add("userArtist", tsw.ElapsedMilliseconds);
2018-11-14 03:19:28 +00:00
}
sw.Stop();
2018-12-05 16:04:21 +00:00
timings.Add("operation", sw.ElapsedMilliseconds);
this.Logger.LogDebug("ById Timings: id [{0}], includes [{1}], timings [{3}]", id, includes, JsonConvert.SerializeObject(timings));
return new OperationResult<Artist>(result.Messages)
2018-11-06 21:55:31 +00:00
{
2018-11-11 01:11:58 +00:00
Data = result?.Data,
Errors = result?.Errors,
IsNotFoundResult = result?.IsNotFoundResult ?? false,
2018-11-11 01:11:58 +00:00
IsSuccess = result?.IsSuccess ?? false,
OperationTime = sw.ElapsedMilliseconds
};
}
2018-11-14 03:19:28 +00:00
private async Task<OperationResult<Artist>> ArtistByIdAction(Guid id, IEnumerable<string> includes)
{
2018-12-05 16:04:21 +00:00
var timings = new Dictionary<string, long>();
var tsw = new Stopwatch();
var sw = Stopwatch.StartNew();
sw.Start();
2018-11-06 21:55:31 +00:00
2018-12-05 16:04:21 +00:00
tsw.Restart();
2018-11-11 16:20:33 +00:00
var artist = this.GetArtist(id);
2018-12-05 16:04:21 +00:00
tsw.Stop();
timings.Add("getArtist", tsw.ElapsedMilliseconds);
2018-11-11 16:20:33 +00:00
2018-11-06 22:59:48 +00:00
if (artist == null)
{
2018-11-11 20:45:44 +00:00
return new OperationResult<Artist>(true, string.Format("Artist Not Found [{0}]", id));
2018-11-06 22:59:48 +00:00
}
2018-12-05 16:04:21 +00:00
tsw.Restart();
2018-11-06 22:59:48 +00:00
var result = artist.Adapt<Artist>();
2018-12-08 19:47:19 +00:00
result.BeginDate = result.BeginDate == null || result.BeginDate == DateTime.MinValue ? null : result.BeginDate;
result.EndDate = result.EndDate == null || result.EndDate == DateTime.MinValue ? null : result.EndDate;
result.BirthDate = result.BirthDate == null || result.BirthDate == DateTime.MinValue ? null : result.BirthDate;
2018-12-05 16:04:21 +00:00
tsw.Stop();
timings.Add("adaptArtist", tsw.ElapsedMilliseconds);
2018-11-06 22:59:48 +00:00
result.Thumbnail = base.MakeArtistThumbnailImage(id);
2018-12-03 04:12:47 +00:00
result.MediumThumbnail = base.MakeThumbnailImage(id, "artist", this.Configuration.MediumImageSize.Width, this.Configuration.MediumImageSize.Height);
2018-12-05 16:04:21 +00:00
tsw.Restart();
2018-11-07 04:33:22 +00:00
result.Genres = artist.Genres.Select(x => new DataToken { Text = x.Genre.Name, Value = x.Genre.RoadieId.ToString() });
2018-12-05 16:04:21 +00:00
tsw.Stop();
timings.Add("genres", tsw.ElapsedMilliseconds);
2018-11-06 22:59:48 +00:00
if (includes != null && includes.Any())
{
if (includes.Contains("releases"))
2018-11-06 21:55:31 +00:00
{
var dtoReleases = new List<ReleaseList>();
2018-11-06 22:59:48 +00:00
foreach (var release in this.DbContext.Releases.Include("Medias").Include("Medias.Tracks").Include("Medias.Tracks").Where(x => x.ArtistId == artist.Id).ToArray())
{
var releaseList = release.Adapt<ReleaseList>();
releaseList.Thumbnail = base.MakeReleaseThumbnailImage(release.RoadieId);
var dtoReleaseMedia = new List<ReleaseMediaList>();
if (includes.Contains("tracks"))
2018-11-06 21:55:31 +00:00
{
2018-11-06 22:59:48 +00:00
foreach (var releasemedia in release.Medias.OrderBy(x => x.MediaNumber).ToArray())
2018-11-06 21:55:31 +00:00
{
2018-11-06 22:59:48 +00:00
var dtoMedia = releasemedia.Adapt<ReleaseMediaList>();
var tracks = new List<TrackList>();
foreach (var t in this.DbContext.Tracks.Where(x => x.ReleaseMediaId == releasemedia.Id).OrderBy(x => x.TrackNumber).ToArray())
2018-11-06 21:55:31 +00:00
{
2018-11-06 22:59:48 +00:00
var track = t.Adapt<TrackList>();
2018-12-07 04:45:09 +00:00
ArtistList trackArtist = null;
2018-11-06 22:59:48 +00:00
if (t.ArtistId.HasValue)
2018-11-06 21:55:31 +00:00
{
2018-11-06 22:59:48 +00:00
var ta = this.DbContext.Artists.FirstOrDefault(x => x.Id == t.ArtistId.Value);
if (ta != null)
{
2018-12-07 04:45:09 +00:00
trackArtist = ArtistList.FromDataArtist(ta, this.MakeArtistThumbnailImage(ta.RoadieId));
2018-11-06 22:59:48 +00:00
}
2018-11-06 21:55:31 +00:00
}
2018-11-06 22:59:48 +00:00
track.TrackArtist = trackArtist;
tracks.Add(track);
2018-11-06 21:55:31 +00:00
}
2018-11-06 22:59:48 +00:00
dtoMedia.Tracks = tracks;
dtoReleaseMedia.Add(dtoMedia);
2018-11-06 21:55:31 +00:00
}
}
2018-11-06 22:59:48 +00:00
releaseList.Media = dtoReleaseMedia;
dtoReleases.Add(releaseList);
2018-11-06 21:55:31 +00:00
}
2018-11-06 22:59:48 +00:00
result.Releases = dtoReleases;
}
if (includes.Contains("stats"))
{
2018-12-05 16:04:21 +00:00
tsw.Restart();
2018-12-07 04:45:09 +00:00
// TODO this should be on artist properties to speed up fetch times
2018-11-06 22:59:48 +00:00
var artistTracks = (from r in this.DbContext.Releases
join rm in this.DbContext.ReleaseMedias on r.Id equals rm.ReleaseId
join t in this.DbContext.Tracks on rm.Id equals t.ReleaseMediaId
where r.ArtistId == artist.Id
select new
{
t.Id,
size = t.FileSize,
time = t.Duration,
isMissing = t.Hash == null
});
var validCartistTracks = artistTracks.Where(x => !x.isMissing);
var trackTime = validCartistTracks.Sum(x => x.time);
result.Statistics = new CollectionStatistics
2018-11-06 21:55:31 +00:00
{
2018-11-06 22:59:48 +00:00
FileSize = artistTracks.Sum(x => (long?)x.size).ToFileSize(),
MissingTrackCount = artistTracks.Where(x => x.isMissing).Count(),
2018-12-07 04:45:09 +00:00
ReleaseCount = artist.ReleaseCount,
2018-11-06 22:59:48 +00:00
ReleaseMediaCount = (from r in this.DbContext.Releases
join rm in this.DbContext.ReleaseMedias on r.Id equals rm.ReleaseId
where r.ArtistId == artist.Id
select rm.Id).Count(),
TrackTime = validCartistTracks.Any() ? TimeSpan.FromSeconds(Math.Floor((double)trackTime / 1000)).ToString(@"dd\:hh\:mm\:ss") : "--:--",
TrackCount = validCartistTracks.Count(),
2018-12-07 04:45:09 +00:00
TrackPlayedCount = artist.PlayedCount
2018-11-06 22:59:48 +00:00
};
2018-12-05 16:04:21 +00:00
tsw.Stop();
timings.Add("stats", tsw.ElapsedMilliseconds);
2018-11-06 22:59:48 +00:00
}
if (includes.Contains("images"))
{
2018-12-05 16:04:21 +00:00
tsw.Restart();
2018-12-04 23:26:27 +00:00
result.Images = this.DbContext.Images.Where(x => x.ArtistId == artist.Id).Select(x => MakeFullsizeImage(x.RoadieId, x.Caption)).ToArray();
2018-12-05 16:04:21 +00:00
tsw.Stop();
timings.Add("images", tsw.ElapsedMilliseconds);
2018-11-06 22:59:48 +00:00
}
if (includes.Contains("associatedartists"))
{
2018-12-05 16:04:21 +00:00
tsw.Restart();
2018-12-03 04:12:47 +00:00
var associatedWithArtists = (from aa in this.DbContext.ArtistAssociations
2018-11-06 22:59:48 +00:00
join a in this.DbContext.Artists on aa.AssociatedArtistId equals a.Id
where aa.ArtistId == artist.Id
2018-12-04 23:26:27 +00:00
select new ArtistList
{
DatabaseId = a.Id,
Id = a.RoadieId,
Artist = new DataToken
{
Text = a.Name,
Value = a.RoadieId.ToString()
},
Thumbnail = this.MakeArtistThumbnailImage(a.RoadieId),
Rating = a.Rating,
CreatedDate = a.CreatedDate,
LastUpdated = a.LastUpdated,
LastPlayed = a.LastPlayed,
PlayedCount = a.PlayedCount,
ReleaseCount = a.ReleaseCount,
TrackCount = a.TrackCount,
SortName = a.SortName
}).ToArray();
2018-11-06 22:59:48 +00:00
2018-12-03 04:12:47 +00:00
var associatedArtists = (from aa in this.DbContext.ArtistAssociations
join a in this.DbContext.Artists on aa.ArtistId equals a.Id
where aa.AssociatedArtistId == artist.Id
2018-12-04 23:26:27 +00:00
select new ArtistList
2018-12-03 04:12:47 +00:00
{
2018-12-04 23:26:27 +00:00
DatabaseId = a.Id,
Id = a.RoadieId,
Artist = new DataToken
{
Text = a.Name,
Value = a.RoadieId.ToString()
},
Thumbnail = this.MakeArtistThumbnailImage(a.RoadieId),
Rating = a.Rating,
CreatedDate = a.CreatedDate,
LastUpdated = a.LastUpdated,
LastPlayed = a.LastPlayed,
PlayedCount = a.PlayedCount,
ReleaseCount = a.ReleaseCount,
TrackCount = a.TrackCount,
SortName = a.SortName
2018-12-03 04:12:47 +00:00
}).ToArray();
2018-12-04 23:26:27 +00:00
result.AssociatedArtists = associatedArtists.Union(associatedWithArtists).OrderBy(x => x.SortName);
2018-12-05 16:04:21 +00:00
tsw.Stop();
timings.Add("associatedartists", tsw.ElapsedMilliseconds);
2018-12-03 04:12:47 +00:00
2018-11-06 22:59:48 +00:00
}
2018-11-07 04:33:22 +00:00
if (includes.Contains("collections"))
{
2018-12-05 16:04:21 +00:00
tsw.Restart();
2018-12-05 05:00:05 +00:00
var collectionPagedRequest = new PagedRequest
{
Limit = 100
};
2018-11-16 03:37:00 +00:00
var r = await this.CollectionService.List(roadieUser: null,
2018-12-05 05:00:05 +00:00
request: collectionPagedRequest, artistId: artist.RoadieId);
2018-11-07 04:33:22 +00:00
if (r.IsSuccess)
{
result.CollectionsWithArtistReleases = r.Rows.ToArray();
}
2018-12-05 16:04:21 +00:00
tsw.Stop();
timings.Add("collections", tsw.ElapsedMilliseconds);
2018-11-07 04:33:22 +00:00
}
if (includes.Contains("playlists"))
{
2018-12-05 16:04:21 +00:00
tsw.Restart();
2018-11-24 01:46:12 +00:00
var pg = new PagedRequest
{
FilterToArtistId = artist.RoadieId
};
var r = await this.PlaylistService.List(pg);
2018-11-07 04:33:22 +00:00
if (r.IsSuccess)
{
result.PlaylistsWithArtistReleases = r.Rows.ToArray();
}
2018-12-05 16:04:21 +00:00
tsw.Stop();
timings.Add("playlists", tsw.ElapsedMilliseconds);
2018-11-07 04:33:22 +00:00
}
2018-11-06 22:59:48 +00:00
if (includes.Contains("contributions"))
{
2018-12-05 16:04:21 +00:00
tsw.Restart();
2018-11-06 22:59:48 +00:00
result.ArtistContributionReleases = (from t in this.DbContext.Tracks
join rm in this.DbContext.ReleaseMedias on t.ReleaseMediaId equals rm.Id
2018-12-03 04:12:47 +00:00
join r in this.DbContext.Releases.Include(x => x.Artist) on rm.ReleaseId equals r.Id
2018-11-06 22:59:48 +00:00
where t.ArtistId == artist.Id
2018-12-03 04:12:47 +00:00
group r by r.Id into rr
select rr)
.ToArray()
.Select(rr => rr.First())
2018-12-07 21:02:38 +00:00
.Select(r => ReleaseList.FromDataRelease(r, r.Artist, this.HttpContext.BaseUrl, MakeArtistThumbnailImage(r.Artist.RoadieId), MakeReleaseThumbnailImage(r.RoadieId)))
.ToArray().OrderBy(x => x.Release.Text).ToArray();
result.ArtistContributionReleases = result.ArtistContributionReleases.Any() ? result.ArtistContributionReleases : null;
2018-12-05 16:04:21 +00:00
tsw.Stop();
timings.Add("contributions", tsw.ElapsedMilliseconds);
2018-11-06 22:59:48 +00:00
}
if (includes.Contains("labels"))
{
2018-12-05 16:04:21 +00:00
tsw.Restart();
2018-11-06 22:59:48 +00:00
result.ArtistLabels = (from l in this.DbContext.Labels
join rl in this.DbContext.ReleaseLabels on l.Id equals rl.LabelId
join r in this.DbContext.Releases on rl.ReleaseId equals r.Id
where r.ArtistId == artist.Id
orderby l.SortName
select new LabelList
{
2018-11-17 02:14:32 +00:00
Id = rl.RoadieId,
2018-11-06 22:59:48 +00:00
Label = new DataToken
2018-11-06 21:55:31 +00:00
{
2018-11-06 22:59:48 +00:00
Text = l.Name,
2018-11-07 04:33:22 +00:00
Value = l.RoadieId.ToString()
2018-11-06 22:59:48 +00:00
},
SortName = l.SortName,
CreatedDate = l.CreatedDate,
LastUpdated = l.LastUpdated,
2018-12-04 23:26:27 +00:00
ArtistCount = l.ArtistCount,
ReleaseCount = l.ReleaseCount,
TrackCount = l.TrackCount,
2018-11-06 22:59:48 +00:00
Thumbnail = MakeLabelThumbnailImage(l.RoadieId)
}).ToArray().GroupBy(x => x.Label.Value).Select(x => x.First()).OrderBy(x => x.SortName).ThenBy(x => x.Label.Text).ToArray();
result.ArtistLabels = result.ArtistLabels.Any() ? result.ArtistLabels : null;
2018-12-05 16:04:21 +00:00
tsw.Stop();
timings.Add("labels", tsw.ElapsedMilliseconds);
2018-11-06 21:55:31 +00:00
}
2018-11-06 22:59:48 +00:00
}
sw.Stop();
2018-12-05 16:04:21 +00:00
timings.Add("operation", sw.ElapsedMilliseconds);
this.Logger.LogDebug("ArtistByIdAction Timings: id [{0}], includes [{1}], timings [{3}]", id, includes, JsonConvert.SerializeObject(timings));
return new OperationResult<Artist>
2018-11-06 22:59:48 +00:00
{
Data = result,
IsSuccess = result != null,
OperationTime = sw.ElapsedMilliseconds
};
2018-11-06 04:31:25 +00:00
}
2018-12-01 18:05:24 +00:00
public async Task<Library.Models.Pagination.PagedResult<ArtistList>> List(User roadieUser, PagedRequest request, bool? doRandomize = false, bool? onlyIncludeWithReleases = true)
2018-11-06 04:31:25 +00:00
{
2018-11-14 14:00:08 +00:00
var sw = new Stopwatch();
sw.Start();
2018-11-21 18:19:38 +00:00
int[] favoriteArtistIds = new int[0];
if(request.FilterFavoriteOnly)
{
favoriteArtistIds = (from a in this.DbContext.Artists
join ua in this.DbContext.UserArtists on a.Id equals ua.ArtistId
where ua.IsFavorite ?? false
2018-12-01 18:05:24 +00:00
where (roadieUser == null || ua.UserId == roadieUser.Id)
2018-11-21 18:19:38 +00:00
select a.Id
).ToArray();
}
2018-12-09 23:30:55 +00:00
int[] labelArtistIds = new int[0];
if(request.FilterToLabelId.HasValue)
{
labelArtistIds = (from l in this.DbContext.Labels
join rl in this.DbContext.ReleaseLabels on l.Id equals rl.LabelId
join r in this.DbContext.Releases on rl.ReleaseId equals r.Id
where l.RoadieId == request.FilterToLabelId
select r.ArtistId)
.Distinct()
.ToArray();
}
var onlyWithReleases = onlyIncludeWithReleases ?? true;
2018-11-14 14:00:08 +00:00
var result = (from a in this.DbContext.Artists
where (!onlyWithReleases || a.ReleaseCount > 0)
2018-11-22 13:48:32 +00:00
where (request.FilterToArtistId == null || a.RoadieId == request.FilterToArtistId)
where (request.FilterMinimumRating == null || a.Rating >= request.FilterMinimumRating.Value)
where (request.FilterValue == "" || (a.Name.Contains(request.FilterValue) || a.SortName.Contains(request.FilterValue) || a.AlternateNames.Contains(request.FilterValue)))
2018-11-21 18:19:38 +00:00
where (!request.FilterFavoriteOnly || favoriteArtistIds.Contains(a.Id))
2018-12-09 23:30:55 +00:00
where (request.FilterToLabelId == null || labelArtistIds.Contains(a.Id))
select new ArtistList
{
DatabaseId = a.Id,
Id = a.RoadieId,
Artist = new DataToken
{
Text = a.Name,
Value = a.RoadieId.ToString()
},
Thumbnail = this.MakeArtistThumbnailImage(a.RoadieId),
Rating = a.Rating,
CreatedDate = a.CreatedDate,
LastUpdated = a.LastUpdated,
2018-12-01 18:05:24 +00:00
LastPlayed = a.LastPlayed,
PlayedCount = a.PlayedCount,
ReleaseCount = a.ReleaseCount,
TrackCount = a.TrackCount,
SortName = a.SortName
}).Distinct();
2018-11-06 04:31:25 +00:00
2018-11-14 14:00:08 +00:00
ArtistList[] rows = null;
var rowCount = result.Count();
if (doRandomize ?? false)
{
2018-12-01 18:05:24 +00:00
var randomLimit = roadieUser?.RandomReleaseLimit ?? 100;
request.Limit = request.LimitValue > randomLimit ? randomLimit : request.LimitValue;
2018-11-14 14:00:08 +00:00
rows = result.OrderBy(x => Guid.NewGuid()).Skip(request.SkipValue).Take(request.LimitValue).ToArray();
}
else
{
2018-11-22 23:12:57 +00:00
string sortBy = "Id";
2018-11-14 14:00:08 +00:00
if (request.ActionValue == User.ActionKeyUserRated)
{
sortBy = string.IsNullOrEmpty(request.Sort) ? request.OrderValue(new Dictionary<string, string> { { "Rating", "DESC" }, { "Artist.Text", "ASC" } }) : request.OrderValue(null);
}
else
{
2018-12-07 04:45:09 +00:00
sortBy = request.OrderValue(new Dictionary<string, string> { { "SortName", "ASC" }, { "Artist.Text", "ASC" } });
2018-11-14 14:00:08 +00:00
}
rows = result.OrderBy(sortBy).Skip(request.SkipValue).Take(request.LimitValue).ToArray();
}
if (rows.Any() && roadieUser != null)
2018-11-14 14:00:08 +00:00
{
2018-12-08 19:47:19 +00:00
var rowIds = rows.Select(x => x.DatabaseId).ToArray();
var userArtistRatings = (from ua in this.DbContext.UserArtists
where ua.UserId == roadieUser.Id
where rowIds.Contains(ua.ArtistId)
select ua).ToArray();
foreach (var userArtistRating in userArtistRatings.Where(x => rows.Select(r => r.DatabaseId).Contains(x.ArtistId)))
2018-11-14 14:00:08 +00:00
{
var row = rows.FirstOrDefault(x => x.DatabaseId == userArtistRating.ArtistId);
if (row != null)
2018-11-14 14:00:08 +00:00
{
row.UserRating = new UserArtist
{
IsDisliked = userArtistRating.IsDisliked ?? false,
IsFavorite = userArtistRating.IsFavorite ?? false,
2018-11-25 20:43:52 +00:00
Rating = userArtistRating.Rating,
RatedDate = userArtistRating.LastUpdated ?? userArtistRating.CreatedDate
2018-11-14 14:00:08 +00:00
};
}
}
}
sw.Stop();
return new Library.Models.Pagination.PagedResult<ArtistList>
{
TotalCount = rowCount,
CurrentPage = request.PageValue,
TotalPages = (int)Math.Ceiling((double)rowCount / request.LimitValue),
OperationTime = sw.ElapsedMilliseconds,
Rows = rows
};
2018-11-06 04:31:25 +00:00
}
}
}