Compare commits
12 commits
310ad26723
...
c5030f4cf0
Author | SHA1 | Date | |
---|---|---|---|
|
c5030f4cf0 | ||
|
d1daee1e03 | ||
|
e241613a15 | ||
|
b4851455dd | ||
|
eb38c47af7 | ||
|
051c15d8d3 | ||
|
ef3aecfe38 | ||
|
7a5ade039d | ||
|
236d0d43b5 | ||
|
7d8a89b417 | ||
|
ad45f94f23 | ||
|
780917069d |
3 changed files with 138 additions and 45 deletions
47
initialdata
Normal file
47
initialdata
Normal file
|
@ -0,0 +1,47 @@
|
||||||
|
INSERT INTO Customer(name, address) VALUES
|
||||||
|
('Finkakor AB', 'Helsingborg'),
|
||||||
|
('Småbröd AB', 'Malmö'),
|
||||||
|
('Kaffebröd AB', 'Landskrona'),
|
||||||
|
('Bjudkakor AB', 'Ystad'),
|
||||||
|
('Kalaskakor AB', 'Trelleborg'),
|
||||||
|
('Partykakor AB', 'Kristianstad'),
|
||||||
|
('Gästkakor AB', 'Hässleholm'),
|
||||||
|
('Skånekakor AB', 'Perstorp');
|
||||||
|
|
||||||
|
INSERT INTO Cookie(name) VALUES
|
||||||
|
('Nut ring'),
|
||||||
|
('Nut cookie'),
|
||||||
|
('Amneris'),
|
||||||
|
('Tango'),
|
||||||
|
('Almond delight'),
|
||||||
|
('Berliner');
|
||||||
|
|
||||||
|
INSERT INTO Ingredient(ingredient_name, stock, unit) VALUES
|
||||||
|
('Bread crumbs', 500000, 'g'),
|
||||||
|
('Butter', 500000, 'g'),
|
||||||
|
('Chocolate', 500000, 'g'),
|
||||||
|
('Chopped almonds', 500000, 'g'),
|
||||||
|
('Cinnamon', 500000, 'g'),
|
||||||
|
('Egg whites', 500000, 'ml'),
|
||||||
|
('Eggs', 500000, 'g'),
|
||||||
|
('Fine-ground nuts', 500000, 'g'),
|
||||||
|
('Flour', 500000, 'g'),
|
||||||
|
('Ground, roasted nuts', 500000, 'g'),
|
||||||
|
('Icing sugar', 500000, 'g'),
|
||||||
|
('Marzipan', 500000, 'g'),
|
||||||
|
('Potato starch', 500000, 'g'),
|
||||||
|
('Roasted, chopped nuts', 500000, 'g'),
|
||||||
|
('Sodium bicarbonate', 500000, 'g'),
|
||||||
|
('Sugar', 500000, 'g'),
|
||||||
|
('Vanilla sugar', 500000, 'g'),
|
||||||
|
('Vanilla', 500000, 'g'),
|
||||||
|
('Wheat flour', 500000, 'g');
|
||||||
|
|
||||||
|
INSERT INTO Recipe (ingredient_name, cookie_name, amount) VALUES
|
||||||
|
('Butter', 'Nut ring', 450),('Flour', 'Nut ring', 450),('Icing sugar', 'Nut ring', 190),('Roasted, chopped nuts', 'Nut ring', 225),
|
||||||
|
('Bread crumbs', 'Nut cookie', 125),('Chocolate', 'Nut cookie', 50),('Egg whites', 'Nut cookie', 350),('Fine-ground nuts', 'Nut cookie', 750),('Ground, roasted nuts', 'Nut cookie', 625),('Sugar', 'Nut cookie', 375),
|
||||||
|
('Butter', 'Amneris', 250),('Eggs', 'Amneris', 250),('Marzipan', 'Amneris', 750),('Potato starch', 'Amneris', 25),('Wheat flour', 'Amneris', 25),
|
||||||
|
('Butter', 'Tango', 200),('Flour', 'Tango', 300),('Sodium bicarbonate', 'Tango', 4),('Sugar', 'Tango', 250),('Vanilla', 'Tango', 2),
|
||||||
|
('Butter', 'Almond delight', 400),('Chopped almonds', 'Almond delight', 279),('Cinnamon', 'Almond delight', 10),('Flour', 'Almond delight', 400),('Sugar', 'Almond delight', 270),
|
||||||
|
('Butter', 'Berliner', 250),('Chocolate', 'Berliner', 50),('Eggs', 'Berliner', 50),('Flour', 'Berliner', 350),('Icing sugar', 'Berliner', 100),('Vanilla sugar', 'Berliner', 5);
|
||||||
|
|
53
sqlCode
53
sqlCode
|
@ -1,53 +1,50 @@
|
||||||
DROP TABLE IF EXISTS OrderSpec;
|
DROP TABLE IF EXISTS OrderSpec;
|
||||||
DROP TABLE IF EXISTS Recipe;
|
DROP TABLE IF EXISTS Recipe;
|
||||||
DROP TABLE IF EXISTS Ingredient;
|
DROP TABLE IF EXISTS Ingredient;
|
||||||
DROP TABLE IF EXISTS Cookie;
|
|
||||||
DROP TABLE IF EXISTS Pallet;
|
DROP TABLE IF EXISTS Pallet;
|
||||||
DROP TABLE IF EXISTS Order;
|
DROP TABLE IF EXISTS Cookie;
|
||||||
|
DROP TABLE IF EXISTS Orders;
|
||||||
DROP TABLE IF EXISTS Customer;
|
DROP TABLE IF EXISTS Customer;
|
||||||
|
|
||||||
CREATE TABLE Customer (
|
CREATE TABLE Customer (
|
||||||
customer_id INT AUTO_INCREMENT PRIMARY KEY,
|
name VARCHAR(255) PRIMARY KEY,
|
||||||
name VARCHAR(255),
|
|
||||||
address VARCHAR(255)
|
address VARCHAR(255)
|
||||||
);
|
);
|
||||||
|
|
||||||
CREATE TABLE Order (
|
CREATE TABLE Orders (
|
||||||
order_id INT AUTO_INCREMENT PRIMARY KEY,
|
order_id INT AUTO_INCREMENT PRIMARY KEY,
|
||||||
customer_id INT,
|
customer_name VARCHAR(255),
|
||||||
delivery_date DATE,
|
delivery_date DATE,
|
||||||
order_date DATE,
|
order_date DATE,
|
||||||
FOREIGN KEY (customer_id) REFERENCES Customer(customer_id)
|
FOREIGN KEY (customer_name) REFERENCES Customer(name)
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE TABLE Cookie (
|
||||||
|
name VARCHAR(255) PRIMARY KEY
|
||||||
);
|
);
|
||||||
|
|
||||||
CREATE TABLE Pallet (
|
CREATE TABLE Pallet (
|
||||||
pallet_id INT AUTO_INCREMENT PRIMARY KEY,
|
pallet_id INT AUTO_INCREMENT PRIMARY KEY,
|
||||||
order_id INT,
|
order_id INT,
|
||||||
manufacture_date DATE,
|
manufacture_date DATETIME,
|
||||||
delivery_date DATE,
|
delivery_date DATE,
|
||||||
blocked BOOLEAN,
|
blocked VARCHAR(255) DEFAULT 'no',
|
||||||
FOREIGN KEY (order_id) REFERENCES Order(order_id)
|
cookie_name VARCHAR(255),
|
||||||
);
|
FOREIGN KEY(cookie_name) REFERENCES Cookie(name),
|
||||||
|
FOREIGN KEY (order_id) REFERENCES Orders(order_id)
|
||||||
CREATE TABLE Cookie (
|
|
||||||
cookie_id INT AUTO_INCREMENT PRIMARY KEY,
|
|
||||||
pallet_id INT,
|
|
||||||
name VARCHAR(255),
|
|
||||||
FOREIGN KEY (pallet_id) REFERENCES Pallet(pallet_id)
|
|
||||||
);
|
);
|
||||||
|
|
||||||
CREATE TABLE OrderSpec (
|
CREATE TABLE OrderSpec (
|
||||||
order_id INT,
|
order_id INT,
|
||||||
cookie_id INT,
|
cookie_name VARCHAR(255),
|
||||||
quantity INT,
|
quantity INT,
|
||||||
PRIMARY KEY (order_id, cookie_id),
|
PRIMARY KEY (order_id, cookie_name),
|
||||||
FOREIGN KEY (order_id) REFERENCES Order(order_id),
|
FOREIGN KEY (order_id) REFERENCES Orders(order_id),
|
||||||
FOREIGN KEY (cookie_id) REFERENCES Cookie(cookie_id)
|
FOREIGN KEY (cookie_name) REFERENCES Cookie(name)
|
||||||
);
|
);
|
||||||
|
|
||||||
CREATE TABLE Ingredient (
|
CREATE TABLE Ingredient (
|
||||||
ingredient_id INT AUTO_INCREMENT PRIMARY KEY,
|
ingredient_name VARCHAR(255) PRIMARY KEY,
|
||||||
name VARCHAR(255),
|
|
||||||
stock INT,
|
stock INT,
|
||||||
unit VARCHAR(50),
|
unit VARCHAR(50),
|
||||||
delivery_date DATE,
|
delivery_date DATE,
|
||||||
|
@ -55,10 +52,10 @@ CREATE TABLE Ingredient (
|
||||||
);
|
);
|
||||||
|
|
||||||
CREATE TABLE Recipe (
|
CREATE TABLE Recipe (
|
||||||
ingredient_id INT,
|
ingredient_name VARCHAR(255),
|
||||||
cookie_id INT,
|
cookie_name VARCHAR(255),
|
||||||
amount FLOAT,
|
amount FLOAT,
|
||||||
PRIMARY KEY (ingredient_id, cookie_id),
|
PRIMARY KEY (ingredient_name, cookie_name),
|
||||||
FOREIGN KEY (ingredient_id) REFERENCES Ingredient(ingredient_id),
|
FOREIGN KEY (ingredient_name) REFERENCES Ingredient(ingredient_name),
|
||||||
FOREIGN KEY (cookie_id) REFERENCES Cookie(cookie_id)
|
FOREIGN KEY (cookie_name) REFERENCES Cookie(name)
|
||||||
);
|
);
|
||||||
|
|
|
@ -1,15 +1,14 @@
|
||||||
package krusty;
|
package krusty;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.DriverManager;
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
|
||||||
import spark.Request;
|
import spark.Request;
|
||||||
import spark.Response;
|
import spark.Response;
|
||||||
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.TreeMap;
|
|
||||||
import java.sql.*;
|
|
||||||
|
|
||||||
import static krusty.Jsonizer.toJson;
|
|
||||||
|
|
||||||
public class Database {
|
public class Database {
|
||||||
/**
|
/**
|
||||||
* Modify it to fit your environment and then use this string when connecting to your database!
|
* Modify it to fit your environment and then use this string when connecting to your database!
|
||||||
|
@ -45,26 +44,38 @@ public class Database {
|
||||||
|
|
||||||
public String getCustomers(Request req, Response res) {
|
public String getCustomers(Request req, Response res) {
|
||||||
String query = "SELECT name, address FROM Customer";
|
String query = "SELECT name, address FROM Customer";
|
||||||
String title = "Customer";
|
String title = "customers"; //ändra inte det är till för testet, söker efter just "customers" små bokstäver
|
||||||
|
|
||||||
return getJson(query, title);
|
return getJson(query, title);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getRawMaterials(Request req, Response res) {
|
public String getRawMaterials(Request req, Response res) {
|
||||||
return "{}";
|
String query = "SELECT ingredient_name as name, stock as amount, unit FROM Ingredient";
|
||||||
|
String title = "raw-materials";
|
||||||
|
|
||||||
|
return getJson(query, title);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getCookies(Request req, Response res) {
|
public String getCookies(Request req, Response res) {
|
||||||
return "{\"cookies\":[]}";
|
String query = "Select name from Cookie";
|
||||||
|
String title = "cookies";
|
||||||
|
|
||||||
|
|
||||||
|
return getJson(query, title);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getRecipes(Request req, Response res) {
|
public String getRecipes(Request req, Response res) {
|
||||||
return "{}";
|
String query = "Select r.cookie_name, i.ingredient_name, r.amount, i.unit From Ingredient i, Recipe r WHERE r.ingredient_name = i.ingredient_name;";
|
||||||
|
String title = "recipes";
|
||||||
|
|
||||||
|
return getJson(query, title);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getPallets(Request req, Response res) {
|
public String getPallets(Request req, Response res) {
|
||||||
return "{\"pallets\":[]}";
|
String query = "Select cookie_name as cookie, blocked From Pallet;";
|
||||||
|
String title = "pallets";
|
||||||
|
return getJson(query, title);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String reset(Request req, Response res) {
|
public String reset(Request req, Response res) {
|
||||||
|
@ -72,17 +83,55 @@ public class Database {
|
||||||
}
|
}
|
||||||
|
|
||||||
public String createPallet(Request req, Response res) {
|
public String createPallet(Request req, Response res) {
|
||||||
return "{}";
|
if (req.queryParams("cookie") != null) {
|
||||||
}
|
String cookie = req.queryParams("cookie");
|
||||||
|
return createPallet(cookie);
|
||||||
|
} else {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected String createPallet(String cookie) {
|
||||||
|
String query = "Insert into Pallet (manufacture_date, cookie_name) VALUES (NOW(), ?);";
|
||||||
|
try (PreparedStatement stmt = conn.prepareStatement(query)) {
|
||||||
|
stmt.setString(1, cookie);
|
||||||
|
stmt.executeUpdate();
|
||||||
|
removeMaterials(cookie);
|
||||||
|
} catch (SQLException e) {
|
||||||
|
return "bob";
|
||||||
|
}
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
private String getJson(String query, String title) { //privat hjälpmetod så man slipper kalla på jsonizer varje gång
|
private String getJson(String query, String title) { //privat hjälpmetod så man slipper kalla på jsonizer varje gång
|
||||||
try {
|
try (PreparedStatement stmt = conn.prepareStatement(query)) {
|
||||||
PreparedStatement stmt = conn.prepareStatement(query);
|
|
||||||
ResultSet rs = stmt.executeQuery();
|
ResultSet rs = stmt.executeQuery();
|
||||||
|
|
||||||
return Jsonizer.toJson(rs, title);
|
return Jsonizer.toJson(rs, title);
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
public void removeMaterials(String cookie) { //privat hjälpmetod för att minska antalet material efter man gör en pallet
|
||||||
|
String query = "Select ingredient_name, amount from Recipe where cookie_name = ?;";
|
||||||
|
String query2 = "UPDATE Ingredient SET stock = stock - ? WHERE ingredient_name = ?;";
|
||||||
|
|
||||||
|
try (PreparedStatement stmt = conn.prepareStatement(query)) {
|
||||||
|
stmt.setString(1, cookie);
|
||||||
|
ResultSet rs = stmt.executeQuery();
|
||||||
|
while(rs.next()) { //loopar igenom och uppdaterar all material
|
||||||
|
try (PreparedStatement stmt2 = conn.prepareStatement(query2)) {
|
||||||
|
stmt2.setInt(1, rs.getInt("amount") * 54); //54 för (10*15*36)/100 = 10 kakor per låda, 15 lådor per box 36 boxar per pallet, materialet är för 100 kakor därför man delar allt med 100
|
||||||
|
stmt2.setString(2, rs.getString("ingredient_name"));
|
||||||
|
stmt2.executeUpdate();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (SQLException e){
|
||||||
|
//return false;
|
||||||
|
}
|
||||||
|
//return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue