source: Git/src/main/java/com/wediscussmovies/project/web/controller/PersonController.java@ 7f36551

main
Last change on this file since 7f36551 was f25e8dd, checked in by Petar Partaloski <ppartaloski@…>, 2 years ago

Fixed and added a better front end, improved clarity

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