public IActionResult UpdateTurmaForEscola(Guid id, [FromBody] EscolaForUpdateDto escola) { if (escola == null) { return(BadRequest()); } var escolaForEscolaFromRepo = _elevaRepository.GetEscola(id); if (escolaForEscolaFromRepo == null) { var escolaToAdd = Mapper.Map <Escola>(escola); escolaToAdd.Id = id; _elevaRepository.AddEscola(escolaToAdd); if (!_elevaRepository.Save()) { throw new Exception("Creating a escola with upserting failed to save."); } var escolaToReturn = Mapper.Map <EscolaDto>(escolaToAdd); return(CreatedAtRoute("GetTurmaForEscola", new { id = id }, escolaToReturn)); } Mapper.Map(escola, escolaForEscolaFromRepo); _elevaRepository.UpdateEscola(escolaForEscolaFromRepo); if (!_elevaRepository.Save()) { throw new Exception($"Updating escola {id} failed on save"); } return(NoContent()); }
public IActionResult PartialUpdateEscola(Guid id, [FromBody] JsonPatchDocument <EscolaForUpdateDto> patchDoc) { if (patchDoc == null) { return(BadRequest()); } var escolaForEscolaFromRepo = _elevaRepository.GetEscola(id); if (escolaForEscolaFromRepo == null) { var escolaDto = new EscolaForUpdateDto(); patchDoc.ApplyTo(escolaDto, ModelState); TryValidateModel(escolaDto); if (!ModelState.IsValid) { return(new UnprocessableEntityObjectResult(ModelState)); } var escolaToAdd = Mapper.Map <Escola>(escolaDto); escolaToAdd.Id = id; _elevaRepository.AddEscola(escolaToAdd); if (!_elevaRepository.Save()) { throw new Exception("Creating a escola with upserting failed to save."); } var escolaToReturn = Mapper.Map <TurmaDto>(escolaToAdd); return(CreatedAtRoute("GetEscola", new { id = escolaToReturn.Id }, escolaToReturn)); } var escolaToPatch = Mapper.Map <EscolaForUpdateDto>(escolaForEscolaFromRepo); patchDoc.ApplyTo(escolaToPatch, ModelState); TryValidateModel(escolaToPatch); if (!ModelState.IsValid) { return(new UnprocessableEntityObjectResult(ModelState)); } // add validation Mapper.Map(escolaToPatch, escolaForEscolaFromRepo); _elevaRepository.UpdateEscola(escolaForEscolaFromRepo); if (!_elevaRepository.Save()) { throw new Exception($"Patching escola {id} failed on save"); } return(NoContent()); }