-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGetOrderDao.java
More file actions
88 lines (77 loc) · 2.64 KB
/
GetOrderDao.java
File metadata and controls
88 lines (77 loc) · 2.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package com.example.order.dao;
import com.example.order.dto.OrderDto;
import com.example.order.dto.ParamsDto;
import com.example.order.util.Database;
import com.example.order.util.ExceptionHandler;
import org.jetbrains.annotations.NotNull;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* DAO to get an order
*/
public class GetOrderDao {
private final static String query = "SELECT * FROM orders o WHERE o.order_id = ?";
private final Database database;
/**
* Constructor
*
* @param database Database object
*/
public GetOrderDao(Database database) {
this.database = database;
}
/**
* Gets an order by its ID
*
* @param paramsDto Object with the parameters for the operation
* @return Object with the main information of an order
*/
public OrderDto getOrderById(ParamsDto paramsDto) {
OrderDto orderDto = null;
try (Connection con = database.getConnection();
PreparedStatement ps = createPreparedStatement(con, paramsDto.getOrderId());
ResultSet rs = createResultSet(ps)
) {
if (rs.first()) {
orderDto = new OrderDto();
/* 'order_id' */
orderDto.setOrderId(rs.getLong(1));
/* 'order_customer_id' */
orderDto.setCustomerId(rs.getLong(2));
/* 'order_date' */
orderDto.setDate(rs.getDate(3));
/* 'order_status' */
orderDto.setStatus(rs.getString(4));
}
} catch (SQLException | IOException ex) {
ExceptionHandler.handleException((SQLException) ex);
}
return orderDto;
}
/**
* Creates a PreparedStatement object to get an order
*
* @param con Connection object
* @param orderId Order ID to set on the PreparedStatement
* @return A PreparedStatement object
* @throws SQLException In case of an error
*/
private @NotNull PreparedStatement createPreparedStatement(@NotNull Connection con, long orderId) throws SQLException {
PreparedStatement ps = con.prepareStatement(query);
ps.setLong(1, orderId);
return ps;
}
/**
* Creates a ResultSet object to get the results of the query
*
* @param ps PreparedStatement object to create the query
* @return A ResultSet object
* @throws SQLException In case of an error
*/
private ResultSet createResultSet(@NotNull PreparedStatement ps) throws SQLException {
return ps.executeQuery();
}
}