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.º 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));
        }