コード例 #1
0
        public async Task <Feedback> CreateAsync(long userId, long orderId, Feedback feedback, CancellationToken cancellationToken)
        {
            ValidateOrderAndCustomer(userId, orderId, cancellationToken, out Order order);
            // Now we check if the order has already been rated and the feedback exists.
            // if the feedback does noot exist, an exception will be thrown.
            Feedback existingFeedback = default;

            try
            {
                existingFeedback = await GetAsync(userId, orderId, cancellationToken);
            }
            catch (ArgumentException)
            {
            }

            if (existingFeedback != null)
            {
                throw new ArgumentException(FeedbackErrorMessages.OrderFeedbackAlreadyExists);
            }

            ValidateRating(feedback.Rating);

            // if all the filters passed, then we can create the new feedback using the repository
            feedback.CreateTime   = DateTime.UtcNow;
            feedback.CustomerSid  = userId;
            feedback.OrderSid     = orderId;
            feedback.FeedbackType = (int)FeedbackType.Order;
            feedback.Products     = await _productFacade.GetProductsByOrderIdAsync(orderId, cancellationToken);

            //update order and feedback
            var savedFeedback = await _orderFeedbackManager.CreateFeedbackAsync(order, feedback, cancellationToken);

            // Populate products just to display them
            savedFeedback.Products = await _productFacade.GetProductsByOrderIdAsync(orderId, cancellationToken);

            await SetCacheAsync(feedback, order, cancellationToken);
            await RemoveFromCacheAsync(nameof(Feedback), string.Concat("GetLatest_Rating_", feedback.Rating.ToString()), cancellationToken);

            return(savedFeedback);
        }