Ejemplo n.º 1
0
        public async Task <ActionResult <BasketData> > UpdateAllBasketAsync([FromBody] UpdateBasketRequest data)
        {
            if (data.Items == null || !data.Items.Any())
            {
                return(BadRequest("Need to pass at least one basket line"));
            }

            // Retrieve the current basket
            var basket = await _basket.GetByIdAsync(data.BuyerId) ?? new BasketData(data.BuyerId);

            var catalogItems = await _catalog.GetCatalogItemsAsync(data.Items.Select(x => x.ProductId));

            foreach (var bitem in data.Items)
            {
                var catalogItem = catalogItems.SingleOrDefault(ci => ci.Id == bitem.ProductId);
                if (catalogItem == null)
                {
                    return(BadRequest($"Basket refers to a non-existing catalog item ({bitem.ProductId})"));
                }

                basket.Items.Add(new BasketDataItem
                {
                    Id          = bitem.Id,
                    ProductId   = catalogItem.Id.ToString(),
                    ProductName = catalogItem.Name,
                    PictureUrl  = catalogItem.PictureUri,
                    UnitPrice   = catalogItem.Price,
                    Quantity    = bitem.Quantity
                });
            }

            await _basket.UpdateAsync(basket);

            return(basket);
        }
Ejemplo n.º 2
0
        public async Task <ActionResult <bool> > UpdateAllBasketAsync([FromBody] UpdateBasketRequest basketRequest)
        {
            if (basketRequest.Items == null || !basketRequest.Items.Any())
            {
                return(BadRequest("Need to provide at least one basket item"));
            }

            var basket = await _basketService.GetByUserIdAsync(basketRequest.UserId) ?? new BasketModel(basketRequest.UserId);

            var productItems = await _productService.GetProductsAsync(basketRequest.Items.Select(x => x.ProductId));

            foreach (var item in basketRequest.Items)
            {
                var productItem = productItems.SingleOrDefault(p => p.Id == item.ProductId);
                if (productItem == null)
                {
                    return(BadRequest($"({item.ProductId}) does not exist."));
                }

                basket.Items.Add(new BasketItemModel()
                {
                    Id              = item.BasketId,
                    ProductId       = productItem.Id,
                    ProductName     = productItem.Name,
                    ProductImageUrl = productItem.ImageUrl,
                    UnitPrice       = productItem.Price,
                    Quantity        = item.Quantity
                });
            }

            var result = await _basketService.UpdateAsync(basket);

            return(result);
        }
    public async Task <ActionResult <BasketData> > UpdateAllBasketAsync(
        [FromBody] UpdateBasketRequest data,
        [FromHeader] string authorization)
    {
        BasketData basket;

        if (data.Items is null || !data.Items.Any())
        {
            basket = new();
        }
        public async Task <ActionResult <BasketData> > UpdateAllBasketAsync([FromBody] UpdateBasketRequest data)
        {
            if (data.Items == null || !data.Items.Any())
            {
                return(BadRequest("Need to pass at least one basket line"));
            }

            // Retrieve the current basket
            var basket = await _basket.GetById(data.BuyerId) ?? new BasketData(data.BuyerId);

            var catalogItems = await _catalog.GetCatalogItemsAsync(data.Items.Select(x => x.ProductId));

            // group by product id to avoid duplicates
            var itemsCalculated = data
                                  .Items
                                  .GroupBy(x => x.ProductId, x => x, (k, i) => new { productId = k, items = i })
                                  .Select(groupedItem =>
            {
                var item      = groupedItem.items.First();
                item.Quantity = groupedItem.items.Sum(i => i.Quantity);
                return(item);
            });

            foreach (var bitem in itemsCalculated)
            {
                var catalogItem = catalogItems.SingleOrDefault(ci => ci.Id == bitem.ProductId);
                if (catalogItem == null)
                {
                    return(BadRequest($"Basket refers to a non-existing catalog item ({bitem.ProductId})"));
                }

                var itemInBasket = basket.Items.FirstOrDefault(x => x.ProductId == bitem.ProductId);
                if (itemInBasket == null)
                {
                    basket.Items.Add(new BasketDataItem()
                    {
                        Id          = bitem.Id,
                        ProductId   = catalogItem.Id,
                        ProductName = catalogItem.Name,
                        PictureUrl  = catalogItem.PictureUri,
                        UnitPrice   = catalogItem.Price,
                        Quantity    = bitem.Quantity
                    });
                }
                else
                {
                    itemInBasket.Quantity = bitem.Quantity;
                }
            }

            await _basket.UpdateAsync(basket);

            return(basket);
        }
Ejemplo n.º 5
0
        public async Task <ActionResult <BasketData> > UpdateAllBasketAsync([FromBody] UpdateBasketRequest data)
        {
            if (data.Items == null || !data.Items.Any())
            {
                return(BadRequest("Need to pass at least one basket line"));
            }

            // Retrieve the current basket
            var basket = await _basket.GetById(data.StudentId) ?? new BasketData(data.StudentId);

            var scholarshipItems = await _scholarship.GetScholarshipItemsAsync(data.Items.Select(x => x.ScholarshipItemId));

            // group by Scholarship Item id to avoid duplicates
            var itemsCalculated = data
                                  .Items
                                  .GroupBy(x => x.ScholarshipItemId, x => x, (k, i) => new { scholarshipItemId = k, items = i })
                                  .Select(groupedItem =>
            {
                var item   = groupedItem.items.First();
                item.Slots = groupedItem.items.Sum(i => i.Slots);
                return(item);
            });

            foreach (var bitem in itemsCalculated)
            {
                var scholarshipItem = scholarshipItems.SingleOrDefault(ci => ci.Id == bitem.ScholarshipItemId);
                if (scholarshipItem == null)
                {
                    return(BadRequest($"Basket refers to a non-existing scholarship item ({bitem.ScholarshipItemId})"));
                }

                var itemInBasket = basket.Items.FirstOrDefault(x => x.ScholarshipItemId == bitem.ScholarshipItemId);
                if (itemInBasket == null)
                {
                    basket.Items.Add(new BasketDataItem()
                    {
                        Id = bitem.Id,
                        ScholarshipItemId   = scholarshipItem.Id,
                        ScholarshipItemName = scholarshipItem.Name,
                        PictureUrl          = scholarshipItem.PictureUri,
                        SlotAmount          = scholarshipItem.Amount,
                        Slots = bitem.Slots
                    });
                }
                else
                {
                    itemInBasket.Slots = bitem.Slots;
                }
            }

            await _basket.UpdateAsync(basket);

            return(basket);
        }
Ejemplo n.º 6
0
        public async Task <ActionResult <BasketData> > UpdateAllBasketAsync([FromBody] UpdateBasketRequest data, [FromHeader] string authorization)
        {
            var basket = new BasketData(data.BuyerId);

            if (data.Items != null && data.Items.Any())
            {
                var catalogItems = await _catalog.GetCatalogItemsAsync(data.Items.Select(x => x.ProductId));

                // group by product id to avoid duplicates
                var itemsCalculated = data
                                      .Items
                                      .GroupBy(x => x.ProductId, x => x, (k, i) => new { productId = k, items = i })
                                      .Select(groupedItem =>
                {
                    var item      = groupedItem.items.First();
                    item.Quantity = groupedItem.items.Sum(i => i.Quantity);
                    return(item);
                });

                foreach (var bitem in itemsCalculated)
                {
                    var catalogItem = catalogItems.SingleOrDefault(ci => ci.Id == bitem.ProductId);
                    if (catalogItem == null)
                    {
                        return(BadRequest($"Basket refers to a non-existing catalog item ({bitem.ProductId})"));
                    }

                    basket.Items.Add(new BasketDataItem()
                    {
                        Id          = bitem.Id,
                        ProductId   = catalogItem.Id,
                        ProductName = catalogItem.Name,
                        PictureUrl  = catalogItem.PictureUri,
                        UnitPrice   = catalogItem.Price,
                        Quantity    = bitem.Quantity
                    });
                }
            }

            await _basket.UpdateAsync(basket, authorization);

            return(basket);
        }
        public async Task <ActionResult <BasketData> > UpdateAllBasketAsync([FromBody] UpdateBasketRequest data)
        {
            if (data.Items == null || !data.Items.Any())
            {
                return(BadRequest("Need to pass at least one basket line"));
            }
            if (data.Coupon == null)
            {
                return(BadRequest("Something wrong with your coupon"));
            }
            if (data.BuyerId == null)
            {
                return(BadRequest("Something wrong with your BuyerID"));
            }
            // Retrieve the current basket
            var basket = await _basket.GetById(data.BuyerId) ?? new BasketData(data.BuyerId);

            var catalogItems = await _catalog.GetCatalogItemsAsync(data.Items.Select(x => x.ProductId));

            if (catalogItems == null)
            {
                return(BadRequest("What?"));
            }
            basket.Coupon = basket.Coupon ?? new Coupon
            {
                Discount = 0
            };

            if (basket.Coupon == null)
            {
                return(BadRequest("Something wrong with your coupon part 2"));
            }

            // group by product id to avoid duplicates
            var itemsCalculated = data
                                  .Items
                                  .GroupBy(x => x.ProductId, x => x, (k, i) => new { productId = k, items = i })
                                  .Select(groupedItem =>
            {
                var item      = groupedItem.items.First();
                item.Quantity = groupedItem.items.Sum(i => i.Quantity);
                return(item);
            });

            foreach (var bitem in itemsCalculated)
            {
                var catalogItem = catalogItems.SingleOrDefault(ci => ci.Id == bitem.ProductId);
                if (catalogItem == null)
                {
                    return(BadRequest($"Basket refers to a non-existing catalog item ({bitem.ProductId})"));
                }

                var itemInBasket = basket.Items.FirstOrDefault(x => x.ProductId == bitem.ProductId);
                if (itemInBasket == null)
                {
                    basket.Items.Add(new BasketDataItem()
                    {
                        Id           = bitem.Id,
                        ProductId    = catalogItem.Id,
                        ProductName  = catalogItem.Name,
                        PictureUrl   = catalogItem.PictureUri,
                        UnitPrice    = catalogItem.Price,
                        Quantity     = bitem.Quantity,
                        isDiscounted = false
                    });
                }
                else
                {
                    itemInBasket.Quantity = bitem.Quantity;
                }
            }

            if (basket.Coupon == null || basket.Items == null || basket.BuyerId == null)
            {
                return(BadRequest("Something wrong with your coupon part 2"));
            }

            Console.WriteLine($"{basket.BuyerId.ToString()} , {basket.Items.Count.ToString()} , {basket.Coupon.ToString()}, Debug Spot");

            await _basket.UpdateAsync(basket);

            return(basket);
        }