public IActionResult UpdateSongForArtist(Guid artistId, Guid songId, SongForUpdateDto song) { if (!repository.ArtistExists(artistId)) { return(NotFound()); } var songEntity = repository.GetSong(artistId, songId); if (songEntity == null) { // Upserting song with PUT var songToCreate = mapper.Map <Song>(song); songToCreate.Id = songId; repository.AddSongForArtist(artistId, songToCreate); repository.Commit(); var songToReturn = mapper.Map <SongDto>(songToCreate); return(CreatedAtRoute("GetSong", new { artistId, songId = songToCreate.Id }, songToReturn)); } mapper.Map(song, songEntity); repository.Commit(); return(NoContent()); }
public IActionResult PartiallyUpdateSongForArtist(Guid artistId, Guid songId, [FromBody] JsonPatchDocument <SongForUpdateDto> patchDocument) { if (!repository.ArtistExists(artistId)) { return(NotFound()); } var songEntity = repository.GetSong(artistId, songId); if (songEntity == null) { var songToCreate = new SongForUpdateDto(); patchDocument.ApplyTo(songToCreate, ModelState); if (!TryValidateModel(songToCreate)) { return(ValidationProblem(ModelState)); } var songToCreateEntity = mapper.Map <Song>(songToCreate); songToCreateEntity.Id = songId; repository.AddSongForArtist(artistId, songToCreateEntity); repository.Commit(); var songToReturn = mapper.Map <SongDto>(songToCreateEntity); return(CreatedAtRoute("GetSong", new { artistId, songId = songToCreateEntity.Id }, songToReturn)); } var songToPatch = mapper.Map <SongForUpdateDto>(songEntity); patchDocument.ApplyTo(songToPatch, ModelState); if (!TryValidateModel(songToPatch)) { return(ValidationProblem(ModelState)); } mapper.Map(songToPatch, songEntity); repository.Commit(); return(NoContent()); }
public IActionResult UpdateSong(int id, SongForUpdateDto song) { if (song == null) { return(BadRequest()); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var songResponse = new SongResponse(); try { var songToUpdate = _songRepository.Get(id); if (songToUpdate == null) { songResponse.AlertMessage = string.Format(MagicString.SongWithIdDoesntExist, id); return(BadRequest(songResponse)); } Mapper.Map(song, songToUpdate); if (!_songRepository.Save()) { songResponse.AlertMessage = MagicString.ProblemOucuredDuringSavingSongToDatabase; return(BadRequest(songResponse)); } return(Ok()); } catch (Exception ex) { songResponse.AlertMessage = ex.Message; return(BadRequest(songResponse)); } }