Exemple #1
0
        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());
        }