Ejemplo n.º 1
0
        public async Task <IActionResult> CreateCourseReview(int courseId, [FromBody] CourseReviewCreateRequestDto request)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    var errors = ModelState.Values.SelectMany(x => x.Errors).Select(e => e.ErrorMessage).ToList();
                    return(BadRequest(errors));
                }
                var result = await _courseReviewServices.CreateCourseReviewAsync(courseId, request);

                return(Ok(result));
            }
            catch (CourseReviewException ex)
            {
                return(BadRequest(ex.Message));
            }
            catch (BadRequestException ex)
            {
                return(BadRequest(ex.Message));
            }
            catch (NotFoundException ex)
            {
                return(NotFound(ex.Message));
            }
            catch (Exception ex)
            {
                _logger.LogCritical($"CreateCourseReview() Error: {ex}");
                return(StatusCode(500, "Internal Server Error"));
            }
        }
Ejemplo n.º 2
0
        public async Task <CourseReviewDto> CreateCourseReviewAsync(int courseId, CourseReviewCreateRequestDto request)
        {
            // 1. check user and course
            var user = await GetCurrentUser();

            var course = await _courseRepository.GetCourseAsync(courseId);

            if (course == null)
            {
                throw new NotFoundException("Course not found");
            }
            // 2. check user's exsisting review
            var review = await _courseReviewRepository.GetCourseReviewByCourseIdAndUserIdAsync(user.Id, courseId);

            if (review != null)
            {
                throw new BadRequestException("You have reviewed this course already.");
            }
            // 3. create review and update course
            var newReview = Review.Create(course, user, request.Comment, request.Score);
            await _courseReviewRepository.CreateAsync(newReview);

            await _courseReviewRepository.SaveAsync();

            return(Mapper.Map <CourseReviewDto>(newReview));
        }