Exemplo n.º 1
0
        public async Task <IActionResult> Create([FromBody] PlaylistNameBM playlistNameModel)
        {
            //validate the name of the playlist
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            MyUser user = _helper.GetAuthUser(this.HttpContext, true);

            if (user == null)
            {
                return(Unauthorized());
            }

            //TODO : there must be a limit for the number of playlists that an user may have.
            //Temporary hack:
            if (user.Playlists.Count() == 20)
            {
                return(BadRequest("Too many playlists."));
            }

            //create new playlist, add it to user's playlists, save context async:
            Playlist playlist = new Playlist(playlistNameModel.playlistName, user);

            user.Playlists.Add(playlist);
            await _context.SaveChangesAsync();

            PlaylistBasicVM playlistVM = new PlaylistBasicVM(playlist);

            return(Ok(playlistVM));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Remove(string playlistIdString, string songIdString)
        {
            //Validate string id as a valid GUID id:
            if (!Guid.TryParse(songIdString, out Guid songId))
            {
                return(NotFound());
            }

            //Find the playlist:
            Playlist playlist = _helper.GetUserPlaylistById(this.HttpContext, playlistIdString);

            if (playlist == null)
            {
                return(NotFound());
            }

            //Get PlayableSong, if it exists:
            _context.Entry(playlist).Reference(x => x.Playable).Load();
            _context.Entry(playlist.Playable).Collection(x => x.PlayableSongs).Load();
            PlayableSong playableSong = playlist.PlayableSongs.FirstOrDefault(x => x.SongId == songId);

            if (playableSong == null)
            {
                return(NotFound());
            }

            //Update positions, every song that comes after the
            //removed position should go back a position:
            int removedPosition = playableSong.Position;

            foreach (PlayableSong z in playlist.PlayableSongs)
            {
                if (z.Position > removedPosition)
                {
                    z.Position--;
                }
            }

            //Remove song, save changes:
            playlist.PlayableSongs.Remove(playableSong);
            await _context.SaveChangesAsync();

            PlaylistBasicVM playlistVM = new PlaylistBasicVM {
                PlaylistId = playlist.PlaylistId
            };

            return(Ok(playlistVM));
        }
Exemplo n.º 3
0
        public IActionResult GetPrivateSongsPlaylist()
        {
            MyUser user = _helper.GetAuthUser(this.HttpContext, true);

            if (user == null)
            {
                return(Unauthorized());
            }

            //load relevant info from context:
            _context.Entry(user).Reference(x => x.PrivateSongPlaylist).Load();
            _context.Entry(user.PrivateSongPlaylist).Reference(x => x.Playlist).Load();
            PlaylistBasicVM playlist = new PlaylistBasicVM(user.PrivateSongPlaylist.Playlist);

            return(Ok(playlist));
        }
Exemplo n.º 4
0
        public async Task <IActionResult> Delete(string playlistIdString)
        {
            Playlist playlist = _helper.GetUserPlaylistById(this.HttpContext, playlistIdString);

            if (playlist == null)
            {
                return(NotFound());
            }

            //remove playlist, save context:
            _context.Playlists.Remove(playlist);
            await _context.SaveChangesAsync();

            //return playlistBasicVM with null name,
            //to inform client that the playlist was deleted
            PlaylistBasicVM playlistVM = new PlaylistBasicVM();

            playlistVM.PlaylistId = playlist.PlaylistId;

            return(Ok(playlistVM));
        }
Exemplo n.º 5
0
        public async Task <IActionResult> Edit([FromBody] PlaylistNameBM newName, string playlistIdString)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            Playlist playlist = _helper.GetUserPlaylistById(this.HttpContext, playlistIdString);

            if (playlist == null)
            {
                return(NotFound());
            }

            //change playlist's name, save context:
            playlist.Name = newName.playlistName;
            await _context.SaveChangesAsync();

            PlaylistBasicVM playlistVM = new PlaylistBasicVM(playlist);

            return(Ok(playlistVM));
        }
Exemplo n.º 6
0
        public async Task <IActionResult> CreateMultiple([FromBody] string[] songIdsStrings, string playlistIdString)
        {
            //validate the array of songIds:
            if (songIdsStrings == null || songIdsStrings.Length == 0 || songIdsStrings.Length >= 50)
            {
                return(NotFound());
            }

            //validate songIds as valid GUID ids:
            Guid[] songIds = new Guid[songIdsStrings.Length];
            for (int i = 0; i < songIds.Length; i++)
            {
                if (!Guid.TryParse(songIdsStrings[i], out songIds[i]))
                {
                    return(NotFound());
                }
            }

            //If any of the song ids doesn't exist, return 'NotFound':
            List <Song> songs = new List <Song>();

            foreach (Guid id in songIds)
            {
                Song s = _context.Songs.FirstOrDefault(x => x.SongId == id);
                if (s == null)
                {
                    return(NotFound());
                }
                else
                {
                    songs.Add(s);
                }
            }

            //Find the playlist to which songs should be added:
            Playlist playlist = _helper.GetUserPlaylistById(this.HttpContext, playlistIdString);

            if (playlist == null)
            {
                return(NotFound());
            }

            _context.Entry(playlist).Reference(x => x.Playable).Load();
            _context.Entry(playlist.Playable).Collection(x => x.PlayableSongs).Load();

            //Add songs by creating the corresponding PlayableSong objects:
            foreach (Song s in songs)
            {
                //check if the song already exists in playlist, if so, skip
                if (!playlist.PlayableSongs.Any(x => x.SongId == s.SongId))
                {
                    PlayableSong ps = new PlayableSong();
                    ps.PlayableId = playlist.Playable.PlayableId;
                    ps.Playable   = playlist.Playable;
                    ps.SongId     = s.SongId;
                    ps.Song       = s;
                    ps.Position   = playlist.PlayableSongs.Count(); //add at the end of playlist!
                    playlist.PlayableSongs.Add(ps);
                }
            }

            await _context.SaveChangesAsync();

            PlaylistBasicVM playlistVM = new PlaylistBasicVM {
                PlaylistId = playlist.PlaylistId
            };

            return(Ok(playlistVM));
        }
Exemplo n.º 7
0
        public async Task <IActionResult> EditPosition(string playlistIdString, [FromBody] PlayableSongBM playableSongModel)
        {
            //Find the playlist:
            Playlist playlist = _helper.GetUserPlaylistById(this.HttpContext, playlistIdString);

            if (playlist == null)
            {
                return(NotFound());
            }

            _context.Entry(playlist).Reference(x => x.Playable).Load();
            _context.Entry(playlist.Playable).Collection(x => x.PlayableSongs).Load();

            //Validate string id as a valid GUID id:
            if (!Guid.TryParse(playableSongModel.SongId, out Guid songId))
            {
                return(NotFound());
            }

            //Find the PlayableSong:
            PlayableSong playableSong = playlist.PlayableSongs.FirstOrDefault(x => x.SongId == songId);

            if (playableSong == null)
            {
                return(NotFound());
            }

            //Validate the new position:
            int wantedPosition = playableSongModel.Position;

            if (wantedPosition < 0 || wantedPosition >= playlist.PlayableSongs.Count)
            {
                return(NotFound());
            }

            //Update positions of other songs in playlist:
            int currentPosition = playableSong.Position;

            if (wantedPosition < currentPosition)
            {
                foreach (PlayableSong z in playlist.PlayableSongs) //moves songs down
                {
                    if (z.Position >= wantedPosition && z.Position < currentPosition)
                    {
                        z.Position++;
                    }
                }
            }
            else if (wantedPosition > currentPosition)
            {
                foreach (PlayableSong z in playlist.PlayableSongs) //move songs up
                {
                    if (z.Position <= wantedPosition && z.Position > currentPosition)
                    {
                        z.Position--;
                    }
                }
            }

            //Edit song position and save:
            playableSong.Position = wantedPosition;
            await _context.SaveChangesAsync();

            PlaylistBasicVM playlistVM = new PlaylistBasicVM {
                PlaylistId = playlist.PlaylistId
            };

            return(Ok(playlistVM));
        }