Пример #1
0
 public static TrackList FromDataTrack(string trackPlayUrl,
                                       Data.Track track,
                                       int releaseMediaNumber,
                                       Release release,
                                       Data.Artist artist,
                                       Data.Artist trackArtist,
                                       string baseUrl,
                                       Image trackThumbnail,
                                       Image releaseThumbnail,
                                       Image artistThumbnail,
                                       Image trackArtistThumbnail)
 {
     return(new TrackList
     {
         DatabaseId = track.Id,
         Id = track.RoadieId,
         Track = new DataToken
         {
             Text = track.Title,
             Value = track.RoadieId.ToString()
         },
         Release = ReleaseList.FromDataRelease(release, artist, baseUrl, artistThumbnail, releaseThumbnail),
         LastPlayed = track.LastPlayed,
         Artist = ArtistList.FromDataArtist(artist, artistThumbnail),
         TrackArtist = trackArtist == null ? null : ArtistList.FromDataArtist(trackArtist, trackArtistThumbnail),
         TrackNumber = track.TrackNumber,
         MediaNumber = releaseMediaNumber,
         CreatedDate = track.CreatedDate,
         LastUpdated = track.LastUpdated,
         Duration = track.Duration,
         FileSize = track.FileSize,
         ReleaseDate = release.ReleaseDate,
         PlayedCount = track.PlayedCount,
         Rating = track.Rating,
         Status = track.Status,
         Title = track.Title,
         TrackPlayUrl = trackPlayUrl,
         Thumbnail = trackThumbnail
     });
 }
Пример #2
0
        public async Task <Library.Models.Pagination.PagedResult <models.BookmarkList> > List(User roadieUser, PagedRequest request, bool?doRandomize = false, BookmarkType?filterType = null)
        {
            var sw = new Stopwatch();

            sw.Start();
            var result = from b in DbContext.Bookmarks
                         join u in DbContext.Users on b.UserId equals u.Id
                         where b.UserId == roadieUser.Id
                         where filterType == null || b.BookmarkType == filterType
                         select new models.BookmarkList
            {
                Comment  = b.Comment,
                Position = b.Position,
                User     = new models.DataToken
                {
                    Text  = u.UserName,
                    Value = u.RoadieId.ToString()
                },
                DatabaseId       = b.Id,
                Id               = b.RoadieId,
                CreatedDate      = b.CreatedDate,
                LastUpdated      = b.LastUpdated,
                Type             = b.BookmarkType,
                BookmarkTargetId = b.BookmarkTargetId
            };

            var sortBy = string.IsNullOrEmpty(request.Sort)
                ? request.OrderValue(new Dictionary <string, string> {
                { "CreatedDate", "DESC" }
            })
                : request.OrderValue();
            var rowCount = result.Count();
            var rows     = result.OrderBy(sortBy).Skip(request.SkipValue).Take(request.LimitValue).ToArray();

            var user = await GetUser(roadieUser.UserId);

            foreach (var row in rows)
            {
                switch (row.Type)
                {
                case BookmarkType.Artist:
                    var artist = DbContext.Artists.FirstOrDefault(x => x.Id == row.BookmarkTargetId);
                    if (artist == null)
                    {
                        continue;
                    }
                    row.Bookmark = new models.DataToken
                    {
                        Text  = artist.Name,
                        Value = artist.RoadieId.ToString()
                    };
                    row.Artist =
                        models.ArtistList.FromDataArtist(artist, ImageHelper.MakeArtistThumbnailImage(Configuration, HttpContext, artist.RoadieId));
                    row.Thumbnail = ImageHelper.MakeArtistThumbnailImage(Configuration, HttpContext, artist.RoadieId);
                    row.SortName  = artist.SortName ?? artist.Name;
                    break;

                case BookmarkType.Release:
                    var release = DbContext.Releases.Include(x => x.Artist)
                                  .FirstOrDefault(x => x.Id == row.BookmarkTargetId);
                    if (release == null)
                    {
                        continue;
                    }
                    row.Bookmark = new models.DataToken
                    {
                        Text  = release.Title,
                        Value = release.RoadieId.ToString()
                    };
                    row.Release = ReleaseList.FromDataRelease(release, release.Artist, HttpContext.BaseUrl,
                                                              ImageHelper.MakeArtistThumbnailImage(Configuration, HttpContext, release.Artist.RoadieId),
                                                              ImageHelper.MakeReleaseThumbnailImage(Configuration, HttpContext, release.RoadieId));
                    row.Thumbnail = ImageHelper.MakeReleaseThumbnailImage(Configuration, HttpContext, release.RoadieId);
                    row.SortName  = release.SortTitleValue;
                    break;

                case BookmarkType.Track:
                    var track = DbContext.Tracks
                                .Include(x => x.ReleaseMedia)
                                .Include(x => x.ReleaseMedia.Release)
                                .Include(x => x.ReleaseMedia.Release.Artist)
                                .Include(x => x.TrackArtist)
                                .FirstOrDefault(x => x.Id == row.BookmarkTargetId);
                    if (track == null)
                    {
                        continue;
                    }
                    row.Bookmark = new models.DataToken
                    {
                        Text  = track.Title,
                        Value = track.RoadieId.ToString()
                    };
                    row.Track = models.TrackList.FromDataTrack(MakeTrackPlayUrl(user, HttpContext.BaseUrl, track.RoadieId),
                                                               track,
                                                               track.ReleaseMedia.MediaNumber,
                                                               track.ReleaseMedia.Release,
                                                               track.ReleaseMedia.Release.Artist,
                                                               track.TrackArtist,
                                                               HttpContext.BaseUrl,
                                                               ImageHelper.MakeTrackThumbnailImage(Configuration, HttpContext, track.RoadieId),
                                                               ImageHelper.MakeReleaseThumbnailImage(Configuration, HttpContext, track.ReleaseMedia.Release.RoadieId),
                                                               ImageHelper.MakeArtistThumbnailImage(Configuration, HttpContext, track.ReleaseMedia.Release.Artist.RoadieId),
                                                               ImageHelper.MakeArtistThumbnailImage(Configuration, HttpContext, track.TrackArtist == null
                                ? null
                                : (Guid?)track.TrackArtist.RoadieId));
                    row.Track.TrackPlayUrl = MakeTrackPlayUrl(user, HttpContext.BaseUrl, track.RoadieId);
                    row.Thumbnail          = ImageHelper.MakeTrackThumbnailImage(Configuration, HttpContext, track.RoadieId);
                    row.SortName           = track.Title;
                    break;

                case BookmarkType.Playlist:
                    var playlist = DbContext.Playlists
                                   .Include(x => x.User)
                                   .FirstOrDefault(x => x.Id == row.BookmarkTargetId);
                    if (playlist == null)
                    {
                        continue;
                    }
                    row.Bookmark = new models.DataToken
                    {
                        Text  = playlist.Name,
                        Value = playlist.RoadieId.ToString()
                    };
                    row.Playlist = PlaylistList.FromDataPlaylist(playlist, playlist.User,
                                                                 ImageHelper.MakePlaylistThumbnailImage(Configuration, HttpContext, playlist.RoadieId),
                                                                 ImageHelper.MakeUserThumbnailImage(Configuration, HttpContext, playlist.User.RoadieId));
                    row.Thumbnail = ImageHelper.MakePlaylistThumbnailImage(Configuration, HttpContext, playlist.RoadieId);
                    row.SortName  = playlist.Name;
                    break;

                case BookmarkType.Collection:
                    var collection = DbContext.Collections.FirstOrDefault(x => x.Id == row.BookmarkTargetId);
                    if (collection == null)
                    {
                        continue;
                    }
                    row.Bookmark = new models.DataToken
                    {
                        Text  = collection.Name,
                        Value = collection.RoadieId.ToString()
                    };
                    row.Collection = CollectionList.FromDataCollection(collection,
                                                                       (from crc in DbContext.CollectionReleases
                                                                        where crc.CollectionId == collection.Id
                                                                        select crc.Id).Count(), ImageHelper.MakeCollectionThumbnailImage(Configuration, HttpContext, collection.RoadieId));
                    row.Thumbnail = ImageHelper.MakeCollectionThumbnailImage(Configuration, HttpContext, collection.RoadieId);
                    row.SortName  = collection.SortName ?? collection.Name;
                    break;

                case BookmarkType.Label:
                    var label = DbContext.Labels.FirstOrDefault(x => x.Id == row.BookmarkTargetId);
                    if (label == null)
                    {
                        continue;
                    }
                    row.Bookmark = new models.DataToken
                    {
                        Text  = label.Name,
                        Value = label.RoadieId.ToString()
                    };
                    row.Label     = models.LabelList.FromDataLabel(label, ImageHelper.MakeLabelThumbnailImage(Configuration, HttpContext, label.RoadieId));
                    row.Thumbnail = ImageHelper.MakeLabelThumbnailImage(Configuration, HttpContext, label.RoadieId);
                    row.SortName  = label.SortName ?? label.Name;
                    break;
                }
            }

            ;
            sw.Stop();
            return(new Library.Models.Pagination.PagedResult <models.BookmarkList>
            {
                TotalCount = rowCount,
                CurrentPage = request.PageValue,
                TotalPages = (int)Math.Ceiling((double)rowCount / request.LimitValue),
                OperationTime = sw.ElapsedMilliseconds,
                Rows = rows
            });
        }
Пример #3
0
        private Task <OperationResult <Track> > TrackByIdAction(Guid id, IEnumerable <string> includes)
        {
            var sw = Stopwatch.StartNew();

            sw.Start();

            var track = this.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        = base.MakeTrackThumbnailImage(id);
            result.MediumThumbnail  = base.MakeThumbnailImage(id, "track", this.Configuration.MediumImageSize.Width, this.Configuration.MediumImageSize.Height);
            result.ReleaseMediaId   = track.ReleaseMedia.RoadieId.ToString();
            result.Artist           = ArtistList.FromDataArtist(track.ReleaseMedia.Release.Artist, this.MakeArtistThumbnailImage(track.ReleaseMedia.Release.Artist.RoadieId));
            result.ArtistThumbnail  = this.MakeArtistThumbnailImage(track.ReleaseMedia.Release.Artist.RoadieId);
            result.Release          = ReleaseList.FromDataRelease(track.ReleaseMedia.Release, track.ReleaseMedia.Release.Artist, this.HttpContext.BaseUrl, this.MakeArtistThumbnailImage(track.ReleaseMedia.Release.Artist.RoadieId), this.MakeReleaseThumbnailImage(track.ReleaseMedia.Release.RoadieId));
            result.ReleaseThumbnail = this.MakeReleaseThumbnailImage(track.ReleaseMedia.Release.RoadieId);
            if (track.ArtistId.HasValue)
            {
                var trackArtist = this.DbContext.Artists.FirstOrDefault(x => x.Id == track.ArtistId);
                if (trackArtist == null)
                {
                    this.Logger.LogWarning($"Unable to find Track Artist [{ track.ArtistId }");
                }
                else
                {
                    result.TrackArtist          = ArtistList.FromDataArtist(trackArtist, this.MakeArtistThumbnailImage(trackArtist.RoadieId));
                    result.TrackArtistToken     = result.TrackArtist.Artist;
                    result.TrackArtistThumbnail = this.MakeArtistThumbnailImage(trackArtist.RoadieId);
                }
            }
            if (includes != null && includes.Any())
            {
                if (includes.Contains("stats"))
                {
                    result.Statistics = new Library.Models.Statistics.TrackStatistics
                    {
                        FileSizeFormatted = ((long?)track.FileSize).ToFileSize(),
                        Time        = new TimeInfo((decimal)track.Duration).ToFullFormattedString(),
                        PlayedCount = track.PlayedCount
                    };
                    var userTracks = (from t in this.DbContext.Tracks
                                      join ut in this.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);
                    }
                }
            }

            sw.Stop();
            return(Task.FromResult(new OperationResult <Track>
            {
                Data = result,
                IsSuccess = result != null,
                OperationTime = sw.ElapsedMilliseconds
            }));
        }
Пример #4
0
        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
            }));
        }
Пример #5
0
        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
            });
        }
Пример #6
0
        private Task <OperationResult <User> > UserByIdAction(Guid id, IEnumerable <string> includes)
        {
            var user = GetUser(id);

            if (user == null)
            {
                return(Task.FromResult(new OperationResult <User>(true, string.Format("User Not Found [{0}]", id))));
            }
            var model = user.Adapt <User>();

            if (includes != null && includes.Any())
            {
                if (includes.Contains("stats"))
                {
                    var userArtists =
                        DbContext.UserArtists.Include(x => x.Artist).Where(x => x.UserId == user.Id).ToArray() ??
                        new data.UserArtist[0];
                    var userReleases =
                        DbContext.UserReleases.Include(x => x.Release).Where(x => x.UserId == user.Id).ToArray() ??
                        new data.UserRelease[0];
                    var userTracks =
                        DbContext.UserTracks.Include(x => x.Track).Where(x => x.UserId == user.Id).ToArray() ??
                        new data.UserTrack[0];

                    var mostPlayedArtist = (from a in DbContext.Artists
                                            join r in DbContext.Releases on a.Id equals r.ArtistId
                                            join rm in DbContext.ReleaseMedias on r.Id equals rm.ReleaseId
                                            join t in DbContext.Tracks on rm.Id equals t.ReleaseMediaId
                                            join ut in DbContext.UserTracks on t.Id equals ut.TrackId
                                            where ut.UserId == user.Id
                                            select new { a, ut.PlayedCount })
                                           .GroupBy(a => a.a)
                                           .Select(x => new
                    {
                        Artist = x.Key,
                        Played = x.Sum(t => t.PlayedCount)
                    })
                                           .OrderByDescending(x => x.Played)
                                           .FirstOrDefault();

                    var mostPlayedReleaseId = (from r in DbContext.Releases
                                               join rm in DbContext.ReleaseMedias on r.Id equals rm.ReleaseId
                                               join t in DbContext.Tracks on rm.Id equals t.ReleaseMediaId
                                               join ut in DbContext.UserTracks on t.Id equals ut.TrackId
                                               where ut.UserId == user.Id
                                               select new { r, ut.PlayedCount })
                                              .GroupBy(r => r.r)
                                              .Select(x => new
                    {
                        Release = x.Key,
                        Played  = x.Sum(t => t.PlayedCount)
                    })
                                              .OrderByDescending(x => x.Played)
                                              .Select(x => x.Release.RoadieId)
                                              .FirstOrDefault();

                    var mostPlayedRelease = GetRelease(mostPlayedReleaseId);

                    var mostPlayedTrackUserTrack = userTracks
                                                   .OrderByDescending(x => x.PlayedCount)
                                                   .FirstOrDefault();
                    var mostPlayedTrack = mostPlayedTrackUserTrack == null
                        ? null
                        : DbContext.Tracks
                                          .Include(x => x.TrackArtist)
                                          .Include(x => x.ReleaseMedia)
                                          .Include("ReleaseMedia.Release")
                                          .Include("ReleaseMedia.Release.Artist")
                                          .FirstOrDefault(x => x.Id == mostPlayedTrackUserTrack.TrackId);

                    model.Statistics = new UserStatistics
                    {
                        MostPlayedArtist = mostPlayedArtist == null
                            ? null
                            : models.ArtistList.FromDataArtist(mostPlayedArtist.Artist,
                                                               MakeArtistThumbnailImage(mostPlayedArtist.Artist.RoadieId)),
                        MostPlayedRelease = mostPlayedRelease == null
                            ? null
                            : ReleaseList.FromDataRelease(mostPlayedRelease,
                                                          mostPlayedRelease.Artist,
                                                          HttpContext.BaseUrl,
                                                          MakeArtistThumbnailImage(mostPlayedRelease.Artist.RoadieId),
                                                          MakeReleaseThumbnailImage(mostPlayedRelease.RoadieId)),
                        MostPlayedTrack = mostPlayedTrack == null
                            ? null
                            : models.TrackList.FromDataTrack(
                            MakeTrackPlayUrl(user, mostPlayedTrack.Id, mostPlayedTrack.RoadieId),
                            mostPlayedTrack,
                            mostPlayedTrack.ReleaseMedia.MediaNumber,
                            mostPlayedTrack.ReleaseMedia.Release,
                            mostPlayedTrack.ReleaseMedia.Release.Artist,
                            mostPlayedTrack.TrackArtist,
                            HttpContext.BaseUrl,
                            MakeTrackThumbnailImage(mostPlayedTrack.RoadieId),
                            MakeReleaseThumbnailImage(mostPlayedTrack.ReleaseMedia.Release.RoadieId),
                            MakeArtistThumbnailImage(mostPlayedTrack.ReleaseMedia.Release.Artist.RoadieId),
                            MakeArtistThumbnailImage(mostPlayedTrack.TrackArtist == null
                                    ? null
                                    : (Guid?)mostPlayedTrack.TrackArtist.RoadieId)),
                        RatedArtists      = userArtists.Where(x => x.Rating > 0).Count(),
                        FavoritedArtists  = userArtists.Where(x => x.IsFavorite ?? false).Count(),
                        DislikedArtists   = userArtists.Where(x => x.IsDisliked ?? false).Count(),
                        RatedReleases     = userReleases.Where(x => x.Rating > 0).Count(),
                        FavoritedReleases = userReleases.Where(x => x.IsFavorite ?? false).Count(),
                        DislikedReleases  = userReleases.Where(x => x.IsDisliked ?? false).Count(),
                        RatedTracks       = userTracks.Where(x => x.Rating > 0).Count(),
                        PlayedTracks      = userTracks.Where(x => x.PlayedCount.HasValue).Select(x => x.PlayedCount).Sum(),
                        FavoritedTracks   = userTracks.Where(x => x.IsFavorite ?? false).Count(),
                        DislikedTracks    = userTracks.Where(x => x.IsDisliked ?? false).Count()
                    };
                }
            }

            return(Task.FromResult(new OperationResult <User>
            {
                IsSuccess = true,
                Data = model
            }));
        }
Пример #7
0
        private async Task <OperationResult <User> > UserByIdAction(Guid id, IEnumerable <string> includes)
        {
            var timings = new Dictionary <string, long>();
            var tsw     = new Stopwatch();

            tsw.Restart();
            var user = await GetUser(id).ConfigureAwait(false);

            tsw.Stop();
            timings.Add("getUser", tsw.ElapsedMilliseconds);

            if (user == null)
            {
                return(new OperationResult <User>(true, string.Format("User Not Found [{0}]", id)));
            }
            tsw.Restart();
            var model = user.Adapt <User>();

            model.MediumThumbnail = ImageHelper.MakeThumbnailImage(Configuration, HttpContext, id, "user", Configuration.MediumImageSize.Width, Configuration.MediumImageSize.Height);
            model.IsAdmin         = user.UserRoles?.Any(x => x.Role?.NormalizedName == "ADMIN") ?? false;
            model.IsEditor        = model.IsAdmin || (user.UserRoles?.Any(x => x.Role?.NormalizedName == "EDITOR") ?? false);
            tsw.Stop();
            timings.Add("adapt", tsw.ElapsedMilliseconds);

            if (includes?.Any() == true && includes.Contains("stats"))
            {
                tsw.Restart();
                var userArtists  = DbContext.UserArtists.Include(x => x.Artist).Where(x => x.UserId == user.Id).ToArray() ?? new data.UserArtist[0];
                var userReleases = DbContext.UserReleases.Include(x => x.Release).Where(x => x.UserId == user.Id).ToArray() ?? new data.UserRelease[0];
                var userTracks   = DbContext.UserTracks.Include(x => x.Track).Where(x => x.UserId == user.Id).ToArray() ?? new data.UserTrack[0];

                var mostPlayedArtist = await DbContext.MostPlayedArtist(user.Id).ConfigureAwait(false);

                var mostPlayedRelease = await DbContext.MostPlayedRelease(user.Id).ConfigureAwait(false);

                var lastPlayedTrack = await GetTrack((await DbContext.MostPlayedTrack(user.Id).ConfigureAwait(false))?.RoadieId ?? Guid.Empty).ConfigureAwait(false);

                var mostPlayedTrack = await GetTrack((await DbContext.LastPlayedTrack(user.Id).ConfigureAwait(false))?.RoadieId ?? Guid.Empty).ConfigureAwait(false);

                model.Statistics = new UserStatistics
                {
                    LastPlayedTrack = lastPlayedTrack == null
                        ? null
                        : models.TrackList.FromDataTrack(
                        MakeTrackPlayUrl(user, HttpContext.BaseUrl, lastPlayedTrack.RoadieId),
                        lastPlayedTrack,
                        lastPlayedTrack.ReleaseMedia.MediaNumber,
                        lastPlayedTrack.ReleaseMedia.Release,
                        lastPlayedTrack.ReleaseMedia.Release.Artist,
                        lastPlayedTrack.TrackArtist,
                        HttpContext.BaseUrl,
                        ImageHelper.MakeTrackThumbnailImage(Configuration, HttpContext, lastPlayedTrack.RoadieId),
                        ImageHelper.MakeReleaseThumbnailImage(Configuration, HttpContext, lastPlayedTrack.ReleaseMedia.Release.RoadieId),
                        ImageHelper.MakeArtistThumbnailImage(Configuration, HttpContext, lastPlayedTrack.ReleaseMedia.Release.Artist.RoadieId),
                        ImageHelper.MakeArtistThumbnailImage(Configuration, HttpContext, lastPlayedTrack.TrackArtist == null
                                ? null
                                : (Guid?)lastPlayedTrack.TrackArtist.RoadieId)),
                    MostPlayedArtist = mostPlayedArtist == null
                        ? null
                        : models.ArtistList.FromDataArtist(mostPlayedArtist,
                                                           ImageHelper.MakeArtistThumbnailImage(Configuration, HttpContext, mostPlayedArtist.RoadieId)),
                    MostPlayedRelease = mostPlayedRelease == null
                        ? null
                        : ReleaseList.FromDataRelease(mostPlayedRelease,
                                                      mostPlayedRelease.Artist,
                                                      HttpContext.BaseUrl,
                                                      ImageHelper.MakeArtistThumbnailImage(Configuration, HttpContext, mostPlayedRelease.Artist.RoadieId),
                                                      ImageHelper.MakeReleaseThumbnailImage(Configuration, HttpContext, mostPlayedRelease.RoadieId)),
                    MostPlayedTrack = mostPlayedTrack == null
                        ? null
                        : models.TrackList.FromDataTrack(
                        MakeTrackPlayUrl(user, HttpContext.BaseUrl, mostPlayedTrack.RoadieId),
                        mostPlayedTrack,
                        mostPlayedTrack.ReleaseMedia.MediaNumber,
                        mostPlayedTrack.ReleaseMedia.Release,
                        mostPlayedTrack.ReleaseMedia.Release.Artist,
                        mostPlayedTrack.TrackArtist,
                        HttpContext.BaseUrl,
                        ImageHelper.MakeTrackThumbnailImage(Configuration, HttpContext, mostPlayedTrack.RoadieId),
                        ImageHelper.MakeReleaseThumbnailImage(Configuration, HttpContext, mostPlayedTrack.ReleaseMedia.Release.RoadieId),
                        ImageHelper.MakeArtistThumbnailImage(Configuration, HttpContext, mostPlayedTrack.ReleaseMedia.Release.Artist.RoadieId),
                        ImageHelper.MakeArtistThumbnailImage(Configuration, HttpContext, mostPlayedTrack.TrackArtist == null
                                ? null
                                : (Guid?)mostPlayedTrack.TrackArtist.RoadieId)),
                    RatedArtists      = userArtists.Count(x => x.Rating > 0),
                    FavoritedArtists  = userArtists.Count(x => x.IsFavorite ?? false),
                    DislikedArtists   = userArtists.Count(x => x.IsDisliked ?? false),
                    RatedReleases     = userReleases.Count(x => x.Rating > 0),
                    FavoritedReleases = userReleases.Count(x => x.IsFavorite ?? false),
                    DislikedReleases  = userReleases.Count(x => x.IsDisliked ?? false),
                    RatedTracks       = userTracks.Count(x => x.Rating > 0),
                    PlayedTracks      = userTracks.Where(x => x.PlayedCount.HasValue).Select(x => x.PlayedCount).Sum(),
                    FavoritedTracks   = userTracks.Count(x => x.IsFavorite ?? false),
                    DislikedTracks    = userTracks.Count(x => x.IsDisliked ?? false)
                };
                tsw.Stop();
                timings.Add("stats", tsw.ElapsedMilliseconds);
            }
            Logger.LogInformation($"ByIdAction: User `{ user }`: includes [{includes.ToCSV()}], timings: [{ timings.ToTimings() }]");
            return(new OperationResult <User>
            {
                IsSuccess = true,
                Data = model
            });
        }