/// <summary> /// create initial playlist rating /// </summary> /// /// <param name="playlist"> /// rating to attach this rating to /// </param> /// /// <param name="song"> /// song being rated /// </param> /// /// <param name="creator"> /// initial creator of the rating /// </param> public PlaylistSongRating(Playlist playlist, Song song, FbUser creator) : this() { Song = song; Playlist = playlist; Creator = creator; }
private static PlaylistSongRating SaveSong(IRepository repository, Playlist playlist, FbUser votingUser, string artistName, string songName, double duration, string videoId, short rating, string imageUrl) { var song = new Song { Name = songName, ArtistName = artistName, Duration = duration, VideoID = videoId, ImageUrl = imageUrl }; repository.Save(song); var playlistRating = playlist.AddSong(song, votingUser, playlist.IsOwner(votingUser.Id)); repository.SaveOrUpdate(playlistRating); return playlistRating; }
public virtual JsonResult AddSong( long playlistId, string songName, string videoId, short newRatingValue, string songImage, string songArtist, double? songDuration = 0) { /* song being added */ var dbResponse = DataBaseUpdateResponse.MissingValidValues; var playlist = Repository.Get<Playlist>(playlistId); var userRole = GetCurrentUserPrivileges(playlist); var allowedUsers = MatchUserPrivilege(userRole, UserPrivileges.User) || MatchUserPrivilege(userRole, UserPrivileges.Owner); if (playlist == null) { return BuildFailureResult( (short) dbResponse, App_GlobalResources.Errors.invalidPlaylist); } /* rating value should be either 1 or -1 */ if (newRatingValue != 1 && newRatingValue != -1) { return BuildFailureResult( (short) DataBaseUpdateResponse.MissingValidValues, App_GlobalResources.Errors.invalidRatingValue); } if (!allowedUsers) { return BuildFailureResult( (short) dbResponse, App_GlobalResources.Errors.invalidUser); } if (!IsVotingAllowed(playlist)) { return BuildFailureResult( (short) dbResponse, App_GlobalResources.Errors.accessDenied); } /* checking to see if user has exceed the number of songs * allowed */ var numOfSongsLeft = playlist.GetNumOfSongsLeft(CurrentUser); /* playlist owner can add as many songs as he wantes */ if (numOfSongsLeft <= 0 && !MatchUserPrivilege(playlist, UserPrivileges.Owner)) { return BuildFailureResult( (short) DataBaseUpdateResponse.LimitReached, App_GlobalResources.Errors.playlistSongLimitReached); } var song = Repository.Query<Song>().FirstOrDefault( innerSong => innerSong.VideoID == videoId); if (song == null) { song = new Song(); dbResponse = DataBaseUpdateResponse.Added; } else { dbResponse = DataBaseUpdateResponse.Updated; } song.VideoID = videoId; song.Name = songName; song.Duration = songDuration; song.ArtistName = songArtist; song.ImageUrl = songImage; Repository.SaveOrUpdate(song); /* automaticaly rating song with specified value */ PlaylistSongRating rating; lock (SessionLock) { rating = playlist.AddSong(song, CurrentUser, CurrentUserFBInfo.IsAdmin); Repository.SaveOrUpdate(rating); Repository.SaveOrUpdate(playlist); } SessionLock = null; var userDTO = BuildUserDTO(playlist, userRole); var songMapping = SongWithUserRatingDtoFactory.Map(rating, CurrentUserFBInfo.IsAdmin); songMapping.Rating = newRatingValue; var playlistDto = PlaylistDTOFactory.Map(playlist); var data = new { song = songMapping, user = userDTO, playlist = playlistDto }; return BuildResult((short) dbResponse, string.Empty, data); }
/// <summary> /// find the song in the database or create one /// </summary> /// /// <param name="link"> /// YouTube link /// </param> /// /// <param name="name"> /// name of the song (YouTube title) /// </param> /// /// <param name="caption"> /// caption of the post /// </param> /// ///<returns> /// either song from the database or a new one (added to the database) /// </returns> private Song FindOrCreateSong( string link, string name, string caption) { /* lookup by video ID */ var songVideoId = FindVideoId(link); if (songVideoId == null) { return null; } var song = Repository.Query<Song>() .Where(s => s.VideoID.Equals(songVideoId)) .FirstOrDefault(); if (song != null) { return song; } /* look up by artist name and song title */ var songDetailsNameAttempt = ParseSongNameAndArtist(name); var songDetailsCaptionAttempt = ParseSongNameAndArtist(caption); var songDetails = new string[2]; songDetails[0] = string.IsNullOrEmpty(songDetailsNameAttempt[0]) ? songDetailsCaptionAttempt[0] : songDetailsNameAttempt[0]; songDetails[1] = string.IsNullOrEmpty(songDetailsNameAttempt[1]) ? songDetailsCaptionAttempt[1] : songDetailsNameAttempt[1]; /* create new song */ song = new Song { ArtistName = songDetails[0], Name = songDetails[1], LastUpdated = DateTime.Now, VideoID = songVideoId }; return song; }
public static SongWithUserRatingDTO Map(Song song, long ratingId) { var retVal = Mapper.Map<Song, SongWithUserRatingDTO>(song); retVal.RatingId = ratingId; return retVal; }
public virtual bool HasUserRatedSong(Song song, FbUser CurrentUser) { var queriedWeddingSongsStats = Ratings.Where(weddingSongsStas => weddingSongsStas.Song.Id == song.Id).FirstOrDefault(); if (queriedWeddingSongsStats == null) { return false; } var ratedSong = queriedWeddingSongsStats.Details.ToList() .Where(songRating => songRating.VotingUser.Id == CurrentUser.Id) .FirstOrDefault(); return (ratedSong != null); }
public virtual Song[] FilterOnlySongsInPlaylist(Song[] songs) { var songsInWedding = new List<Song>(); for (var index = 0; index < songs.Length; index++) { var statistic = Ratings.Where(stats => stats.Song.Id == songs[index].Id) .FirstOrDefault(); if (statistic != null) { var foundSong = statistic.Song; songsInWedding.Add(foundSong); } } return songsInWedding.ToArray(); }
public virtual PlaylistSongRating AddSong(Song song, FbUser user, bool addedByAdmin, short rating = 1) { var playlistSongRating = new PlaylistSongRating { Playlist = this, Song = song, Creator = user }; Ratings.Add(playlistSongRating); var palylistSongRating = RateSong(playlistSongRating, user, rating, addedByAdmin, true); if (addedByAdmin) { palylistSongRating.IsAddedByAdmin = true; } return palylistSongRating; }
public virtual bool DoesRatingExist(Song song, FbUser ratingUser) { var currentStatistics = GetCurrentStatistics(song); if (currentStatistics == null) { return false; } return currentStatistics.Details.Any(_details => _details.VotingUser.Id == ratingUser.Id); }
private PlaylistSongRating GetCurrentStatistics(Song song) { lock (GetType()) { var currentStatistics = Ratings .Where(s => s.Song.Id == song.Id) .Where(s => s.Playlist == this) .Select(s => s) .FirstOrDefault(); return currentStatistics; } }