1 |
1 |
package cz.zcu.yamanager.repository;
|
2 |
2 |
|
3 |
|
import cz.zcu.yamanager.domain.Test;
|
|
3 |
import cz.zcu.yamanager.dto.*;
|
4 |
4 |
import org.slf4j.Logger;
|
5 |
5 |
import org.slf4j.LoggerFactory;
|
6 |
6 |
import org.springframework.beans.factory.annotation.Autowired;
|
7 |
7 |
import org.springframework.jdbc.core.JdbcTemplate;
|
8 |
8 |
import org.springframework.stereotype.Repository;
|
9 |
9 |
|
|
10 |
import java.sql.ResultSet;
|
|
11 |
import java.time.LocalDate;
|
10 |
12 |
import java.util.List;
|
11 |
13 |
|
12 |
14 |
@Repository
|
... | ... | |
21 |
23 |
this.jdbc = jdbc;
|
22 |
24 |
}
|
23 |
25 |
|
24 |
|
public List<Test> all() {
|
25 |
|
return jdbc.query("SELECT id, message FROM test",
|
26 |
|
(rs, rowNum) -> new Test(rs.getLong("id"), rs.getString("message")));
|
|
26 |
public List<AuthorizationRequest> getAllUserRequests() {
|
|
27 |
return jdbc.query("SELECT id, first_name, last_name FROM end_user", (ResultSet rs, int rowNum) -> {
|
|
28 |
AuthorizationRequest request = new AuthorizationRequest();
|
|
29 |
request.setId(rs.getLong("id"));
|
|
30 |
request.setUserName(rs.getString("first_name"), rs.getString("last_name"));
|
|
31 |
|
|
32 |
// No timestamp
|
|
33 |
|
|
34 |
return request;
|
|
35 |
});
|
|
36 |
}
|
|
37 |
|
|
38 |
public List<AuthorizationRequest> getAllPendingUserRequests() {
|
|
39 |
return jdbc.query("SELECT id, first_name, last_name FROM end_user WHERE status_id=2", (ResultSet rs, int rowNum) -> {
|
|
40 |
AuthorizationRequest request = new AuthorizationRequest();
|
|
41 |
request.setId(rs.getLong("id"));
|
|
42 |
request.setUserName(rs.getString("first_name"), rs.getString("last_name"));
|
|
43 |
|
|
44 |
// No timestamp
|
|
45 |
|
|
46 |
return request;
|
|
47 |
});
|
|
48 |
}
|
|
49 |
|
|
50 |
public void updateApproval(BasicRequest request) {
|
|
51 |
if (request.getType() == RequestType.AUTHORIZATION) {
|
|
52 |
jdbc.update("UPDATE end_user SET status_id=? WHERE id=?", request.getStatus().ordinal(), request.getId());
|
|
53 |
} else {
|
|
54 |
jdbc.update("UPDATE vacation_day SET status_id=? WHERE id=?", request.getStatus().ordinal(), request.getId());
|
|
55 |
}
|
|
56 |
}
|
|
57 |
|
|
58 |
public List<CalendarItem> getCalendarItems(long userId, LocalDate from, LocalDate to) {
|
|
59 |
return jdbc.query("SELECT vacation_date, time_from, time_to, type_id, status_id FROM vacation_day WHERE user_id=? AND vacation_date >= ? AND vacation_date <= ?",
|
|
60 |
new Object[]{userId, from, to}, (ResultSet rs, int rowNum) -> {
|
|
61 |
CalendarItem item = new CalendarItem();
|
|
62 |
item.setDate(rs.getDate("day.vacation_date").toLocalDate());
|
|
63 |
item.setFrom(rs.getTime("day.time_from").toLocalTime());
|
|
64 |
item.setTo(rs.getTime("day.time_to").toLocalTime());
|
|
65 |
item.setType(VacationType.values()[rs.getByte("type_id")]);
|
|
66 |
item.setStatus(RequestStatus.values()[rs.getByte("status_id")]);
|
|
67 |
return item;
|
|
68 |
});
|
|
69 |
}
|
|
70 |
|
|
71 |
/*public void insertCalendarItem(CalendarItem item) {
|
|
72 |
jdbc.update("INSERT INTO vacation_day (vacation_date, time_from, time_to, user_id, status_id, type_id) VALUES (?,?,?,?,?,?)",
|
|
73 |
item.getDate(), item.getFrom(), item.getTo(), item.getUserId(), item.getStatus().ordinal(), item.getType().ordinal());
|
|
74 |
}
|
|
75 |
|
|
76 |
public void updateCalendarItem(CalendarItem item) {
|
|
77 |
jdbc.update("UPDATE vacation_day SET vacation_date=?, time_from=?, time_to=?, user_id=?, status_id=?, type_id=? WHERE id=?",
|
|
78 |
item.getDate(), item.getFrom(), item.getTo(), item.getUserId(), item.getStatus().ordinal(), item.getType().ordinal(), item.getId());
|
|
79 |
}*/
|
|
80 |
|
|
81 |
public DefaultSettings getLastSettings() {
|
|
82 |
return jdbc.queryForObject("SELECT * FROM default_settings ORDER BY id DESC LIMIT 1", (ResultSet rs, int rowNum) -> {
|
|
83 |
DefaultSettings settings = new DefaultSettings();
|
|
84 |
settings.setSickDay(rs.getShort("no_sick_days"), VacationUnit.DAY);
|
|
85 |
settings.setNotification(rs.getTimestamp("alert").toLocalDateTime());
|
|
86 |
return settings;
|
|
87 |
});
|
|
88 |
}
|
|
89 |
|
|
90 |
public void insertSettings(DefaultSettings settings) {
|
|
91 |
jdbc.update("INSERT INTO default_settings (no_sick_days, alert) VALUES (?, ?)", settings.getSickDay().getValue(), settings.getNotification());
|
|
92 |
}
|
|
93 |
|
|
94 |
public List<BasicProfileUser> getAllBasicUsers() {
|
|
95 |
return jdbc.query("SELECT id, first_name, last_name, photo FROM end_user", (ResultSet rs, int rowNum) -> {
|
|
96 |
BasicProfileUser user = new BasicProfileUser();
|
|
97 |
user.setId(rs.getLong("id"));
|
|
98 |
user.setName(rs.getString("first_name"), rs.getString("last_name"));
|
|
99 |
user.setPhoto(rs.getString("photo"));
|
|
100 |
return user;
|
|
101 |
});
|
|
102 |
}
|
|
103 |
|
|
104 |
public FullUserProfile getFullUser(long id) {
|
|
105 |
return jdbc.queryForObject("SELECT first_name, last_name, no_vacation, no_sick_days, alert, photo, role_id, status_id FROM end_user WHERE id=?",
|
|
106 |
new Object[]{id}, (ResultSet rs, int rowNum) -> {
|
|
107 |
FullUserProfile user = new FullUserProfile();
|
|
108 |
user.setId(id);
|
|
109 |
user.setName(rs.getString("first_name"), rs.getString("last_name"));
|
|
110 |
user.setVacation(rs.getFloat("no_vacation"), VacationUnit.HOUR);
|
|
111 |
user.setSickDay(rs.getShort("no_sick_days"), VacationUnit.DAY);
|
|
112 |
user.setNotification(rs.getTimestamp("alert").toLocalDateTime());
|
|
113 |
user.setPhoto(rs.getString("photo"));
|
|
114 |
user.setRole(UserRole.values()[rs.getByte("role_id")]);
|
|
115 |
user.setStatus(UserStatus.values()[rs.getByte("status_id")]);
|
|
116 |
return user;
|
|
117 |
});
|
|
118 |
}
|
|
119 |
|
|
120 |
public FullUserProfile getFullUser(String token) {
|
|
121 |
return jdbc.queryForObject("SELECT id, first_name, last_name, no_vacation, no_sick_days, alert, photo, role_id, status_id FROM end_user WHERE token=?",
|
|
122 |
new Object[]{token}, (ResultSet rs, int rowNum) -> {
|
|
123 |
FullUserProfile user = new FullUserProfile();
|
|
124 |
user.setId(rs.getLong("id"));
|
|
125 |
user.setName(rs.getString("first_name"), rs.getString("last_name"));
|
|
126 |
user.setVacation(rs.getFloat("no_vacation"), VacationUnit.HOUR);
|
|
127 |
user.setSickDay(rs.getShort("no_sick_days"), VacationUnit.DAY);
|
|
128 |
user.setNotification(rs.getTimestamp("alert").toLocalDateTime());
|
|
129 |
user.setPhoto(rs.getString("photo"));
|
|
130 |
user.setRole(UserRole.values()[rs.getByte("role_id")]);
|
|
131 |
user.setStatus(UserStatus.values()[rs.getByte("status_id")]);
|
|
132 |
return user;
|
|
133 |
});
|
27 |
134 |
}
|
28 |
135 |
}
|
Re #7442 Basic database queries