Skip to content

Commit da0a6bc

Browse files
author
Ferenc Hammerl
committed
Add createdBy endpoint to pollsController
1 parent ce3f1ce commit da0a6bc

File tree

4 files changed

+33
-2
lines changed

4 files changed

+33
-2
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
@@ -14,9 +14,18 @@ public class PollsController {
1414
@Autowired
1515
private PollRepository pollRepository;
1616

17+
// Test: http://localhost:8080/api/v1/polls/list
1718
@GetMapping
1819
@RequestMapping("list")
1920
public List<Poll> list() {
2021
return pollRepository.findAll();
2122
}
23+
24+
25+
// Test: http://localhost:8080/api/v1/polls/createdBy?userId=4
26+
@GetMapping
27+
@RequestMapping("createdBy")
28+
public List<Poll> createdBy(int userId) {
29+
return pollRepository.findByCreatorId(userId);
30+
}
2231
}

src/main/java/com/fhammerl/polls/models/Poll.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ public class Poll {
2222
private String preferencesType;
2323
private String state;
2424
private String title;
25+
@Column(name = "created_by_user_id")
26+
private int createdByUserId;
2527

2628
@OneToMany(mappedBy = "poll", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY)
2729
private List<Participant> participants;
@@ -89,5 +91,12 @@ public List<Participant> getParticipants() {
8991
public void setParticipants(List<Participant> participant) {
9092
this.participants = participant;
9193
}
94+
public int getCreatedByUserId() {
95+
return this.createdByUserId;
96+
}
97+
98+
public void setCreatedByUserId(int createdByUserId) {
99+
this.createdByUserId = createdByUserId;
100+
}
92101

93102
}
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
package com.fhammerl.polls.repositories;
22

33
import org.springframework.data.jpa.repository.JpaRepository;
4+
import org.springframework.data.jpa.repository.Query;
5+
import org.springframework.data.repository.query.Param;
6+
7+
import java.util.List;
8+
49
import com.fhammerl.polls.models.Poll;
510

6-
public interface PollRepository extends JpaRepository<Poll, Long> {
11+
public interface PollRepository extends JpaRepository<Poll, Integer> {
12+
@Query("SELECT p FROM poll p where p.createdByUserId = :id")
13+
List<Poll> findByCreatorId(@Param("id") Integer userId);
714
}

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ class PollsControllerTests {
2727
@Test
2828
void listAllWorks() throws Exception {
2929
List<Poll> polls = pollsController.list();
30-
assertEquals(polls.toArray().length, 2, "Unexpected number of polls found!");
30+
assertEquals(polls.toArray().length, 3, "Unexpected number of polls found!");
31+
}
32+
33+
@Test
34+
void listCreatedByUserWorks() throws Exception {
35+
List<Poll> polls = pollsController.createdBy(4);
36+
assertEquals(polls.toArray().length, 2, "Unexpected number of polls found for user 4!");
3137
}
3238
}

0 commit comments

Comments
 (0)