Skip to content

Commit 4f35f71

Browse files
committed
don't use pager when listing posts
1 parent 4dbf664 commit 4f35f71

File tree

8 files changed

+74
-25
lines changed

8 files changed

+74
-25
lines changed

src/main/java/com/myapp/controller/FeedController.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
@RequestMapping("/api/feed")
1717
public class FeedController {
1818

19+
private static final int DEFAULT_PAGE_SIZE = 20;
20+
1921
private final MicropostRepository micropostRepository;
2022
private final SecurityContextService securityContextService;
2123

@@ -26,12 +28,12 @@ public FeedController(MicropostRepository micropostRepository, SecurityContextSe
2628
}
2729

2830
@RequestMapping
29-
public List<Micropost> feed(@RequestParam("since_id") Optional<Long> sinceId,
30-
@RequestParam("max_id") Optional<Long> maxId,
31+
public List<Micropost> feed(@RequestParam("sinceId") Optional<Long> sinceId,
32+
@RequestParam("maxId") Optional<Long> maxId,
3133
@RequestParam("count") Optional<Integer> count) {
3234
User currentUser = securityContextService.currentUser();
3335
return micropostRepository
34-
.findAsFeed(currentUser, sinceId, maxId, count.orElse(20));
36+
.findAsFeed(currentUser, sinceId, maxId, count.orElse(DEFAULT_PAGE_SIZE));
3537
}
3638

3739
}
Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,23 @@
11
package com.myapp.controller;
22

3+
import com.myapp.domain.Micropost;
34
import com.myapp.domain.User;
45
import com.myapp.repository.MicropostRepository;
56
import com.myapp.repository.UserRepository;
6-
import com.myapp.domain.Micropost;
77
import org.springframework.beans.factory.annotation.Autowired;
8-
import org.springframework.data.domain.Page;
9-
import org.springframework.data.domain.PageRequest;
10-
import org.springframework.data.domain.Pageable;
11-
import org.springframework.data.domain.Sort;
128
import org.springframework.web.bind.annotation.PathVariable;
139
import org.springframework.web.bind.annotation.RequestMapping;
1410
import org.springframework.web.bind.annotation.RequestParam;
1511
import org.springframework.web.bind.annotation.RestController;
1612

13+
import java.util.List;
1714
import java.util.Optional;
1815

1916
@RestController
2017
@RequestMapping("/api/users/{userId}/microposts")
2118
public class UserMicropostController {
2219

23-
private static final Integer DEFAULT_PAGE_SIZE = 5;
20+
private static final int DEFAULT_PAGE_SIZE = 20;
2421

2522
private final UserRepository userRepository;
2623
private final MicropostRepository micropostRepository;
@@ -32,13 +29,12 @@ public UserMicropostController(UserRepository userRepository, MicropostRepositor
3229
}
3330

3431
@RequestMapping
35-
public Page<Micropost> list(@PathVariable("userId") Long userId,
36-
@RequestParam(value = "page") Optional<Integer> page,
37-
@RequestParam(value = "size") Optional<Integer> size) {
32+
public List<Micropost> list(@PathVariable("userId") Long userId,
33+
@RequestParam("sinceId") Optional<Long> sinceId,
34+
@RequestParam("maxId") Optional<Long> maxId,
35+
@RequestParam("count") Optional<Integer> count) {
3836
User user = userRepository.findOne(userId);
39-
Pageable pageRequest = new PageRequest(page.orElse(1) - 1,
40-
size.orElse(DEFAULT_PAGE_SIZE), Sort.Direction.DESC, "createdAt");
41-
42-
return micropostRepository.findByUser(user, pageRequest);
37+
return micropostRepository
38+
.findByUser(user, sinceId, maxId, count.orElse(DEFAULT_PAGE_SIZE));
4339
}
4440
}

src/main/java/com/myapp/dto/UserParams.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import com.fasterxml.jackson.annotation.JsonProperty;
44
import com.myapp.domain.User;
55
import lombok.Value;
6+
import org.slf4j.Logger;
7+
import org.slf4j.LoggerFactory;
68
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
79

810
import javax.validation.constraints.Size;
@@ -11,6 +13,8 @@
1113
@Value
1214
public class UserParams {
1315

16+
private static final Logger logger = LoggerFactory.getLogger(UserParams.class);
17+
1418
private String email;
1519
@Size(min = 8, max = 100)
1620
private String password;

src/main/java/com/myapp/repository/MicropostRepository.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,4 @@ public interface MicropostRepository extends JpaRepository<Micropost, Long>, Mic
1010

1111
Integer countByUser(User user);
1212

13-
Page<Micropost> findByUser(User user, Pageable pageRequest);
14-
1513
}

src/main/java/com/myapp/repository/MicropostRepositoryCustom.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,8 @@ List<Micropost> findAsFeed(User user,
1313
Optional<Long> maxId,
1414
Integer maxSize);
1515

16+
List<Micropost> findByUser(User user,
17+
Optional<Long> sinceId,
18+
Optional<Long> maxId,
19+
Integer maxSize);
1620
}

src/main/java/com/myapp/repository/MicropostRepositoryImpl.java

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
@SuppressWarnings("unused")
1717
public class MicropostRepositoryImpl implements MicropostRepositoryCustom {
1818

19+
@SuppressWarnings("SpringJavaAutowiredMembersInspection")
1920
@Autowired
2021
EntityManager entityManager;
2122

@@ -43,9 +44,32 @@ public List<Micropost> findAsFeed(User user,
4344
cb.equal(root.get("user"), user),
4445
cb.exists(relationshipSubquery)
4546
),
46-
sinceId.map(id -> cb.and(cb.greaterThan(root.get("id"), id)))
47+
sinceId.map(id -> cb.greaterThan(root.get("id"), id))
4748
.orElse(cb.conjunction()),
48-
maxId.map(id -> cb.and(cb.lessThan(root.get("id"), id)))
49+
maxId.map(id -> cb.lessThan(root.get("id"), id))
50+
.orElse(cb.conjunction())
51+
);
52+
query.orderBy(cb.desc(root.get("id")));
53+
54+
return entityManager
55+
.createQuery(query)
56+
.setMaxResults(Optional.ofNullable(maxSize).orElse(20))
57+
.getResultList();
58+
}
59+
60+
@Override
61+
public List<Micropost> findByUser(User user,
62+
Optional<Long> sinceId,
63+
Optional<Long> maxId,
64+
Integer maxSize) {
65+
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
66+
CriteriaQuery<Micropost> query = cb.createQuery(Micropost.class);
67+
Root<Micropost> root = query.from(Micropost.class);
68+
query.where(
69+
cb.equal(root.get("user"), user),
70+
sinceId.map(id -> cb.greaterThan(root.get("id"), id))
71+
.orElse(cb.conjunction()),
72+
maxId.map(id -> cb.lessThan(root.get("id"), id))
4973
.orElse(cb.conjunction())
5074
);
5175
query.orderBy(cb.desc(root.get("id")));

src/test/groovy/com/myapp/controller/UserMicropostControllerTest.groovy

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import com.myapp.repository.RepositoryTestConfig
88
import com.myapp.repository.UserRepository
99
import org.springframework.beans.factory.annotation.Autowired
1010
import org.springframework.test.context.ContextConfiguration
11-
import org.springframework.test.web.servlet.result.MockMvcResultHandlers
1211

1312
import static org.hamcrest.Matchers.hasSize
1413
import static org.hamcrest.Matchers.is
@@ -40,10 +39,8 @@ class UserMicropostControllerTest extends BaseControllerTest {
4039

4140
then:
4241
response.andExpect(status().isOk())
43-
.andExpect(jsonPath('$.content').exists())
44-
.andExpect(jsonPath('$.content', hasSize(1)))
45-
.andExpect(jsonPath('$.content[0].content', is("my content")))
46-
.andDo(MockMvcResultHandlers.print())
42+
.andExpect(jsonPath('$', hasSize(1)))
43+
.andExpect(jsonPath('$[0].content', is("my content")))
4744
}
4845

4946
}

src/test/groovy/com/myapp/repository/MicropostRepositoryTest.groovy

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,28 @@ class MicropostRepositoryTest extends Specification {
6969
result.first() == post1
7070
}
7171

72+
def "can find posts by user"() {
73+
given:
74+
User user = userRepository.save(new User(username: "akira@test.com", password: "secret", name: "akira"))
75+
Micropost post1 = micropostRepository.save(new Micropost(content: "test1", user: user))
76+
Micropost post2 = micropostRepository.save(new Micropost(content: "test2", user: user))
77+
Micropost post3 = micropostRepository.save(new Micropost(content: "test3", user: user))
78+
79+
when:
80+
List<Micropost> result = micropostRepository
81+
.findByUser(user, Optional.of(post2.id), Optional.empty(), null)
82+
83+
then:
84+
result.size() == 1
85+
result.first() == post3
86+
87+
when:
88+
result = micropostRepository
89+
.findByUser(user, Optional.empty(), Optional.of(post2.id), null)
90+
91+
then:
92+
result.size() == 1
93+
result.first() == post1
94+
}
95+
7296
}

0 commit comments

Comments
 (0)