Skip to content

Commit 5af2012

Browse files
Christian WansartChristian Wansart
authored andcommitted
fix participants count, fix NPE, add schema.sql
1 parent 700fc13 commit 5af2012

File tree

8 files changed

+309
-22
lines changed

8 files changed

+309
-22
lines changed

build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ version = '1.0.2'
99

1010
java {
1111
toolchain {
12-
languageVersion = JavaLanguageVersion.of(21)
12+
languageVersion = JavaLanguageVersion.of(17)
1313
}
1414
}
1515

@@ -23,8 +23,8 @@ dependencies {
2323
implementation 'org.springframework.boot:spring-boot-starter-web'
2424
implementation 'org.springframework.boot:spring-boot-starter-security'
2525
developmentOnly 'org.springframework.boot:spring-boot-devtools'
26-
//runtimeOnly 'com.h2database:h2'
27-
runtimeOnly 'org.postgresql:postgresql'
26+
runtimeOnly 'com.h2database:h2'
27+
//runtimeOnly 'org.postgresql:postgresql'
2828
testImplementation 'org.springframework.boot:spring-boot-starter-test'
2929
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
3030
}

schema.sql

Lines changed: 263 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,263 @@
1+
--
2+
-- PostgreSQL database dump
3+
--
4+
5+
-- Dumped from database version 15.7 (Debian 15.7-0+deb12u1)
6+
-- Dumped by pg_dump version 15.7 (Debian 15.7-0+deb12u1)
7+
8+
SET statement_timeout = 0;
9+
SET lock_timeout = 0;
10+
SET idle_in_transaction_session_timeout = 0;
11+
SET client_encoding = 'UTF8';
12+
SET standard_conforming_strings = on;
13+
SELECT pg_catalog.set_config('search_path', '', false);
14+
SET check_function_bodies = false;
15+
SET xmloption = content;
16+
SET client_min_messages = warning;
17+
SET row_security = off;
18+
19+
SET default_tablespace = '';
20+
21+
SET default_table_access_method = heap;
22+
23+
--
24+
-- Name: choice; Type: TABLE; Schema: public; Owner: unipoll
25+
--
26+
27+
CREATE TABLE public.choice (
28+
id bigint NOT NULL,
29+
name character varying(255)
30+
);
31+
32+
33+
ALTER TABLE public.choice OWNER TO unipoll;
34+
35+
--
36+
-- Name: choice_seq; Type: SEQUENCE; Schema: public; Owner: unipoll
37+
--
38+
39+
CREATE SEQUENCE public.choice_seq
40+
START WITH 1
41+
INCREMENT BY 50
42+
NO MINVALUE
43+
NO MAXVALUE
44+
CACHE 1;
45+
46+
47+
ALTER TABLE public.choice_seq OWNER TO unipoll;
48+
49+
--
50+
-- Name: poll; Type: TABLE; Schema: public; Owner: unipoll
51+
--
52+
53+
CREATE TABLE public.poll (
54+
deleted boolean NOT NULL,
55+
id bigint NOT NULL,
56+
name character varying(255) NOT NULL
57+
);
58+
59+
60+
ALTER TABLE public.poll OWNER TO unipoll;
61+
62+
--
63+
-- Name: poll_choices; Type: TABLE; Schema: public; Owner: unipoll
64+
--
65+
66+
CREATE TABLE public.poll_choices (
67+
choices_id bigint NOT NULL,
68+
poll_id bigint NOT NULL
69+
);
70+
71+
72+
ALTER TABLE public.poll_choices OWNER TO unipoll;
73+
74+
--
75+
-- Name: poll_seq; Type: SEQUENCE; Schema: public; Owner: unipoll
76+
--
77+
78+
CREATE SEQUENCE public.poll_seq
79+
START WITH 1
80+
INCREMENT BY 50
81+
NO MINVALUE
82+
NO MAXVALUE
83+
CACHE 1;
84+
85+
86+
ALTER TABLE public.poll_seq OWNER TO unipoll;
87+
88+
--
89+
-- Name: vote; Type: TABLE; Schema: public; Owner: unipoll
90+
--
91+
92+
CREATE TABLE public.vote (
93+
id bigint NOT NULL,
94+
poll_id bigint,
95+
user_id character varying(255)
96+
);
97+
98+
99+
ALTER TABLE public.vote OWNER TO unipoll;
100+
101+
--
102+
-- Name: vote_choices; Type: TABLE; Schema: public; Owner: unipoll
103+
--
104+
105+
CREATE TABLE public.vote_choices (
106+
choices_id bigint NOT NULL,
107+
vote_id bigint NOT NULL
108+
);
109+
110+
111+
ALTER TABLE public.vote_choices OWNER TO unipoll;
112+
113+
--
114+
-- Name: vote_seq; Type: SEQUENCE; Schema: public; Owner: unipoll
115+
--
116+
117+
CREATE SEQUENCE public.vote_seq
118+
START WITH 1
119+
INCREMENT BY 50
120+
NO MINVALUE
121+
NO MAXVALUE
122+
CACHE 1;
123+
124+
125+
ALTER TABLE public.vote_seq OWNER TO unipoll;
126+
127+
--
128+
-- Data for Name: choice; Type: TABLE DATA; Schema: public; Owner: unipoll
129+
--
130+
131+
COPY public.choice (id, name) FROM stdin;
132+
\.
133+
134+
135+
--
136+
-- Data for Name: poll; Type: TABLE DATA; Schema: public; Owner: unipoll
137+
--
138+
139+
COPY public.poll (deleted, id, name) FROM stdin;
140+
\.
141+
142+
143+
--
144+
-- Data for Name: poll_choices; Type: TABLE DATA; Schema: public; Owner: unipoll
145+
--
146+
147+
COPY public.poll_choices (choices_id, poll_id) FROM stdin;
148+
\.
149+
150+
151+
--
152+
-- Data for Name: vote; Type: TABLE DATA; Schema: public; Owner: unipoll
153+
--
154+
155+
COPY public.vote (id, poll_id, user_id) FROM stdin;
156+
\.
157+
158+
159+
--
160+
-- Data for Name: vote_choices; Type: TABLE DATA; Schema: public; Owner: unipoll
161+
--
162+
163+
COPY public.vote_choices (choices_id, vote_id) FROM stdin;
164+
\.
165+
166+
167+
--
168+
-- Name: choice_seq; Type: SEQUENCE SET; Schema: public; Owner: unipoll
169+
--
170+
171+
SELECT pg_catalog.setval('public.choice_seq', 1, false);
172+
173+
174+
--
175+
-- Name: poll_seq; Type: SEQUENCE SET; Schema: public; Owner: unipoll
176+
--
177+
178+
SELECT pg_catalog.setval('public.poll_seq', 1, false);
179+
180+
181+
--
182+
-- Name: vote_seq; Type: SEQUENCE SET; Schema: public; Owner: unipoll
183+
--
184+
185+
SELECT pg_catalog.setval('public.vote_seq', 1, false);
186+
187+
188+
--
189+
-- Name: choice choice_pkey; Type: CONSTRAINT; Schema: public; Owner: unipoll
190+
--
191+
192+
ALTER TABLE ONLY public.choice
193+
ADD CONSTRAINT choice_pkey PRIMARY KEY (id);
194+
195+
196+
--
197+
-- Name: poll_choices poll_choices_choices_id_key; Type: CONSTRAINT; Schema: public; Owner: unipoll
198+
--
199+
200+
ALTER TABLE ONLY public.poll_choices
201+
ADD CONSTRAINT poll_choices_choices_id_key UNIQUE (choices_id);
202+
203+
204+
--
205+
-- Name: poll poll_pkey; Type: CONSTRAINT; Schema: public; Owner: unipoll
206+
--
207+
208+
ALTER TABLE ONLY public.poll
209+
ADD CONSTRAINT poll_pkey PRIMARY KEY (id);
210+
211+
212+
--
213+
-- Name: vote vote_pkey; Type: CONSTRAINT; Schema: public; Owner: unipoll
214+
--
215+
216+
ALTER TABLE ONLY public.vote
217+
ADD CONSTRAINT vote_pkey PRIMARY KEY (id);
218+
219+
220+
--
221+
-- Name: poll_choices fk2s3m71jjvf3jfpepl4pqei6lb; Type: FK CONSTRAINT; Schema: public; Owner: unipoll
222+
--
223+
224+
ALTER TABLE ONLY public.poll_choices
225+
ADD CONSTRAINT fk2s3m71jjvf3jfpepl4pqei6lb FOREIGN KEY (choices_id) REFERENCES public.choice(id);
226+
227+
228+
--
229+
-- Name: vote_choices fkajw4c0op1loej0mabw29d2h69; Type: FK CONSTRAINT; Schema: public; Owner: unipoll
230+
--
231+
232+
ALTER TABLE ONLY public.vote_choices
233+
ADD CONSTRAINT fkajw4c0op1loej0mabw29d2h69 FOREIGN KEY (choices_id) REFERENCES public.choice(id);
234+
235+
236+
--
237+
-- Name: vote fkc2jlu007tq2iwcwkmpdfxjh26; Type: FK CONSTRAINT; Schema: public; Owner: unipoll
238+
--
239+
240+
ALTER TABLE ONLY public.vote
241+
ADD CONSTRAINT fkc2jlu007tq2iwcwkmpdfxjh26 FOREIGN KEY (poll_id) REFERENCES public.poll(id);
242+
243+
244+
--
245+
-- Name: vote_choices fksrtqgn4tdo63x6erlush6biux; Type: FK CONSTRAINT; Schema: public; Owner: unipoll
246+
--
247+
248+
ALTER TABLE ONLY public.vote_choices
249+
ADD CONSTRAINT fksrtqgn4tdo63x6erlush6biux FOREIGN KEY (vote_id) REFERENCES public.vote(id);
250+
251+
252+
--
253+
-- Name: poll_choices fktmiorly5frpkb1ysrc660jq2n; Type: FK CONSTRAINT; Schema: public; Owner: unipoll
254+
--
255+
256+
ALTER TABLE ONLY public.poll_choices
257+
ADD CONSTRAINT fktmiorly5frpkb1ysrc660jq2n FOREIGN KEY (poll_id) REFERENCES public.poll(id);
258+
259+
260+
--
261+
-- PostgreSQL database dump complete
262+
--
263+

src/main/java/de/cwansart/unipoll/controller/ListController.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,17 @@
1111

1212
import de.cwansart.unipoll.entity.Poll;
1313
import de.cwansart.unipoll.repository.PollRepository;
14+
import de.cwansart.unipoll.repository.VoteRepository;
1415
import de.cwansart.unipoll.service.AuthService;
1516

1617
@Controller
1718
public class ListController {
1819
@Autowired
1920
private PollRepository pollRepo;
2021

22+
@Autowired
23+
private VoteRepository voteRepo;
24+
2125
@Autowired
2226
private AuthService auth;
2327

@@ -28,6 +32,7 @@ public String showAll(@RequestParam(name = "p", defaultValue = "0") int page, Mo
2832
}
2933

3034
Page<Poll> polls = pollRepo.findAll(PageRequest.of(page, 20, Sort.by(Sort.Direction.ASC, "id")));
35+
polls.getContent().forEach(p -> p.setParticipants(voteRepo.countByPollId(p.getId())));
3136
model.addAttribute("polls", polls.getContent());
3237
model.addAttribute("totalNum", polls.getTotalElements());
3338
model.addAttribute("totalPages", polls.getTotalPages());

src/main/java/de/cwansart/unipoll/controller/VoteController.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ public String show(
4545
throw new ResponseStatusException(HttpStatusCode.valueOf(404), "poll does not exist");
4646
}
4747

48-
Optional<Cookie> userIdCookie = Arrays.asList(request.getCookies()).stream().filter(c -> c.getName().equals("unipoll-user-id")).findFirst();
48+
Optional<Cookie> userIdCookie = request.getCookies() != null ?
49+
Arrays.asList(request.getCookies()).stream().filter(c -> c.getName().equals("unipoll-user-id")).findFirst() :
50+
Optional.empty();
4951
// check if user already voted
5052
if (userIdCookie.isPresent() && voteRepo.findByPollIdAndUserId(id, userIdCookie.get().getValue()).isPresent()) {
5153
return "redirect:/results?id=" + id + "&v=1";

src/main/java/de/cwansart/unipoll/entity/Poll.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import jakarta.persistence.GenerationType;
1010
import jakarta.persistence.Id;
1111
import jakarta.persistence.OneToMany;
12+
import jakarta.persistence.Transient;
1213

1314
@Entity
1415
public class Poll {
@@ -21,6 +22,9 @@ public class Poll {
2122
@OneToMany(cascade = CascadeType.ALL)
2223
private List<Choice> choices;
2324

25+
@Transient
26+
private long participants;
27+
2428
private boolean deleted;
2529

2630
public Long getId() {
@@ -50,4 +54,11 @@ public boolean isDeleted() {
5054
public void setDeleted(boolean deleted) {
5155
this.deleted = deleted;
5256
}
57+
58+
public void setParticipants(long participants) {
59+
this.participants = participants;
60+
}
61+
public long getParticipants() {
62+
return participants;
63+
}
5364
}

src/main/java/de/cwansart/unipoll/repository/VoteRepository.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@ public interface VoteRepository extends Repository<Vote, Long> {
1212
Optional<Vote> findById(Long id);
1313
Optional<Vote> findByPollIdAndUserId(Long pollId, String userId);
1414
List<Vote> findByPollId(Long pollId);
15+
long countByPollId(Long pollId);
1516
long countById(Long id);
1617
}

0 commit comments

Comments
 (0)