public IActionResult UpdateStudent(Guid studentId, StudentForUpdateDto student) { if (!_repository.StudentExists(studentId)) { return(NotFound()); } var studentToUpdate = _repository.GetStudent(studentId); if (studentToUpdate == null) { var studentToAdd = _mapper.Map <Student>(student); studentToAdd.Id = studentId; _repository.AddStudent(studentToAdd); _repository.Save(); var entityToReturn = _mapper.Map <StudentDto>(studentToAdd); return(CreatedAtRoute("GetStudent", new { studentId = entityToReturn.Id }, entityToReturn)); } // map the entity to a StudentUpdateDto // apply the updated field values to that dto // map the CourseForUpdateDto back to an entity _mapper.Map(student, studentToUpdate); _repository.UpdateStudent(studentToUpdate); _repository.Save(); return(NoContent()); }
public async Task <IActionResult> UpdateStudent(StudentForUpdateDto studentForUpdateDto, int id) { var student = await _repo.GetStudent(id); _mapper.Map(student, studentForUpdateDto); if (await _repo.SaveAll()) { return(NoContent()); } throw new Exception("Cos poszło nie tak podczas aktualizowania informacji o uczniu."); }
public async Task <IActionResult> UpdateStudent(int id, StudentForUpdateDto studentForUpdateDto) { var studentFromRepo = await _repo.GetStudent(id); _mapper.Map(studentForUpdateDto, studentFromRepo); if (await _repo.SaveAll()) { return(NoContent()); } throw new Exception($"Updating student {id} failed on save"); }
public async Task <IActionResult> UpdateStudent(int id, StudentForUpdateDto studentForUpdateDto) { if (id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } var studentFromRepo = await _repo.GetStudent(id); _mapper.Map(studentForUpdateDto, studentFromRepo); if (await _repo.SaveAll()) { return(NoContent()); } throw new Exception($"Updating user {id} failed on save"); }
public ActionResult PartiallyUpdateStudent(Guid studentId, JsonPatchDocument <StudentForUpdateDto> patchDocument) { if (!_repository.StudentExists(studentId)) { return(NotFound()); } var studentToUpdate = _repository.GetStudent(studentId); if (studentToUpdate == null) { var studentDto = new StudentForUpdateDto(); patchDocument.ApplyTo(studentDto, ModelState); if (!TryValidateModel(studentDto)) { return(ValidationProblem(ModelState)); } var studentToAdd = _mapper.Map <Student>(studentDto); studentToAdd.Id = studentId; _repository.AddStudent(studentToAdd); _repository.Save(); var entityToReturn = _mapper.Map <StudentDto>(studentToAdd); return(CreatedAtRoute("GetStudent", new { studentId = entityToReturn.Id }, entityToReturn)); } var studentToPatch = _mapper.Map <StudentForUpdateDto>(studentToUpdate); // add validation patchDocument.ApplyTo(studentToPatch, ModelState); if (!TryValidateModel(studentToPatch)) { return(ValidationProblem(ModelState)); } _mapper.Map(studentToPatch, studentToUpdate); _repository.UpdateStudent(studentToUpdate); _repository.Save(); return(NoContent()); }
public IActionResult Put(int id, [FromBody] StudentForUpdateDto studentDto) { IActionResult result; if (ModelState.IsValid(out result, new[] { studentDto })) { try { var student = _unitOfWork.StudentRepository.FindById(id); Mapper.Map(studentDto, student); _unitOfWork.StudentRepository.Update(student); _unitOfWork.Commit(); result = Ok(student); _logger.LogInformation($"Updated a student entity {student.Id}"); } catch (KeyNotFoundException) { result = NotFound("Could not find the entity with this id"); } } return(result); }