Skip to content

Commit 7e1c172

Browse files
committed
CollectionService.isSeriesInCollection(): port from JPA to JDBC.
Addressed to #120 No functional changes.
1 parent 437df17 commit 7e1c172

File tree

6 files changed

+35
-14
lines changed

6 files changed

+35
-14
lines changed

src/main/java/ru/mystamps/web/controller/SeriesController.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,9 @@ public String showInfo(
193193

194194
model.addAttribute(
195195
"isSeriesInCollection",
196-
collectionService.isSeriesInCollection(currentUser, series)
196+
currentUser == null
197+
? false
198+
: collectionService.isSeriesInCollection(currentUser.getId(), series.getId())
197199
);
198200

199201
return "series/info";
@@ -222,7 +224,7 @@ public String processImage(
222224

223225
model.addAttribute(
224226
"isSeriesInCollection",
225-
collectionService.isSeriesInCollection(currentUser, series)
227+
collectionService.isSeriesInCollection(currentUser.getId(), series.getId())
226228
);
227229

228230
if (result.hasErrors()) {

src/main/java/ru/mystamps/web/dao/JdbcCollectionDao.java

+1
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,5 @@ public interface JdbcCollectionDao {
2424
Iterable<LinkEntityDto> findLastCreated(int quantity);
2525
long countCollectionsOfUsers();
2626
Integer add(AddCollectionDbDto collection);
27+
boolean isSeriesInUserCollection(Integer userId, Integer seriesId);
2728
}

src/main/java/ru/mystamps/web/dao/impl/JdbcCollectionDaoImpl.java

+15
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ public class JdbcCollectionDaoImpl implements JdbcCollectionDao {
4949
@Value("${collection.create}")
5050
private String addCollectionSql;
5151

52+
@Value("${collection.is_series_in_collection}")
53+
private String isSeriesInUserCollectionSql;
54+
5255
@Override
5356
public Iterable<LinkEntityDto> findLastCreated(int quantity) {
5457
return jdbcTemplate.query(
@@ -91,4 +94,16 @@ public Integer add(AddCollectionDbDto collection) {
9194
return Integer.valueOf(holder.getKey().intValue());
9295
}
9396

97+
@Override
98+
public boolean isSeriesInUserCollection(Integer userId, Integer seriesId) {
99+
Map<String, Object> params = new HashMap<>();
100+
params.put("user_id", userId);
101+
params.put("series_id", seriesId);
102+
103+
Long result = jdbcTemplate.queryForObject(isSeriesInUserCollectionSql, params, Long.class);
104+
Validate.validState(result != null, "Query returned null instead of long");
105+
106+
return result > 0;
107+
}
108+
94109
}

src/main/java/ru/mystamps/web/service/CollectionService.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public interface CollectionService {
2626
void createCollection(User user);
2727
Collection addToCollection(User user, Series series);
2828
Collection removeFromCollection(User user, Series series);
29-
boolean isSeriesInCollection(User user, Series series);
29+
boolean isSeriesInCollection(Integer userId, Integer seriesId);
3030
long countCollectionsOfUsers();
3131
Iterable<LinkEntityDto> findRecentlyCreated(int quantity);
3232
}

src/main/java/ru/mystamps/web/service/CollectionServiceImpl.java

+6-11
Original file line numberDiff line numberDiff line change
@@ -108,25 +108,20 @@ public Collection removeFromCollection(User user, Series series) {
108108

109109
@Override
110110
@Transactional(readOnly = false)
111-
public boolean isSeriesInCollection(User user, Series series) {
112-
Validate.isTrue(series != null, "Series must be non null");
111+
public boolean isSeriesInCollection(Integer userId, Integer seriesId) {
112+
Validate.isTrue(seriesId != null, "Series id must be non null");
113113

114-
if (user == null) {
114+
if (userId == null) {
115115
// Anonymous user doesn't have collection
116116
return false;
117117
}
118118

119-
// We can't just invoke user.getCollection().getSeries() because
120-
// it will lead to LazyInitializationException. To workaround this
121-
// we are loading collection by invoking dao.
122-
Collection collection = collectionDao.findOne(user.getCollection().getId());
123-
124-
boolean isSeriesInCollection = collection.getSeries().contains(series);
119+
boolean isSeriesInCollection = jdbcCollectionDao.isSeriesInUserCollection(userId, seriesId);
125120

126121
LOG.debug(
127122
"Series #{} belongs to collection of user #{}: {}",
128-
series.getId(),
129-
user.getId(),
123+
seriesId,
124+
userId,
130125
isSeriesInCollection
131126
);
132127

src/main/resources/sql/collection_dao_queries.properties

+8
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,11 @@ VALUES \
2929
( :user_id \
3030
, :slug \
3131
)
32+
33+
collection.is_series_in_collection = \
34+
SELECT COUNT(*) \
35+
FROM collections c \
36+
JOIN collections_series cs \
37+
ON cs.collection_id = c.id \
38+
WHERE c.user_id = :user_id \
39+
AND cs.series_id = :series_id

0 commit comments

Comments
 (0)