77import java .time .LocalDate ;
88
99import java .sql .Connection ;
10+ import java .sql .PreparedStatement ;
1011import java .sql .ResultSet ;
1112import java .sql .Statement ;
1213import java .sql .SQLException ;
1314
14- import com .codecool .onlineshop .dao .Connector ;
1515import com .codecool .onlineshop .model .Order ;
16+ import com .codecool .onlineshop .model .OrderStatus ;
1617import com .codecool .onlineshop .model .Product ;
1718import com .codecool .onlineshop .model .User ;
1819
1920public class OrdersDaoImpl implements OrdersDao {
20- private Connector connector ;
21+
2122 private Connection connection ;
22- private Statement statement ;
23- private ResultSet resultSet ;
24- private Order order ;
2523 private List <Order > orders ;
2624
2725 public OrdersDaoImpl () {
28- connector = new Connector ("src/main/resources/databases/OnlineShop.db" );
29- connection = connector .getDatabaseConnection ();
3026 orders = new ArrayList <>();
3127 addOrderData ();
3228 }
@@ -40,48 +36,93 @@ public List<Order> getOrderData() {
4036 public void addOrder (User user ) {
4137 Iterator <Product > basketIterator = user .getBasket ().getIterator ();
4238 LocalDate localDate = LocalDate .now ();
43- String date = localDate .getDayOfMonth () + "-" + localDate .getMonthValue () + "-" + localDate .getYear ();
44- Product currentProduct ;
45- int productId ;
46- int productAmount ;
47- int productAmountPrice ;
48- int userId ;
49- Integer orderId ;
50- String productName ;
5139
52- if (orders .isEmpty ()) {
53- orderId = 1 ;
54- } else {
55- orders .sort (Order ::compareTo );
56- orderId = orders .get (orders .size ()-1 ).getOrderId () + 1 ;
57- }
40+ connection = initializeConnection ();
41+ PreparedStatement insertOrder ;
42+ String insertOrderString = "INSERT INTO OrderDetails"
43+ + "(OrderID, ProductID, ProductName, ProductAmount, ProductAmountPrice, UserID, Date, Status)"
44+ + "VALUES (?, ?, ?, ?, ?, ?, ?, ?)" ;
5845
46+ String date = localDate .getDayOfMonth () + "-" + localDate .getMonthValue () + "-" + localDate .getYear ();
47+ int orderId = getIncrementedOrderId ();
5948 while (basketIterator .hasNext ()) {
60- currentProduct = basketIterator .next ();
61- productId = currentProduct .getId ();
62- productName = currentProduct .getName ();
63- productAmount = currentProduct .getAmount ();
64- productAmountPrice = currentProduct .getAmount () * currentProduct .getPrice ();
65- userId = user .getId ();
49+ Product currentProduct = basketIterator .next ();
50+ String productName = currentProduct .getName ();
51+ int productId = currentProduct .getId ();
52+ int productAmount = currentProduct .getAmount ();
53+ int productAmountPrice = currentProduct .getAmount () * currentProduct .getPrice ();
54+ int userId = user .getId ();
6655
6756 try {
68- statement = connection .createStatement ();
69- String insertInto = "INSERT INTO OrderDetails (OrderID, ProductID, ProductName, ProductAmount, ProductAmountPrice, UserID, Date) VALUES ("
70- + orderId + "," + productId + ",\" " + productName + "\" ," + productAmount + "," + productAmountPrice + "," + userId
71- + ",\" " + date + "\" );" ;
72- statement .executeUpdate (insertInto );
73- statement .close ();
57+ insertOrder = connection .prepareStatement (insertOrderString );
58+ insertOrder .setInt (1 , orderId );
59+ insertOrder .setInt (2 , productId );
60+ insertOrder .setString (3 , productName );
61+ insertOrder .setInt (4 , productAmount );
62+ insertOrder .setInt (5 , productAmountPrice );
63+ insertOrder .setInt (6 , userId );
64+ insertOrder .setString (7 , date );
65+ insertOrder .setString (8 , OrderStatus .PENDING .name ());
66+ insertOrder .executeUpdate ();
67+ insertOrder .close ();
68+ connection .close ();
7469 } catch (SQLException e ) {
7570 e .printStackTrace ();
7671 }
7772 }
7873 }
7974
80- private List <Order > addOrderData () {
75+ @ Override
76+ public void updateOrderStatus (int orderId , String status ) {
77+ connection = initializeConnection ();
78+ PreparedStatement updateOrderStatus ;
79+ String updateOrderStatusString = "UPDATE OrderDetails SET Status = ? WHERE OrderID = ?" ;
80+ try {
81+ updateOrderStatus = connection .prepareStatement (updateOrderStatusString );
82+ updateOrderStatus .setInt (2 , orderId );
83+ updateOrderStatus .setString (1 , status );
84+ updateOrderStatus .executeUpdate ();
85+ updateOrderStatus .close ();
86+ connection .close ();
87+ } catch (SQLException e ) {
88+ e .printStackTrace ();
89+ }
90+ }
91+
92+ public boolean isValid (int orderId ) {
93+ for (Order order : orders ) {
94+ if (order .getOrderId () == orderId ) {
95+ return true ;
96+ }
97+ }
98+ return false ;
99+ }
100+
101+ private int getIncrementedOrderId () {
102+ int orderId ;
103+
104+ if (orders .isEmpty ()) {
105+ orderId = 1 ;
106+ } else {
107+ orders .sort (Order ::compareTo );
108+ orderId = orders .get (orders .size ()-1 ).getOrderId () + 1 ;
109+ }
110+ return orderId ;
111+ }
112+
113+ private Connection initializeConnection () {
114+ final String DATABASEPATH = "src/main/resources/databases/OnlineShop.db" ;
115+ Connector connector = new Connector (DATABASEPATH );
116+ return connector .getDatabaseConnection ();
117+ }
118+
119+ private void addOrderData () {
120+ connection = initializeConnection ();
121+ Order order ;
81122
82123 try {
83- statement = connection .createStatement ();
84- resultSet = statement .executeQuery ("SELECT * FROM OrderDetails;" );
124+ Statement statement = connection .createStatement ();
125+ ResultSet resultSet = statement .executeQuery ("SELECT * FROM OrderDetails;" );
85126 while (resultSet .next ()) {
86127 int orderId = resultSet .getInt ("OrderID" );
87128 int productId = resultSet .getInt ("ProductID" );
@@ -90,6 +131,7 @@ private List<Order> addOrderData() {
90131 int productAmountPrice = resultSet .getInt ("ProductAmountPrice" );
91132 int userId = resultSet .getInt ("UserID" );
92133 String date = resultSet .getString ("Date" );
134+ String status = resultSet .getString ("Status" );
93135
94136 order = new Order .Builder ()
95137 .withOrderId (orderId )
@@ -99,14 +141,15 @@ private List<Order> addOrderData() {
99141 .withProductAmountPrice (productAmountPrice )
100142 .withUserId (userId )
101143 .withDate (date )
144+ .withStatus (status )
102145 .build ();
103146 orders .add (order );
104147 }
105148 resultSet .close ();
106149 statement .close ();
150+ connection .close ();
107151 } catch (SQLException e ) {
108152 e .printStackTrace ();
109153 }
110- return orders ;
111154 }
112155}
0 commit comments