public async Task <ActionResult <ProductItemResult> > PostProduct(ProductItem product)
        {
            ProductItemResult result = new ProductItemResult();

            try
            {
                var item = await _products.CreateAsync(product);

                result.IsSuccessful = true;
                result.ResultData.Add(item);
            }
            catch (ItemAlreadyExistsException e)
            {
                result.IsSuccessful = false;
                result.ErrorMessages.Add(e.Message);

                _logger?.LogError(e, "Error");

                return(Conflict(result));
            }
            catch (Exception e)
            {
                _logger?.LogDebug($"Unknown error", e);
                throw e;
            }
            return(Created("", result));
        }
        public async Task <ActionResult <ProductItemResult> > GetProduct(string id)
        {
            ProductItemResult result = new ProductItemResult();

            try
            {
                var item = await _products.GetAsync(id);

                result.IsSuccessful = true;
                result.ResultData   = new List <ProductItem>()
                {
                    item
                };
            }
            catch (ItemNotFoundException e)
            {
                result.IsSuccessful = false;
                result.ErrorMessages.Add(e.Message);

                _logger?.LogError(e.Message);

                return(NotFound(result));
            }
            catch (Exception e)
            {
                _logger.LogDebug($"Unknown error", e);
                throw e;
            }
            return(Ok(result));
        }
        public async Task <ActionResult <ProductItemResult> > DeleteProduct(string id)
        {
            ProductItemResult result = new ProductItemResult();

            try
            {
                var deleteResult = await _products.DeleteByIdAsync(id);

                result.IsSuccessful = deleteResult;
                if (!result.IsSuccessful)
                {
                    result.ErrorMessages.Add($"Something went wrong while deleting '{id}'");
                    return(UnprocessableEntity(result));
                }
            }
            catch (ItemNotFoundException e)
            {
                result.IsSuccessful = false;
                result.ErrorMessages.Add(e.Message);
                return(NotFound(result));
            }
            catch (Exception e)
            {
                _logger?.LogDebug($"Unknown error", e);
                throw e;
            }

            return(Ok(result));
        }
        public async Task <ActionResult <ProductItemResult> > GetProducts()
        {
            var products = await _products.GetAllAsync();

            var result = new ProductItemResult()
            {
                IsSuccessful = true,
                ResultData   = products
            };

            return(Ok(result));
        }
        public async Task <ActionResult <ProductItemResult> > UpdateProduct(string id, [FromBody] ProductItem product)
        {
            ProductItemResult result = new ProductItemResult();

            if (id != product.Id)
            {
                result.IsSuccessful = false;
                result.ErrorMessages.Add("Id does not match with object id");
                return(BadRequest(result));
            }
            try
            {
                var update = await _products.UpdateAsync(id, product);

                result.IsSuccessful = true;
                result.ResultData.Add(update);
            }
            catch (ItemNotFoundException e)
            {
                result.IsSuccessful = false;
                result.ErrorMessages.Add(e.Message);

                return(NotFound(result));
            }
            catch (ItemAlreadyExistsException e)
            {
                result.IsSuccessful = false;
                result.ErrorMessages.Add(e.Message);

                return(Conflict(result));
            }
            catch (Exception e)
            {
                _logger?.LogDebug($"Unknown error", e);
                throw e;
            }
            return(Ok(result));
        }
示例#6
0
        public ProductItemResult ToCanonicalResult()
        {
            ProductItemResult result = new ProductItemResult();

            result.ASIN            = this.ASIN;
            result.DetailPage      = this.DetailPageURL;
            result.Title           = this.ItemAttributes.Title;
            result.LowestNewPrice  = "";
            result.LowestUsedPrice = "";
            result.ImageUrl        = "";

            if ((null != this.ImageSets) && (null != this.ImageSets[0]) && (null != this.ImageSets[0].ImageSet) &&
                (null != this.ImageSets[0].ImageSet[0]) && (null != this.ImageSets[0].ImageSet[0].SmallImage))
            {
                result.ImageUrl = this.ImageSets[0].ImageSet[0].SmallImage.URL;
            }

            if ((null != this.OfferSummary) && (null != this.OfferSummary.LowestNewPrice))
            {
                result.LowestNewPrice = this.OfferSummary.LowestNewPrice.FormattedPrice;
                Int32.TryParse(this.OfferSummary.TotalNew, out result.TotalNew);
            }

            if ((null != this.OfferSummary) && (null != this.OfferSummary.LowestUsedPrice))
            {
                result.LowestUsedPrice = this.OfferSummary.LowestUsedPrice.FormattedPrice;
                Int32.TryParse(this.OfferSummary.TotalUsed, out result.TotalUsed);
            }

            /********* The logic below makes sense if multiple offers were being returned, but as of Nov 11, 2011, Amazon
             * ***** changed its APIs to only return one offer per item.  ***/
            // go thru the offers and try to get the listing id of the lowest offer in each condition category
            int lowestNewPriceAmount  = Int32.MaxValue;
            int lowestUsedPriceAmount = Int32.MaxValue;

            if ((null != Offers) && (null != Offers.Offer))
            {
                foreach (AmazonApi.Offer offer in Offers.Offer)
                {
                    if (Condition.New.ToString().Equals(offer.OfferAttributes.Condition))
                    {
                        // go thru each listing and compare price with known lowest new price
                        if ((null != result.LowestNewPrice) && (null != offer.OfferListing))
                        {
                            foreach (OfferListing listing in offer.OfferListing)
                            {
                                // if (result.LowestNewPrice.Equals(listing.Price.FormattedPrice))
                                int listingAmount;
                                if (Int32.TryParse(listing.Price.Amount, out listingAmount) &&
                                    (lowestNewPriceAmount > listingAmount))
                                {
                                    lowestNewPriceAmount      = listingAmount;
                                    result.LowestNewListingId = listing.OfferListingId;
                                    result.LowestNewPrice     = listing.Price.FormattedPrice;
                                }
                            }
                        }
                    }
                    else if (Condition.Used.ToString().Equals(offer.OfferAttributes.Condition))
                    {
                        // go thru each listing and compare price with known lowest new price
                        if ((null != result.LowestUsedPrice) && (null != offer.OfferListing))
                        {
                            foreach (OfferListing listing in offer.OfferListing)
                            {
                                // if (result.LowestUsedPrice.Equals(listing.Price.FormattedPrice))
                                int listingAmount;
                                if (Int32.TryParse(listing.Price.Amount, out listingAmount) &&
                                    (lowestUsedPriceAmount > listingAmount))
                                {
                                    lowestUsedPriceAmount      = listingAmount;
                                    result.LowestUsedListingId = listing.OfferListingId;
                                    result.LowestUsedPrice     = listing.Price.FormattedPrice;
                                }
                            }
                        }
                    } // else
                }     // foreach
            }         // if

            /****
             * // go thru the offers and try to get the listing id of the lowest offer in each condition category
             * if ((null != Offers) && (null != Offers.Offer) && (null != Offers.Offer[0]) && (null != Offers.Offer[0].OfferListing))
             * {
             *  OfferListing listing = Offers.Offer[0].OfferListing[0];
             *  if (null != listing)
             *  {
             *      if (Condition.New.ToString().Equals(Offers.Offer[0].OfferAttributes.Condition))
             *      {
             *          result.LowestNewListingId = listing.OfferListingId;
             *          result.LowestNewPrice = listing.Price.FormattedPrice;
             *      }
             *      else if (Condition.Used.ToString().Equals(Offers.Offer[0].OfferAttributes.Condition))
             *      {
             *          result.LowestUsedListingId = listing.OfferListingId;
             *          result.LowestUsedPrice = listing.Price.FormattedPrice;
             *      }
             *  }
             * }
             * ****/

            return(result);
        }