Ejemplo n.º 1
0
        public async Task <IResult> UpdateRating(ProductRatingReviewModel reviewModel)
        {
            var result = new Result()
            {
                Operation = Operation.Update,
                Status    = Status.Success
            };

            try
            {
                var rating = _context.ProductRatingReviews.Where(p => p.RatingId == reviewModel.RatingId).FirstOrDefault();
                rating.MapFromModel(reviewModel);
                await _context.SaveChangesAsync();

                result.StatusCode = HttpStatusCode.OK;
                return(result);
            }
            catch (Exception e)
            {
                result.Status     = Status.Error;
                result.StatusCode = HttpStatusCode.InternalServerError;
                result.Body       = e;
                result.Message    = e.Message;
                return(result);
            }
        }
Ejemplo n.º 2
0
        public async Task <IResult> RateProduct(ProductRatingReviewModel productRatingReview)
        {
            var result = new Result()
            {
                Operation = Operation.Create,
                Status    = Status.Success
            };

            try
            {
                var existingRating = _context.ProductRatingReviews.Where(r => r.UserId == productRatingReview.UserId && r.ProductId == productRatingReview.ProductId).FirstOrDefault();
                if (existingRating != null)
                {
                    result.Message    = "User has already rated the product";
                    result.Status     = Status.Fail;
                    result.StatusCode = HttpStatusCode.BadRequest;
                    return(result);
                }
                _context.ProductRatingReviews.Add(productRatingReview);
                await _context.SaveChangesAsync();

                result.StatusCode = HttpStatusCode.OK;
                return(result);
            }
            catch (Exception e)
            {
                result.Body       = e;
                result.Message    = e.Message;
                result.StatusCode = HttpStatusCode.InternalServerError;
                result.Status     = Status.Error;
                return(result);
            }
        }
Ejemplo n.º 3
0
        public async Task <IResult> DeleteRating(int productId)
        {
            var result = new Result()
            {
                Operation = Operation.Delete,
                Status    = Status.Success
            };

            try
            {
                int userdetail  = _specificClaim.GetSpecificClaim("Id");
                var checkRating = await _productRepository.GetProductRatingById(userdetail, productId);

                if (checkRating.Status != Status.Success)
                {
                    result.Message    = "User does not match";
                    result.Status     = Status.Fail;
                    result.StatusCode = HttpStatusCode.BadRequest;
                    return(result);
                }
                ProductRatingReviewModel reviewModel = checkRating.Body;
                var deleteRating = await _productRepository.DeleteRating(reviewModel.RatingId);

                return(deleteRating);
            }
            catch (Exception e)
            {
                result.Status     = Status.Success;
                result.StatusCode = HttpStatusCode.InternalServerError;
                result.Body       = e;
                result.Message    = e.Message;
                return(result);
            }
        }
Ejemplo n.º 4
0
        public async Task <IResult> GetProductRatingById(int productId)
        {
            var result = new Result()
            {
                Operation = Operation.Read,
                Status    = Status.Success
            };

            try
            {
                var userDetail = _specificClaim.GetSpecificClaim("Id");
                var rating     = await _productRepository.GetProductRatingById(userDetail, productId);

                if (rating.Status != Status.Success)
                {
                    return(rating);
                }
                ProductRatingReviewViewModel viewModel     = new ProductRatingReviewViewModel();
                ProductRatingReviewModel     productRating = rating.Body;
                viewModel.MapFromModel(productRating);
                viewModel.UserName = _specificClaim.GetSpecificClaim("userName");

                result.Body       = viewModel;
                result.StatusCode = HttpStatusCode.OK;
                return(result);
            }
            catch (Exception e)
            {
                result.Status     = Status.Error;
                result.StatusCode = HttpStatusCode.InternalServerError;
                result.Body       = e;
                result.Message    = e.Message;
                return(result);
            }
        }
Ejemplo n.º 5
0
        public async Task <IResult> RateProduct(ProductRatingReviewViewModel productRatingReview)
        {
            var result = new Result()
            {
                Operation = Operation.Create,
                Status    = Status.Success
            };

            try
            {
                ProductRatingReviewModel reviewModel = new ProductRatingReviewModel();
                reviewModel.MapFromViewModel(productRatingReview);
                reviewModel.UserId     = _specificClaim.GetSpecificClaim("Id");
                reviewModel.RatingDate = DateTime.Now;
                var rating = await _productRepository.RateProduct(reviewModel);

                return(rating);
            }
            catch (Exception e)
            {
                result.Body       = e;
                result.Message    = e.Message;
                result.Status     = Status.Error;
                result.StatusCode = HttpStatusCode.InternalServerError;
                return(result);
            }
        }
Ejemplo n.º 6
0
        public async Task <IResult> UpdateRating(ProductRatingReviewViewModel productRatingReview)
        {
            var result = new Result()
            {
                Operation = Operation.Update,
                Status    = Status.Success
            };

            try
            {
                int userDetail = _specificClaim.GetSpecificClaim("Id");
                var rating     = await _productRepository.GetProductRatingById(userDetail, productRatingReview.ProductId);

                if (rating.Status != Status.Success)
                {
                    result.Message    = "Rating by this user does not exist.";
                    result.Status     = Status.Fail;
                    result.StatusCode = HttpStatusCode.BadRequest;
                    return(result);
                }
                ProductRatingReviewModel productRatingModel = new ProductRatingReviewModel();
                productRatingModel.MapFromViewModel(productRatingReview);
                productRatingModel.RatingDate = DateTime.Now;
                productRatingModel.UserId     = rating.Body.UserId;
                var updateRating = await _productRepository.UpdateRating(productRatingModel);

                return(updateRating);
            }
            catch (Exception e)
            {
                result.Body       = e;
                result.Message    = e.Message;
                result.Status     = Status.Error;
                result.StatusCode = HttpStatusCode.InternalServerError;
                return(result);
            }
        }