Skip to content

Commit 33a7067

Browse files
committed
CollectionService.removeFromCollection(): port from JPA to JDBC.
Addressed to #120 No functional changes.
1 parent 79a6a9e commit 33a7067

File tree

8 files changed

+46
-26
lines changed

8 files changed

+46
-26
lines changed

src/main/java/ru/mystamps/web/config/ServicesConfig.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,6 @@ public class ServicesConfig {
5656
@Inject
5757
private ImageDao imageDao;
5858

59-
@Inject
60-
private CollectionDao collectionDao;
61-
6259
@Inject
6360
private JavaMailSender mailSender;
6461

@@ -80,7 +77,7 @@ public CategoryService getCategoryService() {
8077

8178
@Bean
8279
public CollectionService getCollectionService() {
83-
return new CollectionServiceImpl(collectionDao, daoConfig.getJdbcCollectionDao());
80+
return new CollectionServiceImpl(daoConfig.getJdbcCollectionDao());
8481
}
8582

8683
@Bean

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ public String removeFromCollection(
282282
return null;
283283
}
284284

285-
Collection collection = collectionService.removeFromCollection(currentUser, series);
285+
UrlEntityDto collection = collectionService.removeFromCollection(currentUser.getId(), series.getId());
286286

287287
redirectAttributes.addFlashAttribute("justRemovedSeries", true);
288288

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,5 @@ public interface JdbcCollectionDao {
2828
boolean isSeriesInUserCollection(Integer userId, Integer seriesId);
2929
UrlEntityDto findCollectionUrlEntityByUserId(Integer userId);
3030
void addSeriesToCollection(Integer collectionId, Integer seriesId);
31+
void removeSeriesFromCollection(Integer collectionId, Integer seriesId);
3132
}

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323

2424
import org.apache.commons.lang3.Validate;
2525

26+
import org.slf4j.Logger;
27+
import org.slf4j.LoggerFactory;
28+
2629
import org.springframework.beans.factory.annotation.Value;
2730
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
2831
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
@@ -39,6 +42,7 @@
3942
@RequiredArgsConstructor
4043
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
4144
public class JdbcCollectionDaoImpl implements JdbcCollectionDao {
45+
private static final Logger LOG = LoggerFactory.getLogger(JdbcCollectionDaoImpl.class);
4246

4347
private final NamedParameterJdbcTemplate jdbcTemplate;
4448

@@ -61,6 +65,9 @@ public class JdbcCollectionDaoImpl implements JdbcCollectionDao {
6165
@Value("${collection.add_series_to_collection}")
6266
private String addSeriesToCollectionSql;
6367

68+
@Value("${collection.remove_series_from_collection}")
69+
private String removeSeriesFromCollectionSql;
70+
6471
@Override
6572
public Iterable<LinkEntityDto> findLastCreated(int quantity) {
6673
return jdbcTemplate.query(
@@ -140,4 +147,23 @@ public void addSeriesToCollection(Integer collectionId, Integer seriesId) {
140147
);
141148
}
142149

150+
@SuppressWarnings("PMD.AvoidLiteralsInIfCondition")
151+
@Override
152+
public void removeSeriesFromCollection(Integer collectionId, Integer seriesId) {
153+
Map<String, Object> params = new HashMap<>();
154+
params.put("collection_id", collectionId);
155+
params.put("series_id", seriesId);
156+
157+
int affected = jdbcTemplate.update(removeSeriesFromCollectionSql, params);
158+
if (affected != 1) {
159+
// CheckStyle: ignore LineLength for next 2 lines
160+
LOG.warn(
161+
"Unexpected number of affected rows after removing series #{} from collection #{}: %{}",
162+
seriesId,
163+
collectionId,
164+
affected
165+
);
166+
}
167+
}
168+
143169
}

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,14 @@
1717
*/
1818
package ru.mystamps.web.service;
1919

20-
import ru.mystamps.web.entity.Collection;
21-
import ru.mystamps.web.entity.Series;
2220
import ru.mystamps.web.entity.User;
2321
import ru.mystamps.web.service.dto.LinkEntityDto;
2422
import ru.mystamps.web.service.dto.UrlEntityDto;
2523

2624
public interface CollectionService {
2725
void createCollection(User user);
2826
UrlEntityDto addToCollection(Integer userId, Integer seriesId);
29-
Collection removeFromCollection(User user, Series series);
27+
UrlEntityDto removeFromCollection(Integer userId, Integer seriesId);
3028
boolean isSeriesInCollection(Integer userId, Integer seriesId);
3129
long countCollectionsOfUsers();
3230
Iterable<LinkEntityDto> findRecentlyCreated(int quantity);

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

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,8 @@
2727

2828
import lombok.RequiredArgsConstructor;
2929

30-
import ru.mystamps.web.dao.CollectionDao;
3130
import ru.mystamps.web.dao.JdbcCollectionDao;
3231
import ru.mystamps.web.dao.dto.AddCollectionDbDto;
33-
import ru.mystamps.web.entity.Collection;
34-
import ru.mystamps.web.entity.Series;
3532
import ru.mystamps.web.entity.User;
3633
import ru.mystamps.web.service.dto.LinkEntityDto;
3734
import ru.mystamps.web.service.dto.UrlEntityDto;
@@ -41,7 +38,6 @@
4138
public class CollectionServiceImpl implements CollectionService {
4239
private static final Logger LOG = LoggerFactory.getLogger(CollectionServiceImpl.class);
4340

44-
private final CollectionDao collectionDao;
4541
private final JdbcCollectionDao jdbcCollectionDao;
4642

4743
@Override
@@ -86,24 +82,22 @@ public UrlEntityDto addToCollection(Integer userId, Integer seriesId) {
8682
@Override
8783
@Transactional
8884
@PreAuthorize("hasAuthority('UPDATE_COLLECTION')")
89-
public Collection removeFromCollection(User user, Series series) {
90-
Validate.isTrue(user != null, "User must be non null");
91-
Validate.isTrue(series != null, "Series must be non null");
85+
public UrlEntityDto removeFromCollection(Integer userId, Integer seriesId) {
86+
Validate.isTrue(userId != null, "User id must be non null");
87+
Validate.isTrue(seriesId != null, "Series id must be non null");
9288

93-
// We can't just invoke user.getCollection().getSeries() because
94-
// it will lead to LazyInitializationException. To workaround this
95-
// we are loading collection by invoking dao.
96-
Collection collection = collectionDao.findOne(user.getCollection().getId());
89+
UrlEntityDto url = jdbcCollectionDao.findCollectionUrlEntityByUserId(userId);
90+
Integer collectionId = url.getId();
9791

98-
collection.getSeries().remove(series);
92+
jdbcCollectionDao.removeSeriesFromCollection(collectionId, seriesId);
9993

10094
LOG.info(
10195
"Series #{} has been removed from collection of user #{}",
102-
series.getId(),
103-
user.getId()
96+
seriesId,
97+
userId
10498
);
10599

106-
return collection;
100+
return url;
107101
}
108102

109103
@Override

src/main/resources/sql/collection_dao_queries.properties

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,9 @@ VALUES \
5353
( :collection_id \
5454
, :series_id \
5555
)
56+
57+
collection.remove_series_from_collection = \
58+
DELETE \
59+
FROM collections_series \
60+
WHERE collection_id = :collection_id \
61+
AND series_id = :series_id

src/test/groovy/ru/mystamps/web/service/CollectionServiceImplTest.groovy

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,15 @@ package ru.mystamps.web.service
2020
import spock.lang.Specification
2121
import spock.lang.Unroll
2222

23-
import ru.mystamps.web.dao.CollectionDao
2423
import ru.mystamps.web.dao.JdbcCollectionDao
2524

2625
class CollectionServiceImplTest extends Specification {
27-
private CollectionDao collectionDao = Mock()
2826
private JdbcCollectionDao jdbcCollectionDao = Mock()
2927

3028
private CollectionService service
3129

3230
def setup() {
33-
service = new CollectionServiceImpl(collectionDao, jdbcCollectionDao)
31+
service = new CollectionServiceImpl(jdbcCollectionDao)
3432
}
3533

3634
//

0 commit comments

Comments
 (0)