public RelatedSongs GetRelatedSongs(Song song) { ParamIs.NotNull(() => song); var songs = new RelatedSongs(); var songId = song.Id; var loadedSongs = new List<int>(20) { songId }; var creditableArtists = song.Artists.Where(a => a.Artist != null && !a.IsSupport).ToArray(); var mainArtists = GetMainArtists(song, creditableArtists); if (mainArtists != null && mainArtists.Any()) { var mainArtistIds = mainArtists.Select(a => a.Id).ToArray(); var songsByMainArtists = ctx.Query() .Where(al => al.Id != songId && !al.Deleted && al.AllArtists.Any(a => !a.Artist.Deleted //&& (al.ArtistString.Default != ArtistHelper.VariousArtists) && !a.IsSupport && mainArtistIds.Contains(a.Artist.Id))) .OrderBy(a => a.RatingScore) .Distinct() .Take(16) .ToArray(); songs.ArtistMatches = songsByMainArtists; loadedSongs.AddRange(songsByMainArtists.Select(s => s.Id)); } if (song.RatingScore > 0) { var userIds = song.UserFavorites.Select(u => u.User.Id).ToArray(); var likeMatches = ctx.OfType<FavoriteSongForUser>() .Query() .Where(f => userIds.Contains(f.User.Id) && !loadedSongs.Contains(f.Song.Id) && !f.Song.Deleted) .GroupBy(f => f.Song.Id) .Select(f => new { Song = f.Key, Ratings = f.Count() }) .OrderByDescending(f => f.Ratings) .Select(s => s.Song) .Take(12) .ToArray(); songs.LikeMatches = ctx.Query().Where(s => likeMatches.Contains(s.Id)).ToArray(); loadedSongs.AddRange(likeMatches); } if (song.Tags.Tags.Any()) { // Take top 5 tags var tagNames = song.Tags.Usages.OrderByDescending(u => u.Count).Take(5).Select(t => t.Tag.Name).ToArray(); var songsWithTags = ctx.Query().Where(al => al.Id != songId && !loadedSongs.Contains(al.Id) && !al.Deleted && al.Tags.Usages.Any(t => tagNames.Contains(t.Tag.Name))) .OrderBy(a => a.RatingScore) .Take(12) .ToArray(); songs.TagMatches = songsWithTags; } return songs; }
public RelatedSongs GetRelatedSongs(Song song, SongRelationsFields fields = SongRelationsFields.All, int count = 12) { ParamIs.NotNull(() => song); var songs = new RelatedSongs(); var songId = song.Id; var loadedSongs = new List <int>(count * 2) { songId }; if (fields.HasFlag(SongRelationsFields.ArtistMatches)) { var creditableArtists = song.Artists.Where(a => a.Artist != null && !a.IsSupport).ToArray(); var mainArtists = GetMainArtists(song, creditableArtists); if (mainArtists != null && mainArtists.Any()) { var mainArtistIds = mainArtists.Select(a => a.Id).ToArray(); var songsByMainArtists = ctx.Query() .Where(s => s.Id != songId && !s.Deleted && s.AllArtists.Any(a => !a.IsSupport && mainArtistIds.Contains(a.Artist.Id))) .OrderBy(SongSortRule.RatingScore) .Take(count) .ToArray(); songs.ArtistMatches = songsByMainArtists; loadedSongs.AddRange(songsByMainArtists.Select(s => s.Id)); } } if (fields.HasFlag(SongRelationsFields.LikeMatches) && song.RatingScore > 0) { var likeMatches = GetLikeMatches(song, loadedSongs, count); songs.LikeMatches = ctx.Query().Where(s => likeMatches.Contains(s.Id)).ToArray(); loadedSongs.AddRange(likeMatches); } if (fields.HasFlag(SongRelationsFields.TagMatches) && song.Tags.Tags.Any()) { // Take top 5 tags var tagIds = song.Tags.Usages .OrderByDescending(u => u.Count) .Take(5) .Select(t => t.Tag.Id) .ToArray(); var songsWithTags = ctx.Query().Where(s => s.Id != songId && !loadedSongs.Contains(s.Id) && !s.Deleted && s.Tags.Usages.Any(t => tagIds.Contains(t.Tag.Id))) .OrderBy(SongSortRule.RatingScore) .Take(count) .ToArray(); songs.TagMatches = songsWithTags; } return(songs); }
public RelatedSongs GetRelatedSongs(Song song) { ParamIs.NotNull(() => song); var songs = new RelatedSongs(); var songId = song.Id; var loadedSongs = new List <int>(20) { songId }; var creditableArtists = song.Artists.Where(a => a.Artist != null && !a.IsSupport).ToArray(); var mainArtists = GetMainArtists(song, creditableArtists); if (mainArtists != null && mainArtists.Any()) { var mainArtistIds = mainArtists.Select(a => a.Id).ToArray(); var songsByMainArtists = ctx.Query() .Where(al => al.Id != songId && !al.Deleted && al.AllArtists.Any(a => !a.Artist.Deleted //&& (al.ArtistString.Default != ArtistHelper.VariousArtists) && !a.IsSupport && mainArtistIds.Contains(a.Artist.Id))) .OrderBy(a => a.RatingScore) .Distinct() .Take(16) .ToArray(); songs.ArtistMatches = songsByMainArtists; loadedSongs.AddRange(songsByMainArtists.Select(s => s.Id)); } if (song.RatingScore > 0) { var userIds = song.UserFavorites.Select(u => u.User.Id).ToArray(); var likeMatches = ctx.OfType <FavoriteSongForUser>() .Query() .Where(f => userIds.Contains(f.User.Id) && !loadedSongs.Contains(f.Song.Id) && !f.Song.Deleted) .GroupBy(f => f.Song.Id) .Select(f => new { Song = f.Key, Ratings = f.Count() }) .OrderByDescending(f => f.Ratings) .Select(s => s.Song) .Take(12) .ToArray(); songs.LikeMatches = ctx.Query().Where(s => likeMatches.Contains(s.Id)).ToArray(); loadedSongs.AddRange(likeMatches); } if (song.Tags.Tags.Any()) { // Take top 5 tags var tagNames = song.Tags.Usages.OrderByDescending(u => u.Count).Take(5).Select(t => t.Tag.Name).ToArray(); var songsWithTags = ctx.Query().Where(al => al.Id != songId && !loadedSongs.Contains(al.Id) && !al.Deleted && al.Tags.Usages.Any(t => tagNames.Contains(t.Tag.Name))) .OrderBy(a => a.RatingScore) .Take(12) .ToArray(); songs.TagMatches = songsWithTags; } return(songs); }