public bool updateCakeInformationInDatabase(CakeModel changedTo) { string sqlUpdateString = $"UPDATE CAKE " + $"SET NAME = @Name, " + $"IMPORTPRICE = @ImportPrice, " + $"SELLINGPRICE = @SellingPrice, " + $"AMOUNT = @Amount, " + $"CATEGORYID = {changedTo.Category.ID}, " + $"UNIT = @Unit, " + $"INFORMATION = @Information " + $"WHERE ID = {changedTo.ID}"; using (var cnn = new SQLiteConnection(_connectionString)) { var output = cnn.Execute(sqlUpdateString, changedTo); if (output == 1) { return(true); } else { return(false); } } }
private void AddProductScreen_Loaded(object sender, RoutedEventArgs e) { Product = new CakeModel(); CategoryList = new ObservableCollection <CategoryModel>(DatabaseAccess.LoadAllCategories()); cbbCategory.ItemsSource = CategoryList; }
public async Task <CakeModel> Add(CakeModel cake) { if (cake == null) { throw new NullReferenceException(nameof(cake)); } cake.Id = new Guid(); await db.AddAsync(cake); return(cake); }
public int addCake(CakeModel cakeModel) // without cake ID { if (cakeModel.Category.ID >= 0) { string insertSQLstring = $"INSERT INTO CAKE(NAME,IMPORTPRICE,SELLINGPRICE,AMOUNT,CATEGORYID,UNIT,INFORMATION) " + $"VALUES (@Name,@ImportPrice,@SellingPrice,@Amount,{cakeModel.Category.ID},@Unit,@Information)"; using (var cnn = new SQLiteConnection(_connectionString)) { // insert try { cnn.Execute(insertSQLstring, cakeModel); } catch (Exception ex) { Console.WriteLine(ex.Message); return(0); } } } else { // add new category string newCategorryName = cakeModel.Category.Name; var categoeyServices = new CategoryServices(); categoeyServices.AddCategory(new CategoryModel { Name = newCategorryName }); // load new category ID string sqlLoadId = "SELECT IFNULL(MAX(ID), 0) FROM CATEGORY"; int currentMaxCategoryId; using (var cnn = new SQLiteConnection(_connectionString)) { currentMaxCategoryId = cnn.QueryFirst <int>(sqlLoadId, new DynamicParameters()); // add cake normally string insertSQLstring = $"INSERT INTO CAKE(NAME,IMPORTPRICE,SELLINGPRICE,AMOUNT,CATEGORYID,UNIT,INFORMATION) " + $"VALUES (@Name,@ImportPrice,@SellingPrice,@Amount,{currentMaxCategoryId},@Unit,@Information)"; cnn.Execute(insertSQLstring, cakeModel); } } string SQLoadMaxIDCake = "SELECT ifnull(max(id),0) from cake"; int currentMaxCakeId = 0; using (var cnn = new SQLiteConnection(_connectionString)) { currentMaxCakeId = cnn.QueryFirst <int>(SQLoadMaxIDCake, new DynamicParameters()); } return(currentMaxCakeId); }
private void doneButtonClick(int CakeID, MainViewModel param) { CakeModel newCake = new CakeModel(); newCake.Name = Name; newCake.ImportPrice = ImportPrice; newCake.SellingPrice = SellingPrice; newCake.Amount = Amount + OldAmount; newCake.Information = Information; newCake.Unit = Unit; newCake.ID = CakeID; if (CategoryID == -1) { CategoryModel tempCategory = new CategoryModel(); tempCategory.ID = -1; tempCategory.Name = CategoryNameData; newCake.Category = tempCategory; } else { newCake.Category = AllCategory[CategoryID]; } int ID = CakeID; cakeServices.updateCakeInformationInDatabase(newCake); if (ImageSource == null) { ImageSource = ""; } var directory = AppDomain.CurrentDomain.BaseDirectory; directory += "Database\\Images\\CakeImages"; if (!Directory.Exists(directory)) { Directory.CreateDirectory(directory); } string fileName = ID.ToString() + ".png"; string sourcePath = ImageSource; string targetPath = directory; string sourceFile = System.IO.Path.Combine(sourcePath, ""); string destFile = System.IO.Path.Combine(targetPath, fileName); if (sourceFile != destFile) { System.GC.Collect(); System.GC.WaitForPendingFinalizers(); System.IO.File.Copy(sourceFile, destFile, true); } MessageBox.Show("Cập nhật sản phẩm mới thành công!!!"); mainViewModel.SelectedViewModel = new HomePageViewModel(param); }
public CakeModel loadSingleCake(int IdToLoad) { var result = new CakeModel(); var catetoryService = new CategoryServices(); string sqlString = $"SELECT * FROM CAKE WHERE ID = {IdToLoad}"; using (var cnn = new SQLiteConnection(_connectionString)) { var output = cnn.QueryFirst <CakeModel>(sqlString, new DynamicParameters()); int categoryID = cnn.QueryFirst <int>($"SELECT CategoryID FROM CAKE WHERE ID = {IdToLoad}"); result = output; var categoryService = new CategoryServices(); result.Category = categoryService.LoadSingleCategory(categoryID); } return(result); }
public UpdateCakePageViewModel(int CakeID, MainViewModel param) { this.mainViewModel = param; AllCategory = new ObservableCollection <CategoryModel>(); List <CategoryModel> tempList = new List <CategoryModel>(); tempList = categoryServices.LoadAll(); for (int i = 0; i < tempList.Count(); i++) { AllCategory.Add(tempList[i]); } addImageButtonCommand = new RelayCommand(o => updateImageButtonClick()); doneButtonCommand = new RelayCommand(o => doneButtonClick(CakeID, param)); CakeModel myCake = new CakeModel(); myCake = cakeServices.loadSingleCake(CakeID); Name = myCake.Name; ImportPrice = myCake.ImportPrice; SellingPrice = myCake.SellingPrice; Amount = 0; Information = myCake.Information; Unit = myCake.Unit; OldAmount = myCake.Amount; for (int i = 0; i < AllCategory.Count(); i++) { if (AllCategory[i].Name == myCake.Category.Name) { CategoryID = i; } } //Image var directory = AppDomain.CurrentDomain.BaseDirectory; directory += "Database\\Images\\CakeImages\\"; ImageSource = directory + myCake.ID + ".png"; OnPropertyChanged(nameof(Name)); OnPropertyChanged(nameof(ImportPrice)); OnPropertyChanged(nameof(SellingPrice)); OnPropertyChanged(nameof(Amount)); OnPropertyChanged(nameof(Information)); OnPropertyChanged(nameof(Unit)); OnPropertyChanged(nameof(ImageSource)); }
public IActionResult Create([FromForm] CakeModel newCakeModel) { if (ModelState.IsValid) { using (var context = new CakeDbContext()) { // Create database if it does not exist context.Database.EnsureCreated(); context.CakeDbSet.Add(newCakeModel); // Commit changes context.SaveChanges(); } // return RedirectToAction("Index"); return(RedirectToAction("CakeChart")); } else { return(View(newCakeModel)); } }
public EditProductPage(CakeModel cakeModel) { InitializeComponent(); Product = cakeModel; this.DataContext = Product; }
private void ConfirmCheckoutBtn_Click(object sender, RoutedEventArgs e) { OrderModel order = new OrderModel(); int totalPrice = 0; if (DirectBtn.IsChecked == true) { if (!int.TryParse(tbDirectTotalPrice.Text, out totalPrice)) { MessageBox.Show("Vui lòng nhập tổng giá là 1 số!"); return; } order.OrderStatus = 1; order.ShippingFee = 0; order.TotalPrice = totalPrice; order.IsDirect = 1; } else if (DeliveryBtn.IsChecked == true) { int shippingFee = 30000; if (!int.TryParse(tbShippingFee.Text, out shippingFee)) { MessageBox.Show("Vui lòng nhập phí giao hàng là 1 số!"); return; } if (tbShippingAddress.Text.Trim().Length == 0) { MessageBox.Show("Vui lòng nhập địa chỉ giao hàng!"); return; } if (!int.TryParse(tbDirectTotalPrice.Text, out totalPrice)) { MessageBox.Show("Vui lòng nhập tổng thu là 1 số!"); return; } order.ShippingAddress = tbShippingAddress.Text; order.ShippingFee = shippingFee; order.OrderStatus = 0; order.TotalPrice = totalPrice + shippingFee; order.IsDirect = 0; } order.OrderDate = DateTime.Now.ToString(); if (Cart.IsOldCustomer) { int customerId = Cart.OldCustomer.CustomerId; order.CustomerId = customerId; } else { int customerId = DatabaseAccess.SaveCustomer(Cart.NewCustomer); Cart.NewCustomer.CustomerId = customerId; order.CustomerId = customerId; } foreach (var cake in Cart.CartList) { CakeModel cakeModel = DatabaseAccess.FindCakeById(cake.CakeId); int cakeQuantity = cakeModel.CakeQuantity; cakeModel.CakeQuantity = cakeModel.CakeQuantity - cake.CartQuantity; if (cakeModel.CakeQuantity < 0) { cakeModel.CakeQuantity = cakeQuantity; MessageBox.Show("Không đủ số lượng bánh trong kho"); return; } DatabaseAccess.UpdateCake(cakeModel); } int orderId = DatabaseAccess.SaveOrder(order); foreach (var cake in Cart.CartList) { DatabaseAccess.SaveOrderItem(orderId, cake.CakeId, cake.CartQuantity); } Cart.CartList.Clear(); Cart.IsOldCustomer = true; Cart.NewCustomer = null; Cart.OldCustomer = null; Cart.HasData = false; DirectBtn.IsChecked = true; tbDirectTotalPrice.Text = ""; tbDirectTotalPrice.Text = ""; tbShippingAddress.Text = ""; MessageBox.Show("Tạo đơn hàng thành công"); }
private void DetailProductPage_Loaded(object sender, RoutedEventArgs e) { Product = DatabaseAccess.FindCakeById(CakeId); this.DataContext = Product; }