Compare commits

..

12 commits

Author SHA1 Message Date
Ivan Bogosavljevic
c5030f4cf0 Merge branch 'main' of https://github.com/dDogge/ProjectDatabase2024 2024-04-18 00:23:30 +02:00
Ivan Bogosavljevic
d1daee1e03 Bara reset och getPallets som ska fixas tror jag, allt annat fungerar 2024-04-18 00:23:25 +02:00
Ivan Bogosavljevic
e241613a15
Update sqlCode 2024-04-17 23:30:01 +02:00
Ivan Bogosavljevic
b4851455dd
Update initialdata 2024-04-17 23:19:51 +02:00
Ivan Bogosavljevic
eb38c47af7
Uppdaterad sqlkod 2024-04-17 23:19:16 +02:00
Ivan Bogosavljevic
051c15d8d3 Börjat lite med createPallet men ej klar. följt compendiet kanske ej rätt 2024-04-17 18:53:14 +02:00
Ivan Bogosavljevic
ef3aecfe38 getRecipes fungerar enligt anvisningar finns ej testfall för att testa detta dock 2024-04-17 11:09:27 +02:00
Ivan Bogosavljevic
7a5ade039d Fixat getRawMaterials och getCookies 2024-04-17 10:28:01 +02:00
Ivan Bogosavljevic
236d0d43b5 getCustomers testet fungerar nu, title ska vara customers för testet letar efter just det ordet 2024-04-17 10:17:28 +02:00
Ivan Bogosavljevic
7d8a89b417 Merge branch 'main' of https://github.com/dDogge/ProjectDatabase2024 2024-04-17 00:21:00 +02:00
Ivan Bogosavljevic
ad45f94f23
Create initialdata 2024-04-16 23:27:14 +02:00
Ivan Bogosavljevic
780917069d
Fick problem när tablen hette Order bytte till Orders istället 2024-04-16 22:40:30 +02:00
3 changed files with 138 additions and 45 deletions

47
initialdata Normal file
View 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
View file

@ -1,53 +1,50 @@
DROP TABLE IF EXISTS OrderSpec;
DROP TABLE IF EXISTS Recipe;
DROP TABLE IF EXISTS Ingredient;
DROP TABLE IF EXISTS Cookie;
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;
CREATE TABLE Customer (
customer_id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255),
name VARCHAR(255) PRIMARY KEY,
address VARCHAR(255)
);
CREATE TABLE Order (
CREATE TABLE Orders (
order_id INT AUTO_INCREMENT PRIMARY KEY,
customer_id INT,
customer_name VARCHAR(255),
delivery_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 (
pallet_id INT AUTO_INCREMENT PRIMARY KEY,
order_id INT,
manufacture_date DATE,
manufacture_date DATETIME,
delivery_date DATE,
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)
blocked VARCHAR(255) DEFAULT 'no',
cookie_name VARCHAR(255),
FOREIGN KEY(cookie_name) REFERENCES Cookie(name),
FOREIGN KEY (order_id) REFERENCES Orders(order_id)
);
CREATE TABLE OrderSpec (
order_id INT,
cookie_id INT,
cookie_name VARCHAR(255),
quantity INT,
PRIMARY KEY (order_id, cookie_id),
FOREIGN KEY (order_id) REFERENCES Order(order_id),
FOREIGN KEY (cookie_id) REFERENCES Cookie(cookie_id)
PRIMARY KEY (order_id, cookie_name),
FOREIGN KEY (order_id) REFERENCES Orders(order_id),
FOREIGN KEY (cookie_name) REFERENCES Cookie(name)
);
CREATE TABLE Ingredient (
ingredient_id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255),
ingredient_name VARCHAR(255) PRIMARY KEY,
stock INT,
unit VARCHAR(50),
delivery_date DATE,
@ -55,10 +52,10 @@ CREATE TABLE Ingredient (
);
CREATE TABLE Recipe (
ingredient_id INT,
cookie_id INT,
ingredient_name VARCHAR(255),
cookie_name VARCHAR(255),
amount FLOAT,
PRIMARY KEY (ingredient_id, cookie_id),
FOREIGN KEY (ingredient_id) REFERENCES Ingredient(ingredient_id),
FOREIGN KEY (cookie_id) REFERENCES Cookie(cookie_id)
PRIMARY KEY (ingredient_name, cookie_name),
FOREIGN KEY (ingredient_name) REFERENCES Ingredient(ingredient_name),
FOREIGN KEY (cookie_name) REFERENCES Cookie(name)
);

View file

@ -1,15 +1,14 @@
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!
@ -45,26 +44,38 @@ public class Database {
public String getCustomers(Request req, Response res) {
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);
}
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) {
return "{\"cookies\":[]}";
String query = "Select name from Cookie";
String title = "cookies";
return getJson(query, title);
}
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) {
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) {
@ -72,17 +83,55 @@ public class Database {
}
public String createPallet(Request req, Response res) {
return "{}";
if (req.queryParams("cookie") != null) {
String cookie = req.queryParams("cookie");
return createPallet(cookie);
} else {
return "";
}
}
private String getJson(String query, String title) { //privat hjälpmetod man slipper kalla jsonizer varje gång
try {
PreparedStatement stmt = conn.prepareStatement(query);
ResultSet rs = stmt.executeQuery();
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 man slipper kalla jsonizer varje gång
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;
}
}