Skip to content

Commit 0133c2d

Browse files
author
Christian Wansart
committed
finish main functionality
1 parent 09477dc commit 0133c2d

15 files changed

+238
-25
lines changed

src/main/java/de/cwansart/unipoll/AuthService.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,15 @@
88
@Component
99
@Scope(value = "session", proxyMode = ScopedProxyMode.TARGET_CLASS)
1010
public class AuthService {
11-
private final String storedPassword = "$2a$12$2R9Zcwr8gaslSNqV1ee6K.qDZutsnRkRaGaEeRkbBzYtDumFtCJdK";
11+
private final String storedPassword = "$2a$10$r9rH7cW8tIOJnzPX81SyOOTlAM0xNMPUf/Jvqr2IMEHpyuqN.lj8y";
1212
private boolean isAuthenticated = false;
1313

1414
public boolean isAuthenticated() {
15-
// return isAuthenticated;
16-
return true;
15+
return isAuthenticated;
1716
}
1817

1918
public boolean login(String password) {
20-
// isAuthenticated = BCrypt.checkpw(password, storedPassword);
21-
// return this.isAuthenticated;
22-
isAuthenticated = true;
23-
return isAuthenticated;
19+
isAuthenticated = BCrypt.checkpw(password, storedPassword);
20+
return this.isAuthenticated;
2421
}
2522
}

src/main/java/de/cwansart/unipoll/CreateController.java

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,16 @@
1212
import org.springframework.web.bind.annotation.PostMapping;
1313

1414
class CreateForm {
15+
private String name;
1516
private String topics;
17+
18+
public String getName() {
19+
return name;
20+
}
21+
public void setName(String name) {
22+
this.name = name;
23+
}
24+
1625
public String getTopics() {
1726
return topics;
1827
}
@@ -32,7 +41,7 @@ public class CreateController {
3241
@GetMapping("/create")
3342
public String create(Model model) {
3443
if (!auth.isAuthenticated()) {
35-
return "redirect:/login";
44+
return "redirect:/login?p=create";
3645
}
3746
model.addAttribute("createForm", new CreateForm());
3847
return "create";
@@ -41,16 +50,25 @@ public String create(Model model) {
4150
@PostMapping("/create")
4251
public String doCreate(@ModelAttribute("createForm") CreateForm createForm, Model model) {
4352
if (!auth.isAuthenticated()) {
44-
return "redirect:/login";
53+
return "redirect:/login?p=create";
54+
}
55+
56+
// TODO: check error validation with BindingResult and @Valid with Spring Boot
57+
if (createForm.getName() == null || createForm.getName().isBlank()) {
58+
model.addAttribute("name_error", "Missing name!");
59+
model.addAttribute("form", new CreateForm());
60+
return "create";
4561
}
4662

4763
List<String> topics = Arrays.asList(createForm.getTopics().split(","));
4864
if (topics.isEmpty()) {
49-
model.addAttribute("error", "Topics may not be empty!");
65+
model.addAttribute("topics_error", "Topics may not be empty!");
5066
model.addAttribute("form", new CreateForm());
5167
return "create";
5268
}
69+
5370
Poll poll = new Poll();
71+
poll.setName(createForm.getName());
5472
poll.setChoices(topics.stream().map(t -> new Choice(t)).collect(Collectors.toList()));
5573
poll = repo.save(poll);
5674
return "redirect:/vote?id=" + poll.getId();
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package de.cwansart.unipoll;
2+
3+
import java.util.Optional;
4+
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.http.HttpStatusCode;
7+
import org.springframework.stereotype.Controller;
8+
import org.springframework.ui.Model;
9+
import org.springframework.web.bind.annotation.GetMapping;
10+
import org.springframework.web.bind.annotation.PostMapping;
11+
import org.springframework.web.bind.annotation.RequestParam;
12+
import org.springframework.web.server.ResponseStatusException;
13+
14+
@Controller
15+
public class DeleteController {
16+
@Autowired
17+
private AuthService auth;
18+
19+
@Autowired
20+
private PollRepository pollRepo;
21+
22+
@GetMapping("/delete")
23+
public String confirm(@RequestParam(name = "id", required = true) long id, Model model) {
24+
if (!auth.isAuthenticated()) {
25+
return "redirect:/login?p=list";
26+
}
27+
28+
if (!pollRepo.findById(id).isPresent()) {
29+
throw new ResponseStatusException(HttpStatusCode.valueOf(404), "poll not found");
30+
}
31+
32+
model.addAttribute("id", id);
33+
return "confirm_delete";
34+
}
35+
36+
@PostMapping("/delete")
37+
public String delete(@RequestParam(name = "id", required = true) long id, Model model) {
38+
if (!auth.isAuthenticated()) {
39+
return "redirect:/login?p=list";
40+
}
41+
42+
Optional<Poll> poll = pollRepo.findById(id);
43+
if (!poll.isPresent()) {
44+
throw new ResponseStatusException(HttpStatusCode.valueOf(404), "poll not found");
45+
}
46+
poll.get().setDeleted(true);
47+
pollRepo.save(poll.get());
48+
return "redirect:/list";
49+
}
50+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package de.cwansart.unipoll;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.data.domain.Page;
5+
import org.springframework.data.domain.PageRequest;
6+
import org.springframework.data.domain.Sort;
7+
import org.springframework.stereotype.Controller;
8+
import org.springframework.ui.Model;
9+
import org.springframework.web.bind.annotation.GetMapping;
10+
import org.springframework.web.bind.annotation.RequestParam;
11+
12+
@Controller
13+
public class ListController {
14+
@Autowired
15+
private PollRepository pollRepo;
16+
17+
@Autowired
18+
private AuthService auth;
19+
20+
@GetMapping("/list")
21+
public String showAll(@RequestParam(name = "p", defaultValue = "0") int page, Model model) {
22+
if (!auth.isAuthenticated()) {
23+
return "redirect:/login?p=list";
24+
}
25+
26+
Page<Poll> polls = pollRepo.findAll(PageRequest.of(page, 20, Sort.by(Sort.Direction.ASC, "id")));
27+
model.addAttribute("polls", polls.getContent());
28+
model.addAttribute("totalNum", polls.getTotalElements());
29+
model.addAttribute("totalPages", polls.getTotalPages());
30+
model.addAttribute("currentPage", page + 1);
31+
return "list";
32+
}
33+
}

src/main/java/de/cwansart/unipoll/LoginController.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package de.cwansart.unipoll;
22

33
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.http.HttpStatusCode;
45
import org.springframework.stereotype.Controller;
56
import org.springframework.ui.Model;
67
import org.springframework.web.bind.annotation.GetMapping;
78
import org.springframework.web.bind.annotation.ModelAttribute;
89
import org.springframework.web.bind.annotation.PostMapping;
10+
import org.springframework.web.bind.annotation.RequestParam;
11+
import org.springframework.web.server.ResponseStatusException;
912

1013
class LoginForm {
1114
private String password;
@@ -23,20 +26,24 @@ public class LoginController {
2326
private AuthService auth;
2427

2528
@GetMapping("/login")
26-
public String login(Model model) {
29+
public String login(@RequestParam(name = "p", required = true) String page, Model model) {
2730
if (auth.isAuthenticated()) {
28-
return "redirect:/create";
31+
return "redirect:/" + page;
2932
}
3033
model.addAttribute("loginForm", new LoginForm());
34+
model.addAttribute("page", page);
3135
return "login";
3236
}
3337

3438
@PostMapping("/login")
35-
public String doLogin(@ModelAttribute("loginForm") LoginForm loginForm, Model model) {
39+
public String doLogin(@ModelAttribute("loginForm") LoginForm loginForm, @RequestParam(name = "p", required = true) String page, Model model) {
3640
if (!auth.login(loginForm.getPassword())) {
3741
model.addAttribute("error", "Invalid password!");
3842
} else {
39-
return "redirect:/create";
43+
if (!page.equals("create") && !page.equals("list")) {
44+
throw new ResponseStatusException(HttpStatusCode.valueOf(400), "unknown redirect page");
45+
}
46+
return "redirect:/" + page;
4047
}
4148
model.addAttribute("loginForm", new LoginForm());
4249
return "login";

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.util.List;
44

55
import jakarta.persistence.CascadeType;
6+
import jakarta.persistence.Column;
67
import jakarta.persistence.Entity;
78
import jakarta.persistence.GeneratedValue;
89
import jakarta.persistence.GenerationType;
@@ -13,9 +14,14 @@
1314
public class Poll {
1415
@Id @GeneratedValue(strategy = GenerationType.AUTO)
1516
private Long id;
17+
18+
@Column(nullable = false)
19+
private String name;
1620

1721
@OneToMany(cascade = CascadeType.ALL)
1822
private List<Choice> choices;
23+
24+
private boolean deleted;
1925

2026
public Long getId() {
2127
return id;
@@ -30,4 +36,18 @@ public List<Choice> getChoices() {
3036
public void setChoices(List<Choice> choices) {
3137
this.choices = choices;
3238
}
39+
40+
public String getName() {
41+
return name;
42+
}
43+
public void setName(String name) {
44+
this.name = name;
45+
}
46+
47+
public boolean isDeleted() {
48+
return deleted;
49+
}
50+
public void setDeleted(boolean deleted) {
51+
this.deleted = deleted;
52+
}
3353
}

src/main/java/de/cwansart/unipoll/PollRepository.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
import java.util.Optional;
44

5-
import org.springframework.data.repository.Repository;
5+
import org.springframework.data.repository.PagingAndSortingRepository;
66

7-
public interface PollRepository extends Repository<Poll, Long> {
7+
public interface PollRepository extends PagingAndSortingRepository<Poll, Long> {
88
Poll save(Poll poll);
99
Optional<Poll> findById(Long id);
1010
}

src/main/java/de/cwansart/unipoll/ResultController.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,6 @@ public class ResultController {
5353
@Autowired
5454
private VoteRepository voteRepo;
5555

56-
// public String showAll(Model model) {
57-
// return "all_polls";
58-
// }
59-
6056
@GetMapping("/results")
6157
public String show(
6258
@RequestParam(name = "id", required = true) Long id,

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public String show(
6767
throw new ResponseStatusException(HttpStatusCode.valueOf(404), "poll does not exist");
6868
}
6969

70-
String userId = String.valueOf(Math.random()*1000); //request.getRemoteAddr();
70+
String userId = request.getRemoteAddr();
7171

7272
// check if user already voted
7373
if (voteRepo.findByIdAndUserId(id, userId).isPresent()) {
@@ -91,7 +91,7 @@ public String save(
9191
throw new ResponseStatusException(HttpStatusCode.valueOf(404), "poll does not exist");
9292
}
9393

94-
String userId = String.valueOf(Math.random()*1000); //request.getRemoteAddr();
94+
String userId = request.getRemoteAddr();
9595

9696
// check if user has already voted
9797
if (voteRepo.findByIdAndUserId(id, userId).isPresent()) {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ public interface VoteRepository extends Repository<Vote, Long> {
1010
Optional<Vote> findById(Long id);
1111
Optional<Vote> findByIdAndUserId(Long id, String userId);
1212
List<Vote> findByPollId(Long pollId);
13+
long countById(Long id);
1314
}

0 commit comments

Comments
 (0)