7
7
import java .sql .Statement ;
8
8
import java .time .LocalDateTime ;
9
9
import java .util .ArrayList ;
10
+ import java .util .List ;
10
11
11
12
import com .example .bean .EmployeeBean ;
12
13
13
14
public interface RestService {
14
15
public default ResponseMessage <EmployeeBean > getEmployees () {
15
16
ResponseMessage <EmployeeBean > message = new ResponseMessage <>();
16
17
17
- try (Connection conn = DatabaseUtil .getConnection ();
18
- Statement stmt = conn .createStatement ();
19
- ResultSet res = stmt .executeQuery ("select * from employees;" )) {
18
+ try (Connection conn = DatabaseProvider .getConnection ();
19
+ Statement stmt = conn .createStatement ();
20
+ ResultSet res = stmt .executeQuery ("select * from employees;" )) {
20
21
21
- message .setTimeStamp (LocalDateTime .now ());
22
- message .setStatus ("SUCCESS" );
23
- message .setMsg (EmployeeBean .ofResultSet (res ));
22
+ message .setTimeStamp (LocalDateTime .now ().toString ());
23
+ message .setStatus ("Ok" );
24
+ List <EmployeeBean > tmp = EmployeeBean .ofResultSet (res );
25
+ message .setCount (tmp .size ());
26
+ message .setMsg (tmp );
24
27
} catch (SQLException e ) {
25
28
e .printStackTrace ();
26
29
27
- message .setTimeStamp (LocalDateTime .now ());
28
- message .setStatus ("ERROR " );
30
+ message .setTimeStamp (LocalDateTime .now (). toString () );
31
+ message .setStatus ("Error " );
29
32
message .setMsg (new ArrayList <>());
30
33
}
31
34
@@ -36,23 +39,28 @@ public default ResponseMessage<EmployeeBean> getEmployeeById(Integer id) {
36
39
ResponseMessage <EmployeeBean > message = new ResponseMessage <>();
37
40
String sql = "select * from employees where id=?;" ;
38
41
39
- try (Connection conn = DatabaseUtil .getConnection ();
40
- PreparedStatement stmt = conn .prepareStatement (sql )) {
41
-
42
+ try (Connection conn = DatabaseProvider .getConnection (); PreparedStatement stmt = conn .prepareStatement (sql )) {
43
+
42
44
stmt .setInt (1 , id );
43
45
ResultSet res = stmt .executeQuery ();
44
46
47
+ EmployeeBean e = EmployeeBean .ofSingleResult (res );
48
+
45
49
ArrayList <EmployeeBean > tmp = new ArrayList <>();
46
- tmp .add (EmployeeBean .ofSingleResult (res ));
47
50
48
- message .setTimeStamp (LocalDateTime .now ());
49
- message .setStatus ("OK" );
51
+ if (e .getId () != -1 ) {
52
+ tmp .add (e );
53
+ }
54
+
55
+ message .setTimeStamp (LocalDateTime .now ().toString ());
56
+ message .setStatus ("Ok" );
50
57
message .setMsg (tmp );
58
+ message .setCount (tmp .size ());
51
59
} catch (SQLException e ) {
52
60
e .printStackTrace ();
53
61
54
- message .setTimeStamp (LocalDateTime .now ());
55
- message .setStatus ("ERROR " );
62
+ message .setTimeStamp (LocalDateTime .now (). toString () );
63
+ message .setStatus ("Error " );
56
64
message .setMsg (new ArrayList <>());
57
65
}
58
66
@@ -63,19 +71,18 @@ public default ResponseMessage<EmployeeBean> deleteEmployee(Integer id) {
63
71
ResponseMessage <EmployeeBean > message = new ResponseMessage <>();
64
72
String sql = "delete from employees where id=?;" ;
65
73
66
- try (Connection conn = DatabaseUtil .getConnection ();
67
- PreparedStatement stmt = conn .prepareStatement (sql )) {
74
+ try (Connection conn = DatabaseProvider .getConnection (); PreparedStatement stmt = conn .prepareStatement (sql )) {
68
75
stmt .setInt (1 , id );
69
76
stmt .executeUpdate ();
70
77
71
- message .setTimeStamp (LocalDateTime .now ());
72
- message .setStatus ("deleted " );
78
+ message .setTimeStamp (LocalDateTime .now (). toString () );
79
+ message .setStatus ("Ok " );
73
80
message .setMsg (new ArrayList <>());
74
81
} catch (SQLException e ) {
75
82
e .printStackTrace ();
76
83
77
- message .setTimeStamp (LocalDateTime .now ());
78
- message .setStatus ("error " );
84
+ message .setTimeStamp (LocalDateTime .now (). toString () );
85
+ message .setStatus ("Error " );
79
86
message .setMsg (new ArrayList <>());
80
87
}
81
88
@@ -85,33 +92,24 @@ public default ResponseMessage<EmployeeBean> deleteEmployee(Integer id) {
85
92
public default ResponseMessage <EmployeeBean > updateEmployee (EmployeeBean employee ) {
86
93
ResponseMessage <EmployeeBean > message = new ResponseMessage <>();
87
94
EmployeeBean current = new EmployeeBean ();
88
-
95
+
89
96
String sql1 = "select * from employees where id=?;" ;
90
- try (Connection conn = DatabaseUtil .getConnection ();
91
- PreparedStatement stmt = conn .prepareStatement (sql1 )) {
92
-
97
+ try (Connection conn = DatabaseProvider .getConnection (); PreparedStatement stmt = conn .prepareStatement (sql1 )) {
98
+
93
99
stmt .setInt (1 , employee .getId ());
94
100
ResultSet res = stmt .executeQuery ();
95
101
96
102
current = EmployeeBean .ofSingleResult (res );
97
103
} catch (SQLException e ) {
98
104
e .printStackTrace ();
99
105
}
100
-
106
+
101
107
current .update (employee );
102
-
103
- String sql2 = "update employees set firstName=?,"
104
- + "lastName=?,"
105
- + "birthDate=?,"
106
- + "gender=?,"
107
- + "company=?,"
108
- + "department=?,"
109
- + "employerId=?,"
110
- + "email=? "
111
- + "where id=?;" ;
112
-
113
- try (Connection conn = DatabaseUtil .getConnection ();
114
- PreparedStatement stmt = conn .prepareStatement (sql2 )) {
108
+
109
+ String sql2 = "update employees set firstName=?," + "lastName=?," + "birthDate=?," + "gender=?," + "company=?,"
110
+ + "department=?," + "employerId=?," + "email=? " + "where id=?;" ;
111
+
112
+ try (Connection conn = DatabaseProvider .getConnection (); PreparedStatement stmt = conn .prepareStatement (sql2 )) {
115
113
116
114
stmt .setString (1 , current .getFirstName ());
117
115
stmt .setString (2 , current .getLastName ());
@@ -125,12 +123,13 @@ public default ResponseMessage<EmployeeBean> updateEmployee(EmployeeBean employe
125
123
126
124
stmt .executeUpdate ();
127
125
128
- message .setTimeStamp (LocalDateTime .now ());
126
+ message .setTimeStamp (LocalDateTime .now (). toString () );
129
127
message .setStatus ("Ok" );
130
128
131
129
ArrayList <EmployeeBean > tmp = new ArrayList <>();
132
130
tmp .add (current );
133
131
message .setMsg (tmp );
132
+ message .setCount (tmp .size ());
134
133
} catch (SQLException e ) {
135
134
e .printStackTrace ();
136
135
}
0 commit comments