private Task <OperationResult <Playlist> > PlaylistByIdAction(Guid id, IEnumerable <string> includes = null) { var sw = Stopwatch.StartNew(); sw.Start(); var playlist = this.GetPlaylist(id); if (playlist == null) { return(Task.FromResult(new OperationResult <Playlist>(true, string.Format("Playlist Not Found [{0}]", id)))); } var result = playlist.Adapt <Playlist>(); result.AlternateNames = playlist.AlternateNames; result.Tags = playlist.Tags; result.URLs = playlist.URLs; var maintainer = this.DbContext.Users.Include(x => x.UserRoles).Include("UserRoles.Role").FirstOrDefault(x => x.Id == playlist.UserId); result.Maintainer = UserList.FromDataUser(maintainer, this.MakeUserThumbnailImage(maintainer.RoadieId)); result.Thumbnail = this.MakePlaylistThumbnailImage(playlist.RoadieId); result.MediumThumbnail = base.MakeThumbnailImage(id, "playlist", this.Configuration.MediumImageSize.Width, this.Configuration.MediumImageSize.Height); if (includes != null && includes.Any()) { var playlistTracks = (from pl in this.DbContext.Playlists join pltr in this.DbContext.PlaylistTracks on pl.Id equals pltr.PlayListId join t in this.DbContext.Tracks on pltr.TrackId equals t.Id where pl.Id == playlist.Id select new { t, pltr }).ToArray(); if (includes.Contains("stats")) { result.Statistics = new Library.Models.Statistics.ReleaseGroupingStatistics { ReleaseCount = result.ReleaseCount, TrackCount = result.TrackCount, TrackSize = result.DurationTime, FileSize = playlistTracks.Sum(x => (long?)x.t.FileSize).ToFileSize() }; } if (includes.Contains("tracks")) { result.Tracks = (from plt in playlistTracks join rm in this.DbContext.ReleaseMedias on plt.t.ReleaseMediaId equals rm.Id join r in this.DbContext.Releases on rm.ReleaseId equals r.Id join releaseArtist in this.DbContext.Artists on r.ArtistId equals releaseArtist.Id join trackArtist in this.DbContext.Artists on plt.t.ArtistId equals trackArtist.Id into tas from trackArtist in tas.DefaultIfEmpty() select new PlaylistTrack { ListNumber = plt.pltr.ListNumber, Track = TrackList.FromDataTrack(null, plt.t, rm.MediaNumber, r, releaseArtist, trackArtist, this.HttpContext.BaseUrl, this.MakeTrackThumbnailImage(plt.t.RoadieId), this.MakeReleaseThumbnailImage(r.RoadieId), this.MakeArtistThumbnailImage(releaseArtist.RoadieId), this.MakeArtistThumbnailImage(trackArtist == null ? null : (Guid?)trackArtist.RoadieId)) }).ToArray(); } } sw.Stop(); return(Task.FromResult(new OperationResult <Playlist> { Data = result, IsSuccess = result != null, OperationTime = sw.ElapsedMilliseconds })); }
private async Task <OperationResult <Label> > LabelByIdAction(Guid id, IEnumerable <string> includes = null) { var timings = new Dictionary <string, long>(); var tsw = new Stopwatch(); var sw = Stopwatch.StartNew(); sw.Start(); tsw.Restart(); var label = await GetLabel(id); tsw.Stop(); timings.Add("GetLabel", tsw.ElapsedMilliseconds); if (label == null) { return(new OperationResult <Label>(true, string.Format("Label Not Found [{0}]", id))); } tsw.Restart(); var result = label.Adapt <Label>(); result.AlternateNames = label.AlternateNames; result.Tags = label.Tags; result.URLs = label.URLs; result.Thumbnail = ImageHelper.MakeLabelThumbnailImage(Configuration, HttpContext, label.RoadieId); result.MediumThumbnail = ImageHelper.MakeThumbnailImage(Configuration, HttpContext, id, "label", Configuration.MediumImageSize.Width, Configuration.MediumImageSize.Height); tsw.Stop(); timings.Add("adapt", tsw.ElapsedMilliseconds); if (includes != null && includes.Any()) { if (includes.Contains("stats")) { tsw.Restart(); var labelTracks = from l in DbContext.Labels join rl in DbContext.ReleaseLabels on l.Id equals rl.LabelId into rld from rl in rld.DefaultIfEmpty() join r in DbContext.Releases on rl.ReleaseId equals r.Id join rm in DbContext.ReleaseMedias on r.Id equals rm.ReleaseId join t in DbContext.Tracks on rm.Id equals t.ReleaseMediaId where l.Id == label.Id select new { t.Duration, t.FileSize }; result.Duration = labelTracks.Sum(x => x.Duration); result.Statistics = new ReleaseGroupingStatistics { TrackCount = label.TrackCount, ArtistCount = label.ArtistCount, ReleaseCount = label.ReleaseCount, TrackSize = result.DurationTime, FileSize = labelTracks.Sum(x => (long?)x.FileSize).ToFileSize() }; tsw.Stop(); timings.Add("stats", tsw.ElapsedMilliseconds); } if (includes.Contains("comments")) { tsw.Restart(); var labelComments = DbContext.Comments.Include(x => x.User) .Where(x => x.LabelId == label.Id) .OrderByDescending(x => x.CreatedDate) .ToArray(); if (labelComments.Any()) { var comments = new List <Comment>(); var commentIds = labelComments.Select(x => x.Id).ToArray(); var userCommentReactions = (from cr in DbContext.CommentReactions where commentIds.Contains(cr.CommentId) select cr).ToArray(); foreach (var labelComment in labelComments) { var comment = labelComment.Adapt <Comment>(); comment.DatabaseId = labelComment.Id; comment.User = UserList.FromDataUser(labelComment.User, ImageHelper.MakeUserThumbnailImage(Configuration, HttpContext, labelComment.User.RoadieId)); comment.DislikedCount = userCommentReactions.Count(x => x.CommentId == labelComment.Id && x.ReactionValue == CommentReaction.Dislike); comment.LikedCount = userCommentReactions.Count(x => x.CommentId == labelComment.Id && x.ReactionValue == CommentReaction.Like); comments.Add(comment); } result.Comments = comments; } tsw.Stop(); timings.Add("comments", tsw.ElapsedMilliseconds); } } sw.Stop(); Logger.LogInformation($"ByIdAction: Label `{ label }`: includes [{includes.ToCSV()}], timings: [{ timings.ToTimings() }]"); return(new OperationResult <Label> { Data = result, IsSuccess = result != null, OperationTime = sw.ElapsedMilliseconds }); }
private Task <OperationResult <Track> > TrackByIdAction(Guid id, IEnumerable <string> includes) { var sw = Stopwatch.StartNew(); sw.Start(); var track = GetTrack(id); if (track == null) { return(Task.FromResult(new OperationResult <Track>(true, string.Format("Track Not Found [{0}]", id)))); } var result = track.Adapt <Track>(); result.IsLocked = (track.IsLocked ?? false) || (track.ReleaseMedia.IsLocked ?? false) || (track.ReleaseMedia.Release.IsLocked ?? false) || (track.ReleaseMedia.Release.Artist.IsLocked ?? false); result.Thumbnail = MakeTrackThumbnailImage(id); result.MediumThumbnail = MakeThumbnailImage(id, "track", Configuration.MediumImageSize.Width, Configuration.MediumImageSize.Height); result.ReleaseMediaId = track.ReleaseMedia.RoadieId.ToString(); result.Artist = ArtistList.FromDataArtist(track.ReleaseMedia.Release.Artist, MakeArtistThumbnailImage(track.ReleaseMedia.Release.Artist.RoadieId)); result.ArtistThumbnail = MakeArtistThumbnailImage(track.ReleaseMedia.Release.Artist.RoadieId); result.Release = ReleaseList.FromDataRelease(track.ReleaseMedia.Release, track.ReleaseMedia.Release.Artist, HttpContext.BaseUrl, MakeArtistThumbnailImage(track.ReleaseMedia.Release.Artist.RoadieId), MakeReleaseThumbnailImage(track.ReleaseMedia.Release.RoadieId)); result.ReleaseThumbnail = MakeReleaseThumbnailImage(track.ReleaseMedia.Release.RoadieId); if (track.ArtistId.HasValue) { var trackArtist = DbContext.Artists.FirstOrDefault(x => x.Id == track.ArtistId); if (trackArtist == null) { Logger.LogWarning($"Unable to find Track Artist [{track.ArtistId}"); } else { result.TrackArtist = ArtistList.FromDataArtist(trackArtist, MakeArtistThumbnailImage(trackArtist.RoadieId)); result.TrackArtistToken = result.TrackArtist.Artist; result.TrackArtistThumbnail = MakeArtistThumbnailImage(trackArtist.RoadieId); } } if (includes != null && includes.Any()) { if (includes.Contains("stats")) { result.Statistics = new TrackStatistics { FileSizeFormatted = ((long?)track.FileSize).ToFileSize(), Time = new TimeInfo((decimal)track.Duration).ToFullFormattedString(), PlayedCount = track.PlayedCount }; var userTracks = (from t in DbContext.Tracks join ut in DbContext.UserTracks on t.Id equals ut.TrackId where t.Id == track.Id select ut).ToArray(); if (userTracks != null && userTracks.Any()) { result.Statistics.DislikedCount = userTracks.Count(x => x.IsDisliked ?? false); result.Statistics.FavoriteCount = userTracks.Count(x => x.IsFavorite ?? false); } } if (includes.Contains("comments")) { var trackComments = DbContext.Comments.Include(x => x.User).Where(x => x.TrackId == track.Id) .OrderByDescending(x => x.CreatedDate).ToArray(); if (trackComments.Any()) { var comments = new List <Comment>(); var commentIds = trackComments.Select(x => x.Id).ToArray(); var userCommentReactions = (from cr in DbContext.CommentReactions where commentIds.Contains(cr.CommentId) select cr).ToArray(); foreach (var trackComment in trackComments) { var comment = trackComment.Adapt <Comment>(); comment.DatabaseId = trackComment.Id; comment.User = UserList.FromDataUser(trackComment.User, MakeUserThumbnailImage(trackComment.User.RoadieId)); comment.DislikedCount = userCommentReactions.Count(x => x.CommentId == trackComment.Id && x.ReactionValue == CommentReaction.Dislike); comment.LikedCount = userCommentReactions.Count(x => x.CommentId == trackComment.Id && x.ReactionValue == CommentReaction.Like); comments.Add(comment); } result.Comments = comments; } } } sw.Stop(); return(Task.FromResult(new OperationResult <Track> { Data = result, IsSuccess = result != null, OperationTime = sw.ElapsedMilliseconds })); }
private async Task <OperationResult <Playlist> > PlaylistByIdAction(Guid id, IEnumerable <string> includes = null) { var timings = new Dictionary <string, long>(); var tsw = new Stopwatch(); var sw = Stopwatch.StartNew(); sw.Start(); tsw.Restart(); var playlist = await GetPlaylist(id); tsw.Stop(); timings.Add("getPlaylist", tsw.ElapsedMilliseconds); if (playlist == null) { return(new OperationResult <Playlist>(true, string.Format("Playlist Not Found [{0}]", id))); } tsw.Restart(); var result = playlist.Adapt <Playlist>(); result.AlternateNames = playlist.AlternateNames; result.Tags = playlist.Tags; result.URLs = playlist.URLs; var maintainer = DbContext.Users.Include(x => x.UserRoles).Include("UserRoles.Role").FirstOrDefault(x => x.Id == playlist.UserId); result.Maintainer = UserList.FromDataUser(maintainer, ImageHelper.MakeUserThumbnailImage(Configuration, HttpContext, maintainer.RoadieId)); result.Thumbnail = ImageHelper.MakePlaylistThumbnailImage(Configuration, HttpContext, playlist.RoadieId); result.MediumThumbnail = ImageHelper.MakeThumbnailImage(Configuration, HttpContext, id, "playlist", Configuration.MediumImageSize.Width, Configuration.MediumImageSize.Height); tsw.Stop(); timings.Add("adapt", tsw.ElapsedMilliseconds); if (includes != null && includes.Any()) { var playlistTracks = (from pl in DbContext.Playlists join pltr in DbContext.PlaylistTracks on pl.Id equals pltr.PlayListId join t in DbContext.Tracks on pltr.TrackId equals t.Id where pl.Id == playlist.Id select new { t, pltr }).ToArray(); if (includes.Contains("stats")) { tsw.Restart(); result.Statistics = new ReleaseGroupingStatistics { ReleaseCount = result.ReleaseCount, TrackCount = result.TrackCount, TrackSize = result.DurationTime, FileSize = playlistTracks.Sum(x => (long?)x.t.FileSize).ToFileSize() }; tsw.Stop(); timings.Add("stats", tsw.ElapsedMilliseconds); } if (includes.Contains("tracks")) { tsw.Restart(); result.Tracks = (from plt in playlistTracks join rm in DbContext.ReleaseMedias on plt.t.ReleaseMediaId equals rm.Id join r in DbContext.Releases on rm.ReleaseId equals r.Id join releaseArtist in DbContext.Artists on r.ArtistId equals releaseArtist.Id join trackArtist in DbContext.Artists on plt.t.ArtistId equals trackArtist.Id into tas from trackArtist in tas.DefaultIfEmpty() select new PlaylistTrack { ListNumber = plt.pltr.ListNumber, Track = TrackList.FromDataTrack(null, plt.t, rm.MediaNumber, r, releaseArtist, trackArtist, HttpContext.BaseUrl, ImageHelper.MakeTrackThumbnailImage(Configuration, HttpContext, plt.t.RoadieId), ImageHelper.MakeReleaseThumbnailImage(Configuration, HttpContext, r.RoadieId), ImageHelper.MakeArtistThumbnailImage(Configuration, HttpContext, releaseArtist.RoadieId), ImageHelper.MakeArtistThumbnailImage(Configuration, HttpContext, trackArtist == null ? null : (Guid?)trackArtist.RoadieId)) }).ToArray(); tsw.Stop(); timings.Add("tracks", tsw.ElapsedMilliseconds); } if (includes.Contains("comments")) { tsw.Restart(); var playlistComments = DbContext.Comments.Include(x => x.User) .Where(x => x.PlaylistId == playlist.Id) .OrderByDescending(x => x.CreatedDate) .ToArray(); if (playlistComments.Any()) { var comments = new List <Comment>(); var commentIds = playlistComments.Select(x => x.Id).ToArray(); var userCommentReactions = (from cr in DbContext.CommentReactions where commentIds.Contains(cr.CommentId) select cr).ToArray(); foreach (var playlistComment in playlistComments) { var comment = playlistComment.Adapt <Comment>(); comment.DatabaseId = playlistComment.Id; comment.User = UserList.FromDataUser(playlistComment.User, ImageHelper.MakeUserThumbnailImage(Configuration, HttpContext, playlistComment.User.RoadieId)); comment.DislikedCount = userCommentReactions.Count(x => x.CommentId == playlistComment.Id && x.ReactionValue == CommentReaction.Dislike); comment.LikedCount = userCommentReactions.Count(x => x.CommentId == playlistComment.Id && x.ReactionValue == CommentReaction.Like); comments.Add(comment); } result.Comments = comments; } tsw.Stop(); timings.Add("comments", tsw.ElapsedMilliseconds); } } sw.Stop(); Logger.LogInformation($"ByIdAction: Playlist `{ playlist }`: includes [{includes.ToCSV()}], timings: [{ timings.ToTimings() }]"); return(new OperationResult <Playlist> { Data = result, IsSuccess = result != null, OperationTime = sw.ElapsedMilliseconds }); }
private async Task <OperationResult <Collection> > CollectionByIdAction(Guid id, IEnumerable <string> includes = null) { var timings = new Dictionary <string, long>(); var tsw = new Stopwatch(); var sw = Stopwatch.StartNew(); sw.Start(); tsw.Restart(); var collection = await GetCollection(id); tsw.Stop(); timings.Add("getCollection", tsw.ElapsedMilliseconds); if (collection == null) { return(new OperationResult <Collection>(true, string.Format("Collection Not Found [{0}]", id))); } var result = collection.Adapt <Collection>(); var maintainer = DbContext.Users.FirstOrDefault(x => x.Id == collection.MaintainerId); result.Maintainer = new DataToken { Text = maintainer.UserName, Value = maintainer.RoadieId.ToString() }; result.AlternateNames = collection.AlternateNames; result.Tags = collection.Tags; result.URLs = collection.URLs; result.Thumbnail = ImageHelper.MakeCollectionThumbnailImage(Configuration, HttpContext, collection.RoadieId); result.MediumThumbnail = ImageHelper.MakeThumbnailImage(Configuration, HttpContext, id, "collection", Configuration.MediumImageSize.Width, Configuration.MediumImageSize.Height); result.CollectionFoundCount = (from crc in DbContext.CollectionReleases where crc.CollectionId == collection.Id select crc.Id).Count(); tsw.Stop(); timings.Add("adapt", tsw.ElapsedMilliseconds); if (includes != null && includes.Any()) { if (includes.Contains("list")) { result.ListInCSVFormat = collection.ListInCSVFormat; result.ListInCSV = collection.ListInCSV; } else { result.ListInCSV = null; result.ListInCSVFormat = null; } if (includes.Contains("releases")) { tsw.Restart(); result.Releases = (from crc in DbContext.CollectionReleases join r in DbContext.Releases.Include(x => x.Artist) on crc.ReleaseId equals r.Id where crc.CollectionId == collection.Id orderby crc.ListNumber select new CollectionRelease { ListNumber = crc.ListNumber, Release = ReleaseList.FromDataRelease(r, r.Artist, HttpContext.BaseUrl, ImageHelper.MakeArtistThumbnailImage(Configuration, HttpContext, r.Artist.RoadieId), ImageHelper.MakeReleaseThumbnailImage(Configuration, HttpContext, r.RoadieId)) }).ToArray(); tsw.Stop(); timings.Add("releases", tsw.ElapsedMilliseconds); } if (includes.Contains("stats")) { tsw.Restart(); var collectionReleases = from crc in DbContext.CollectionReleases join r in DbContext.Releases on crc.ReleaseId equals r.Id where crc.CollectionId == collection.Id select r; var collectionTracks = from crc in DbContext.CollectionReleases join r in DbContext.Releases on crc.ReleaseId equals r.Id join rm in DbContext.ReleaseMedias on r.Id equals rm.ReleaseId join t in DbContext.Tracks on rm.Id equals t.ReleaseMediaId where crc.CollectionId == collection.Id select t; result.Statistics = new CollectionStatistics { ArtistCount = collectionReleases.Select(x => x.ArtistId).Distinct().Count(), FileSize = collectionTracks.Sum(x => (long?)x.FileSize).ToFileSize(), MissingTrackCount = collectionTracks.Count(x => x.Hash != null), ReleaseCount = collectionReleases.Count(), ReleaseMediaCount = collectionReleases.Sum(x => x.MediaCount), Duration = collectionReleases.Sum(x => (long?)x.Duration), TrackCount = collectionReleases.Sum(x => x.TrackCount), TrackPlayedCount = collectionReleases.Sum(x => x.PlayedCount) }; tsw.Stop(); timings.Add("stats", tsw.ElapsedMilliseconds); } if (includes.Contains("comments")) { tsw.Restart(); var collectionComments = DbContext.Comments.Include(x => x.User) .Where(x => x.CollectionId == collection.Id) .OrderByDescending(x => x.CreatedDate) .ToArray(); if (collectionComments.Any()) { var comments = new List <Comment>(); var commentIds = collectionComments.Select(x => x.Id).ToArray(); var userCommentReactions = (from cr in DbContext.CommentReactions where commentIds.Contains(cr.CommentId) select cr).ToArray(); foreach (var collectionComment in collectionComments) { var comment = collectionComment.Adapt <Comment>(); comment.DatabaseId = collectionComment.Id; comment.User = UserList.FromDataUser(collectionComment.User, ImageHelper.MakeUserThumbnailImage(Configuration, HttpContext, collectionComment.User.RoadieId)); comment.DislikedCount = userCommentReactions.Count(x => x.CommentId == collectionComment.Id && x.ReactionValue == CommentReaction.Dislike); comment.LikedCount = userCommentReactions.Count(x => x.CommentId == collectionComment.Id && x.ReactionValue == CommentReaction.Like); comments.Add(comment); } result.Comments = comments; } tsw.Stop(); timings.Add("comments", tsw.ElapsedMilliseconds); } } Logger.LogInformation($"ByIdAction: Collection `{ collection }`: includes [{includes.ToCSV()}], timings: [{ timings.ToTimings() }]"); sw.Stop(); return(new OperationResult <Collection> { Data = result, IsSuccess = result != null, OperationTime = sw.ElapsedMilliseconds }); }