Skip to content

Commit 2675e9f

Browse files
committed
task: add collections_series.id field to make possible to distinguish different instaces of a series.
This change is required for separate removal of a series instances from a collection. Part of #1123
1 parent 86abc32 commit 2675e9f

File tree

6 files changed

+39
-7
lines changed

6 files changed

+39
-7
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public interface CollectionDao {
3434
Integer add(AddCollectionDbDto collection);
3535
void markAsModified(Integer userId, Date updatedAt);
3636
boolean isSeriesInUserCollection(Integer userId, Integer seriesId);
37-
void addSeriesToUserCollection(AddToCollectionDbDto dto);
37+
Integer addSeriesToUserCollection(AddToCollectionDbDto dto);
3838
void removeSeriesFromUserCollection(Integer userId, Integer seriesId);
3939
CollectionInfoDto findCollectionInfoBySlug(String slug);
4040
}

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

+4-3
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,14 @@ public void addToCollection(Integer userId, AddToCollectionDto dto) {
8282
collectionDto.setCurrency(dto.getCurrency().toString());
8383
}
8484

85-
collectionDao.addSeriesToUserCollection(collectionDto);
85+
Integer seriesInstanceId = collectionDao.addSeriesToUserCollection(collectionDto);
8686
collectionDao.markAsModified(userId, new Date());
8787

8888
log.info(
89-
"Series #{} ({}) has been added to collection",
89+
"Series #{} ({}) has been added to collection: #{}",
9090
dto.getSeriesId(),
91-
formatSeriesInfo(collectionDto)
91+
formatSeriesInfo(collectionDto),
92+
seriesInstanceId
9293
);
9394
}
9495

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

+11-2
Original file line numberDiff line numberDiff line change
@@ -219,15 +219,22 @@ public boolean isSeriesInUserCollection(Integer userId, Integer seriesId) {
219219
}
220220

221221
@Override
222-
public void addSeriesToUserCollection(AddToCollectionDbDto dto) {
222+
public Integer addSeriesToUserCollection(AddToCollectionDbDto dto) {
223223
Map<String, Object> params = new HashMap<>();
224224
params.put("user_id", dto.getOwnerId());
225225
params.put("series_id", dto.getSeriesId());
226226
params.put("number_of_stamps", dto.getNumberOfStamps());
227227
params.put("price", dto.getPrice());
228228
params.put("currency", dto.getCurrency());
229229

230-
int affected = jdbcTemplate.update(addSeriesToCollectionSql, params);
230+
KeyHolder holder = new GeneratedKeyHolder();
231+
232+
int affected = jdbcTemplate.update(
233+
addSeriesToCollectionSql,
234+
new MapSqlParameterSource(params),
235+
holder,
236+
JdbcUtils.ID_KEY_COLUMN
237+
);
231238

232239
// CheckStyle: ignore LineLength for next 3 lines
233240
Validate.validState(
@@ -237,6 +244,8 @@ public void addSeriesToUserCollection(AddToCollectionDbDto dto) {
237244
dto.getOwnerId(),
238245
affected
239246
);
247+
248+
return holder.getKey().intValue();
240249
}
241250

242251
@SuppressWarnings("PMD.AvoidLiteralsInIfCondition")

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

+1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@
66
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.0.xsd">
77

88
<include file="0.4.2/2019-11-17--drop_unique_series_from_collection.xml" relativeToChangelogFile="true" />
9+
<include file="0.4.2/2019-11-27--add_collections_series_id.xml" relativeToChangelogFile="true" />
910

1011
</databaseChangeLog>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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-3.1.xsd">
7+
8+
<changeSet id="add-collections_series-id-field" author="php-coder" context="scheme">
9+
10+
<addColumn tableName="collections_series">
11+
<column name="id" type="INTEGER" autoIncrement="true" beforeColumn="collection_id" />
12+
</addColumn>
13+
14+
<addPrimaryKey
15+
tableName="collections_series"
16+
columnNames="id"
17+
constraintName="pk_collections_series_id" />
18+
19+
</changeSet>
20+
21+
</databaseChangeLog>

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ class CollectionServiceImplTest extends Specification {
194194
assert dto.seriesId == expectedSeriesId
195195
assert dto.numberOfStamps == expectedNumberOfStamps
196196
return true
197-
})
197+
}) >> Random.id()
198198
and:
199199
1 * collectionDao.markAsModified(
200200
expectedUserId,

0 commit comments

Comments
 (0)