コード例 #1
0
        public async Task <ActionResult <StudentResource> > UpdateStudent(int id, [FromBody] SaveStudentResource saveStudentResource)
        {
            var validator        = new SaveStudentResourceValidator();
            var validationResult = await validator.ValidateAsync(saveStudentResource);

            var requestIsInvalid = id == 0 || !validationResult.IsValid;

            if (requestIsInvalid)
            {
                _logger.LogError(DateTime.Now + $"\nUpdating student with {id} id failed, validation isn't valid " + validationResult.Errors);
                return(BadRequest(validationResult.Errors));
            }

            var studentToBeUpdate = await _studentService.GetStudentById(id);

            var student = _mapper.Map <SaveStudentResource, Student>(saveStudentResource);

            student.Id = id;

            await _studentService.UpdateStudent(student);

            var updatedStudent = await _studentService.GetStudentById(id);

            var updatedStudentResource = _mapper.Map <Student, StudentResource>(updatedStudent);

            return(Ok(updatedStudentResource));
        }
コード例 #2
0
        public async Task <IActionResult> PutAsync(int id, [FromBody] SaveStudentResource resource)
        {
            var student = mapper.Map <SaveStudentResource, Student>(resource);
            var result  = await studentService.UpdateAsync(id, student);

            if (!result.Success)
            {
                return(BadRequest(result));
            }
            return(Ok(result));
        }
コード例 #3
0
        public async Task <ActionResult <StudentResource> > CreateStudent(
            [FromBody] SaveStudentResource saveStudentResource)
        {
            var studentDtoToCreate = _mapper.Map <StudentDto>(saveStudentResource);
            var newStudentDto      = await _studentService.CreateStudentAsync(studentDtoToCreate);

            var createdStudentDto = await _studentService.GetStudentByIdAsync(newStudentDto.Id);

            var studentResource = _mapper.Map <StudentResource>(createdStudentDto);

            return(Ok(studentResource));
        }
コード例 #4
0
        public async Task <IActionResult> PostAsync([FromBody] SaveStudentResource resource)
        {
            var student = _mapper.Map <SaveStudentResource, Student>(resource);
            var result  = await _studentService.AddAsync(student);

            if (!result.Success)
            {
                return(BadRequest(new ErrorResource(result.Message)));
            }

            var studentResource = _mapper.Map <Student, StudentResource>(result.Student);

            return(Ok(studentResource));
        }
コード例 #5
0
        public async Task UpdateStudent_IdLowerThanOneValidationError_Test(int id)
        {
            // Arrange
            var newStudent = new SaveStudentResource {
                Name = "Update"
            };
            // Act
            var result = await Controller.UpdateStudent(id, newStudent);

            var badRequest = result.Result as BadRequestObjectResult;

            // Assert
            Assert.That(badRequest is BadRequestObjectResult);
        }
コード例 #6
0
        public async Task CreateStudent_EmailValidationError_Test()
        {
            // Arrange
            var newStudent = new SaveStudentResource {
                Name = "New", Email = "Q", Mobile = "+7 (931) 945-23-45"
            };
            // Act
            var result = await Controller.CreateStudent(newStudent);

            var badRequest = result.Result as BadRequestObjectResult;

            // Assert
            Assert.That(badRequest is BadRequestObjectResult);
        }
コード例 #7
0
        public async Task <IActionResult> PostAsync([FromBody] SaveStudentResource resource)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState.GetErrorMessages()));
            }
            var student = _mapper.Map <SaveStudentResource, Student>(resource);
            var result  = await _studentService.SaveAsync(student);

            if (!result.Success)
            {
                return(BadRequest(result.Message));
            }
            var studentResource = _mapper.Map <Student, StudentResource>(result.Resource);

            return(Ok(studentResource));
        }
コード例 #8
0
        public async Task UpdateStudent_NormalConditions_Test(int id)
        {
            // Arrange
            var newStudent = new SaveStudentResource {
                Name = "Update", Email = "*****@*****.**", Mobile = "+7 (931) 945-23-45"
            };
            // Act
            await Controller.UpdateStudent(id, newStudent);

            var result = await Controller.GetAllStudents();

            var okObject   = result.Result as OkObjectResult;
            var collection = okObject.Value as IEnumerable <StudentResource>;

            // Assert
            Service.Verify(x => x.UpdateStudent(It.IsAny <Student>()), Times.Once);
            Assert.That(collection.ElementAt(0).Name, Is.EqualTo("Update"));
        }
コード例 #9
0
        public async Task <IActionResult> Create([FromBody] SaveStudentResource saveStudentResource)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var student = _mapper.Map <SaveStudentResource, Student>(saveStudentResource);

            _unitOfWork.Students.Add(student);
            await _unitOfWork.CompleteAsync();

            student = await _unitOfWork.Students.GetStudentAsync(student.Id);

            var studentResource = Mapper.Map <Student, StudentResource>(student);

            return(Ok(studentResource));
        }
コード例 #10
0
        public async Task <ActionResult <StudentResource> > CreateStudent([FromBody] SaveStudentResource saveStudentResource)
        {
            var validator        = new SaveStudentResourceValidator();
            var validationResult = await validator.ValidateAsync(saveStudentResource);

            if (!validationResult.IsValid)
            {
                _logger.LogError(DateTime.Now + $"\nCreating student failed, validation isn't valid " + validationResult.Errors.ToString());
                return(BadRequest(validationResult.Errors));
            }

            var studentToCreate = _mapper.Map <SaveStudentResource, Student>(saveStudentResource);

            var result = await _studentService.CreateStudent(studentToCreate);

            var createdStudent = _mapper.Map <Student, StudentResource>(result);

            return(Ok(createdStudent));
        }
コード例 #11
0
        public async Task <ActionResult <StudentResource> > UpdateStudent(int id,
                                                                          [FromBody] SaveStudentResource saveStudentResource)
        {
            var studentDto = _mapper.Map <StudentDto>(saveStudentResource);

            var studentDtoToUpdate = await _studentService.GetStudentByIdAsync(id);

            await _studentService.UpdateStudentAsync(studentDtoToUpdate, studentDto);

            var updatedStudentDto = await _studentService.GetStudentByIdAsync(id);

            if (updatedStudentDto == null)
            {
                return(NotFound());
            }

            var updatedStudentResource = _mapper.Map <StudentResource>(updatedStudentDto);

            return(Ok(updatedStudentResource));
        }
コード例 #12
0
        public async Task <IActionResult> Update(int id, [FromBody] SaveStudentResource studentResource)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var student = await _unitOfWork.Students.GetStudentAsync(id);

            if (student == null)
            {
                return(NotFound());
            }

            _mapper.Map(studentResource, student);
            await _unitOfWork.CompleteAsync();

            var result = _mapper.Map <Student, SaveStudentResource>(student);

            return(Ok(result));
        }