Пример #1
0
        public async Task <IActionResult> OrderLesson(OrderLessonDto lesson)
        {
            var response = await _mediator.Send(new OrderNewLessonByStudentRequest
            {
                NewLessonDto = lesson
            });

            if (response.CompletedWithSuccess)
            {
                return(Ok());
            }

            else
            {
                Log.Error(response.ErrorMessage.Title);
                Log.Error(response.ErrorMessage.Message);

                return(BadRequest(response.ErrorMessage));
            }
        }
        public async Task ShoulsNotAcceptNewLessonOrderFromStudentForBusyTutor()
        {
            var newOrderedLesson = new OrderLessonDto
            {
                Length        = 2,
                Location      = "Warsaw, Poland",
                Term          = new DateTimeOffset(2019, 12, 30, 16, 20, 00, 00, TimeSpan.Zero),
                TopicCategory = new LessonTopicCategoryDto
                {
                    Id           = Guid.NewGuid(),
                    CategoryName = "Physics"
                },
                TutorId = _tutorId
            };

            var orderedLesson = await _studentService.OrderLessonAsync(_studentId, newOrderedLesson);

            Assert.False(orderedLesson.CompletedWithSuccess);
            Assert.NotNull(orderedLesson.ErrorMessage);
            Assert.Equal("Specific term for lesson is unavailable", orderedLesson.ErrorMessage.Title);
        }
        public async Task <ApiResponse <LessonDto> > OrderLessonAsync(Guid studentId, OrderLessonDto lesson)
        {
            if (studentId == null || studentId == Guid.Empty)
            {
                return(new ApiResponse <LessonDto>()
                       .SetAsFailureResponse(Errors.Lesson.StudentIdForLessonIsEmpty()));
            }

            var tutorPlannedLessons = await _lessonRepository.GetPlannedForTutor(lesson.TutorId);

            foreach (var tutorPlannedLesson in tutorPlannedLessons)
            {
                if (tutorPlannedLesson.Term != lesson.Term)
                {
                    double timeDifferenceBetweenLessons = tutorPlannedLesson.Term.Subtract(lesson.Term).TotalMinutes;
                    if (timeDifferenceBetweenLessons < 30)
                    {
                        return(new ApiResponse <LessonDto>()
                               .SetAsFailureResponse(Errors.Lesson.TooShortTimePeriodBetweenLessons()));
                    }
                }

                else
                {
                    return(new ApiResponse <LessonDto>()
                           .SetAsFailureResponse(Errors.Lesson.TutorAlreadyBookedForLessonProposedTime()));
                }
            }

            var newLesson = new Lesson
            {
                Length        = lesson.Length,
                Location      = lesson.Location,
                Term          = lesson.Term,
                TopicCategory = new LessonTopicCategory
                {
                    Id           = lesson.TopicCategory.Id,
                    CategoryName = lesson.TopicCategory.CategoryName
                },
                TutorId = lesson.TutorId
            };

            var orderedLesson = await _lessonRepository.AddAsync(newLesson);

            var orderedLessonDto = new LessonDto
            {
                Id = orderedLesson.Id,
                AcceptedByTutor    = orderedLesson.AcceptedByTutor,
                CanceledByTutor    = orderedLesson.CanceledByTutor,
                CancelledByStudent = orderedLesson.CancelledByStudent,
                Length             = orderedLesson.Length,
                Location           = orderedLesson.Location,
                StudentId          = orderedLesson.StudentId,
                Term          = orderedLesson.Term,
                TopicCategory = new LessonTopicCategoryDto
                {
                    Id           = orderedLesson.TopicCategory.Id,
                    CategoryName = orderedLesson.TopicCategory.CategoryName
                },
                TutorId = orderedLesson.TutorId
            };

            return(new ApiResponse <LessonDto>
            {
                Result = orderedLessonDto
            });
        }