Skip to content

Commit 82d0e4c

Browse files
author
Ferenc Hammerl
committed
Add endpoint to search by title
Tested in postman.
1 parent f848470 commit 82d0e4c

File tree

4 files changed

+34
-0
lines changed

4 files changed

+34
-0
lines changed

src/main/java/com/fhammerl/polls/controllers/PollsController.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import com.fhammerl.polls.repositories.PollRepository;
99
import com.fhammerl.polls.models.Poll;
10+
import com.fhammerl.polls.models.SearchQuery;
1011

1112
@RestController
1213
@RequestMapping("api/v1/polls")
@@ -28,4 +29,12 @@ public List<Poll> list() {
2829
public List<Poll> createdBy(int userId) {
2930
return pollRepository.findByCreatorId(userId);
3031
}
32+
33+
// Test: http://localhost:8080/api/v1/polls/search
34+
// TODO: review post vs get for search. Sticking to post for now because of url encoding
35+
@PostMapping
36+
@RequestMapping("search") // TODO: review ambiguous naming
37+
public List<Poll> search(@RequestBody SearchQuery query) {
38+
return pollRepository.findByTitle(query.getTitle());
39+
}
3140
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.fhammerl.polls.models;
2+
3+
public class SearchQuery {
4+
private String title;
5+
6+
public String getTitle() {
7+
return this.title;
8+
}
9+
10+
public void setTitle(String title) {
11+
this.title = title;
12+
}
13+
}

src/main/java/com/fhammerl/polls/repositories/PollRepository.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,7 @@
1111
public interface PollRepository extends JpaRepository<Poll, Integer> {
1212
@Query("SELECT p FROM poll p where p.createdByUserId = :id")
1313
List<Poll> findByCreatorId(@Param("id") Integer userId);
14+
15+
@Query("SELECT p FROM poll p where p.title = :title")
16+
List<Poll> findByTitle(@Param("title") String title);
1417
}

src/test/java/com/fhammerl/polls/PollsControllerTests.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.util.List;
66

77
import com.fhammerl.polls.models.Poll;
8+
import com.fhammerl.polls.models.SearchQuery;
89

910
import org.junit.jupiter.api.Test;
1011
import org.springframework.beans.factory.annotation.Autowired;
@@ -35,4 +36,12 @@ void listCreatedByUserWorks() throws Exception {
3536
List<Poll> polls = pollsController.createdBy(4);
3637
assertEquals(polls.toArray().length, 2, "Unexpected number of polls found for user 4!");
3738
}
39+
40+
@Test
41+
void getByTitle() throws Exception {
42+
SearchQuery query = new SearchQuery();
43+
query.setTitle("谁是最坏蛋奇迹超级英雄?");
44+
List<Poll> polls = pollsController.search(query);
45+
assertEquals(polls.toArray().length, 1, "Unexpected number of polls found for title '谁是最坏蛋奇迹超级英雄?'!");
46+
}
3847
}

0 commit comments

Comments
 (0)