public async Task <ActionResult> PostLesson(CreateLessonDto lessonDto)
        {
            var createdLesson = await _lessonService.CreateLessonAsync(lessonDto);

            if (createdLesson == null)
            {
                return(StatusCode(422, "Cannot create lesson"));
            }

            return(Ok(createdLesson));
        }
        public async Task <LessonDto> CreateLessonAsync(CreateLessonDto lessonDto)
        {
            try
            {
                var createdLessonEntity = _mapper.Map <Lesson>(lessonDto);

                var foundTheme = await _unitOfWork.ThemeRepository.GetThemeByNameAsync(createdLessonEntity.Theme?.Name);

                if (foundTheme == null)
                {
                    _unitOfWork.ThemeRepository.Add(createdLessonEntity.Theme);
                }
                else
                {
                    createdLessonEntity.Theme = foundTheme;
                }

                _unitOfWork.LessonRepository.Add(createdLessonEntity);

                if (lessonDto.LessonVisits != null)
                {
                    for (int i = 0; i < createdLessonEntity.Visits.Count; i++)
                    {
                        createdLessonEntity.Visits[i].Lesson = createdLessonEntity;

                        _unitOfWork.VisitRepository.Add(createdLessonEntity.Visits[i]);
                    }
                }

                await _unitOfWork.CommitAsync();

                return(_mapper.Map <LessonDto>(createdLessonEntity));
            }
            catch
            {
                _unitOfWork.Rollback();

                return(null);
            }
        }
Beispiel #3
0
        public IActionResult CreateLesson(int courseId, [FromBody] CreateLessonDto lesson)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            var course = _courseRepository.Get(courseId, true);

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

            var lessonToSave = _mapper.Map <Lesson>(lesson);

            _courseRepository.AddLessonToCourse(courseId, lessonToSave);
            _courseRepository.Save();

            var createdLesson = _mapper.Map <LessonDto>(lessonToSave);

            return(CreatedAtRoute("GetLesson", new { courseId, id = createdLesson.Id },
                                  createdLesson));
        }
        public async Task CeateLessonAsync()
        {
            //Arrange
            Theme theme = new Theme
            {
                Name = "ExampleName",
                Id   = 5
            };

            Mentor mentor = new Mentor
            {
                Id = 2
            };

            StudentGroup studentGroup = new StudentGroup
            {
                Id = 3
            };

            List <VisitDto> visitsDto = new List <VisitDto>()
            {
            };
            List <Visit> visits = new List <Visit>()
            {
            };

            var createdLesson = new LessonDto()
            {
                Id             = 7,
                ThemeName      = "ExampleName",
                MentorId       = 2,
                StudentGroupId = 3,
                LessonDate     = DateTime.Parse("2020-11-18T15:00:00.384Z"),
                LessonVisits   = visitsDto
            };

            var createLessonDto = new CreateLessonDto
            {
                ThemeName      = "ExampleName",
                MentorId       = 2,
                StudentGroupId = 3,
                LessonDate     = DateTime.Parse("2020-11-18T15:00:00.384Z"),
                LessonVisits   = visitsDto
            };

            var lessonRepositoryMock = new Mock <ILessonRepository>();

            lessonRepositoryMock.Setup(x => x.Add(It.IsAny <Lesson>()))
            .Callback <Lesson>(x => {
                x.Id             = 7;
                x.LessonDate     = DateTime.Parse("2020-11-18T15:00:00.384Z");
                x.MentorId       = 2;
                x.StudentGroupId = 3;
                x.ThemeId        = 5;
                x.Mentor         = mentor;
                x.StudentGroup   = studentGroup;
                x.Theme          = theme;
                x.Visits         = visits;
            });

            var themeRepositoryMock = new Mock <IThemeRepository>();

            themeRepositoryMock.Setup(x => x.Add(It.IsAny <Theme>()))
            .Callback <Theme>(x =>
            {
                x.Id   = 5;
                x.Name = "ExampleName";
            });

            _unitOfWorkMock.Setup(x => x.LessonRepository).Returns(lessonRepositoryMock.Object);
            _unitOfWorkMock.Setup(x => x.ThemeRepository).Returns(themeRepositoryMock.Object);

            var lessonService = new LessonService(
                _unitOfWorkMock.Object,
                _mapper
                );

            //Act
            var result = await lessonService.CreateLessonAsync(createLessonDto);

            //Assert
            Assert.NotNull(result);

            Assert.Equal(createdLesson.Id, result.Id);
            Assert.Equal(createdLesson.LessonDate, result.LessonDate);
            Assert.Equal(createdLesson.LessonVisits.Count, result.LessonVisits.Count);

            for (int i = 0; i < result.LessonVisits?.Count; i++)
            {
                Assert.Equal(createdLesson.LessonVisits[i]?.Comment, result.LessonVisits[i]?.Comment);
                Assert.Equal(createdLesson.LessonVisits[i]?.Presence, result.LessonVisits[i]?.Presence);
                Assert.Equal(createdLesson.LessonVisits[i]?.StudentId, result.LessonVisits[i]?.StudentId);
                Assert.Equal(createdLesson.LessonVisits[i]?.StudentMark, result.LessonVisits[i]?.StudentMark);
            }

            Assert.Equal(createdLesson.MentorId, result.MentorId);
            Assert.Equal(createdLesson.StudentGroupId, result.StudentGroupId);
            Assert.Equal(createdLesson.ThemeName, result.ThemeName);
        }