Beispiel #1
0
        public async Task <IActionResult> AddReview([FromBody] ReviewDto reviewData)
        {
            //reviewData.orderId here will not be sent
            if (reviewData.Stars < 1 || reviewData.Stars > 5)
            {
                return(BadRequest("Star evaluation missing or is in incorrect format"));
            }

            var username = JWTtoken.GetUsernameFromToken(Request);

            if (username == null)
            {
                return(BadRequest());
            }

            var acc = await _accountManager.FindByNameAsync(username);

            var newestOrder = _ordersRepository.GetUserPurchaseHistory(acc.Id)
                              .OrderByDescending(order => order.Date)
                              .FirstOrDefault();

            if (newestOrder == null)
            {
                return(BadRequest("Order not found"));
            }

            if (_reviewsRepository.GetById(newestOrder.Id) != null)
            {
                return(BadRequest("This order already has a review"));
            }

            Review newReview = new Review
            {
                Id          = newestOrder.Id,
                Description = reviewData.Description,
                Stars       = reviewData.Stars
            };
            Review addedReview = _reviewsRepository.Add(newReview);

            if (addedReview == null)
            {
                return(BadRequest("Review could not be added"));
            }
            return(Ok("Review added successfully"));
        }