public IHttpActionResult PutProductsTable(int id, ProductsTable productsTable) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != productsTable.Pid) { return(BadRequest()); } db.Entry(productsTable).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!ProductsTableExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }
public ActionResult Create([Bind(Include = "ID_PROV,NOMBRE,DIRECCION,NUM_TELF")] PROVEEDOR pROVEEDOR) { if (ModelState.IsValid) { db.PROVEEDORs.Add(pROVEEDOR); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(pROVEEDOR)); }
public ActionResult Create([Bind(Include = "ID,NOMBRE,DESCRIPCION,FECHA_VENC,ID_PROVEEDOR")] Producto producto) { if (ModelState.IsValid) { db.Productoes.Add(producto); db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.ID_PROVEEDOR = new SelectList(db.PROVEEDORs, "ID_PROV", "NOMBRE", producto.ID_PROVEEDOR); return(View(producto)); }
public IHttpActionResult CreateProduct([FromBody] Product us) { if (ModelState.IsValid) { dbContext.Products.Add(us); dbContext.SaveChanges(); return(Ok(us)); } else { return(BadRequest()); } }
public bool SaveData(Product product) { try { db.Products.Add(product); db.SaveChanges(); return(true); } catch (Exception) { return(false); } }
public string UpdateOrder(Order _order) { string result = string.Empty; ProductsEntities entities = new ProductsEntities(); int availability = GetCurrentAvailability(_order.ProductId); Order updatedOrder = (from o in entities.Orders where o.ProductId == _order.ProductId select o).FirstOrDefault(); if (availability - (_order.Quantity - updatedOrder.Quantity) >= 0) { Product updatedProduct = (from p in entities.Products where p.ProductId == _order.ProductId select p).FirstOrDefault(); updatedProduct.Quantity = availability - (_order.Quantity - updatedOrder.Quantity); updatedOrder.Quantity = _order.Quantity; entities.SaveChanges(); result = "Order Updated"; } else { result = "Unavailability of the product"; } return(result); }
// Controller method for confirmation of the purchase. public ActionResult Summary() { // Open the database. using (ProductsEntities database = new ProductsEntities()) { // Retrieve UserID and OrderID. var intUser = Convert.ToInt32(Session["UserId"]); var intOrd = Convert.ToInt32(Session["OrderId"]); // Retrieve Customer details from database. var cust = from c in database.Customers where c.UserId == intUser select c; // Retrieve Order details. var ord = from o in database.Orders where o.OrderId == intOrd select o; // Update Order status. Order order = ord.FirstOrDefault(); try { order.OrderStatus = "COMPLETE"; database.SaveChanges(); } catch (System.NullReferenceException) { return(RedirectToAction("../Shop/Index")); } // Retrieve list of Order Products. var prodOrd = from po in database.Order_Products where po.OrderID == intOrd select po; var modelCust = cust.Include("User").ToList(); var modelOrd = ord.Include("Customer").ToList(); var modelProdOrd = prodOrd.Include("Order").ToList(); // Reset Session Variables for the Cart and Order. Session["OrderId"] = null; Session["cart"] = null; foreach (var ct in cust) { Debug.WriteLine(ct.Address); } ViewBag.ModelCust = modelCust; ViewBag.ModelOrd = modelOrd; // Order Completed - Display Order Summary. return(View(modelProdOrd)); } }
private void SaveDBChanges() { try { db.SaveChanges(); } catch (Exception ex) { View.WarningMessage(ex.Message); } }
public string DeleteOrder(Order _order) { ProductsEntities entities = new ProductsEntities(); int availability = GetCurrentAvailability(_order.ProductId); Product updatedProduct = (from p in entities.Products where p.ProductId == _order.ProductId select p).FirstOrDefault(); Order updatedOrder = (from o in entities.Orders where o.ProductId == _order.ProductId select o).FirstOrDefault(); updatedProduct.Quantity = availability + updatedOrder.Quantity; entities.Orders.Remove(updatedOrder); entities.SaveChanges(); return("Order Removed"); }
// Add a user to the database controller method. public ActionResult AddUser(string Username) { // Reset the session variables used in this method. Session["exists"] = ""; Session["match"] = ""; // Open the database. using (ProductsEntities database = new ProductsEntities()) { // See if the requested username already exists. if (Request["username"] != null) { // Convert the POST username to a string for use in Linq query. var strItem = Request["username"].ToString(); // Find the requested username in the database. var match = from user in database.Users where user.Username == strItem select user; // Loop through the matched usernames to see if one does exist. foreach (User u in match) { // Set the session variable for username already exists to true. Session["exists"] = "true"; // Redirect back to the RegisterUser page. return(RedirectToAction("RegisterUser")); } } // If the username is available, continue. // Create a new user to add to the database. User newUser = new User(); newUser.Username = Request["Username"]; newUser.Password = Request["Password"]; newUser.ConfirmPassword = Request["ConfirmPassword"]; // Add the new user to the database and save the changes. database.Users.Add(newUser); database.SaveChanges(); // Redirect to the Thank you for registering page. return(RedirectToAction("ThankYou")); } }
public ActionResult RegisterCustomer() { // Open the database. using (ProductsEntities database = new ProductsEntities()) { // If the current state of the view model is valid. if (ModelState.IsValid) { // Create a new customer and assign the details from the POST data. Customer customer = new Customer(); customer.FirstName = Request["firstname"]; customer.LastName = Request["lastname"]; customer.Email = Request["email"]; customer.Address = Request["address"]; customer.Suburb = Request["suburb"]; customer.State = Request["state"]; customer.Postcode = Convert.ToInt32(Request["postcode"]); customer.UserId = Convert.ToInt32(Session["UserId"]); // Add the new customer details to the database and save the changes. db.Customers.Add(customer); db.SaveChanges(); // If the cart exists and has products in it return to the cart view. if (Session["cart"] != null && ((Dictionary <Product, int>)Session["cart"]).Count() > 0) { return(View("Index")); } // Otherwise return to the cart is empty view. else { return(View("../Cart/Empty")); } } // If the view model state is not valid, return to the registration page. return(View("RegisterCustomer")); } }
public string AddOrder(Order _order) { string empty = string.Empty; string result = empty; ProductsEntities entities = new ProductsEntities(); int availability = GetCurrentAvailability(_order.ProductId); if (availability - _order.Quantity >= 0) { Product updatedProduct = (from p in entities.Products where p.ProductId == _order.ProductId select p).FirstOrDefault(); updatedProduct.Quantity = availability - _order.Quantity; entities.Orders.Add(_order); entities.SaveChanges(); result = "Order placed"; } else { result = "Unavailability of the product"; } return(result); }
public void Add(TEntity obj) { table.Add(obj); db.SaveChanges(); }
// GET: Checkout public ActionResult Checkout() { // Open and automatically dispose of database. using (ProductsEntities database = new ProductsEntities()) { // Convert session object to int for use with database. var convertInt = Convert.ToInt32(Session["UserId"]); // Get the cart dictionary from the session variable. Dictionary <Product, int> cart = (Dictionary <Product, int>)Session["cart"]; // If no order for the session exists. if (Session["OrderId"] == null) { // Get customers that match the session UserId. var cust = from c in database.Customers where c.UserId == convertInt select c; // Create a new Order for adding to the database. Order order = new Order(); // Get the customer found in the above linq query. Customer customer = cust.FirstOrDefault(); // Assign the order details. order.CustId = customer.CustId; order.OrderDate = DateTime.Now; order.OrderStatus = "OPEN"; // Add the order to the database and save. database.Orders.Add(order); database.SaveChanges(); // Loop through the cart. foreach (KeyValuePair <Product, int> entry in cart) { // Order the products, recording OrderId, ProductId, quantity and current price. Order_Products ordProd = new Order_Products(); ordProd.OrderID = order.OrderId; ordProd.ProductID = entry.Key.ProductID; ordProd.Quantity = entry.Value; ordProd.Price = entry.Key.Price; // Add the product orders to the database and save the changes. database.Order_Products.Add(ordProd); database.SaveChanges(); } // Set the session variable OrderId to indicate the order has been created and products added. Session["OrderId"] = order.OrderId; } // If the order already exists. else { // Convert the session order id to an int for use with the database. var convertIntOrder = Convert.ToInt32(Session["OrderId"]); // Find all the product orders for the session Order ID. var orderProd = from o in database.Order_Products where o.OrderID == convertIntOrder select o; // Remove the product orders from the database. foreach (var op in orderProd) { database.Order_Products.Remove(op); } // Re-add the product orders from the cart to the database to reflect any new changes. // Loop through the cart. foreach (KeyValuePair <Product, int> entry in cart) { // Order the products, recording OrderId, ProductId, quantity and current price. Order_Products ordProd = new Order_Products(); ordProd.OrderID = convertIntOrder; ordProd.ProductID = entry.Key.ProductID; ordProd.Quantity = entry.Value; ordProd.Price = entry.Key.Price; // Add the product orders to the database. database.Order_Products.Add(ordProd); } // Save the database changes. database.SaveChanges(); } return(View()); } }