public async Task CreateHomework_StudentIdEmptyValidationError_Test()
        {
            // Arrange
            var newHomework = new SaveHomeworkResource {
                LectureId = 1, Mark = 5, Task = "New"
            };
            // Act
            var result = await Controller.CreateHomework(newHomework);

            var badRequest = result.Result as BadRequestObjectResult;

            // Assert
            Assert.That(badRequest is BadRequestObjectResult);
        }
        public async Task UpdateCourse_StudentIdEmptyValidationErrror_Test(int id)
        {
            // Arrange
            var updateHomework = new SaveHomeworkResource
            {
                LectureId = 1,
                Mark      = 5,
                Task      = "Update"
            };
            // Act
            var result = await Controller.UpdateHomework(id, updateHomework);

            var badRequest = result.Result as BadRequestObjectResult;

            // Assert
            Assert.That(badRequest is BadRequestObjectResult);
        }
        public async Task CreateHomework_NormalConditions_Test()
        {
            // Arrange
            var newHomework = new SaveHomeworkResource {
                LectureId = 1, StudentId = 1, Mark = 5, Task = "New"
            };
            // Act
            await Controller.CreateHomework(newHomework);

            var result = await Controller.GetAllHomeworks();

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

            // Assert
            Service.Verify(x => x.CreateHomework(It.IsAny <Homework>()), Times.Once);
            Assert.That(collection.ElementAt(2).Task, Is.EqualTo("New"));
        }
Ejemplo n.º 4
0
        public async Task <ActionResult <HomeworkWithStudentsAndLectureResource> > UpdateHomework(int id, [FromBody] SaveHomeworkResource saveHomeworkResource)
        {
            var validator        = new SaveHomeworkResourceValidator();
            var validationResult = await validator.ValidateAsync(saveHomeworkResource);

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

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

            var homeworkToBeUpdate = await _homeworkService.GetHomeworkWithLectureAndStudentById(id);

            var homework = _mapper.Map <SaveHomeworkResource, Homework>(saveHomeworkResource);

            homework.Id = id;

            await _homeworkService.UpdateHomework(homework);

            await _homeworkService.CheckAverageMark(homework.StudentId, homework.Lecture.CourseId, new MobileSenderService());

            var updatedHomework = await _homeworkService.GetHomeworkWithLectureAndStudentById(id);

            var updatedHomeworkResource = _mapper.Map <Homework, HomeworkWithStudentsAndLectureResource>(updatedHomework);

            return(Ok(updatedHomeworkResource));
        }
Ejemplo n.º 5
0
        public async Task <ActionResult <HomeworkWithStudentsAndLectureResource> > CreateHomework([FromBody] SaveHomeworkResource saveHomeworkResource)
        {
            var validator        = new SaveHomeworkResourceValidator();
            var validationResult = await validator.ValidateAsync(saveHomeworkResource);

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

            var homeworkToCreate = _mapper.Map <SaveHomeworkResource, Homework>(saveHomeworkResource);

            var result = await _homeworkService.CreateHomework(homeworkToCreate);

            var createdHomework = _mapper.Map <Homework, HomeworkWithStudentsAndLectureResource>(result);

            return(Ok(createdHomework));
        }