Skip to content

Commit

Permalink
Removed redundant customers field in Order
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Chen authored and Andrew Chen committed Jan 11, 2017
1 parent 6f86251 commit 9b11284
Show file tree
Hide file tree
Showing 9 changed files with 168 additions and 126 deletions.
73 changes: 0 additions & 73 deletions reservation2.sql

This file was deleted.

114 changes: 114 additions & 0 deletions reservationCurrent.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
#creating tables and primary keys
CREATE TABLE tables(
tables_id INT NOT NULL AUTO_INCREMENT,
capacity INT NOT NULL,

PRIMARY KEY (tables_id)
);

CREATE TABLE item(
item_id INT NOT NULL AUTO_INCREMENT,
price DECIMAL(13,2) NOT NULL,
name VARCHAR(20) NOT NULL,
description VARCHAR(50),

PRIMARY KEY (item_id)
);

CREATE TABLE customer(
customer_id INT NOT NULL AUTO_INCREMENT,
first_name VARCHAR(30) NOT NULL,
last_name VARCHAR(30) NOT NULL,
username VARCHAR (20) NOT NULL,
email VARCHAR(30) NOT NULL,
phone VARCHAR(15) NOT NULL,

PRIMARY KEY (customer_id),
UNIQUE (username)
);

CREATE TABLE reservation(
reservation_id INT NOT NULL AUTO_INCREMENT,
date DATETIME NOT NULL,
tables_id INT NOT NULL,
customer_id INT NOT NULL,

PRIMARY KEY (reservation_id),
FOREIGN KEY (tables_id)
REFERENCES tables(tables_id)
ON DELETE CASCADE,
FOREIGN KEY (customer_id)
REFERENCES customer(customer_id)
ON DELETE CASCADE
);

CREATE TABLE orders(
orders_id INT NOT NULL AUTO_INCREMENT,
reservation_id INT NOT NULL,

PRIMARY KEY (orders_id),
FOREIGN KEY (reservation_id)
REFERENCES reservation(reservation_id)
ON DELETE CASCADE
);

CREATE TABLE orderItem(
order_item_id INT NOT NULL AUTO_INCREMENT,
orders_id INT NOT NULL,
item_id INT NOT NULL,
quantity INT NOT NULL,

PRIMARY KEY (order_item_id),
FOREIGN KEY (orders_id)
REFERENCES orders(orders_id)
ON DELETE CASCADE,
FOREIGN KEY (item_id)
REFERENCES item(item_id)
ON DELETE CASCADE
);

#check foreign keys
#SHOW CREATE TABLE `reservation`;

#Test Data

#tables
INSERT INTO tables (tables_id, capacity) VALUES (1, 6);
INSERT INTO tables (capacity) VALUES (6);
INSERT INTO tables (capacity) VALUES (6);
INSERT INTO tables (capacity) VALUES (2);
INSERT INTO tables (capacity) VALUES (2);
INSERT INTO tables (capacity) VALUES (2);
INSERT INTO tables (capacity) VALUES (8);

#customer
INSERT INTO customer (customer_id, first_name, last_name, username, email, phone) VALUES (1, 'Jason', 'Bourne', 'bourne2win', '[email protected]', '3829879876');

#items
INSERT INTO item
VALUES(1, 10.0, 'Regular Pizza', '18 inch classic pizza pie.');

INSERT INTO item(price, name, description)
VALUES(15.0, 'Anchovy Pizza', '18 inch pizza pie with anchovies.');

INSERT INTO item(price, name, description)
VALUES(15.0, 'Hawaiian Pizza', '18 inch pizza pie with ham and pineapple');

INSERT INTO item(price, name, description)
VALUES(15.0, 'French Fries Pizza', '18 inch pizza pie topped with french fries');

commit;

#run below to drop all tables while ignoring foreign key constraints

SET FOREIGN_KEY_CHECKS = 0;
drop table orderItem;
drop table reservation;
drop table customer;
drop table item;
drop table orders;
drop table tables;

show tables;

SET FOREIGN_KEY_CHECKS = 1;
File renamed without changes.
16 changes: 1 addition & 15 deletions src/main/java/com/revature/beans/Order.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,11 @@ public class Order {
@JoinColumn(name = "reservation_id")
private Reservation reservation;

@OneToOne
@JoinColumn(name = "customer_id", nullable = true)
private Customer customer;

@OneToMany(mappedBy = "order", fetch = FetchType.EAGER)
private Set<OrderItem> orderItems;

public Order(Reservation reservation, Customer customer) {
public Order(Reservation reservation) {
this.reservation = reservation;
this.customer = customer;
}

public Order() {
Expand All @@ -48,14 +43,6 @@ public void setReservation(Reservation reservation) {
this.reservation = reservation;
}

public Customer getCustomer() {
return customer;
}

public void setCustomer(Customer customer) {
this.customer = customer;
}

public Set<OrderItem> getorderItems() {
return orderItems;
}
Expand All @@ -69,7 +56,6 @@ public String toString() {
return "Order{" +
"id=" + id +
", reservation=" + reservation.getId() +
", customer=" + customer.getId() +
", orderItems=" + orderItems +
'}';
}
Expand Down
15 changes: 14 additions & 1 deletion src/main/java/com/revature/beans/Reservation.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,15 @@ public class Reservation {
@JoinColumn(name = "customer_id")
private Customer customer;

public Reservation(Date date, ReservationTable table, Customer customer) {
@Column
private int patrons;

public Reservation(Date date, ReservationTable table, Customer customer, int patrons) {
super();
this.date = date;
this.table = table;
this.customer = customer;
this.patrons = patrons;
}

public Reservation() {
Expand All @@ -41,6 +45,7 @@ public String toString() {
", date=" + date +
", table=" + table +
", customer=" + customer +
", patrons=" + patrons +
'}';
}

Expand Down Expand Up @@ -75,4 +80,12 @@ public Customer getCustomer() {
public void setCustomer(Customer customer) {
this.customer = customer;
}

public int getPatrons() {
return patrons;
}

public void setPatrons(int patrons) {
this.patrons = patrons;
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/revature/web/OrderController.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public HashSet<Order> getOrder(@RequestParam(value="q") int id) {
consumes = "application/json")
public @ResponseBody HttpEntity submitReservation(@RequestBody @Valid Order order,
BindingResult result){
System.out.println(order.getCustomer().getFirstName());
// System.out.println(order.getCustomer().getFirstName());
if(result.hasErrors()){
return new ResponseEntity("Failed", HttpStatus.BAD_REQUEST);
}
Expand Down
31 changes: 15 additions & 16 deletions src/test/java/com/revature/data/OrderDaoImplTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.revature.data;

import com.revature.beans.Customer;
import com.revature.beans.Order;
import com.revature.beans.Reservation;
import org.junit.AfterClass;
Expand All @@ -14,45 +13,45 @@ public class OrderDaoImplTest {
static AbstractApplicationContext context;

@BeforeClass
public static void getClassPathAP(){
public static void getClassPathAP() {
context = new ClassPathXmlApplicationContext("beans.xml");
}


@AfterClass
public static void endClassPathAP() {
context.registerShutdownHook();
}

@Test
public void getOrderByCustomerId() {
DataFacade facade = context.getBean("facade", DataFacade.class);
public void getOrderByCustomerId() {
DataFacade facade = context.getBean("facade", DataFacade.class);
System.out.println(facade.getOrderByReservationId(1));
}
@Ignore
}

@Ignore
@Test
public void getOrderById() {
DataFacade facade = context.getBean("facade", DataFacade.class);
System.out.println(facade.getOrderById(1));
}

@Ignore
@Ignore
@Test
public void insert() {
DataFacade facade = context.getBean("facade", DataFacade.class);

Customer customer = facade.getCustomerById(1);
Reservation reservation = facade.getReservationById(1);
Order order = new Order(reservation, customer);
Order order = new Order(reservation);

facade.insertOrder(order);
}

@Ignore
@Ignore
@Test
public void delete() {
DataFacade facade = context.getBean("facade", DataFacade.class);

Order order = facade.getOrderById(2);
facade.deleteOrder(order);
}

@AfterClass
public static void endClassPathAP(){
context.registerShutdownHook();
}
}
Loading

0 comments on commit 9b11284

Please sign in to comment.