Skip to content

Commit 566d72f

Browse files
Shkarinphp-coder
authored andcommitted
Update the collections.updated_at/by fields when series added/removed.
1 parent 29f594f commit 566d72f

File tree

8 files changed

+105
-2
lines changed

8 files changed

+105
-2
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
*/
1818
package ru.mystamps.web.dao;
1919

20+
import java.util.Date;
2021
import java.util.List;
2122

2223
import ru.mystamps.web.dao.dto.AddCollectionDbDto;
@@ -27,6 +28,7 @@ public interface CollectionDao {
2728
List<LinkEntityDto> findLastCreated(int quantity);
2829
long countCollectionsOfUsers();
2930
Integer add(AddCollectionDbDto collection);
31+
void markAsModified(Integer updatedBy, Date updatedAt);
3032
boolean isSeriesInUserCollection(Integer userId, Integer seriesId);
3133
void addSeriesToUserCollection(Integer userId, Integer seriesId);
3234
void removeSeriesFromUserCollection(Integer userId, Integer seriesId);

src/main/java/ru/mystamps/web/dao/dto/AddCollectionDbDto.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
*/
1818
package ru.mystamps.web.dao.dto;
1919

20+
import java.util.Date;
21+
2022
import lombok.Getter;
2123
import lombok.Setter;
2224
import lombok.ToString;
@@ -27,4 +29,5 @@
2729
public class AddCollectionDbDto {
2830
private Integer ownerId;
2931
private String slug;
32+
private Date updatedAt;
3033
}

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package ru.mystamps.web.dao.impl;
1919

2020
import java.util.Collections;
21+
import java.util.Date;
2122
import java.util.HashMap;
2223
import java.util.List;
2324
import java.util.Map;
@@ -56,6 +57,9 @@ public class JdbcCollectionDao implements CollectionDao {
5657

5758
@Value("${collection.create}")
5859
private String addCollectionSql;
60+
61+
@Value("${collection.mark_as_modified}")
62+
private String markAsModifiedSql;
5963

6064
@Value("${collection.is_series_in_collection}")
6165
private String isSeriesInUserCollectionSql;
@@ -92,6 +96,8 @@ public Integer add(AddCollectionDbDto collection) {
9296
Map<String, Object> params = new HashMap<>();
9397
params.put("user_id", collection.getOwnerId());
9498
params.put("slug", collection.getSlug());
99+
params.put("updated_at", collection.getUpdatedAt());
100+
params.put("updated_by", collection.getOwnerId());
95101

96102
KeyHolder holder = new GeneratedKeyHolder();
97103

@@ -110,6 +116,27 @@ public Integer add(AddCollectionDbDto collection) {
110116

111117
return Integer.valueOf(holder.getKey().intValue());
112118
}
119+
120+
/**
121+
* @author John Shkarin
122+
*/
123+
@Override
124+
public void markAsModified(Integer updatedBy, Date updatedAt) {
125+
Map<String, Object> params = new HashMap<>();
126+
params.put("updated_at", updatedAt);
127+
params.put("updated_by", updatedBy);
128+
129+
int affected = jdbcTemplate.update(
130+
markAsModifiedSql,
131+
params
132+
);
133+
134+
Validate.validState(
135+
affected == 1,
136+
"Unexpected number of affected rows after updating collection: %d",
137+
affected
138+
);
139+
}
113140

114141
@Override
115142
public boolean isSeriesInUserCollection(Integer userId, Integer seriesId) {

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
*/
1818
package ru.mystamps.web.service;
1919

20+
import java.util.Date;
2021
import java.util.List;
2122

2223
import org.apache.commons.lang3.StringUtils;
@@ -59,6 +60,7 @@ public void createCollection(Integer ownerId, String ownerLogin) {
5960
"Slug for string '%s' must be non empty", ownerLogin
6061
);
6162
collection.setSlug(slug);
63+
collection.setUpdatedAt(new Date());
6264

6365
Integer id = collectionDao.add(collection);
6466

@@ -73,6 +75,7 @@ public void addToCollection(Integer userId, Integer seriesId) {
7375
Validate.isTrue(seriesId != null, "Series id must be non null");
7476

7577
collectionDao.addSeriesToUserCollection(userId, seriesId);
78+
collectionDao.markAsModified(userId, new Date());
7679

7780
LOG.info("Series #{} has been added to collection of user #{}", seriesId, userId);
7881
}
@@ -85,6 +88,7 @@ public void removeFromCollection(Integer userId, Integer seriesId) {
8588
Validate.isTrue(seriesId != null, "Series id must be non null");
8689

8790
collectionDao.removeSeriesFromUserCollection(userId, seriesId);
91+
collectionDao.markAsModified(userId, new Date());
8892

8993
LOG.info("Series #{} has been removed from collection of user #{}", seriesId, userId);
9094
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,6 @@
2525
<include file="0.4/2016-12-05--make_name_ru_optional.xml" relativeToChangelogFile="true" />
2626
<include file="0.4/2017-01-06--top_categories.xml" relativeToChangelogFile="true" />
2727
<include file="0.4/2017-01-25--united_kingdom_country.xml" relativeToChangelogFile="true" />
28+
<include file="0.4/2017-01-29--add_updater_data_to_collections.xml" relativeToChangelogFile="true" />
2829

2930
</databaseChangeLog>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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.0.xsd">
7+
8+
<changeSet id="add-updater-data-to-collections-table" author="john" context="scheme">
9+
<!-- Since MySQL 5.6.5 it's possible to use `NOW()` as default value for a column
10+
but we're doing the same in 3 steps to support also old versions. -->
11+
<addColumn tableName="collections">
12+
<column name="updated_at" type="DATETIME" />
13+
<column name="updated_by" type="INTEGER" />
14+
</addColumn>
15+
16+
<update tableName="collections">
17+
<column name="updated_at" type="DATETIME" valueComputed="${NOW}" />
18+
</update>
19+
20+
<update tableName="collections">
21+
<column name="updated_by"
22+
type="INTEGER"
23+
valueComputed="(user_id)" />
24+
</update>
25+
26+
<addNotNullConstraint tableName="collections" columnName="updated_at" columnDataType="DATETIME" />
27+
28+
<addNotNullConstraint tableName="collections" columnName="updated_by" columnDataType="INTEGER" />
29+
30+
<addForeignKeyConstraint
31+
baseTableName="collections"
32+
baseColumnNames="updated_by"
33+
constraintName="fk_collections_users_id"
34+
referencedTableName="users"
35+
referencedColumnNames="id" />
36+
37+
</changeSet>
38+
39+
</databaseChangeLog>

src/main/resources/sql/collection_dao_queries.properties

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,22 @@ INSERT \
2424
INTO collections \
2525
( user_id \
2626
, slug \
27+
, updated_at \
28+
, updated_by \
2729
) \
2830
VALUES \
2931
( :user_id \
3032
, :slug \
33+
, :updated_at \
34+
, :updated_by \
3135
)
3236

37+
collection.mark_as_modified = \
38+
UPDATE collections c \
39+
SET c.updated_at = :updated_at \
40+
, c.updated_by = :updated_by \
41+
WHERE c.user_id = :updated_by
42+
3343
collection.is_series_in_collection = \
3444
SELECT COUNT(*) \
3545
FROM collections c \

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

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
*/
1818
package ru.mystamps.web.service
1919

20+
import ru.mystamps.web.tests.DateUtils
2021
import spock.lang.Specification
2122
import spock.lang.Unroll
2223

@@ -111,7 +112,7 @@ class CollectionServiceImplTest extends Specification {
111112
}
112113

113114
@SuppressWarnings(['ClosureAsLastMethodParameter', 'UnnecessaryReturnKeyword'])
114-
def "addToCollection() should pass arguments to dao"() {
115+
def "addToCollection() should add series to collection and mark it as modified"() {
115116
given:
116117
Integer expectedUserId = 123
117118
Integer expectedSeriesId = 456
@@ -125,6 +126,14 @@ class CollectionServiceImplTest extends Specification {
125126
assert seriesId == expectedSeriesId
126127
return true
127128
})
129+
and:
130+
1 * collectionDao.markAsModified({ Integer updatedBy ->
131+
assert updatedBy == expectedUserId
132+
return true
133+
}, { Date updatedAt ->
134+
assert DateUtils.roughlyEqual(updatedAt, new Date())
135+
return true
136+
})
128137
}
129138

130139
//
@@ -146,7 +155,7 @@ class CollectionServiceImplTest extends Specification {
146155
}
147156

148157
@SuppressWarnings(['ClosureAsLastMethodParameter', 'UnnecessaryReturnKeyword'])
149-
def "removeFromCollection() should remove series from collection"() {
158+
def "removeFromCollection() should remove series from collection and mark it as modified"() {
150159
given:
151160
Integer expectedUserId = 123
152161
Integer expectedSeriesId = 456
@@ -160,6 +169,14 @@ class CollectionServiceImplTest extends Specification {
160169
assert seriesId == expectedSeriesId
161170
return true
162171
})
172+
and:
173+
1 * collectionDao.markAsModified({ Integer updatedBy ->
174+
assert updatedBy == expectedUserId
175+
return true
176+
}, { Date updatedAt ->
177+
assert DateUtils.roughlyEqual(updatedAt, new Date())
178+
return true
179+
})
163180
}
164181

165182
//

0 commit comments

Comments
 (0)