Skip to content

Commit 9172184

Browse files
committed
improve: show last added series first on the pages with a collection info, series info and collection estimation
Fix #1621
1 parent 625ecc9 commit 9172184

File tree

12 files changed

+107
-37
lines changed

12 files changed

+107
-37
lines changed

NEWS.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
- (infrastructure) discontinue usage of Danger
55
- (infrastructure) discontinue usage of 0pdd
66
- (infrastructure) migrate CI from Travis to GitHub Actions
7+
- (improvement) show last added series first on the pages with a collection info, series info and collection estimation
78

89
0.4.6
910
- (feature) users can add a comment to a series

src/main/config/spotbugs-filter.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,5 +282,16 @@
282282
<Class name="ru.mystamps.web.feature.site.SuspiciousActivityDto" />
283283
<Bug pattern="EI_EXPOSE_REP,EI_EXPOSE_REP2" />
284284
</Match>
285+
<Match>
286+
<!--
287+
@todo #1621 AddToCollectionDbDto: port to a new datetime API
288+
-->
289+
<Class name="ru.mystamps.web.feature.collection.AddToCollectionDbDto" />
290+
<Or>
291+
<Method name="getAddedAt" />
292+
<Method name="setAddedAt" />
293+
</Or>
294+
<Bug pattern="EI_EXPOSE_REP,EI_EXPOSE_REP2" />
295+
</Match>
285296

286297
</FindBugsFilter>

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import lombok.Setter;
2222

2323
import java.math.BigDecimal;
24+
import java.util.Date;
2425

2526
@Getter
2627
@Setter
@@ -30,4 +31,5 @@ public class AddToCollectionDbDto {
3031
private Integer numberOfStamps;
3132
private BigDecimal price;
3233
private String currency;
34+
private Date addedAt;
3335
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ public void createCollection(Integer ownerId, String ownerLogin) {
6565
log.info("Collection #{} has been created ({})", id, collection);
6666
}
6767

68+
// @todo #1621 Add 3 integration tests to check that the last added series is shown first
6869
@Override
6970
@Transactional
7071
@PreAuthorize(HasAuthority.UPDATE_COLLECTION)
@@ -74,10 +75,12 @@ public void addToCollection(Integer userId, AddToCollectionDto dto) {
7475
Validate.isTrue(dto.getNumberOfStamps() != null, "Number of stamps must be non null");
7576
Validate.isTrue(dto.getSeriesId() != null, "Series id must be non null");
7677

78+
Date now = new Date();
7779
AddToCollectionDbDto collectionDto = new AddToCollectionDbDto();
7880
collectionDto.setOwnerId(userId);
7981
collectionDto.setSeriesId(dto.getSeriesId());
8082
collectionDto.setNumberOfStamps(dto.getNumberOfStamps());
83+
collectionDto.setAddedAt(now);
8184

8285
if (dto.getPrice() != null) {
8386
Validate.validState(
@@ -89,7 +92,7 @@ public void addToCollection(Integer userId, AddToCollectionDto dto) {
8992
}
9093

9194
Integer seriesInstanceId = collectionDao.addSeriesToUserCollection(collectionDto);
92-
collectionDao.markAsModified(userId, new Date());
95+
collectionDao.markAsModified(userId, now);
9396

9497
log.info(
9598
"Series #{} ({}) has been added to collection: #{}",

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ public Integer addSeriesToUserCollection(AddToCollectionDbDto dto) {
248248
params.put("number_of_stamps", dto.getNumberOfStamps());
249249
params.put("price", dto.getPrice());
250250
params.put("currency", dto.getCurrency());
251+
params.put("added_at", dto.getAddedAt());
251252

252253
KeyHolder holder = new GeneratedKeyHolder();
253254

src/main/java/ru/mystamps/web/support/spring/jdbc/MapIntegerIntegerResultSetExtractor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
import java.sql.ResultSet;
2626
import java.sql.SQLException;
27-
import java.util.HashMap;
27+
import java.util.LinkedHashMap;
2828
import java.util.Map;
2929

3030
@RequiredArgsConstructor
@@ -38,7 +38,7 @@ public class MapIntegerIntegerResultSetExtractor
3838
public Map<Integer, Integer> extractData(ResultSet rs)
3939
throws SQLException, DataAccessException {
4040

41-
Map<Integer, Integer> result = new HashMap<>();
41+
Map<Integer, Integer> result = new LinkedHashMap<>();
4242

4343
while (rs.next()) {
4444
Integer key = JdbcUtils.getInteger(rs, keyFieldName);

src/main/resources/liquibase/version/0.4.7.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@
88
<include file="0.4.7/2021-07-18--series_import_parsed_image_urls.xml" relativeToChangelogFile="true" />
99
<include file="0.4.7/2021-11-28--series_and_nullable_perforated_field.xml" relativeToChangelogFile="true" />
1010
<include file="0.4.7/2022-09-08--re_apply_column_comments.xml" relativeToChangelogFile="true" />
11+
<include file="0.4.7/2023-07-27--collection_added_at.xml" relativeToChangelogFile="true" />
1112

1213
</databaseChangeLog>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<databaseChangeLog
3+
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
6+
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.3.xsd">
7+
8+
<changeSet id="add-added_at-field-to-collections_series" author="php-coder" context="scheme">
9+
10+
<addColumn tableName="collections_series">
11+
<column name="added_at" type="DATETIME" />
12+
</addColumn>
13+
14+
<!-- Unfortunately, H2 doesn't support UPDATE with JOIN -->
15+
<sql>
16+
UPDATE collections_series cs
17+
SET added_at = (
18+
SELECT c.updated_at
19+
FROM collections c
20+
WHERE c.id = cs.collection_id
21+
)
22+
</sql>
23+
24+
<addNotNullConstraint
25+
tableName="collections_series"
26+
columnDataType="DATETIME"
27+
columnName="added_at" />
28+
29+
</changeSet>
30+
31+
</databaseChangeLog>

src/main/resources/sql/collection_dao_queries.properties

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ collection.find_series_by_collection_id = \
3939
ON cat.id = s.category_id \
4040
LEFT JOIN countries count \
4141
ON count.id = s.country_id \
42-
WHERE cs.collection_id = :collection_id
42+
WHERE cs.collection_id = :collection_id \
43+
ORDER BY cs.added_at DESC \
44+
, cs.id DESC
4345

4446
collection.find_series_with_prices_by_slug = \
4547
SELECT s.id \
@@ -57,7 +59,9 @@ collection.find_series_with_prices_by_slug = \
5759
ON s.id = cs.series_id \
5860
LEFT JOIN countries count \
5961
ON count.id = s.country_id \
60-
WHERE c.slug = :slug
62+
WHERE c.slug = :slug \
63+
ORDER BY cs.added_at DESC \
64+
, cs.id DESC
6165

6266
collection.find_all_for_sitemap = \
6367
SELECT c.slug AS id \
@@ -122,12 +126,14 @@ SELECT COUNT(*) \
122126
AND cs.series_id = :series_id
123127

124128
collection.find_series_instances = \
125-
SELECT cs.id, cs.number_of_stamps \
126-
FROM collections c \
127-
JOIN collections_series cs \
128-
ON cs.collection_id = c.id \
129-
WHERE c.user_id = :user_id \
130-
AND cs.series_id = :series_id
129+
SELECT cs.id, cs.number_of_stamps \
130+
FROM collections c \
131+
JOIN collections_series cs \
132+
ON cs.collection_id = c.id \
133+
WHERE c.user_id = :user_id \
134+
AND cs.series_id = :series_id \
135+
ORDER BY cs.added_at DESC \
136+
, cs.id DESC
131137

132138
collection.add_series_to_collection = \
133139
INSERT \
@@ -137,12 +143,14 @@ INSERT \
137143
, number_of_stamps \
138144
, price \
139145
, currency \
146+
, added_at \
140147
) \
141148
SELECT c.id AS collection_id \
142149
, :series_id AS series_id \
143150
, :number_of_stamps AS number_of_stamps \
144151
, :price as price \
145152
, :currency as currency \
153+
, :added_at as added_at \
146154
FROM collections c \
147155
WHERE c.user_id = :user_id
148156

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ class CollectionServiceImplTest extends Specification {
193193
assert dto.ownerId == expectedUserId
194194
assert dto.seriesId == expectedSeriesId
195195
assert dto.numberOfStamps == expectedNumberOfStamps
196+
assert DateUtils.roughlyEqual(dto.addedAt, new Date())
196197
return true
197198
}) >> Random.id()
198199
and:

0 commit comments

Comments
 (0)