Exemple #1
0
        public static SONG AddSongToDB(string name, string artistName, string albumName, List <string> genreNames, int year, int durationSec)
        {
            var context = DJPlaylistContext.GetContext();

            var song = context.SONGS.FirstOrDefault(i => i.NAME == name && i.ARTIST_NAME == artistName && i.ALBUM_NAME == albumName && i.YEAR == year);

            if (song == null)
            {
                song = new SONG(name, artistName, albumName, year, durationSec);
                context.SONGS.Add(song);
                context.SaveChanges();
            }

            foreach (var genreName in genreNames)
            {
                var genre = context.GENRES.FirstOrDefault(i => i.DESCRIPTION == genreName);
                if (genre == null)
                {
                    genre = new GENRE(genreName);
                    context.GENRES.Add(genre);
                    context.SaveChanges();
                }
                var songGenre = context.SONG_GENRES.FirstOrDefault(i => i.SONG_ID == song.ID && i.GENRE_ID == genre.ID);
                if (songGenre == null)
                {
                    songGenre = new SONG_GENRE(song, genre);
                    context.SONG_GENRES.Add(songGenre);
                    context.SaveChanges();
                }
            }

            return(song);
        }
Exemple #2
0
        private static PLAYLIST MoveSong(int userID, int playlistSongID, bool moveUp)
        {
            var context = DJPlaylistContext.GetContext();
            var plSong  = context.PLAYLIST_SONGS.FirstOrDefault(i => i.ID == playlistSongID);

            if (plSong == null)
            {
                throw new Exception("Playlist Song not found");
            }
            var playList = plSong.PLAYLIST;

            if (plSong.PLAYLIST.USER_ID != userID)
            {
                throw new Exception("Only owner can modify playlist");
            }
            if (moveUp)
            {
                playList.MoveSongUp(plSong);
            }
            else
            {
                playList.MoveSongDown(plSong);
            }
            context.SaveChanges();
            return(playList);
        }
Exemple #3
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);
        }
Exemple #4
0
        public static PLAYLIST_SONG PlayPlaylistSong(int id)
        {
            var context      = DJPlaylistContext.GetContext();
            var playlistSong = context.PLAYLIST_SONGS.FirstOrDefault(i => i.ID == id);
            var songPlay     = PlaySong(playlistSong.SONG_ID, playlistSong.PLAYLIST.USER_ID);

            return(DJPlaylistContext.GetContext().PLAYLIST_SONGS.FirstOrDefault(i => i.ID == playlistSong.ID));
        }
Exemple #5
0
        public static USER RegisterUser(string firstName, string lastName, string email)
        {
            var context = DJPlaylistContext.GetContext();

            if (context.USERS.Any(i => i.EMAIL == email))
            {
                throw new Exception("There already a registered DJ associated to this email");
            }
            var user = new USER(firstName, lastName, email);

            context.USERS.Add(user);
            context.SaveChanges();
            return(user);
        }
Exemple #6
0
        public static PLAYLIST RemoveSongFromPlaylist(int playlistSongID)
        {
            var context  = DJPlaylistContext.GetContext();
            var plSong   = context.PLAYLIST_SONGS.FirstOrDefault(i => i.ID == playlistSongID);
            var playlist = plSong.PLAYLIST;

            if (plSong == null)
            {
                throw new Exception("Playlist Song not found");
            }
            //var playList = plSong.PLAYLIST;
            //if (plSong.PLAYLIST.USER_ID != userID)
            //    throw new Exception("Only owner can remove songs to playlist");
            context.PLAYLIST_SONGS.Remove(plSong);
            context.SaveChanges();
            return(playlist);
        }
Exemple #7
0
        public static PLAYLIST RenamePlaylist(int playListID, string newName)
        {
            var context  = DJPlaylistContext.GetContext();
            var playlist = context.PLAYLISTS.FirstOrDefault(i => i.ID == playListID);

            if (playlist == null)
            {
                throw new Exception("Playlist Not Found");
            }
            var userID = playlist.USER_ID;

            if (context.PLAYLISTS.Any(i => i.NAME == newName && i.USER_ID == userID))
            {
                throw new Exception("There is already a playlist with this name");
            }
            playlist.NAME = newName;
            context.SaveChanges();
            return(playlist);
        }
Exemple #8
0
        public static SONG_PLAY PlaySong(int songID, int userId)
        {
            var context = DJPlaylistContext.GetContext();
            var song    = context.SONGS.FirstOrDefault(i => i.ID == songID);

            if (song == null)
            {
                throw new Exception("Song not found");
            }
            var user = context.USERS.FirstOrDefault(i => i.ID == userId);

            if (user == null)
            {
                throw new Exception("User not found");
            }
            var songPlay = new SONG_PLAY(song, user);

            context.SONG_PLAYS.Add(songPlay);
            context.SaveChanges();
            return(songPlay);
        }
Exemple #9
0
        public static PLAYLIST CreatePlaylist(int userID, string name, bool isPublic = false)
        {
            var context = DJPlaylistContext.GetContext();
            var user    = context.USERS.FirstOrDefault(i => i.ID == userID);

            if (user == null)
            {
                throw new Exception("User not found");
            }
            if (context.PLAYLISTS.Any(i => i.NAME == name && i.USER_ID == userID))
            {
                throw new Exception("There is already a playlist with this name");
            }
            var playlist = new PLAYLIST(name, userID, isPublic);

            context.PLAYLISTS.Add(playlist);
            context.SaveChanges();
            var userPlaylist = new USER_PLAYLIST(user, playlist);

            context.USER_PLAYLISTS.Add(userPlaylist);
            context.SaveChanges();
            return(playlist);
        }
Exemple #10
0
 public static List <USER> GetAllUsers()
 {
     return(DJPlaylistContext.GetContext().USERS.ToList());
 }
Exemple #11
0
 public static IQueryable <PLAYLIST_SONG> GetAllPlaylistSongs()
 {
     return(DJPlaylistContext.GetContext().PLAYLIST_SONGS);
 }
Exemple #12
0
 public static PLAYLIST_SONG GetPlaylistSong(int id)
 {
     return(DJPlaylistContext.GetContext().PLAYLIST_SONGS.FirstOrDefault(i => i.ID == id));
 }
Exemple #13
0
 public static IQueryable <PLAYLIST> GetAllPlaylists()
 {
     return(DJPlaylistContext.GetContext().PLAYLISTS);
 }
Exemple #14
0
 public static PLAYLIST GetPlaylist(int playlistId)
 {
     return(DJPlaylistContext.GetContext().PLAYLISTS.FirstOrDefault(i => i.ID == playlistId));
 }
Exemple #15
0
 public static IQueryable <SONG> GetAllSongs()
 {
     return(DJPlaylistContext.GetContext().SONGS);
 }
Exemple #16
0
 public static SONG GetSong(int songID)
 {
     return(DJPlaylistContext.GetContext().SONGS.FirstOrDefault(i => i.ID == songID));
 }