forked from woowacourse/spring-roomescape-member
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJdbcReservationRepository.java
More file actions
293 lines (270 loc) · 10.2 KB
/
Copy pathJdbcReservationRepository.java
File metadata and controls
293 lines (270 loc) · 10.2 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
package roomescape.domain.reservation;
import java.sql.PreparedStatement;
import java.sql.Statement;
import java.time.LocalDate;
import java.util.List;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;
import roomescape.domain.reservationdate.ReservationDate;
import roomescape.domain.reservationtime.ReservationTime;
import roomescape.domain.theme.Theme;
@Repository
@RequiredArgsConstructor
public class JdbcReservationRepository implements ReservationRepository {
private static final String COLUMN_ID = "id";
private static final String COLUMN_NAME = "name";
private static final String COLUMN_DATE_ID = "date_id";
private static final String COLUMN_DATE = "date";
private static final String COLUMN_TIME_ID = "time_id";
private static final String COLUMN_START_AT = "start_at";
private static final String COLUMN_THEME_ID = "theme_id";
private static final String COLUMN_THEME_NAME = "theme_name";
private static final String COLUMN_THEME_CONTENT = "theme_content";
private static final String COLUMN_THEME_URL = "theme_url";
private static final String COLUMN_CONTENT = "content";
private static final String COLUMN_URL = "url";
private static final String INSERT_SQL = "insert into reservation(name, date_id, time_id, theme_id) values (?, ?, ?, ?)";
private static final String FIND_ALL_SQL =
"""
select r.id, r.name,
rd.id as date_id, rd.date,
rt.id as time_id, rt.start_at,
th.id as theme_id, th.name as theme_name, th.content as theme_content, th.url as theme_url
from reservation r
join reservation_date rd on r.date_id = rd.id
join reservation_time rt on r.time_id = rt.id
join theme th on r.theme_id = th.id
order by r.id
""";
private static final String COUNT_BY_TIME_ID_SQL =
"""
select count(*)
from reservation
where time_id = ?
""";
private static final String COUNT_BY_RESERVATION_DATE_ID_SQL =
"""
select count(*)
from reservation
where date_id = ?
""";
private static final String DELETE_BY_ID_SQL = "delete from reservation where id = ?";
private static final String FIND_BY_THEME_AND_DATE_SQL =
"""
select time_id
from reservation
where theme_id = ? and date_id = ?
""";
private static final String FIND_POPULAR_THEME_SQL =
"""
select
th.id,
th.name,
th.content,
th.url
from reservation r
join reservation_date rd on r.date_id = rd.id
join theme th on r.theme_id = th.id
where rd.date between ? and ?
group by th.id, th.name, th.content, th.url
order by count(r.id) desc, th.id asc
limit ?
""";
private static final String COUNT_BY_THEME_ID_SQL =
"""
select count(*)
from reservation
where theme_id = ?
""";
private static final String EXISTS_RESERVATION_BY_TIME_AND_DATE_AND_THEME_SQL =
"""
select exists(
select 1
from reservation r
where time_id = ? and date_id = ? and theme_id = ?
)
""";
private static final String EXISTS_OTHER_RESERVATION_BY_TIME_AND_DATE_AND_THEME_SQL =
"""
select exists(
select 1
from reservation r
where r.id <> ? and time_id = ? and date_id = ? and theme_id = ?
)
""";
private static final String FIND_BY_NAME_SQL =
"""
select r.id, r.name,
rd.id as date_id, rd.date,
rt.id as time_id, rt.start_at,
th.id as theme_id, th.name as theme_name, th.content as theme_content, th.url as theme_url
from reservation r
join reservation_date rd on r.date_id = rd.id
join reservation_time rt on r.time_id = rt.id
join theme th on r.theme_id = th.id
where r.name = ?
order by rd.date desc, rt.start_at desc, r.id desc
""";
private static final String FIND_BY_ID_SQL =
"""
select r.id, r.name,
rd.id as date_id, rd.date,
rt.id as time_id, rt.start_at,
th.id as theme_id, th.name as theme_name, th.content as theme_content, th.url as theme_url
from reservation r
join reservation_date rd on r.date_id = rd.id
join reservation_time rt on r.time_id = rt.id
join theme th on r.theme_id = th.id
where r.id = ?
""";
private static final String UPDATE_SQL =
"""
update reservation
set name = ?, date_id = ?, time_id = ?, theme_id = ?
where id = ?
""";
private final JdbcTemplate jdbcTemplate;
@Override
public Reservation save(Reservation reservation) {
KeyHolder keyHolder = new GeneratedKeyHolder();
jdbcTemplate.update(connection -> {
PreparedStatement ps = connection.prepareStatement(INSERT_SQL, Statement.RETURN_GENERATED_KEYS);
ps.setString(1, reservation.getName());
ps.setLong(2, reservation.getDate().getId());
ps.setLong(3, reservation.getTime().getId());
ps.setLong(4, reservation.getTheme().getId());
return ps;
}, keyHolder);
long id = extractId(keyHolder);
return Reservation.createWithId(id, reservation);
}
@Override
public List<Reservation> findAll() {
return jdbcTemplate.query(FIND_ALL_SQL, reservationRowMapper());
}
@Override
public int deleteById(Long id) {
return jdbcTemplate.update(DELETE_BY_ID_SQL, id);
}
@Override
public int countByTimeId(Long timeId) {
Integer count = jdbcTemplate.queryForObject(COUNT_BY_TIME_ID_SQL, Integer.class, timeId);
if (count == null) {
return 0;
}
return count;
}
@Override
public int countByReservationDateId(Long dateId) {
Integer count = jdbcTemplate.queryForObject(COUNT_BY_RESERVATION_DATE_ID_SQL, Integer.class, dateId);
if (count == null) {
return 0;
}
return count;
}
@Override
public List<Long> findReservedTimes(Long themeId, Long dateId) {
return jdbcTemplate.query(FIND_BY_THEME_AND_DATE_SQL, reservationTimeIdRowMapper(), themeId, dateId);
}
@Override
public List<Theme> findPopularThemes(int rankLimit, LocalDate startDay, LocalDate endDay) {
return jdbcTemplate.query(FIND_POPULAR_THEME_SQL, popularThemeRowMapper(), startDay, endDay, rankLimit);
}
@Override
public int countByThemeId(Long themeId) {
Integer count = jdbcTemplate.queryForObject(COUNT_BY_THEME_ID_SQL, Integer.class, themeId);
if (count == null) {
return 0;
}
return count;
}
@Override
public boolean existsReservation(Long timeId, Long dateId, Long themeId) {
Boolean exists = jdbcTemplate.queryForObject(
EXISTS_RESERVATION_BY_TIME_AND_DATE_AND_THEME_SQL,
Boolean.class,
timeId,
dateId,
themeId
);
return exists != null && exists;
}
@Override
public boolean existsOtherReservation(Long id, Long timeId, Long dateId, Long themeId) {
Boolean exists = jdbcTemplate.queryForObject(
EXISTS_OTHER_RESERVATION_BY_TIME_AND_DATE_AND_THEME_SQL,
Boolean.class,
id,
timeId,
dateId,
themeId
);
return exists != null && exists;
}
@Override
public List<Reservation> findByName(String name) {
return jdbcTemplate.query(FIND_BY_NAME_SQL, reservationRowMapper(), name);
}
@Override
public Optional<Reservation> findById(Long id) {
List<Reservation> result = jdbcTemplate.query(FIND_BY_ID_SQL, reservationRowMapper(), id);
return result.stream().findFirst();
}
@Override
public Optional<Reservation> update(Long id, Reservation withoutId) {
int updatedCount = jdbcTemplate.update(
UPDATE_SQL,
withoutId.getName(),
withoutId.getDate().getId(),
withoutId.getTime().getId(),
withoutId.getTheme().getId(),
id
);
if (updatedCount == 0) {
return Optional.empty();
}
return findById(id);
}
private RowMapper<Reservation> reservationRowMapper() {
return (rs, rowNum) -> Reservation.of(
rs.getLong(COLUMN_ID),
rs.getString(COLUMN_NAME),
ReservationDate.of(
rs.getLong(COLUMN_DATE_ID),
rs.getDate(COLUMN_DATE).toLocalDate()
),
ReservationTime.of(
rs.getLong(COLUMN_TIME_ID),
rs.getTime(COLUMN_START_AT).toLocalTime()
),
Theme.of(
rs.getLong(COLUMN_THEME_ID),
rs.getString(COLUMN_THEME_NAME),
rs.getString(COLUMN_THEME_CONTENT),
rs.getString(COLUMN_THEME_URL)
)
);
}
private RowMapper<Long> reservationTimeIdRowMapper() {
return (rs, rowNum) -> rs.getLong(COLUMN_TIME_ID);
}
private RowMapper<Theme> popularThemeRowMapper() {
return (rs, rowNum) -> Theme.of(
rs.getLong(COLUMN_ID),
rs.getString(COLUMN_NAME),
rs.getString(COLUMN_CONTENT),
rs.getString(COLUMN_URL)
);
}
private long extractId(KeyHolder keyHolder) {
if (keyHolder.getKey() == null) {
throw new IllegalStateException("생성 키를 조회할 수 없습니다.");
}
return keyHolder.getKey().longValue();
}
}