Skip to content

Commit 5a640d4

Browse files
committed
refactor: move countSeriesOf() method from SeriesService to CollectionService.
This change also remove dependency on series from collection module. Fix #932 No functional changes.
1 parent f965938 commit 5a640d4

14 files changed

+49
-54
lines changed

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

-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ public interface SeriesDao {
4242

4343
long countAll();
4444
long countAllStamps();
45-
long countSeriesOfCollection(Integer collectionId);
4645
long countSeriesById(Integer seriesId);
4746
long countAddedSince(Date date);
4847
long countUpdatedSince(Date date);

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

-12
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,6 @@ public class JdbcSeriesDao implements SeriesDao {
8888
@Value("${series.count_all_stamps}")
8989
private String countAllStampsSql;
9090

91-
@Value("${series.count_series_of_collection}")
92-
private String countSeriesOfCollectionSql;
93-
9491
@Value("${series.count_series_by_id}")
9592
private String countSeriesByIdSql;
9693

@@ -251,15 +248,6 @@ public long countAllStamps() {
251248
return jdbcTemplate.queryForObject(countAllStampsSql, Collections.emptyMap(), Long.class);
252249
}
253250

254-
@Override
255-
public long countSeriesOfCollection(Integer collectionId) {
256-
return jdbcTemplate.queryForObject(
257-
countSeriesOfCollectionSql,
258-
Collections.singletonMap("collection_id", collectionId),
259-
Long.class
260-
);
261-
}
262-
263251
@Override
264252
public long countSeriesById(Integer seriesId) {
265253
return jdbcTemplate.queryForObject(

src/main/java/ru/mystamps/web/feature/collection/CollectionConfig.java

-3
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828

2929
import ru.mystamps.web.feature.category.CategoryService;
3030
import ru.mystamps.web.feature.country.CountryService;
31-
import ru.mystamps.web.service.SeriesService;
3231

3332
/**
3433
* Spring configuration that is required for using collections in an application.
@@ -45,7 +44,6 @@ public static class Controllers {
4544
private final CategoryService categoryService;
4645
private final CollectionService collectionService;
4746
private final CountryService countryService;
48-
private final SeriesService seriesService;
4947
private final MessageSource messageSource;
5048

5149
@Bean
@@ -54,7 +52,6 @@ public CollectionController collectionController() {
5452
categoryService,
5553
collectionService,
5654
countryService,
57-
seriesService,
5855
messageSource
5956
);
6057
}

src/main/java/ru/mystamps/web/feature/collection/CollectionController.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
import ru.mystamps.web.Url;
3838
import ru.mystamps.web.feature.category.CategoryService;
3939
import ru.mystamps.web.feature.country.CountryService;
40-
import ru.mystamps.web.service.SeriesService;
4140
import ru.mystamps.web.util.LocaleUtils;
4241

4342
@Controller
@@ -47,7 +46,6 @@ public class CollectionController {
4746
private final CategoryService categoryService;
4847
private final CollectionService collectionService;
4948
private final CountryService countryService;
50-
private final SeriesService seriesService;
5149
private final MessageSource messageSource;
5250

5351
// @todo #884 /collection/{slug}: add a link to collection estimation page
@@ -82,7 +80,7 @@ public String showInfoBySlug(
8280
if (seriesOfCollection.iterator().hasNext()) {
8381
long categoryCounter = categoryService.countCategoriesOf(collectionId);
8482
long countryCounter = countryService.countCountriesOf(collectionId);
85-
long seriesCounter = seriesService.countSeriesOf(collectionId);
83+
long seriesCounter = collectionService.countSeriesOf(collectionId);
8684
long stampsCounter = collectionService.countStampsOf(collectionId);
8785

8886
List<Object[]> categoriesStat = categoryService.getStatisticsOf(collectionId, lang);

src/main/java/ru/mystamps/web/feature/collection/CollectionDao.java

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public interface CollectionDao {
2929
List<SeriesInCollectionWithPriceDto> findSeriesWithPricesBySlug(String slug, String lang);
3030
long countCollectionsOfUsers();
3131
long countUpdatedSince(Date date);
32+
long countSeriesOfCollection(Integer collectionId);
3233
long countStampsOfCollection(Integer collectionId);
3334
Integer add(AddCollectionDbDto collection);
3435
void markAsModified(Integer userId, Date updatedAt);

src/main/java/ru/mystamps/web/feature/collection/CollectionService.java

+2
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,15 @@
2222

2323
import ru.mystamps.web.dao.dto.LinkEntityDto;
2424

25+
@SuppressWarnings("PMD.TooManyMethods")
2526
public interface CollectionService {
2627
void createCollection(Integer ownerId, String ownerLogin);
2728
void addToCollection(Integer userId, AddToCollectionDto dto);
2829
void removeFromCollection(Integer userId, Integer seriesId);
2930
boolean isSeriesInCollection(Integer userId, Integer seriesId);
3031
long countCollectionsOfUsers();
3132
long countUpdatedSince(Date date);
33+
long countSeriesOf(Integer collectionId);
3234
long countStampsOf(Integer collectionId);
3335
List<LinkEntityDto> findRecentlyCreated(int quantity);
3436
List<SeriesInCollectionDto> findSeriesInCollection(Integer collectionId, String lang);

src/main/java/ru/mystamps/web/feature/collection/CollectionServiceImpl.java

+8
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,14 @@ public long countUpdatedSince(Date date) {
138138
return collectionDao.countUpdatedSince(date);
139139
}
140140

141+
@Override
142+
@Transactional(readOnly = true)
143+
public long countSeriesOf(Integer collectionId) {
144+
Validate.isTrue(collectionId != null, "Collection id must be non null");
145+
146+
return collectionDao.countSeriesOfCollection(collectionId);
147+
}
148+
141149
@Override
142150
@Transactional(readOnly = true)
143151
public long countStampsOf(Integer collectionId) {

src/main/java/ru/mystamps/web/feature/collection/JdbcCollectionDao.java

+12
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ public class JdbcCollectionDao implements CollectionDao {
6262
@Value("${collection.count_updated_since}")
6363
private String countUpdatedSinceSql;
6464

65+
@Value("${collection.count_series_of_collection}")
66+
private String countSeriesOfCollectionSql;
67+
6568
@Value("${collection.count_stamps_of_collection}")
6669
private String countStampsOfCollectionSql;
6770

@@ -139,6 +142,15 @@ public long countUpdatedSince(Date date) {
139142
);
140143
}
141144

145+
@Override
146+
public long countSeriesOfCollection(Integer collectionId) {
147+
return jdbcTemplate.queryForObject(
148+
countSeriesOfCollectionSql,
149+
Collections.singletonMap("collection_id", collectionId),
150+
Long.class
151+
);
152+
}
153+
142154
@Override
143155
public long countStampsOfCollection(Integer collectionId) {
144156
return jdbcTemplate.queryForObject(

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

-2
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ public interface SeriesService {
3535
void addImageToSeries(AddImageDto dto, Integer seriesId, Integer userId);
3636
long countAll();
3737
long countAllStamps();
38-
// @todo #927 SeriesService.countSeriesOf(): move to CollectionService
39-
long countSeriesOf(Integer collectionId);
4038
long countAddedSince(Date date);
4139
long countUpdatedSince(Date date);
4240
boolean isSeriesExist(Integer seriesId);

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

-8
Original file line numberDiff line numberDiff line change
@@ -178,14 +178,6 @@ public long countAllStamps() {
178178
return seriesDao.countAllStamps();
179179
}
180180

181-
@Override
182-
@Transactional(readOnly = true)
183-
public long countSeriesOf(Integer collectionId) {
184-
Validate.isTrue(collectionId != null, "Collection id must be non null");
185-
186-
return seriesDao.countSeriesOfCollection(collectionId);
187-
}
188-
189181
@Override
190182
@Transactional(readOnly = true)
191183
public long countAddedSince(Date date) {

src/main/resources/sql/collection_dao_queries.properties

+5
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ SELECT COUNT(*) \
6565
FROM collections \
6666
WHERE updated_at >= :date
6767

68+
collection.count_series_of_collection = \
69+
SELECT COUNT(*) AS counter \
70+
FROM collections_series cs \
71+
WHERE cs.collection_id = :collection_id
72+
6873
collection.count_stamps_of_collection = \
6974
SELECT COALESCE(SUM(cs.number_of_stamps), 0) AS counter \
7075
FROM collections_series cs \

src/main/resources/sql/series_dao_queries.properties

-5
Original file line numberDiff line numberDiff line change
@@ -178,11 +178,6 @@ series.count_all_stamps = \
178178
SELECT COALESCE(SUM(s.quantity), 0) \
179179
FROM series s
180180

181-
series.count_series_of_collection = \
182-
SELECT COUNT(*) AS counter \
183-
FROM collections_series cs \
184-
WHERE cs.collection_id = :collection_id
185-
186181
series.count_series_by_id = \
187182
SELECT COUNT(*) \
188183
FROM series \

src/test/groovy/ru/mystamps/web/feature/collection/CollectionServiceImplTest.groovy

+20
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,26 @@ class CollectionServiceImplTest extends Specification {
316316
result == expectedResult
317317
}
318318

319+
//
320+
// Tests for countSeriesOf()
321+
//
322+
323+
def 'countSeriesOf() should throw exception when argument is null'() {
324+
when:
325+
service.countSeriesOf(null)
326+
then:
327+
thrown IllegalArgumentException
328+
}
329+
330+
def 'countSeriesOf() should pass argument to dao'() {
331+
given:
332+
Integer expectedCollectionId = Random.id()
333+
when:
334+
service.countSeriesOf(expectedCollectionId)
335+
then:
336+
1 * collectionDao.countSeriesOfCollection(expectedCollectionId) >> positiveLong()
337+
}
338+
319339
//
320340
// Tests for countStampsOf()
321341
//

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

-20
Original file line numberDiff line numberDiff line change
@@ -573,26 +573,6 @@ class SeriesServiceImplTest extends Specification {
573573
result == expectedResult
574574
}
575575

576-
//
577-
// Tests for countSeriesOf()
578-
//
579-
580-
def "countSeriesOf() should throw exception when argument is null"() {
581-
when:
582-
service.countSeriesOf(null)
583-
then:
584-
thrown IllegalArgumentException
585-
}
586-
587-
def "countSeriesOf() should pass argument to dao"() {
588-
given:
589-
Integer expectedCollectionId = Random.id()
590-
when:
591-
service.countSeriesOf(expectedCollectionId)
592-
then:
593-
1 * seriesDao.countSeriesOfCollection(expectedCollectionId) >> positiveLong()
594-
}
595-
596576
//
597577
// Tests for countAddedSince()
598578
//

0 commit comments

Comments
 (0)