1 | package com.wediscussmovies.project.web.controller;
|
---|
2 |
|
---|
3 | import com.wediscussmovies.project.LoggedUser;
|
---|
4 | import com.wediscussmovies.project.model.Person;
|
---|
5 | import com.wediscussmovies.project.model.User;
|
---|
6 | import com.wediscussmovies.project.service.MovieService;
|
---|
7 | import com.wediscussmovies.project.service.PersonService;
|
---|
8 | import org.springframework.stereotype.Controller;
|
---|
9 | import org.springframework.ui.Model;
|
---|
10 | import org.springframework.web.bind.annotation.GetMapping;
|
---|
11 | import org.springframework.web.bind.annotation.PathVariable;
|
---|
12 | import org.springframework.web.bind.annotation.PostMapping;
|
---|
13 | import org.springframework.web.bind.annotation.RequestParam;
|
---|
14 |
|
---|
15 | import java.sql.Date;
|
---|
16 | import java.util.List;
|
---|
17 |
|
---|
18 | @Controller
|
---|
19 | public class PersonController {
|
---|
20 | private final PersonService personService;
|
---|
21 | private final MovieService movieService;
|
---|
22 |
|
---|
23 | public PersonController(PersonService personService, MovieService movieService) {
|
---|
24 | this.personService = personService;
|
---|
25 | this.movieService = movieService;
|
---|
26 | }
|
---|
27 |
|
---|
28 | /*
|
---|
29 | 1. Prebaruvanjeto po ime i prezime posoodvetno e da bide na frontend
|
---|
30 | Da ne se preoptovaruva server i klient da ceka koga moze vo browser vekje dobieni da se filtrirat
|
---|
31 | */
|
---|
32 | @GetMapping("/actors")
|
---|
33 | public String getActors(Model model, @RequestParam(required = false) String nameAndSurname){
|
---|
34 | List<Person> persons = personService.findPersonsByNameOrSurname('A',nameAndSurname);
|
---|
35 | model.addAttribute("persons", persons);
|
---|
36 | model.addAttribute("contentTemplate", "personsList");
|
---|
37 |
|
---|
38 | return "template";
|
---|
39 | }
|
---|
40 | /*
|
---|
41 | 2. Prebaruvanjeto po ime i prezime posoodvetno e da bide na frontend
|
---|
42 | Da ne se preoptovaruva server i klient da ceka koga moze vo browser vekje dobieni da se filtrirat
|
---|
43 | */
|
---|
44 | @GetMapping("/directors")
|
---|
45 | public String getDirectors(Model model, @RequestParam(required = false) String nameAndSurname){
|
---|
46 | List<Person> persons = personService.findPersonsByNameOrSurname('D',nameAndSurname);
|
---|
47 | model.addAttribute("persons", persons);
|
---|
48 | model.addAttribute("contentTemplate", "personsList");
|
---|
49 | return "template";
|
---|
50 | }
|
---|
51 |
|
---|
52 | @GetMapping("/persons/{id}")
|
---|
53 | public String getPerson(@PathVariable Integer id, Model model){
|
---|
54 | Person person = personService.findById(id);
|
---|
55 | //Error handling, could be null!!!!!!!!!
|
---|
56 | model.addAttribute("person", person);
|
---|
57 | addModelPropertiesForUser(model);
|
---|
58 | model.addAttribute("contentTemplate", "personShow");
|
---|
59 | return "template";
|
---|
60 | }
|
---|
61 |
|
---|
62 | @GetMapping("/persons/add")
|
---|
63 | public String addingFormForPerson( Model model){
|
---|
64 |
|
---|
65 | addModelPropertiesForForm(model);
|
---|
66 | return "template";
|
---|
67 | }
|
---|
68 | @GetMapping("persons/edit/{personId}")
|
---|
69 | public String editPerson(@PathVariable Integer personId, Model model){
|
---|
70 |
|
---|
71 | try {
|
---|
72 | Person person = this.personService.findById(personId);
|
---|
73 | addModelPropertiesForForm(model);
|
---|
74 | model.addAttribute("person",person);
|
---|
75 | model.addAttribute("movieActors",this.personService.findAllMoviesByPerson(person));
|
---|
76 | return "template";
|
---|
77 | }
|
---|
78 | catch (RuntimeException exc){
|
---|
79 | return "redirect:/actors?erorr="+exc.getMessage();
|
---|
80 | }
|
---|
81 | }
|
---|
82 |
|
---|
83 | @PostMapping("/persons/save")
|
---|
84 | public String savePerson(
|
---|
85 | @RequestParam String name,
|
---|
86 | @RequestParam String surname,
|
---|
87 | @RequestParam Character type,
|
---|
88 | @RequestParam Date birthDate,
|
---|
89 | @RequestParam String imageUrl,
|
---|
90 | @RequestParam String description,
|
---|
91 | @RequestParam(required = false) List<Integer> movieIds){
|
---|
92 |
|
---|
93 | String returnedUrl = getReturnedUrl(type);
|
---|
94 |
|
---|
95 | try {
|
---|
96 |
|
---|
97 | Person person = this.personService.save(name, surname, type, birthDate, imageUrl, description, movieIds);
|
---|
98 | return returnedUrl;
|
---|
99 | }
|
---|
100 | catch (RuntimeException exc){
|
---|
101 | return returnedUrl + "?error" + exc.getMessage();
|
---|
102 | }
|
---|
103 |
|
---|
104 | }
|
---|
105 | @PostMapping("/persons/save/{personId}")
|
---|
106 | public String editPerson(
|
---|
107 | @PathVariable Integer personId,
|
---|
108 | @RequestParam String name,
|
---|
109 | @RequestParam String surname,
|
---|
110 | @RequestParam Character type,
|
---|
111 | @RequestParam Date birthDate,
|
---|
112 | @RequestParam String imageUrl,
|
---|
113 | @RequestParam String description,
|
---|
114 | @RequestParam(required = false) List<Integer> movieIds){
|
---|
115 |
|
---|
116 | String returnedUrl = getReturnedUrl(type);
|
---|
117 | try {
|
---|
118 |
|
---|
119 | Person person = this.personService.edit(personId,name, surname, type, birthDate, imageUrl, description, movieIds);
|
---|
120 | return returnedUrl;
|
---|
121 | }
|
---|
122 | catch (RuntimeException exc){
|
---|
123 | return returnedUrl + "?error" + exc.getMessage();
|
---|
124 | }
|
---|
125 |
|
---|
126 | }
|
---|
127 | private String getReturnedUrl(Character type){
|
---|
128 | if (type.equals('A'))
|
---|
129 | return "redirect:/actors";
|
---|
130 | return "redirect:/directors";
|
---|
131 | }
|
---|
132 | private void addModelPropertiesForForm(Model model){
|
---|
133 | model.addAttribute("contentTemplate", "personsAdd");
|
---|
134 | model.addAttribute("moviesActors",this.movieService.listAllByType('A'));
|
---|
135 | model.addAttribute("moviesDirectors",this.movieService.listAllByType('D'));
|
---|
136 |
|
---|
137 | }
|
---|
138 |
|
---|
139 | private void addModelPropertiesForUser(Model model){
|
---|
140 | User user = LoggedUser.getLoggedUser();
|
---|
141 | model.addAttribute("user",user);
|
---|
142 | }
|
---|
143 |
|
---|
144 |
|
---|
145 | }
|
---|