Skip to content

Instantly share code, notes, and snippets.

@ciprianna
Created June 22, 2015 04:07
Show Gist options
  • Save ciprianna/0fe2ae2279eda34b7219 to your computer and use it in GitHub Desktop.
Save ciprianna/0fe2ae2279eda34b7219 to your computer and use it in GitHub Desktop.
Inventory Manager with Sinatra
<head><center><h1>Add a New Shoe</h1></center></head>
<body>
<center>
<form action="/save_new_shoe">
<p>Product name
<input type="text" name="name" placeholder="Product Name"></p>
<p>Cost
<input type="number" name="cost" placeholder="Cost"></p>
<p>Color
<input type="text" name="color" placeholder="Color"></p>
Category
<select name="category_id">
<% Category.all.each do |category| %>
<option value="<%= category.id %>"><%= category.name %></option>
<% end %>
</select>
<p>Location
<select name="location_id">
<% Location.all.each do |location| %>
<option value="<%= location.id %>"><%= location.name %></option>
<% end %>
</select></p>
<p>Quantity
<input type="number" name="location_stock" placeholder="Quantity"></p>
<input type="submit">
</form>
</center>
</body>
<center>~~~~~~~~~~~~~~~~~~~~~~</center>
<footer>
<center><button><a href="/home">Return to Home Page</a></button></center>
</footer>
# Driver
require "sqlite3"
require "sinatra"
require "sinatra/reloader"
require "pry"
require_relative "menu_module.rb"
require_relative "validity_module.rb"
require_relative "database_class_methods.rb"
require_relative "database_instance_methods.rb"
require_relative "shoes.rb"
require_relative "location.rb"
require_relative "category.rb"
# Creates the database connection
DATABASE = SQLite3::Database.new("shoe_inventory.db")
# Creates the table
DATABASE.execute("CREATE TABLE IF NOT EXISTS shoes (id INTEGER PRIMARY KEY, name TEXT NOT NULL, cost INTEGER NOT NULL, color TEXT NOT NULL, category_id INTEGER, location_id INTEGER, location_stock INTEGER);")
DATABASE.execute("CREATE TABLE IF NOT EXISTS categories (id INTEGER PRIMARY KEY, name TEXT);")
DATABASE.execute("CREATE TABLE IF NOT EXISTS locations (id INTEGER PRIMARY KEY, name TEXT);")
# Returns the results as a Hash
DATABASE.results_as_hash = true
################################################################################
get "/home" do
erb :index
end
get "/current_stock" do
erb :current_stock
end
get "/quantity_information" do
erb :quantity_information
end
get "/add_shoe" do
erb :add_shoe
end
get "/update_product" do
erb :update_product
end
get "/save_shoe_update" do
shoe_to_change = Shoe.find(params["shoe_id"])
shoe_to_change.name = params["name"]
shoe_to_change.cost = params["cost"]
shoe_to_change.color = params["color"]
shoe_to_change.category_id = params["category_id"]
shoe_to_change.location_id = params["location_id"]
if shoe_to_change.save_valid
erb :success
else
erb :failure
end
end
get "/view_by_cost" do
erb :view_by_cost
end
get "/cost_category" do
erb :cost_category
end
get "/location_information" do
erb :location_information
end
get "/category_information" do
erb :category_information
end
get "/delete_shoe" do
erb :delete_shoe
end
get "/view_quantities" do
erb :view_quantities
end
get "/low_quantities" do
erb :low_quantities
end
get "/update_quantities" do
erb :update_quantities
end
get "/save_quantity_update" do
# Creates new Shoe Object
shoe_to_update = Shoe.find(params["shoe_id"].to_i)
# Updates shoe quantity and routes to either success or failure erb
if shoe_to_update.update_quantity(params["new_quantity"].to_i)
erb :success
else
erb :failure
end
end
get "/save_new_shoe" do
# Creates new Shoe Object from form information
@shoe_to_add = Shoe.new({"name" => params["name"], "cost" => params["cost"].to_i, "color" => params["color"], "category_id" => params["category_id"].to_i, "location_id" => params["location_id"].to_i, "location_stock" => params["location_stock"].to_i})
# Runs add_to_database method and leads to success or failure erbs
if @shoe_to_add.add_to_database
erb :success
else
erb :failure
end
end
<h2>View by Cost</h2>
<body>
<h3>Select a category to view:</h3>
<form action="/cost_category">
<select name="cost_category">
<option value="high">High - $100+</option>
<option value="medium">Medium - $50-$99</option>
<option value="low">Low - $0-$49</option>
</select>
<input type="submit">
</form>
<ul>
<% if params["cost_category"] == "high" %>
<% shoes_by_price = Shoe.where_cost("high") %>
<% shoes_by_price.each do |shoe| %>
<li><%= shoe.id %>, <i><%= shoe.name %>, <b>Cost</b>: $<%= shoe.cost %>, <b>Color</b>: <%= shoe.color %>, <b>Category ID</b>: <%= shoe.category_id %>, <b>Location ID</b>: <%= shoe.location_id %>, <b>Quantity</b>: <%= shoe.location_stock %></li>
<% end %>
<% elsif params["cost_category"] == "medium" %>
<% shoes_by_price = Shoe.where_cost("medium") %>
<% shoes_by_price.each do |shoe| %>
<li><%= shoe.id %>, <i><%= shoe.name %>, <b>Cost</b>: $<%= shoe.cost %>, <b>Color</b>: <%= shoe.color %>, <b>Category ID</b>: <%= shoe.category_id %>, <b>Location ID</b>: <%= shoe.location_id %>, <b>Quantity</b>: <%= shoe.location_stock %></li>
<% end %>
<% elsif params["cost_category"] == "low" %>
<% shoes_by_price = Shoe.where_cost("low") %>
<% shoes_by_price.each do |shoe| %>
<li><%= shoe.id %>, <i><%= shoe.name %>, <b>Cost</b>: $<%= shoe.cost %>, <b>Color</b>: <%= shoe.color %>, <b>Category ID</b>: <%= shoe.category_id %>, <b>Location ID</b>: <%= shoe.location_id %>, <b>Quantity</b>: <%= shoe.location_stock %></li>
<% end %>
<% end %>
</ul>
</body>
<footer>
<center><button><a href="/home">Return to Home Page</a></button></center>
</footer>
<html>
<head>
<center>
<h2>Current Stock:<h2>
</center>
</head>
<body>
<ul>
<% Shoe.all.each do |shoe| %>
<li><b><%= shoe.id %></b> - <i><%= shoe.name %></i>, <b>Cost</b>: $<%= shoe.cost %>, <b>Color</b>: <%= shoe.color %>, <b>Category ID</b>: <%= shoe.category_id %>, <b>Location ID</b>: <%= shoe.location_id %>, <b>Quantity</b>: <%= shoe.location_stock %>
<% end %>
</ul>
<p><h3>Product Actions</h3>
<ul>
<li><a href="/add_shoe">Add a new product to the inventory</a></li>
<li><a href="/update_product">Update shoe information</a></li>
<li><a href="/delete_shoe">Delete a shoe</a></li>
</ul>
</p>
</body>
<footer><center><button><a href="/home">Return to Home Page</a></button></center></footer>
</html>
<head><center><h2>Womp womp...Something went wrong.</h2></center></head>
<footer>
<center><button><a href="/home">Return to Home Page</a></button></center>
</footer>
<html>
<head>
<center>
<h1>Welcome to the Cutesie Bootsie Inventory Management System!</h1>
</center>
</head>
<body>
<h2>Choose what you'd like to do:</h2></body>
<ul>
<li><a href="/current_stock">View current stock</a></li>
<li><a href="/quantity_information">View stock quantities</a></li>
<li><a href="/add_shoe">Add a new product</a></li>
<li><a href="/update_product">Update product information</a></li>
<li><a href="/view_by_cost">View products by cost</a></li>
<li><a href="/location_information">View product location information</a></li>
<li><a href="/category_information">View product category information</a></li>
<li><a href="/delete_shoe">Delete a product</a></li>
</body>
</html>
<html>
<head>
<center>
<h2>Low Quantities:<h2>
</center>
</head>
<body>
<ul>
<% Shoe.where_quantity_is_low.each do |shoe| %>
<li><b><%= shoe.id %></b> - <i><%= shoe.name %></i>, <b>Quantity</b>: <%= shoe.location_stock %>
<% end %>
</ul>
<p><a href="/quantity_information">Return to quantity information</a>
</p>
</body>
<footer>
<center><button><a href="/home">Return to Home Page</a></button></center>
</footer>
</html>
<html>
<head>
<center>
<h2>Quantity Information<h2>
</center>
</head>
<body>
<ul>
<li><a href="/view_quantities">View all stock quantities</a></li>
<li><a href="/low_quantities">View low stock</a></li>
<li><a href="/update_quantities">Update quantities</a></li>
</ul>
</body>
<footer><center><button><a href="/home">Return to Home Page</a></button></center></footer>
</html>
<!--erb should show information if a button is clicked?-->
<!--shows either all quantities or low quantities-->
<!--alternative is to get another page for each, but that seems redundant-->
<head><center><h2>Success!</h2></center></head>
<footer>
<center><button><a href="/home">Return to Home Page</a></button></center>
</footer>
<head><center><h2>Update Product</h2></center></head>
<body>
<form action="/save_shoe_update">
<h3>Choose a shoe to update</h3>
<p><select name="shoe_id">
<% Shoe.all.each do |shoe| %>
<option value="<%= shoe.id %>"><%= shoe.name %></option>
<% end %>
</select></p>
<p><h3>Enter new product information</h3></p>
<p><h4>All fields must be entered</h4></p>
Change Name
<input type="text" name="name" placeholder="Name">
<p>Change Cost
<input type="number" name="cost" placeholder="Cost">
</p>
<p>Change Color
<input type="text" name="color" placeholder="Color">
</p>
<p>Change Category
<select name="category_id">
<% Category.all.each do |category| %>
<option value="<%= category.id %>"><%= category.name %></option>
<% end %>
</select></p>
<p>Location
<select name="location_id">
<% Location.all.each do |location| %>
<option value="<%= location.id %>"><%= location.name %></option>
<% end %>
</select></p>
<p><input type="submit"></p>
</form>
</body>
<footer>
<center>~~~~~~~~~~~~~~~~~~~~~~
<p><button><a href="/home">Return to Home Page</a></button></p>
</center>
</footer>
<html>
<head>
<center>
<h2>Update Shoe Information<h2>
</center>
</head>
<body>
<h3>Select a shoe to update</h3>
<form action="/save_quantity_update">
<select name="shoe_id">
<% Shoe.all.each do |shoe| %>
<option value="<%= shoe.id %>"><%= shoe.name %></option>
<% end %>
</select>
<p>
Enter quantity to add. If removing quantity, enter a negative number.
</p>
<input type="number" name="new_quantity" placeholder="Quantity to add">
<input type="submit">
</form>
<p><a href="/quantity_information">Return to quantity information</a>
</p>
</body>
<footer>
<center><button><a href="/home">Return to Home Page</a></button></center>
</footer>
</html>
<h2>View by Cost</h2>
<body>
<h3>Select a category to view:</h3>
<form action="/cost_category">
<select name="cost_category">
<option value="high">High - $100+</option>
<option value="medium">Medium - $50-$99</option>
<option value="low">Low - $0-$49</option>
</select>
<input type="submit">
</form>
</body>
<footer>
<center><button><a href="/home">Return to Home Page</a></button></center>
</footer>
<html>
<head>
<center>
<h2>Current Stock Quantities:<h2>
</center>
</head>
<body>
<ul>
<% Shoe.all.each do |shoe| %>
<li><b><%= shoe.id %></b> - <i><%= shoe.name %></i>, <b>Quantity</b>: <%= shoe.location_stock %>
<% end %>
</ul>
<p>
<b>Total Stock: </b><%= Shoe.total_stock %>
</p>
<p><a href="/quantity_information">Return to quantity information</a>
</p>
</body>
<footer>
<center><button><a href="/home">Return to Home Page</a></button></center>
</footer>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment