Example #1
0
        public static PLAYLIST_SONG AddSongToPlaylist(int userID, int playlistID, int songID)
        {
            var context  = DJPlaylistContext.GetContext();
            var playlist = context.PLAYLISTS.FirstOrDefault(i => i.ID == playlistID);

            if (playlist == null)
            {
                throw new Exception("Playlist not found");
            }
            var song = context.SONGS.FirstOrDefault(i => i.ID == songID);

            if (song == null)
            {
                throw new Exception("Song not found");
            }
            if (playlist.USER.ID != userID)
            {
                throw new Exception("Only owner can add songs to playlist");
            }
            var order  = playlist.PLAYLIST_SONGS.Any() ? playlist.PLAYLIST_SONGS.Max(i => i.ORDER) + 1 : 1;
            var plSong = new PLAYLIST_SONG(playlist, song, order);

            context.PLAYLIST_SONGS.Add(plSong);
            context.SaveChanges();
            return(plSong);
        }
Example #2
0
        private void ShiftSongDown(PLAYLIST_SONG plSong)
        {
            var songInTargetPosition = PLAYLIST_SONGS.FirstOrDefault(i => i.ORDER == plSong.ORDER + 1);

            if (songInTargetPosition != null)
            {
                MoveSongDown(songInTargetPosition);
            }
            plSong.ORDER = plSong.ORDER + 1;
        }
Example #3
0
        public void MoveSongUp(PLAYLIST_SONG plSong)
        {
            if (!PLAYLIST_SONGS.Any(i => i.ORDER < plSong.ORDER))
            {
                return;
            }
            var targetPlSong =
                PLAYLIST_SONGS.Where(i => i.ORDER < plSong.ORDER).OrderByDescending(k => k.ORDER).FirstOrDefault();
            int newPosition = targetPlSong.ORDER;

            targetPlSong.ORDER = plSong.ORDER;
            plSong.ORDER       = newPosition;
        }
Example #4
0
        public void MoveSongDown(PLAYLIST_SONG plSong)
        {
            if (!PLAYLIST_SONGS.Any(i => i.ORDER > plSong.ORDER))
            {
                return;
            }

            var songInTargetPosition = PLAYLIST_SONGS.Where(i => i.ORDER > plSong.ORDER).OrderBy(k => k.ORDER).FirstOrDefault();
            var newPosition          = songInTargetPosition.ORDER;

            songInTargetPosition.ORDER = plSong.ORDER;
            plSong.ORDER = newPosition;
        }