public bool PlacedOrder(Guid UserId, OrderDataDTO orderData, OrderVariantDTO order) { DateTime today = System.DateTime.Now; orderData.OrderDate = today; orderData.DeliveryDate = today.AddDays(6); int statusid = dbContext.Status.Where(s => s.description == "Progessing").Select(s => s.ID).FirstOrDefault(); Debug.WriteLine(dbContext.Status.Where(s => s.description == "Progessing").Select(s => s.ID).FirstOrDefault()); orderData.StatusID = statusid; orderData.ID = Guid.NewGuid(); Order newOrder = OrderDataMapper.Map <OrderDataDTO, Order>(orderData); dbContext.Orders.Add(newOrder); //orderplcaed mapping insertion OrderPlaced orderPlaced = new OrderPlaced(); orderPlaced.ID = Guid.NewGuid(); orderPlaced.UserID = UserId; orderPlaced.OrderID = orderData.ID; ///end dbContext.OrderPlaceds.Add(orderPlaced); foreach (CartVariantDTO variant in order.ItemOrders.Items) { OrderPlacedVariant variantData = VariantMapper.Map <CartVariantDTO, OrderPlacedVariant>(variant); variantData.ID = Guid.NewGuid(); variantData.OrderPlacedID = orderPlaced.ID; dbContext.OrderPlacedVariants.Add(variantData); } foreach (var cartitem in order.ItemOrders.Items) { dbContext.Variants.FirstOrDefault(p => p.ID == cartitem.VariantID).Inventory -= cartitem.Quantity; dbContext.Variants.FirstOrDefault(p => p.ID == cartitem.VariantID).QuantitySold += cartitem.Quantity; dbContext.Categories.FirstOrDefault(c => c.ID == cartitem.Variant.Product.CategoryID).ProductsSold += cartitem.Quantity; } dbContext.SaveChanges(); return(true); }
/// <summary> /// placeorder of user with address id and userId /// </summary> /// <param name="AddressId"></param> /// <param name="UserId"></param> /// <returns></returns> public bool PlacedOrder(Guid AddressId, Guid UserId) { VariantDTO variantDTO = new VariantDTO(); //validation for variant order //1.get the cart data bool addressVerfiyed = orderDatabase.IsVerfiyAddress(AddressId, UserId); double totalAmount = 0; if (addressVerfiyed) { try { CartsVariantDTO cartsData = cartOrderHelper.GetCartByUserId(UserId); foreach (CartVariantDTO variantAtCart in cartsData.Items) { //2.get each cart varinat data from varinat table variantDTO = cartOrderHelper.GetVariantDetails(variantAtCart.VariantID); //3.check all cart data inventory if (!cartOrderHelper.IsInventoryAvaiable(variantAtCart.Quantity, variantDTO.Inventory)) { throw new OutOfStockException(); } //3.orderlimit if (!cartOrderHelper.IsUserCanOrder(variantAtCart, variantDTO)) { throw new UserOrderLimitExceddException(); } variantAtCart.SellingPrice = variantDTO.ListingPrice - variantDTO.Discount; totalAmount = totalAmount + variantAtCart.SellingPrice * variantAtCart.Quantity; } //validation end OrderVariantDTO order = new OrderVariantDTO(); order.ItemOrders = cartsData; OrderDataDTO orderData = new OrderDataDTO(); orderData.DeliveryAddressID = AddressId; orderData.TotalAmount = totalAmount; orderData.isCancelled = "N"; bool isOrderPlaced = orderDatabase.PlacedOrder(UserId, orderData, order); if (isOrderPlaced) { foreach (CartVariantDTO variant in cartsData.Items) { Guid Id = variant.ID; bool t = cartOrderHelper.DeleteCartVariant(Id, UserId); } return(true); } else { return(false); } } catch (OutOfStockException) { return(false); } catch (UserOrderLimitExceddException) { return(false); } catch (Exception ex) { return(false); } } else { throw new UserDetailIsWrong(); } }