Skip to content

Commit 700fc13

Browse files
author
Christian Wansart
committed
code split up -- clean code lol
1 parent 9f33d38 commit 700fc13

File tree

17 files changed

+135
-128
lines changed

17 files changed

+135
-128
lines changed

src/main/java/de/cwansart/unipoll/CreateController.java renamed to src/main/java/de/cwansart/unipoll/controller/CreateController.java

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

33
import java.util.Arrays;
44
import java.util.List;
@@ -11,24 +11,11 @@
1111
import org.springframework.web.bind.annotation.ModelAttribute;
1212
import org.springframework.web.bind.annotation.PostMapping;
1313

14-
class CreateForm {
15-
private String name;
16-
private String topics;
17-
18-
public String getName() {
19-
return name;
20-
}
21-
public void setName(String name) {
22-
this.name = name;
23-
}
24-
25-
public String getTopics() {
26-
return topics;
27-
}
28-
public void setTopics(String topics) {
29-
this.topics = topics;
30-
}
31-
}
14+
import de.cwansart.unipoll.entity.Choice;
15+
import de.cwansart.unipoll.entity.Poll;
16+
import de.cwansart.unipoll.model.CreateForm;
17+
import de.cwansart.unipoll.repository.PollRepository;
18+
import de.cwansart.unipoll.service.AuthService;
3219

3320
@Controller
3421
public class CreateController {
@@ -69,7 +56,7 @@ public String doCreate(@ModelAttribute("createForm") CreateForm createForm, Mode
6956

7057
Poll poll = new Poll();
7158
poll.setName(createForm.getName());
72-
poll.setChoices(topics.stream().map(t -> new UChoice(t)).collect(Collectors.toList()));
59+
poll.setChoices(topics.stream().map(t -> new Choice(t)).collect(Collectors.toList()));
7360
poll = repo.save(poll);
7461
return "redirect:/vote?id=" + poll.getId();
7562
}

src/main/java/de/cwansart/unipoll/DeleteController.java renamed to src/main/java/de/cwansart/unipoll/controller/DeleteController.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package de.cwansart.unipoll;
1+
package de.cwansart.unipoll.controller;
22

33
import java.util.Optional;
44

@@ -11,6 +11,10 @@
1111
import org.springframework.web.bind.annotation.RequestParam;
1212
import org.springframework.web.server.ResponseStatusException;
1313

14+
import de.cwansart.unipoll.entity.Poll;
15+
import de.cwansart.unipoll.repository.PollRepository;
16+
import de.cwansart.unipoll.service.AuthService;
17+
1418
@Controller
1519
public class DeleteController {
1620
@Autowired

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package de.cwansart.unipoll;
1+
package de.cwansart.unipoll.controller;
22

33
import org.springframework.beans.factory.annotation.Autowired;
44
import org.springframework.data.domain.Page;
@@ -9,6 +9,10 @@
99
import org.springframework.web.bind.annotation.GetMapping;
1010
import org.springframework.web.bind.annotation.RequestParam;
1111

12+
import de.cwansart.unipoll.entity.Poll;
13+
import de.cwansart.unipoll.repository.PollRepository;
14+
import de.cwansart.unipoll.service.AuthService;
15+
1216
@Controller
1317
public class ListController {
1418
@Autowired

src/main/java/de/cwansart/unipoll/LoginController.java renamed to src/main/java/de/cwansart/unipoll/controller/LoginController.java

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

33
import org.springframework.beans.factory.annotation.Autowired;
44
import org.springframework.http.HttpStatusCode;
@@ -10,15 +10,8 @@
1010
import org.springframework.web.bind.annotation.RequestParam;
1111
import org.springframework.web.server.ResponseStatusException;
1212

13-
class LoginForm {
14-
private String password;
15-
public String getPassword() {
16-
return password;
17-
}
18-
public void setPassword(String password) {
19-
this.password = password;
20-
}
21-
}
13+
import de.cwansart.unipoll.model.LoginForm;
14+
import de.cwansart.unipoll.service.AuthService;
2215

2316
@Controller
2417
public class LoginController {

src/main/java/de/cwansart/unipoll/ResultController.java renamed to src/main/java/de/cwansart/unipoll/controller/ResultController.java

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

33
import java.util.List;
44
import java.util.Optional;
@@ -12,39 +12,13 @@
1212
import org.springframework.web.bind.annotation.RequestParam;
1313
import org.springframework.web.server.ResponseStatusException;
1414

15+
import de.cwansart.unipoll.entity.Poll;
16+
import de.cwansart.unipoll.entity.Vote;
17+
import de.cwansart.unipoll.model.Result;
18+
import de.cwansart.unipoll.repository.PollRepository;
19+
import de.cwansart.unipoll.repository.VoteRepository;
1520
import jakarta.servlet.http.HttpServletRequest;
1621

17-
class Result {
18-
private long id;
19-
private String name;
20-
private long count;
21-
22-
public Result(Long id, String name) {
23-
this.id = id;
24-
this.name = name;
25-
}
26-
public long getId() {
27-
return id;
28-
}
29-
public void setId(long id) {
30-
this.id = id;
31-
}
32-
33-
public String getName() {
34-
return name;
35-
}
36-
public void setName(String name) {
37-
this.name = name;
38-
}
39-
40-
public long getCount() {
41-
return count;
42-
}
43-
public void setCount(long count) {
44-
this.count = count;
45-
}
46-
}
47-
4822
@Controller
4923
public class ResultController {
5024
@Autowired

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

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

33
import java.util.ArrayList;
44
import java.util.Arrays;
@@ -16,42 +16,16 @@
1616
import org.springframework.web.bind.annotation.RequestParam;
1717
import org.springframework.web.server.ResponseStatusException;
1818

19+
import de.cwansart.unipoll.entity.Choice;
20+
import de.cwansart.unipoll.entity.Poll;
21+
import de.cwansart.unipoll.entity.Vote;
22+
import de.cwansart.unipoll.model.VoteForm;
23+
import de.cwansart.unipoll.repository.PollRepository;
24+
import de.cwansart.unipoll.repository.VoteRepository;
1925
import jakarta.servlet.http.Cookie;
2026
import jakarta.servlet.http.HttpServletRequest;
2127
import jakarta.servlet.http.HttpServletResponse;
2228

23-
class ChoiceElement {
24-
private int id;
25-
private String name;
26-
public ChoiceElement() {
27-
}
28-
public ChoiceElement(String name, int id) {
29-
this.id = id;
30-
this.name = name;
31-
}
32-
public int getId() {
33-
return id;
34-
}
35-
public String getName() {
36-
return name;
37-
}
38-
}
39-
40-
class VoteForm {
41-
private List<String> selected;
42-
public VoteForm() {
43-
}
44-
public VoteForm(List<String> selected) {
45-
this.selected = selected;
46-
}
47-
public List<String> getSelected() {
48-
return selected;
49-
}
50-
public void setSelected(List<String> selected) {
51-
this.selected = selected;
52-
}
53-
}
54-
5529
@Controller
5630
public class VoteController {
5731
@Autowired
@@ -102,10 +76,10 @@ public String save(
10276
}
10377

10478
// check if selected ids belong to the given poll
105-
List<UChoice> choices = new ArrayList<>();
79+
List<Choice> choices = new ArrayList<>();
10680
for(String s: voteForm.getSelected()) {
10781
long sLong = Long.parseLong(s);
108-
Optional<UChoice> choice = poll.get().getChoices().stream().filter(t -> t.getId().equals(sLong)).findFirst();
82+
Optional<Choice> choice = poll.get().getChoices().stream().filter(t -> t.getId().equals(sLong)).findFirst();
10983
if (choice.isPresent()) {
11084
choices.add(choice.get());
11185
} else {

src/main/java/de/cwansart/unipoll/UChoice.java renamed to src/main/java/de/cwansart/unipoll/entity/Choice.java

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

33
import jakarta.persistence.Entity;
44
import jakarta.persistence.GeneratedValue;
55
import jakarta.persistence.GenerationType;
66
import jakarta.persistence.Id;
77

8-
// for some reason compilation fails when this class is just called "Choice"
98
@Entity
10-
public class UChoice {
9+
public class Choice {
1110
@Id @GeneratedValue(strategy = GenerationType.AUTO)
1211
private Long id;
1312

1413
private String name;
1514

16-
public UChoice() {}
17-
public UChoice(String name) {
15+
public Choice() {}
16+
public Choice(String name) {
1817
this.name = name;
1918
}
2019

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

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

33
import java.util.List;
44

@@ -19,7 +19,7 @@ public class Poll {
1919
private String name;
2020

2121
@OneToMany(cascade = CascadeType.ALL)
22-
private List<UChoice> choices;
22+
private List<Choice> choices;
2323

2424
private boolean deleted;
2525

@@ -30,10 +30,10 @@ public void setId(Long id) {
3030
this.id = id;
3131
}
3232

33-
public List<UChoice> getChoices() {
33+
public List<Choice> getChoices() {
3434
return choices;
3535
}
36-
public void setChoices(List<UChoice> choices) {
36+
public void setChoices(List<Choice> choices) {
3737
this.choices = choices;
3838
}
3939

src/main/java/de/cwansart/unipoll/Vote.java renamed to src/main/java/de/cwansart/unipoll/entity/Vote.java

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

33
import java.util.List;
44

@@ -16,7 +16,7 @@ public class Vote {
1616
private Long id;
1717

1818
@ManyToAny
19-
private List<UChoice> choices;
19+
private List<Choice> choices;
2020

2121
@ManyToOne
2222
private Poll poll;
@@ -30,10 +30,10 @@ public void setId(Long id) {
3030
this.id = id;
3131
}
3232

33-
public List<UChoice> getChoices() {
33+
public List<Choice> getChoices() {
3434
return choices;
3535
}
36-
public void setChoices(List<UChoice> choices) {
36+
public void setChoices(List<Choice> choices) {
3737
this.choices = choices;
3838
}
3939

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package de.cwansart.unipoll.model;
2+
3+
public class CreateForm {
4+
private String name;
5+
private String topics;
6+
7+
public String getName() {
8+
return name;
9+
}
10+
public void setName(String name) {
11+
this.name = name;
12+
}
13+
14+
public String getTopics() {
15+
return topics;
16+
}
17+
public void setTopics(String topics) {
18+
this.topics = topics;
19+
}
20+
}

0 commit comments

Comments
 (0)