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));
        }
Esempio n. 2
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));
        }