示例#1
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);
            }
        }
示例#2
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);
            }
        }
示例#3
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);
        }
示例#4
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());
     }
 }
示例#5
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);
        }
示例#6
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);
            }
        }
示例#7
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();
            }
        }
示例#8
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);
            }
        }
示例#9
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);
        }
示例#10
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);
        }
 public PearsonScoreCalculator(SmartPlayerEntities context)
 {
     _context = context;
 }
示例#12
0
 public BaseRepository(SmartPlayerEntities context)
 {
     _context = context;
 }
 public UserRepository(SmartPlayerEntities context)
     : base(context)
 {
 }
 public MusicRepository(SmartPlayerEntities context)
     : base(context)
 {
 }