|
23 | 23 | import org.slf4j.LoggerFactory;
|
24 | 24 | import org.springframework.beans.factory.annotation.Value;
|
25 | 25 | import org.springframework.dao.EmptyResultDataAccessException;
|
| 26 | +import org.springframework.jdbc.core.ResultSetExtractor; |
26 | 27 | import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
|
27 | 28 | import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
|
28 | 29 | import org.springframework.jdbc.support.GeneratedKeyHolder;
|
29 | 30 | import org.springframework.jdbc.support.KeyHolder;
|
30 | 31 | import ru.mystamps.web.common.JdbcUtils;
|
31 | 32 | import ru.mystamps.web.common.LinkEntityDto;
|
| 33 | +import ru.mystamps.web.support.spring.jdbc.MapIntegerIntegerResultSetExtractor; |
32 | 34 |
|
33 | 35 | import java.util.Collections;
|
34 | 36 | import java.util.Date;
|
|
41 | 43 | public class JdbcCollectionDao implements CollectionDao {
|
42 | 44 | private static final Logger LOG = LoggerFactory.getLogger(JdbcCollectionDao.class);
|
43 | 45 |
|
| 46 | + private static final ResultSetExtractor<Map<Integer, Integer>> INSTANCES_COUNTER_EXTRACTOR = |
| 47 | + new MapIntegerIntegerResultSetExtractor("id", "number_of_stamps"); |
| 48 | + |
44 | 49 | private final NamedParameterJdbcTemplate jdbcTemplate;
|
45 | 50 |
|
46 | 51 | @Value("${collection.find_last_created}")
|
@@ -73,11 +78,14 @@ public class JdbcCollectionDao implements CollectionDao {
|
73 | 78 | @Value("${collection.is_series_in_collection}")
|
74 | 79 | private String isSeriesInUserCollectionSql;
|
75 | 80 |
|
| 81 | + @Value("${collection.find_series_instances}") |
| 82 | + private String findSeriesInstancesSql; |
| 83 | + |
76 | 84 | @Value("${collection.add_series_to_collection}")
|
77 | 85 | private String addSeriesToCollectionSql;
|
78 | 86 |
|
79 |
| - @Value("${collection.remove_series_from_collection}") |
80 |
| - private String removeSeriesFromCollectionSql; |
| 87 | + @Value("${collection.remove_series_instance_from_collection}") |
| 88 | + private String removeSeriesInstanceSql; |
81 | 89 |
|
82 | 90 | @Value("${collection.find_info_by_slug}")
|
83 | 91 | private String findCollectionInfoBySlugSql;
|
@@ -218,6 +226,19 @@ public boolean isSeriesInUserCollection(Integer userId, Integer seriesId) {
|
218 | 226 | return result > 0;
|
219 | 227 | }
|
220 | 228 |
|
| 229 | + @Override |
| 230 | + public Map<Integer, Integer> findSeriesInstances(Integer userId, Integer seriesId) { |
| 231 | + Map<String, Object> params = new HashMap<>(); |
| 232 | + params.put("user_id", userId); |
| 233 | + params.put("series_id", seriesId); |
| 234 | + |
| 235 | + return jdbcTemplate.query( |
| 236 | + findSeriesInstancesSql, |
| 237 | + params, |
| 238 | + INSTANCES_COUNTER_EXTRACTOR |
| 239 | + ); |
| 240 | + } |
| 241 | + |
221 | 242 | @Override
|
222 | 243 | public Integer addSeriesToUserCollection(AddToCollectionDbDto dto) {
|
223 | 244 | Map<String, Object> params = new HashMap<>();
|
@@ -250,17 +271,17 @@ public Integer addSeriesToUserCollection(AddToCollectionDbDto dto) {
|
250 | 271 |
|
251 | 272 | @SuppressWarnings("PMD.AvoidLiteralsInIfCondition")
|
252 | 273 | @Override
|
253 |
| - public void removeSeriesFromUserCollection(Integer userId, Integer seriesId) { |
| 274 | + public void removeSeriesFromUserCollection(Integer userId, Integer seriesInstanceId) { |
254 | 275 | Map<String, Object> params = new HashMap<>();
|
| 276 | + params.put("id", seriesInstanceId); |
255 | 277 | params.put("user_id", userId);
|
256 |
| - params.put("series_id", seriesId); |
257 | 278 |
|
258 |
| - int affected = jdbcTemplate.update(removeSeriesFromCollectionSql, params); |
| 279 | + int affected = jdbcTemplate.update(removeSeriesInstanceSql, params); |
259 | 280 | if (affected != 1) {
|
260 | 281 | // CheckStyle: ignore LineLength for next 2 lines
|
261 | 282 | LOG.warn(
|
262 |
| - "Unexpected number of affected rows after removing series #{} from collection of user #{}: {}", |
263 |
| - seriesId, |
| 283 | + "Unexpected number of affected rows after removing series instance #{} from collection of user #{}: {}", |
| 284 | + seriesInstanceId, |
264 | 285 | userId,
|
265 | 286 | affected
|
266 | 287 | );
|
|
0 commit comments