/// <summary> /// Removes a product from the cart. /// </summary> /// <param name="obj"></param> public void ExecRemoveFromCart(object obj) { CartItem cartItem = (CartItem)CartView.CartUC.Cart.SelectedItem; cartItem.Product.Stock += cartItem.Quantity; Cart.Total -= cartItem.Price; Cart.Items.Remove(cartItem); cartItem.Cart = null; if (cartItem.Product is CPU) { CPUs.Add((CPU)cartItem.Product); } else if (cartItem.Product is GPU) { GPUs.Add((GPU)cartItem.Product); } else if (cartItem.Product is Motherboard) { Motherboards.Add((Motherboard)cartItem.Product); } else if (cartItem.Product is Memory) { Rams.Add((Memory)cartItem.Product); } else if (cartItem.Product is Storage) { StorageComponents.Add((Storage)cartItem.Product); } else if (cartItem.Product is PSU) { PSUs.Add((PSU)cartItem.Product); } else // Case { Cases.Add((Case)cartItem.Product); } }
/// <summary> /// Processes the payment. /// Stock of products decreased, customer gets charged. /// </summary> /// <param name="obj"></param> private async void ExecProceedToPaymentAsync(object obj) { Cart.Customer.Money -= Cart.Total; switch (App.DataSource) { case ConnectionResource.LOCALAPI: await new WebServiceManager <Cart>().PostAsync(Cart); break; case ConnectionResource.LOCALMYSQL: using (var ctx = new MysqlDbContext(ConnectionResource.LOCALMYSQL)) { ctx.Entry(Cart.Customer).State = EntityState.Modified; foreach (var item in Cart.Items) { ctx.Entry(item.Product).State = EntityState.Modified; } ctx.DbSetCarts.Add(Cart); await ctx.SaveChangesAsync(); } break; default: break; } foreach (var item in Cart.Items) { if (item.Product.Stock > 0) { if (item.Product is CPU) { CPUs.Add((CPU)item.Product); } else if (item.Product is GPU) { GPUs.Add((GPU)item.Product); } else if (item.Product is Motherboard) { Motherboards.Add((Motherboard)item.Product); } else if (item.Product is Memory) { Rams.Add((Memory)item.Product); } else if (item.Product is Storage) { StorageComponents.Add((Storage)item.Product); } else if (item.Product is PSU) { PSUs.Add((PSU)item.Product); } else // Case { Cases.Add((Case)item.Product); } } } Cart.Items.Clear(); Cart.Total = 0; }