public IActionResult PartiallyUpdatePublisher(int id, [FromBody] JsonPatchDocument <PublisherForManipulationDto> patchDoc) { var publisherFromRepo = _repo.GetPublisher(id); if (publisherFromRepo == null) { var newPublisher = new PublisherForManipulationDto(); patchDoc.ApplyTo(newPublisher); if (!TryValidateModel(newPublisher)) { return(BadRequest(ModelState)); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var resultAction = CreatePublisher(newPublisher); return(resultAction); } var publisherDto = _mapper.Map <PublisherForManipulationDto>(publisherFromRepo); patchDoc.ApplyTo(publisherDto); _mapper.Map(publisherDto, publisherFromRepo); _repo.UpdatePublisher(publisherFromRepo); _repo.Save(); return(NoContent()); }
public IActionResult CreatePublisher([FromBody] PublisherForManipulationDto publisherToAdd) { var publisherEntity = _mapper.Map <Publisher>(publisherToAdd); _repo.AddPublisher(publisherEntity); _repo.Save(); var publisherToReturn = _mapper.Map <PublisherForReturnDto>(publisherEntity); return(CreatedAtRoute("GetPublisher", new { id = publisherToReturn.Id }, publisherToReturn)); }
public IActionResult UpdatePublisher(int id, PublisherForManipulationDto publisher) { var publisherFromRepo = _repo.GetPublisher(id); //upsert if (publisherFromRepo == null) { var resultAction = CreatePublisher(publisher); return(resultAction); } _mapper.Map(publisher, publisherFromRepo); _repo.UpdatePublisher(publisherFromRepo); _repo.Save(); return(NoContent()); }