コード例 #1
0
        public async Task <ActionResult <Cart> > ApplyVoucher(String Owner, String VoucherId, DateTime date_voucher)
        {
            Cart cart;

            try
            {
                IList <Cart> carts = _cartRepository.GetAll();
                cart = carts.FirstOrDefault <Cart>(x => x.Owner == Owner);
                if (cart == null)
                {
                    throw new Exception(String.Format("The {0}'s cart  does not exist in the database", Owner));
                }

                IList <VoucherStore> voucherValids = await _voucherRepository.validVouchers(cart.StoreId);

                if (voucherValids.Any(c => c.VoucherId == VoucherId))
                {
                    Voucher voucher = await _voucherRepository.GetByID(VoucherId);

                    IList <Tuple <Product, int> > listProducts = new List <Tuple <Product, int> >();
                    decimal total = 0;
                    foreach (CartItem cartItem in cart.Items)
                    {
                        total = total + cartItem.Product.Price * cartItem.Cant;
                        listProducts.Add(cartItem.GetTuple());
                    }
                    decimal discount = voucher.Calculate(listProducts, date_voucher);
                    cart.Total          = total;
                    cart.Total_discount = total - discount;
                    return(cart);
                }
                else
                {
                    return(BadRequest(String.Format("Voucher {0} invalid in Store {1}", VoucherId, cart.StoreId)));
                }
            }
            catch (Exception ex)
            {
                if (ex is MiniMartException)
                {
                    return(BadRequest(((MiniMartException)ex).MinimartMessage));
                }
                else
                {
                    return(BadRequest(ex.Message));
                }
            }
        }
コード例 #2
0
 public async Task <ActionResult <Voucher> > GetByID(string id)
 {
     return(await _voucherRepository.GetByID(id));
 }