// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env, CandyStoreDbContext db) { if (env.IsDevelopment()) { app.UseBrowserLink(); app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler("/Home/Error"); } app.UseAuthentication(); app.UseStaticFiles(); DbInitializer.Initialize(db); app.UseMvc(routes => { routes.MapRoute( name: "default", template: "{controller=Home}/{action=Index}/{id?}"); }); }
private void adminLoginButton_Click(object sender, EventArgs e) { int identificationNumber; var parsed = int.TryParse(identificationNumberBox.Text, out identificationNumber); if (!parsed) { MessageForm.ShowError("Enter a correct whole number value."); return; } using (var context = new CandyStoreDbContext()) { var userFromDb = context.Employees.FirstOrDefault(u => u.IdentificationNumber == identificationNumber); if (userFromDb == null) { MessageForm.ShowError("There is no such employee"); return; } ClearTextBoxes(); var adminManagerForm = new AdminManagerForm(); adminManagerForm.Show(); this.Hide(); } }
private void button1_Click(object sender, EventArgs e) { var product = productInsertStock.Text; int parsedQuantity; var isParsed = int.TryParse(productQuantityToAdd.Text, out parsedQuantity); if (!isParsed || parsedQuantity < 1) { MessageForm.ShowError("Quantity must be a whole positive number."); return; } using (var context = new CandyStoreDbContext()) { var productFromDB = context.Products.FirstOrDefault(pro => pro.Name == product); try { productFromDB.Count += parsedQuantity; context.SaveChanges(); } catch (Exception ex) { MessageForm.ShowError(ex.Message); return; } MessageForm.ShowSuccess("Record successfully added"); productInsertStock.Text = string.Empty; productQuantityToAdd.Text = string.Empty; FillProductsAndCategoriesComboBoxes(); } }
private void saveCategoryButton_Click(object sender, EventArgs e) { if (categoryNameBox.Text == "" || this._categoryImage == null) { MessageForm.ShowError("Category image or category name were not set"); return; } using (var context = new CandyStoreDbContext()) { try { context.Categories.Add(new Category { Name = categoryNameBox.Text, CategoryImage = this._categoryImage }); context.SaveChanges(); } catch (Exception ex) { MessageForm.ShowError(ex.Message); return; } MessageForm.ShowSuccess("Record successfully added"); categoryNameBox.Text = string.Empty; this._categoryImage = null; categoryImageSelectedLabel.Text = "No image selected..."; FillProductsAndCategoriesComboBoxes(); } }
private int CreateOrder() { var orderID = 0; var order = new Order() { Customer = new Customer { FirstName = Session.FirstName, LastName = Session.LastName }, Date = DateTime.Now, TotalPrice = _totalPrice }; using (var context = new CandyStoreDbContext()) { try { context.Orders.Add(order); context.SaveChanges(); orderID = order.OrderID; } catch (Exception ex) { MessageForm.ShowError(ex.Message); return(-1); } return(orderID); } }
private void deleteCategory_Click(object sender, EventArgs e) { var result = PromptMessage.ConfirmationMessage("Are you sure you want to delete this record?"); if (result) { var categoryName = categoryComboBox.Text; if (categoryName == "") { MessageForm.ShowError("You haven't selected category name"); return; } using (var context = new CandyStoreDbContext()) { try { var categoryToDelete = context.Categories.FirstOrDefault(p => p.Name == categoryName); context.Categories.Remove(categoryToDelete); context.SaveChanges(); } catch (Exception ex) { MessageForm.ShowError(ex.Message); } } MessageForm.ShowSuccess("Category deleted"); FillProductsAndCategoriesComboBoxes(); } }
private void FillProductsAndCategoriesComboBoxes() { using (var context = new CandyStoreDbContext()) { List <Category> categories = context.Categories.ToList(); List <Product> products = context.Products.ToList(); //clear all comboboxes so there wouldnt be duplication categoryComboBox.Items.Clear(); productCategoryComboBox.Items.Clear(); productComboBox.Items.Clear(); productInsertStock.Items.Clear(); // fill comboboxes foreach (Category category in categories) { categoryComboBox.Items.Add(category.Name); productCategoryComboBox.Items.Add(category.Name); } foreach (Product product in products) { productComboBox.Items.Add(product.Name); productInsertStock.Items.Add(product.Name); } } }
private bool CheckProductInDatabase(int productID, string operation) { using (var context = new CandyStoreDbContext()) { var productFromDB = context.Products.FirstOrDefault(p => p.ProductID == productID); var productFromDbCount = productFromDB.Count; switch (operation) { case "plus": { productFromDbCount -= 1; if (productFromDbCount < 0) { return(false); } productFromDB.Count = productFromDbCount; } break; case "minus": { productFromDbCount += 1; productFromDB.Count = productFromDbCount; } break; } context.SaveChanges(); } return(true); }
private void ProductsForm_Load(object sender, EventArgs e) { using (var ctx = new CandyStoreDbContext()) { var category = ctx.Categories .FirstOrDefault(c => c.CategoryID == _categoryId); var categoryName = category.Name; var products = category .Products .ToList(); productsList.ValueMember = "ProductID"; productsList.DisplayMember = "Name"; productsList.DataSource = products; categoryNameLbl.Text = categoryName.ToString(); if (!products.Any()) { productQuantityBox.Enabled = false; addToCartBtn.Enabled = false; noProductsLbl.Visible = true; } } }
private void CategoriesForm_Load(object sender, EventArgs e) { using (var ctx = new CandyStoreDbContext()) { var categories = ctx.Categories.ToList(); categoriesList.ValueMember = "CategoryID"; categoriesList.DisplayMember = "Name"; categoriesList.DataSource = categories; } }
private string GetProductCategory(int productID) { var categoryName = ""; using (var context = new CandyStoreDbContext()) { categoryName = context.Products .FirstOrDefault(p => p.ProductID == productID).Category.Name; } return(categoryName); }
private void addToCartBtn_Click(object sender, EventArgs e) { var productQuantity = productQuantityBox.Text; int quantityToNumber; bool result = int.TryParse(productQuantity, out quantityToNumber); if (result) { if (quantityToNumber <= 0) { MessageForm.ShowError("Quantity must be a positive number."); return; } var confirmationResult = PromptMessage.ConfirmationMessage("Are you sure you want to add these records to the shopping cart?"); if (!confirmationResult) { return; } using (var context = new CandyStoreDbContext()) { var productId = int.Parse(productsList.SelectedValue.ToString()); var product = context.Products.FirstOrDefault(p => p.ProductID == productId); if (product.Count < quantityToNumber) { MessageForm.ShowError("Not enough quantity on stock"); return; } else { product.Count -= quantityToNumber; if (Session.Products.ContainsKey(product)) { Session.Products[product] += quantityToNumber; } else { Session.Products.Add(product, quantityToNumber); } } context.SaveChanges(); productQuantityBox.Clear(); onStock.Text = product.Count.ToString(); } } else { MessageForm.ShowError("Quantity must be a whole positive number"); return; } }
private void categoriesList_SelectedIndexChanged(object sender, EventArgs e) { using (var ctx = new CandyStoreDbContext()) { var categoryId = int.Parse(categoriesList.SelectedValue.ToString()); var categoryImage = ctx.Categories .FirstOrDefault(c => c.CategoryID == categoryId) .CategoryImage; Image image; using (MemoryStream ms = new MemoryStream(categoryImage)) { image = Image.FromStream(ms); } categoryPictureBox.Image = image; } }
private void productSave_Click(object sender, EventArgs e) { double productPrice; var isParsed = double.TryParse(productPriceBox.Text, out productPrice); if (!isParsed || productPrice < 0) { MessageForm.ShowError("Price must be a positive number."); return; } var productName = productNameBox.Text; var categoryName = productCategoryComboBox.Text; using (var context = new CandyStoreDbContext()) { try { var product = new Product(); product.Name = productName; product.Price = productPrice; var category = context.Categories.FirstOrDefault(c => c.Name == categoryName); product.Category = category; product.ProductImage = this._productImage; context.Products.Add(product); context.SaveChanges(); } catch (Exception ex) { MessageForm.ShowError(ex.Message); return; } MessageForm.ShowSuccess("Record successfully added"); productNameBox.Text = string.Empty; productPriceBox.Text = string.Empty; productCategoryComboBox.Text = string.Empty; this._productImage = null; imageSelectedLabelProduct.Text = "No image selected..."; FillProductsAndCategoriesComboBoxes(); } }
private void productsList_SelectedIndexChanged(object sender, EventArgs e) { using (var ctx = new CandyStoreDbContext()) { var productId = int.Parse(productsList.SelectedValue.ToString()); var product = ctx.Products .FirstOrDefault(c => c.ProductID == productId); var productImage = product .ProductImage; Image image; using (MemoryStream ms = new MemoryStream(productImage)) { image = Image.FromStream(ms); } productPictureBox.Image = image; productPrice.Text = $"${product.Price}"; onStock.Text = product.Count.ToString(); } }
//internal method called initialize, takes an instance of class CandyStoreDbContext internal static void Initialize(CandyStoreDbContext db) { db.Database.Migrate(); if (db.Products.Count() == 0) { db.Products.Add(new Product { Name = "Almond Chocolate Bar", Image = "/images/dark-brown-milk-candy.jpg", Description = "Delicious Chocolate and Almonds", Price = 2.99m }); db.Products.Add(new Product { Name = "Chocolate Peanut Butter Marshmallow Cookies", Image = "/images/chocolate cookies.jpg", Description = "Best Damn Cookies in America! USA!", Price = 5.99m }); db.SaveChanges(); } }
//this is a constructor, injecting in an instance of candystoredbcontext public ProductController(CandyStoreDbContext candyStoreDbContext) { _candyStoreDbContext = candyStoreDbContext; }
//constructor injecting _candyStoreDbContext using an instance of candyStoreDbContext public CartController(CandyStoreDbContext candyStoreDbContext) { _candyStoreDbContext = candyStoreDbContext; }