Skip to content

Commit 84bbed6

Browse files
committed
CollectionService.addToCollection(): don't return UrlEntityDto.
Addressed to #440 No functional changes.
1 parent 5cc4ba8 commit 84bbed6

File tree

7 files changed

+25
-55
lines changed

7 files changed

+25
-55
lines changed

src/main/java/ru/mystamps/web/controller/SeriesController.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@
4848
import org.springframework.web.bind.annotation.RequestParam;
4949
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
5050

51+
import org.springframework.security.core.annotation.AuthenticationPrincipal;
52+
5153
import lombok.RequiredArgsConstructor;
5254

5355
import ru.mystamps.web.Url;
@@ -66,6 +68,7 @@
6668
import ru.mystamps.web.service.SeriesService;
6769
import ru.mystamps.web.service.dto.SeriesDto;
6870
import ru.mystamps.web.support.spring.security.Authority;
71+
import ru.mystamps.web.support.spring.security.CustomUserDetails;
6972
import ru.mystamps.web.support.spring.security.SecurityContextUtils;
7073
import ru.mystamps.web.util.CatalogUtils;
7174
import ru.mystamps.web.util.LocaleUtils;
@@ -260,7 +263,7 @@ public String processImage(
260263
)
261264
public String addToCollection(
262265
@PathVariable("id") Integer seriesId,
263-
@CurrentUser Integer currentUserId,
266+
@AuthenticationPrincipal CustomUserDetails currentUserDetails,
264267
RedirectAttributes redirectAttributes,
265268
HttpServletResponse response)
266269
throws IOException {
@@ -276,12 +279,14 @@ public String addToCollection(
276279
return null;
277280
}
278281

279-
UrlEntityDto collection = collectionService.addToCollection(currentUserId, seriesId);
282+
Integer userId = currentUserDetails.getUserId();
283+
collectionService.addToCollection(userId, seriesId);
280284

281285
redirectAttributes.addFlashAttribute("justAddedSeries", true);
282286
redirectAttributes.addFlashAttribute("justAddedSeriesId", seriesId);
283-
284-
return redirectTo(Url.INFO_COLLECTION_PAGE, collection.getSlug());
287+
288+
String collectionSlug = currentUserDetails.getUserCollectionSlug();
289+
return redirectTo(Url.INFO_COLLECTION_PAGE, collectionSlug);
285290
}
286291

287292
@RequestMapping(

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public interface CollectionDao {
3030
Integer add(AddCollectionDbDto collection);
3131
boolean isSeriesInUserCollection(Integer userId, Integer seriesId);
3232
UrlEntityDto findCollectionUrlEntityByUserId(Integer userId);
33-
void addSeriesToCollection(String collectionSlug, Integer seriesId);
33+
void addSeriesToUserCollection(Integer userId, Integer seriesId);
3434
void removeSeriesFromCollection(Integer collectionId, Integer seriesId);
3535
CollectionInfoDto findCollectionInfoBySlug(String slug);
3636
}

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,17 +138,19 @@ public UrlEntityDto findCollectionUrlEntityByUserId(Integer userId) {
138138
}
139139

140140
@Override
141-
public void addSeriesToCollection(String collectionSlug, Integer seriesId) {
141+
public void addSeriesToUserCollection(Integer userId, Integer seriesId) {
142142
Map<String, Object> params = new HashMap<>();
143-
params.put("collection_slug", collectionSlug);
143+
params.put("user_id", userId);
144144
params.put("series_id", seriesId);
145145

146146
int affected = jdbcTemplate.update(addSeriesToCollectionSql, params);
147+
148+
// CheckStyle: ignore LineLength for next 3 lines
147149
Validate.validState(
148150
affected == 1,
149-
"Unexpected number of affected rows after adding series #%d to collection '%s': %d",
151+
"Unexpected number of affected rows after adding series #%d to collection of user #%d: %d",
150152
seriesId,
151-
collectionSlug,
153+
userId,
152154
affected
153155
);
154156
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
public interface CollectionService {
2727
void createCollection(Integer ownerId, String ownerLogin);
28-
UrlEntityDto addToCollection(Integer userId, Integer seriesId);
28+
void addToCollection(Integer userId, Integer seriesId);
2929
UrlEntityDto removeFromCollection(Integer userId, Integer seriesId);
3030
boolean isSeriesInCollection(Integer userId, Integer seriesId);
3131
long countCollectionsOfUsers();

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

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -69,22 +69,13 @@ public void createCollection(Integer ownerId, String ownerLogin) {
6969
@Override
7070
@Transactional
7171
@PreAuthorize(HasAuthority.UPDATE_COLLECTION)
72-
public UrlEntityDto addToCollection(Integer userId, Integer seriesId) {
72+
public void addToCollection(Integer userId, Integer seriesId) {
7373
Validate.isTrue(userId != null, "User id must be non null");
7474
Validate.isTrue(seriesId != null, "Series id must be non null");
7575

76-
UrlEntityDto url = collectionDao.findCollectionUrlEntityByUserId(userId);
77-
String collectionSlug = url.getSlug();
78-
79-
collectionDao.addSeriesToCollection(collectionSlug, seriesId);
80-
81-
LOG.info(
82-
"Series #{} has been added to collection of user #{}",
83-
seriesId,
84-
userId
85-
);
76+
collectionDao.addSeriesToUserCollection(userId, seriesId);
8677

87-
return url;
78+
LOG.info("Series #{} has been added to collection of user #{}", seriesId, userId);
8879
}
8980

9081
@Override

src/main/resources/sql/collection_dao_queries.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ INSERT \
4949
SELECT c.id AS collection_id \
5050
, :series_id AS series_id \
5151
FROM collections c \
52-
WHERE c.slug = :collection_slug
52+
WHERE c.user_id = :user_id
5353

5454
collection.remove_series_from_collection = \
5555
DELETE \

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

Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -104,50 +104,22 @@ class CollectionServiceImplTest extends Specification {
104104
thrown IllegalArgumentException
105105
}
106106

107-
def "addToCollection() should find collection by user id"() {
107+
def "addToCollection() should pass arguments to dao"() {
108108
given:
109109
Integer expectedUserId = 123
110-
when:
111-
service.addToCollection(expectedUserId, 321)
112-
then:
113-
1 * collectionDao.findCollectionUrlEntityByUserId({ Integer userId ->
114-
assert userId == expectedUserId
115-
return true
116-
}) >> TestObjects.createUrlEntityDto()
117-
}
118-
119-
def "addToCollection() should add series to collection"() {
120-
given:
121110
Integer expectedSeriesId = 456
122-
and:
123-
UrlEntityDto url = TestObjects.createUrlEntityDto()
124-
and:
125-
String expectedCollectionSlug = url.getSlug()
126-
and:
127-
collectionDao.findCollectionUrlEntityByUserId(_ as Integer) >> url
128111
when:
129-
service.addToCollection(123, expectedSeriesId)
112+
service.addToCollection(expectedUserId, expectedSeriesId)
130113
then:
131-
1 * collectionDao.addSeriesToCollection({ String collectionSlug ->
132-
assert collectionSlug == expectedCollectionSlug
114+
1 * collectionDao.addSeriesToUserCollection({ Integer userId ->
115+
assert userId == expectedUserId
133116
return true
134117
}, { Integer seriesId ->
135118
assert seriesId == expectedSeriesId
136119
return true
137120
})
138121
}
139122

140-
def "addToCollection() should return result from dao"() {
141-
given:
142-
UrlEntityDto expectedUrl = TestObjects.createUrlEntityDto()
143-
and:
144-
collectionDao.findCollectionUrlEntityByUserId(_ as Integer) >> expectedUrl
145-
when:
146-
UrlEntityDto serviceResult = service.addToCollection(123, 456)
147-
then:
148-
serviceResult == expectedUrl
149-
}
150-
151123
//
152124
// Tests for removeFromCollection()
153125
//

0 commit comments

Comments
 (0)