Skip to content

Commit

Permalink
improve: show last added series first on the pages with a collection …
Browse files Browse the repository at this point in the history
…info, series info and collection estimation

Fix #1621
  • Loading branch information
php-coder committed Jul 27, 2023
1 parent 625ecc9 commit 9172184
Show file tree
Hide file tree
Showing 12 changed files with 107 additions and 37 deletions.
1 change: 1 addition & 0 deletions NEWS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
- (infrastructure) discontinue usage of Danger
- (infrastructure) discontinue usage of 0pdd
- (infrastructure) migrate CI from Travis to GitHub Actions
- (improvement) show last added series first on the pages with a collection info, series info and collection estimation

0.4.6
- (feature) users can add a comment to a series
Expand Down
11 changes: 11 additions & 0 deletions src/main/config/spotbugs-filter.xml
Original file line number Diff line number Diff line change
Expand Up @@ -282,5 +282,16 @@
<Class name="ru.mystamps.web.feature.site.SuspiciousActivityDto" />
<Bug pattern="EI_EXPOSE_REP,EI_EXPOSE_REP2" />
</Match>
<Match>
<!--
@todo #1621 AddToCollectionDbDto: port to a new datetime API
-->
<Class name="ru.mystamps.web.feature.collection.AddToCollectionDbDto" />
<Or>
<Method name="getAddedAt" />
<Method name="setAddedAt" />
</Or>
<Bug pattern="EI_EXPOSE_REP,EI_EXPOSE_REP2" />
</Match>

</FindBugsFilter>
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import lombok.Setter;

import java.math.BigDecimal;
import java.util.Date;

@Getter
@Setter
Expand All @@ -30,4 +31,5 @@ public class AddToCollectionDbDto {
private Integer numberOfStamps;
private BigDecimal price;
private String currency;
private Date addedAt;
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public void createCollection(Integer ownerId, String ownerLogin) {
log.info("Collection #{} has been created ({})", id, collection);
}

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

Date now = new Date();
AddToCollectionDbDto collectionDto = new AddToCollectionDbDto();
collectionDto.setOwnerId(userId);
collectionDto.setSeriesId(dto.getSeriesId());
collectionDto.setNumberOfStamps(dto.getNumberOfStamps());
collectionDto.setAddedAt(now);

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

Integer seriesInstanceId = collectionDao.addSeriesToUserCollection(collectionDto);
collectionDao.markAsModified(userId, new Date());
collectionDao.markAsModified(userId, now);

log.info(
"Series #{} ({}) has been added to collection: #{}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ public Integer addSeriesToUserCollection(AddToCollectionDbDto dto) {
params.put("number_of_stamps", dto.getNumberOfStamps());
params.put("price", dto.getPrice());
params.put("currency", dto.getCurrency());
params.put("added_at", dto.getAddedAt());

KeyHolder holder = new GeneratedKeyHolder();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

@RequiredArgsConstructor
Expand All @@ -38,7 +38,7 @@ public class MapIntegerIntegerResultSetExtractor
public Map<Integer, Integer> extractData(ResultSet rs)
throws SQLException, DataAccessException {

Map<Integer, Integer> result = new HashMap<>();
Map<Integer, Integer> result = new LinkedHashMap<>();

while (rs.next()) {
Integer key = JdbcUtils.getInteger(rs, keyFieldName);
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/liquibase/version/0.4.7.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@
<include file="0.4.7/2021-07-18--series_import_parsed_image_urls.xml" relativeToChangelogFile="true" />
<include file="0.4.7/2021-11-28--series_and_nullable_perforated_field.xml" relativeToChangelogFile="true" />
<include file="0.4.7/2022-09-08--re_apply_column_comments.xml" relativeToChangelogFile="true" />
<include file="0.4.7/2023-07-27--collection_added_at.xml" relativeToChangelogFile="true" />

</databaseChangeLog>
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.3.xsd">

<changeSet id="add-added_at-field-to-collections_series" author="php-coder" context="scheme">

<addColumn tableName="collections_series">
<column name="added_at" type="DATETIME" />
</addColumn>

<!-- Unfortunately, H2 doesn't support UPDATE with JOIN -->
<sql>
UPDATE collections_series cs
SET added_at = (
SELECT c.updated_at
FROM collections c
WHERE c.id = cs.collection_id
)
</sql>

<addNotNullConstraint
tableName="collections_series"
columnDataType="DATETIME"
columnName="added_at" />

</changeSet>

</databaseChangeLog>
24 changes: 16 additions & 8 deletions src/main/resources/sql/collection_dao_queries.properties
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ collection.find_series_by_collection_id = \
ON cat.id = s.category_id \
LEFT JOIN countries count \
ON count.id = s.country_id \
WHERE cs.collection_id = :collection_id
WHERE cs.collection_id = :collection_id \
ORDER BY cs.added_at DESC \
, cs.id DESC

collection.find_series_with_prices_by_slug = \
SELECT s.id \
Expand All @@ -57,7 +59,9 @@ collection.find_series_with_prices_by_slug = \
ON s.id = cs.series_id \
LEFT JOIN countries count \
ON count.id = s.country_id \
WHERE c.slug = :slug
WHERE c.slug = :slug \
ORDER BY cs.added_at DESC \
, cs.id DESC

collection.find_all_for_sitemap = \
SELECT c.slug AS id \
Expand Down Expand Up @@ -122,12 +126,14 @@ SELECT COUNT(*) \
AND cs.series_id = :series_id

collection.find_series_instances = \
SELECT cs.id, cs.number_of_stamps \
FROM collections c \
JOIN collections_series cs \
ON cs.collection_id = c.id \
WHERE c.user_id = :user_id \
AND cs.series_id = :series_id
SELECT cs.id, cs.number_of_stamps \
FROM collections c \
JOIN collections_series cs \
ON cs.collection_id = c.id \
WHERE c.user_id = :user_id \
AND cs.series_id = :series_id \
ORDER BY cs.added_at DESC \
, cs.id DESC

collection.add_series_to_collection = \
INSERT \
Expand All @@ -137,12 +143,14 @@ INSERT \
, number_of_stamps \
, price \
, currency \
, added_at \
) \
SELECT c.id AS collection_id \
, :series_id AS series_id \
, :number_of_stamps AS number_of_stamps \
, :price as price \
, :currency as currency \
, :added_at as added_at \
FROM collections c \
WHERE c.user_id = :user_id

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ class CollectionServiceImplTest extends Specification {
assert dto.ownerId == expectedUserId
assert dto.seriesId == expectedSeriesId
assert dto.numberOfStamps == expectedNumberOfStamps
assert DateUtils.roughlyEqual(dto.addedAt, new Date())
return true
}) >> Random.id()
and:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,11 @@ public void getStatisticsOfWithEmptyCollection() {
"/db/series-3-sport-qty7.sql"
},
statements = {
"INSERT INTO collections_series(collection_id, series_id, number_of_stamps) "
+ "VALUES (1, 1, 5), (1, 2, 3), (1, 3, 7)"
// CheckStyle: ignore LineLength for next 1 line
"INSERT INTO collections_series(collection_id, series_id, number_of_stamps, added_at) VALUES"
+ " (1, 1, 5, CURRENT_TIMESTAMP())"
+ ", (1, 2, 3, CURRENT_TIMESTAMP())"
+ ", (1, 3, 7, CURRENT_TIMESTAMP())"
}
)
public void getStatisticsOfWithSeriesWithAllStamps() {
Expand All @@ -93,8 +96,8 @@ public void getStatisticsOfWithSeriesWithAllStamps() {
"/db/series-1-fauna-qty5.sql"
},
statements = {
"INSERT INTO collections_series(collection_id, series_id, number_of_stamps) "
+ "VALUES (1, 1, 2)"
"INSERT INTO collections_series(collection_id, series_id, number_of_stamps, added_at) "
+ "VALUES (1, 1, 2, CURRENT_TIMESTAMP())"
}
)
public void getStatisticsOfWithIncompleteSeries() {
Expand All @@ -114,8 +117,8 @@ public void getStatisticsOfWithIncompleteSeries() {
"/db/series-1-fauna-qty5.sql"
},
statements = {
"INSERT INTO collections_series(collection_id, series_id, number_of_stamps) "
+ "VALUES (1, 1, 5)"
"INSERT INTO collections_series(collection_id, series_id, number_of_stamps, added_at) "
+ "VALUES (1, 1, 5, CURRENT_TIMESTAMP())"
}
)
public void getStatisticsOfInRussian() {
Expand All @@ -135,8 +138,8 @@ public void getStatisticsOfInRussian() {
"/db/series-2-sport-qty3.sql"
},
statements = {
"INSERT INTO collections_series(collection_id, series_id, number_of_stamps) "
+ "VALUES (1, 2, 3)"
"INSERT INTO collections_series(collection_id, series_id, number_of_stamps, added_at) "
+ "VALUES (1, 2, 3, CURRENT_TIMESTAMP())"
}
)
public void getStatisticsOfInRussianWithFallbackToEnglish() {
Expand All @@ -156,8 +159,8 @@ public void getStatisticsOfInRussianWithFallbackToEnglish() {
"/db/series-1-fauna-qty5.sql"
},
statements = {
"INSERT INTO collections_series(collection_id, series_id, number_of_stamps) "
+ "VALUES (1, 1, 5)"
"INSERT INTO collections_series(collection_id, series_id, number_of_stamps, added_at) "
+ "VALUES (1, 1, 5, CURRENT_TIMESTAMP())"
}
)
public void getStatisticsOfInUnsupportedLanguageWithFallbackToEnglish() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,11 @@ public void countCountriesOfCollectionWithEmptyCollection() {
"/db/series-6-france-qty6.sql"
},
statements = {
"INSERT INTO collections_series(collection_id, series_id, number_of_stamps) "
+ "VALUES (1, 4, 5), (1, 5, 4), (1, 6, 6)"
// CheckStyle: ignore LineLength for next 1 line
"INSERT INTO collections_series(collection_id, series_id, number_of_stamps, added_at) VALUES"
+ " (1, 4, 5, CURRENT_TIMESTAMP())"
+ ", (1, 5, 4, CURRENT_TIMESTAMP())"
+ ", (1, 6, 6, CURRENT_TIMESTAMP())"
}
)
public void countCountriesOfCollectionWithMultipleSeriesFromEachCountry() {
Expand All @@ -90,8 +93,10 @@ public void countCountriesOfCollectionWithMultipleSeriesFromEachCountry() {
"/db/series-3-sport-qty7.sql"
},
statements = {
"INSERT INTO collections_series(collection_id, series_id, number_of_stamps) "
+ "VALUES (1, 2, 3), (1, 3, 7)"
// CheckStyle: ignore LineLength for next 1 line
"INSERT INTO collections_series(collection_id, series_id, number_of_stamps, added_at) VALUES"
+ " (1, 2, 3, CURRENT_TIMESTAMP())"
+ ", (1, 3, 7, CURRENT_TIMESTAMP())"
}
)
public void countCountriesOfCollectionWithSeriesFromUnknownCountries() {
Expand Down Expand Up @@ -132,8 +137,11 @@ public void getStatisticsOfWithEmptyCollection() {
"/db/series-6-france-qty6.sql"
},
statements = {
"INSERT INTO collections_series(collection_id, series_id, number_of_stamps) "
+ "VALUES (1, 4, 5), (1, 5, 4), (1, 6, 6)"
// CheckStyle: ignore LineLength for next 1 line
"INSERT INTO collections_series(collection_id, series_id, number_of_stamps, added_at) VALUES"
+ " (1, 4, 5, CURRENT_TIMESTAMP())"
+ ", (1, 5, 4, CURRENT_TIMESTAMP())"
+ ", (1, 6, 6, CURRENT_TIMESTAMP())"
}
)
public void getStatisticsOfWithSeriesWithAllStamps() {
Expand All @@ -159,8 +167,8 @@ public void getStatisticsOfWithSeriesWithAllStamps() {
"/db/series-4-italy-qty5.sql"
},
statements = {
"INSERT INTO collections_series(collection_id, series_id, number_of_stamps) "
+ "VALUES (1, 4, 2)"
"INSERT INTO collections_series(collection_id, series_id, number_of_stamps, added_at) "
+ "VALUES (1, 4, 2, CURRENT_TIMESTAMP())"
}
)
public void getStatisticsOfWithIncompleteSeries() {
Expand All @@ -180,8 +188,8 @@ public void getStatisticsOfWithIncompleteSeries() {
"/db/series-1-fauna-qty5.sql"
},
statements = {
"INSERT INTO collections_series(collection_id, series_id, number_of_stamps) "
+ "VALUES (1, 1, 5)"
"INSERT INTO collections_series(collection_id, series_id, number_of_stamps, added_at) "
+ "VALUES (1, 1, 5, CURRENT_TIMESTAMP())"
}
)
public void getStatisticsOfWithSeriesWithoutCountry() {
Expand All @@ -204,8 +212,8 @@ public void getStatisticsOfWithSeriesWithoutCountry() {
"/db/series-4-italy-qty5.sql"
},
statements = {
"INSERT INTO collections_series(collection_id, series_id, number_of_stamps) "
+ "VALUES (1, 4, 5)"
"INSERT INTO collections_series(collection_id, series_id, number_of_stamps, added_at) "
+ "VALUES (1, 4, 5, CURRENT_TIMESTAMP())"
}
)
public void getStatisticsOfInRussian() {
Expand All @@ -226,8 +234,8 @@ public void getStatisticsOfInRussian() {
"/db/series-5-france-qty4.sql"
},
statements = {
"INSERT INTO collections_series(collection_id, series_id, number_of_stamps) "
+ "VALUES (1, 5, 4)"
"INSERT INTO collections_series(collection_id, series_id, number_of_stamps, added_at) "
+ "VALUES (1, 5, 4, CURRENT_TIMESTAMP())"
}
)
public void getStatisticsOfInRussianWithFallbackToEnglish() {
Expand All @@ -248,8 +256,8 @@ public void getStatisticsOfInRussianWithFallbackToEnglish() {
"/db/series-5-france-qty4.sql"
},
statements = {
"INSERT INTO collections_series(collection_id, series_id, number_of_stamps) "
+ "VALUES (1, 5, 4)"
"INSERT INTO collections_series(collection_id, series_id, number_of_stamps, added_at) "
+ "VALUES (1, 5, 4, CURRENT_TIMESTAMP())"
}
)
public void getStatisticsOfInUnsupportedLanguageWithFallbackToEnglish() {
Expand Down

0 comments on commit 9172184

Please sign in to comment.