/// <summary>
        /// Insert Cart Items data
        /// </summary>
        /// <param name="cartMasterId"></param>
        /// <param name="cartItem"></param>
        /// <returns></returns>
        public bool InsertCartItems(int cartMasterId, InsertCartDetails cartItem)
        {
            try
            {
                if (cartMasterId == 0)
                {
                    cartMasterId = (from cartMaster in _orderManagementContext.TblCartMaster
                                    where cartMaster.TblCustomerId == cartItem.CustomerId &&
                                    cartMaster.TblRestaurantId == cartItem.RestaurantId && cartMaster.IsActive
                                    select cartMaster.Id).FirstOrDefault();
                }

                if (cartMasterId == 0)
                {
                    throw new Exception("Error adding cart item. Try again later");
                }

                bool duplicateMenuCheck = (from cartItems in _orderManagementContext.TblCartItems
                                           where cartItems.TblCartMasterId == cartMasterId &&
                                           cartItems.TblMenuId == cartItem.MenuId
                                           select cartItems.Id).Count() > 0;
                if (duplicateMenuCheck)
                {
                    throw new Exception($"Menu {cartItem.MenuId} for Restaurant {cartItem.RestaurantId} already exists! Try updating the menu using the " +
                                        $"update endpoint.");
                }

                TblCartItems tblCartItems = new TblCartItems()
                {
                    TblCartMasterId        = cartMasterId,
                    TblMenuId              = cartItem.MenuId,
                    Quantity               = cartItem.Quantity,
                    IsItemAvailable        = true,
                    RecordTimeStampCreated = DateTime.Now
                };

                _orderManagementContext.Add(tblCartItems);

                return(SaveChanges());
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        /// <summary>
        /// Updating cart items details
        /// </summary>
        /// <param name="tblCartItems"></param>
        /// <returns></returns>
        public bool UpdateCartItems(InsertCartDetails updateCartDetails)
        {
            try
            {
                if (!DoesCartMasterExists(updateCartDetails.CustomerId))
                {
                    throw new Exception("No items are there in your cart. Continue shopping");
                }

                int tblCartMasterId = (from cartMaster in _orderManagementContext.TblCartMaster
                                       where cartMaster.TblCustomerId == updateCartDetails.CustomerId &&
                                       cartMaster.TblRestaurantId == updateCartDetails.RestaurantId && cartMaster.IsActive
                                       select cartMaster.Id).FirstOrDefault();

                if (tblCartMasterId == 0)
                {
                    throw new Exception($"Cannot update menu for restaurant {updateCartDetails.RestaurantId}! Cart contains items from different restaurant");
                }

                if (!DoesCartItemsExists(tblCartMasterId))
                {
                    throw new Exception("Error in fetching cart items");
                }

                TblCartItems tblCartItem = (from cartItems in _orderManagementContext.TblCartItems
                                            where cartItems.TblCartMasterId == tblCartMasterId &&
                                            cartItems.TblMenuId == updateCartDetails.MenuId
                                            select cartItems).FirstOrDefault();

                if (tblCartItem == null)
                {
                    throw new Exception($"Menu with id {updateCartDetails.MenuId} not found for the customer id {updateCartDetails.CustomerId}");
                }

                tblCartItem.Quantity = updateCartDetails.Quantity;

                return(SaveChanges());
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
示例#3
0
        public ActionResult Update(int customerId, [FromBody] InsertCartDetails updateCartDetails)
        {
            try
            {
                _logService.LogMessage($"Customer Id received at endpoint : api/UpdateCart, Customer ID : {customerId}");
                Task <bool> result = _cartBusiness.UpdateCartDetails(customerId, updateCartDetails);

                if (result.Result)
                {
                    return(Accepted($"Cart details updated for customer : {customerId}"));
                }

                return(this.StatusCode((int)HttpStatusCode.InternalServerError, $"Error updating cart details for customer : {customerId}"));
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }
        }
示例#4
0
        public ActionResult Insert(int customerId, [FromBody] InsertCartDetails insertCartDetails)
        {
            try
            {
                _logService.LogMessage($"Customer Id received at endpoint : api/InsertCart, Customer ID : {customerId}");
                Task <bool> result = _cartBusiness.InsertCartDetails(customerId, insertCartDetails);

                if (result.Result)
                {
                    _logService.LogMessage($"Cart details added for customer : {customerId}");
                    return(Created("GetCartDetails", new { customerId }));
                }

                _logService.LogMessage($"Error inserting cart details for customer : {customerId}");
                return(this.StatusCode((int)HttpStatusCode.InternalServerError, $"Error inserting cart details for customer : {customerId}"));
            }
            catch (Exception ex)
            {
                _logService.LogMessage(ex.Message);
                return(BadRequest(ex.Message));
            }
        }
示例#5
0
        /// <summary>
        /// Updates the cart item based on the customerId & menu Id
        /// </summary>
        /// <param name="customerId"></param>
        /// <param name="updateCartDetails"></param>
        /// <returns></returns>
        public async Task <bool> UpdateCartDetails(int customerId, InsertCartDetails updateCartDetails)
        {
            try
            {
                if (customerId < 1)
                {
                    throw new Exception("Customer Id cannot be less than one");
                }

                if (customerId != updateCartDetails.CustomerId)
                {
                    throw new Exception("Customer Id in URI doesn't match with the one in Body");
                }

                if (updateCartDetails.RestaurantId < 0)
                {
                    throw new Exception("Restaurant Id cannot be less than one");
                }

                if (updateCartDetails.MenuId < 1)
                {
                    throw new Exception("Menu Id cannot be less than one");
                }

                if (updateCartDetails.Quantity < 1)
                {
                    throw new Exception("Quantity cannot be less than one");
                }

                CartItemsSenderDetails cartSenderItems = new CartItemsSenderDetails()
                {
                    RestaurantId = updateCartDetails.RestaurantId,
                    MenuId       = updateCartDetails.MenuId,
                    Quantity     = updateCartDetails.Quantity
                };
                string serializedCartSenderItems = JsonConvert.SerializeObject(cartSenderItems);

                CartItems cartItem;
                using (HttpClient httpClient = new HttpClient())
                {
                    StringContent       stringContent       = new StringContent(serializedCartSenderItems, Encoding.UTF8, "application/json");
                    HttpResponseMessage httpResponseMessage = await httpClient.PostAsync($"http://localhost:10601/api/InsertDetailsCheck/", stringContent);

                    if (httpResponseMessage.IsSuccessStatusCode)
                    {
                        string json = await httpResponseMessage.Content.ReadAsStringAsync();

                        cartItem = JsonConvert.DeserializeObject <CartItems>(json);
                    }
                    else
                    {
                        throw new Exception($"No menu present for Restaurant {updateCartDetails.RestaurantId} and Menu {updateCartDetails.MenuId}");
                    }
                }

                if (!cartItem.IsItemAvailable)
                {
                    throw new Exception($"Cannot add {updateCartDetails.Quantity} for Menu {updateCartDetails.MenuId}. Atmost {cartItem.Quantity} can be added.");
                }

                bool result = _cartRepository.UpdateCartItems(updateCartDetails);

                return(result);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }