Compare commits
No commits in common. "c5030f4cf05201f20c55c3872759dfed3cc2444a" and "310ad267230554b32a56e41899ac77da8d28b337" have entirely different histories.
c5030f4cf0
...
310ad26723
3 changed files with 45 additions and 138 deletions
47
initialdata
47
initialdata
|
@ -1,47 +0,0 @@
|
|||
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,50 +1,53 @@
|
|||
DROP TABLE IF EXISTS OrderSpec;
|
||||
DROP TABLE IF EXISTS Recipe;
|
||||
DROP TABLE IF EXISTS Ingredient;
|
||||
DROP TABLE IF EXISTS Pallet;
|
||||
DROP TABLE IF EXISTS Cookie;
|
||||
DROP TABLE IF EXISTS Orders;
|
||||
DROP TABLE IF EXISTS Pallet;
|
||||
DROP TABLE IF EXISTS Order;
|
||||
DROP TABLE IF EXISTS Customer;
|
||||
|
||||
CREATE TABLE Customer (
|
||||
name VARCHAR(255) PRIMARY KEY,
|
||||
customer_id INT AUTO_INCREMENT PRIMARY KEY,
|
||||
name VARCHAR(255),
|
||||
address VARCHAR(255)
|
||||
);
|
||||
|
||||
CREATE TABLE Orders (
|
||||
CREATE TABLE Order (
|
||||
order_id INT AUTO_INCREMENT PRIMARY KEY,
|
||||
customer_name VARCHAR(255),
|
||||
customer_id INT,
|
||||
delivery_date DATE,
|
||||
order_date DATE,
|
||||
FOREIGN KEY (customer_name) REFERENCES Customer(name)
|
||||
);
|
||||
|
||||
CREATE TABLE Cookie (
|
||||
name VARCHAR(255) PRIMARY KEY
|
||||
FOREIGN KEY (customer_id) REFERENCES Customer(customer_id)
|
||||
);
|
||||
|
||||
CREATE TABLE Pallet (
|
||||
pallet_id INT AUTO_INCREMENT PRIMARY KEY,
|
||||
order_id INT,
|
||||
manufacture_date DATETIME,
|
||||
manufacture_date DATE,
|
||||
delivery_date DATE,
|
||||
blocked VARCHAR(255) DEFAULT 'no',
|
||||
cookie_name VARCHAR(255),
|
||||
FOREIGN KEY(cookie_name) REFERENCES Cookie(name),
|
||||
FOREIGN KEY (order_id) REFERENCES Orders(order_id)
|
||||
blocked BOOLEAN,
|
||||
FOREIGN KEY (order_id) REFERENCES Order(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 (
|
||||
order_id INT,
|
||||
cookie_name VARCHAR(255),
|
||||
cookie_id INT,
|
||||
quantity INT,
|
||||
PRIMARY KEY (order_id, cookie_name),
|
||||
FOREIGN KEY (order_id) REFERENCES Orders(order_id),
|
||||
FOREIGN KEY (cookie_name) REFERENCES Cookie(name)
|
||||
PRIMARY KEY (order_id, cookie_id),
|
||||
FOREIGN KEY (order_id) REFERENCES Order(order_id),
|
||||
FOREIGN KEY (cookie_id) REFERENCES Cookie(cookie_id)
|
||||
);
|
||||
|
||||
CREATE TABLE Ingredient (
|
||||
ingredient_name VARCHAR(255) PRIMARY KEY,
|
||||
ingredient_id INT AUTO_INCREMENT PRIMARY KEY,
|
||||
name VARCHAR(255),
|
||||
stock INT,
|
||||
unit VARCHAR(50),
|
||||
delivery_date DATE,
|
||||
|
@ -52,10 +55,10 @@ CREATE TABLE Ingredient (
|
|||
);
|
||||
|
||||
CREATE TABLE Recipe (
|
||||
ingredient_name VARCHAR(255),
|
||||
cookie_name VARCHAR(255),
|
||||
ingredient_id INT,
|
||||
cookie_id INT,
|
||||
amount FLOAT,
|
||||
PRIMARY KEY (ingredient_name, cookie_name),
|
||||
FOREIGN KEY (ingredient_name) REFERENCES Ingredient(ingredient_name),
|
||||
FOREIGN KEY (cookie_name) REFERENCES Cookie(name)
|
||||
PRIMARY KEY (ingredient_id, cookie_id),
|
||||
FOREIGN KEY (ingredient_id) REFERENCES Ingredient(ingredient_id),
|
||||
FOREIGN KEY (cookie_id) REFERENCES Cookie(cookie_id)
|
||||
);
|
||||
|
|
|
@ -1,14 +1,15 @@
|
|||
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.Response;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
import java.sql.*;
|
||||
|
||||
import static krusty.Jsonizer.toJson;
|
||||
|
||||
public class Database {
|
||||
/**
|
||||
* Modify it to fit your environment and then use this string when connecting to your database!
|
||||
|
@ -44,38 +45,26 @@ public class Database {
|
|||
|
||||
public String getCustomers(Request req, Response res) {
|
||||
String query = "SELECT name, address FROM Customer";
|
||||
String title = "customers"; //ändra inte det är till för testet, söker efter just "customers" små bokstäver
|
||||
String title = "Customer";
|
||||
|
||||
return getJson(query, title);
|
||||
|
||||
}
|
||||
|
||||
public String getRawMaterials(Request req, Response res) {
|
||||
String query = "SELECT ingredient_name as name, stock as amount, unit FROM Ingredient";
|
||||
String title = "raw-materials";
|
||||
|
||||
return getJson(query, title);
|
||||
return "{}";
|
||||
}
|
||||
|
||||
public String getCookies(Request req, Response res) {
|
||||
String query = "Select name from Cookie";
|
||||
String title = "cookies";
|
||||
|
||||
|
||||
return getJson(query, title);
|
||||
return "{\"cookies\":[]}";
|
||||
}
|
||||
|
||||
public String getRecipes(Request req, Response res) {
|
||||
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);
|
||||
return "{}";
|
||||
}
|
||||
|
||||
public String getPallets(Request req, Response res) {
|
||||
String query = "Select cookie_name as cookie, blocked From Pallet;";
|
||||
String title = "pallets";
|
||||
return getJson(query, title);
|
||||
return "{\"pallets\":[]}";
|
||||
}
|
||||
|
||||
public String reset(Request req, Response res) {
|
||||
|
@ -83,55 +72,17 @@ public class Database {
|
|||
}
|
||||
|
||||
public String createPallet(Request req, Response res) {
|
||||
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 "";
|
||||
}
|
||||
|
||||
|
||||
|
||||
return "{}";
|
||||
}
|
||||
|
||||
private String getJson(String query, String title) { //privat hjälpmetod så man slipper kalla på jsonizer varje gång
|
||||
try (PreparedStatement stmt = conn.prepareStatement(query)) {
|
||||
try {
|
||||
PreparedStatement stmt = conn.prepareStatement(query);
|
||||
ResultSet rs = stmt.executeQuery();
|
||||
|
||||
return Jsonizer.toJson(rs, title);
|
||||
} catch (SQLException e) {
|
||||
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