コード例 #1
0
        public PlayerSongDto GetNextSong(NextSongDto songRequest, string username = null)
        {
            using (SmartPlayerEntities context = new SmartPlayerEntities())
            {
                var selectedSong = GetNextSong(songRequest, username, context);
                var songUrl = ExtractSongUrl(selectedSong);

                PlayerSongDto song = ExtractSongResult(username, context, selectedSong, songUrl);

                return song;
            }
        }
コード例 #2
0
        public PlayerSongDto GetSong(int songId, string username)
        {
            using(SmartPlayerEntities context = new SmartPlayerEntities())
            {
                MusicRepository repo = new MusicRepository(context);

                var requestedSong = repo.GetSongById(songId);

                var songUrl = ExtractSongUrl(requestedSong);

                PlayerSongDto song = ExtractSongResult(username, context, requestedSong, songUrl);

                return song;
            }
        }
コード例 #3
0
        public void RateSong(SongRatingDto rating, string userName)
        {
            using (SmartPlayerEntities context = new SmartPlayerEntities())
            {
                UserRepository userRepo = new UserRepository(context);

                User currentUser = userRepo.GetAll().First(x => x.Email == userName);
                context.UserSongVotes.Add(new UserSongVote()
                    {
                        Rating = rating.Rating,
                        SongId = rating.SongId,
                        UserId = currentUser.Id
                    });
                context.SaveChanges();
            }
        }
コード例 #4
0
        public List<SongDto> GetAllSongs()
        {
            using(SmartPlayerEntities context = new SmartPlayerEntities())
            {
                MusicRepository repo = new MusicRepository(context);

                var allSongs = repo.GetAll()
                    .Select(x => new SongDto
                    {
                        Id = x.Id,
                        SongName = x.Name
                    })
                    .ToList();

                return allSongs;
            }
        }
コード例 #5
0
        public void Store(string originalFileName, string guid)
        {
            string mediaServerUrlBase = ConfigurationManager.AppSettings["MediaServerSaveBaseUrl"];
            string fullName = Path.Combine(mediaServerUrlBase, guid);
            using(SmartPlayerEntities context = new SmartPlayerEntities())
            {
                MusicRepository repo = new MusicRepository(context);

                var allSongs = repo.GetAll()
                    .Select(x => new AnalyzableSong { Id = x.Id, PhysicalFileName = x.Guid })
                    .ToList();

                allSongs.ForEach(x => x.PhysicalFileName = Path.Combine(mediaServerUrlBase, x.PhysicalFileName));

                List<double> correlationCoefficients = Analyzer.GetCorreleationCoefficientsFor(fullName, allSongs);

                Song song = new Song()
                {
                    Name = originalFileName,
                    Guid = guid
                };

                for(int i = 0; i < correlationCoefficients.Count; i++)
                {
                    song.CorrelationsAsPrimary.Add(new SongSongCorrelation { SecondarySongId = allSongs[i].Id, CorrelationScore = correlationCoefficients[i] });
                }

                repo.Create(song);
            }
        }
コード例 #6
0
 public List<SongDto> SearchSong(string query)
 {
     using(SmartPlayerEntities context = new SmartPlayerEntities())
     {
         MusicRepository repo = new MusicRepository(context);
         var requestedSongs = repo.SearchByTerm(query);
         return requestedSongs.Select(x => new SongDto() { Id = x.Id, SongName = x.Name }).ToList();
     }
 }
コード例 #7
0
        private int? GetUserRatingForSong(SmartPlayerEntities context, string username, Song song)
        {
            var currentUser = default(User);
            if (!string.IsNullOrWhiteSpace(username))
            {
                var userRepo = new UserRepository(context);
                currentUser = userRepo.GetUserByUsername(username);
            }

            var songVote = default(int?);
            if (currentUser != null && song.UserSongVotes.Where(x => x.UserId == currentUser.Id).Any())
            {
                songVote = song.UserSongVotes.First(x => x.UserId == currentUser.Id).Rating;
            }

            return songVote;
        }
コード例 #8
0
 private PlayerSongDto ExtractSongResult(string username, SmartPlayerEntities context, Song requestedSong, string songUrl)
 {
     PlayerSongDto song = new PlayerSongDto()
     {
         Id = requestedSong.Id,
         Name = requestedSong.Name,
         Url = songUrl,
         CurrentUserVote = GetUserRatingForSong(context, username, requestedSong)
     };
     return song;
 }
コード例 #9
0
        private static List<Song> GetRecommendedSongsForUser(string username, SmartPlayerEntities context)
        {
            var recommendedSongs = new List<Song>();
            if (username != null)
            {
                UserRepository userRepo = new UserRepository(context);
                var userId = userRepo.GetUserByUsername(username).Id;

                PearsonScoreCalculator calculator = new PearsonScoreCalculator(context);
                recommendedSongs = calculator.GetBestSongsForUser(userId);
            }
            return recommendedSongs;
        }
コード例 #10
0
        private static Song GetNextSong(NextSongDto songRequest, string username, SmartPlayerEntities context)
        {
            MusicRepository musicRepo = new MusicRepository(context);
            var excludedSongIdList = songRequest.PlayedSongIds;

            var recommendedSongs = GetRecommendedSongsForUser(username, context);
            recommendedSongs = recommendedSongs.Where(x => !excludedSongIdList.Contains(x.Id)).ToList();

            var similarSongs = musicRepo.GetNextSongBasedOnUserAndGrade(songRequest.CurrentSongId);
            similarSongs = similarSongs.Where(x => !excludedSongIdList.Contains(x.Id)).ToList();

            var safetySet = new Lazy<List<Song>>(() => musicRepo.GetNextSongBasedOnUserAndGrade(songRequest.CurrentSongId, excludedSongIdList));

            var selectedSong = GetNextSong(recommendedSongs, similarSongs, safetySet);
            return selectedSong;
        }
コード例 #11
0
 public PearsonScoreCalculator(SmartPlayerEntities context)
 {
     _context = context;
 }
コード例 #12
0
 public BaseRepository(SmartPlayerEntities context)
 {
     _context = context;
 }