-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathCustomTripCityRepositoryImpl.java
43 lines (36 loc) · 1.72 KB
/
CustomTripCityRepositoryImpl.java
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
package hanglog.trip.domain.repository;
import static hanglog.global.type.StatusType.USABLE;
import hanglog.city.domain.City;
import java.time.LocalDateTime;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;
@RequiredArgsConstructor
@Repository
public class CustomTripCityRepositoryImpl implements CustomTripCityRepository {
private final NamedParameterJdbcTemplate namedParameterJdbcTemplate;
@Override
public void saveAll(final List<City> cities, final Long tripId) {
final String sql = """
INSERT INTO trip_city (city_id, created_at, modified_at, status, trip_id)
VALUES (:cityId, :createdAt, :modifiedAt, :status, :tripId)
""";
namedParameterJdbcTemplate.batchUpdate(sql, getTripCityToSqlParameterSources(cities, tripId));
}
private MapSqlParameterSource[] getTripCityToSqlParameterSources(final List<City> cities, final Long tripId) {
return cities.stream()
.map(city -> getTripCityToSqlParameterSource(city, tripId))
.toArray(MapSqlParameterSource[]::new);
}
private MapSqlParameterSource getTripCityToSqlParameterSource(final City city, final Long tripId) {
final LocalDateTime now = LocalDateTime.now();
return new MapSqlParameterSource()
.addValue("cityId", city.getId())
.addValue("createdAt", now)
.addValue("modifiedAt", now)
.addValue("status", USABLE.name())
.addValue("tripId", tripId);
}
}