public IActionResult CreatePlaylist(Guid userId, [FromBody] PlaylistForCreation playlistForCreation)
        {
            if (playlistForCreation == null)
            {
                return(BadRequest());
            }

            if (!ModelState.IsValid)
            {
                return(new UnprocessableEntityObjectResult(ModelState));
            }

            var mappedPlaylist = Mapper.Map <Entities.Playlist>(playlistForCreation);

            _sprotifyRepository.CreatePlaylist(userId, mappedPlaylist);

            if (!_sprotifyRepository.Save())
            {
                throw new Exception("Creating the playlist failed.");
            }

            var createdPlaylistToReturn = Mapper.Map <Models.Playlist>(mappedPlaylist);

            return(CreatedAtRoute("GetPlaylist",
                                  new
            {
                playlistId = mappedPlaylist.Id
            },
                                  createdPlaylistToReturn));
        }
        public IActionResult AddSongCollectionToPlaylist(Guid playlistId,
                                                         [FromBody] IEnumerable <SongForCreation> songCollection)
        {
            if (songCollection == null)
            {
                return(BadRequest());
            }

            var songEntities = Mapper.Map <IEnumerable <Entities.Song> >(songCollection);

            foreach (var song in songEntities)
            {
                _sprotifyRepository.AddSongToPlaylist(playlistId, song);
            }

            if (!_sprotifyRepository.Save())
            {
                throw new Exception($"Adding a collection of songs to playlist {playlistId} failed on save.");
            }

            var songCollectionToReturn = Mapper.Map <IEnumerable <Models.Song> >(songEntities);
            var idsAsString            = string.Join(",", songCollectionToReturn.Select(a => a.Id));

            return(CreatedAtRoute("GetSongCollection",
                                  new { ids = idsAsString },
                                  songCollectionToReturn));
        }
Ejemplo n.º 3
0
        public IActionResult DeletePlaylist(Guid id)
        {
            var playListEntity = _sprotifyRepository.GetPlaylist(id);

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

            _sprotifyRepository.DeletePlaylist(playListEntity);

            if (!_sprotifyRepository.Save())
            {
                throw new Exception($"Deleting playlist {id} failed.");
            }

            return(NoContent());
        }
Ejemplo n.º 4
0
        public IActionResult CreateSong([FromBody] SongForCreation songForCreation)
        {
            if (songForCreation == null)
            {
                return(BadRequest());
            }

            if (!ModelState.IsValid)
            {
                return(new UnprocessableEntityObjectResult(ModelState));
            }

            var mappedSong = Mapper.Map <Entities.Song>(songForCreation);

            _sprotifyRepository.AddSong(mappedSong);

            if (!_sprotifyRepository.Save())
            {
                throw new Exception("Adding a song failed.");
            }

            var createdSongToReturn = Mapper.Map <Models.Song>(mappedSong);

            return(CreatedAtRoute("GetSong",
                                  new
            {
                songId = mappedSong.Id
            },
                                  createdSongToReturn));
        }
Ejemplo n.º 5
0
        public IActionResult AddSongToPlaylist(Guid playlistId,
                                               [FromBody] SongForCreation songForCreation)
        {
            if (songForCreation == null)
            {
                return(BadRequest());
            }

            if (!ModelState.IsValid)
            {
                return(new UnprocessableEntityObjectResult(ModelState));
            }

            if (!_sprotifyRepository.PlaylistExists(playlistId))
            {
                return(NotFound());
            }

            var mappedSong = Mapper.Map <Entities.Song>(songForCreation);

            _sprotifyRepository.AddSongToPlaylist(playlistId, mappedSong);

            if (!_sprotifyRepository.Save())
            {
                throw new Exception($"Adding a song to playlist {playlistId} failed.");
            }

            var createdSongToReturn = Mapper.Map <Models.Song>(mappedSong);

            return(CreatedAtRoute("GetSongFromPlaylist",
                                  new
            {
                playlistId = mappedSong.PlaylistId,
                songId = mappedSong.Id
            },
                                  createdSongToReturn));
        }