Exemplo n.º 1
0
        /// <summary>
        /// Get price by variation
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task <ResultModel> GetPriceByVariationAsync(ProductPriceVariationViewModel model)
        {
            var resultModel = new ResultModel();

            var prod = await Context.Products.Include(i => i.ProductPrices).FirstOrDefaultAsync(x => x.Id == model.ProductId);

            if (prod != null)
            {
                if (model.VariationId is null)
                {
                    resultModel.IsSuccess = true;
                    resultModel.Result    = new { Price = prod.PriceWithDiscount * model.Quantity };
                    return(resultModel);
                }

                var productVariation = Context.ProductVariations.FirstOrDefault(x => x.Id == model.VariationId);

                if (productVariation is null)
                {
                    resultModel.IsSuccess = false;
                    resultModel.Errors.Add(new ErrorModel(string.Empty, "Invalid parameters"));
                    return(resultModel);
                }

                resultModel.IsSuccess = true;
                resultModel.Result    = new { Price = productVariation.Price * model.Quantity };
                return(resultModel);
            }

            resultModel.IsSuccess = false;
            resultModel.Errors.Add(new ErrorModel(string.Empty, "Invalid parameters"));


            return(resultModel);
        }
Exemplo n.º 2
0
 public async Task <JsonResult> GetPriceByVariation(ProductPriceVariationViewModel model)
 {
     if (ModelState.IsValid)
     {
         return(await JsonAsync(_productService.GetPriceByVariationAsync(model)));
     }
     ModelState.AddCommerceError(CommerceErrorKeys.InvalidModel);
     return(Json(model));
 }