Exemplo n.º 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);
        }
Exemplo n.º 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);
        }
Exemplo n.º 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);
        }
Exemplo n.º 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));
        }
Exemplo n.º 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);
        }
Exemplo n.º 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);
        }
Exemplo n.º 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);
        }
Exemplo n.º 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);
        }
Exemplo n.º 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);
        }
Exemplo n.º 10
0
 public static List <USER> GetAllUsers()
 {
     return(DJPlaylistContext.GetContext().USERS.ToList());
 }
Exemplo n.º 11
0
 public static IQueryable <PLAYLIST_SONG> GetAllPlaylistSongs()
 {
     return(DJPlaylistContext.GetContext().PLAYLIST_SONGS);
 }
Exemplo n.º 12
0
 public static PLAYLIST_SONG GetPlaylistSong(int id)
 {
     return(DJPlaylistContext.GetContext().PLAYLIST_SONGS.FirstOrDefault(i => i.ID == id));
 }
Exemplo n.º 13
0
 public static IQueryable <PLAYLIST> GetAllPlaylists()
 {
     return(DJPlaylistContext.GetContext().PLAYLISTS);
 }
Exemplo n.º 14
0
 public static PLAYLIST GetPlaylist(int playlistId)
 {
     return(DJPlaylistContext.GetContext().PLAYLISTS.FirstOrDefault(i => i.ID == playlistId));
 }
Exemplo n.º 15
0
 public static IQueryable <SONG> GetAllSongs()
 {
     return(DJPlaylistContext.GetContext().SONGS);
 }
Exemplo n.º 16
0
 public static SONG GetSong(int songID)
 {
     return(DJPlaylistContext.GetContext().SONGS.FirstOrDefault(i => i.ID == songID));
 }