source: Git/src/main/java/com/wediscussmovies/project/web/controller/PersonsController.java@ 7bc8942

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

Controller, Repository and Service layer improvements, Entity updating

  • Property mode set to 100644
File size: 4.5 KB
Line 
1package com.wediscussmovies.project.web.controller;
2
3import com.wediscussmovies.project.model.Person;
4import com.wediscussmovies.project.model.PersonType;
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.PostMapping;
10import org.springframework.web.bind.annotation.RequestMapping;
11import org.springframework.web.bind.annotation.RequestParam;
12
13import java.sql.Date;
14import java.util.List;
15
16@Controller
17@RequestMapping(".")
18public class PersonsController {
19 private final PersonService personSerivce;
20
21 public PersonsController(PersonService personSerivce) {
22 this.personSerivce = personSerivce;
23 }
24
25 @GetMapping("/actors")
26 public String getActorsPage(Model model, @RequestParam(required = false) String nameAndSurname){
27 List<Person> actors = personSerivce.findAllActors();
28 if(nameAndSurname != null && !nameAndSurname.isEmpty()){
29 String [] parts = nameAndSurname.split(" ");
30 String name = parts[0];
31 String surname = "";
32 if(parts.length > 1)
33 surname = parts[1];
34 List<Person> actorsByName = personSerivce.findActorsByNameLike("%"+name+"%");
35 if(!surname.isEmpty()){
36 List<Person> actorsBySurname = personSerivce.findActorsBySurnameLike("%"+surname+"%");
37 for(Person p: actorsBySurname){
38 boolean add = true;
39 for(Person a: actorsByName){
40 if(a.getPerson_id() == p.getPerson_id()){
41 add=false;
42 break;
43 }
44 }
45 if(!add)
46 continue;
47 actorsByName.add(p);
48 }
49 }
50 actors = actorsByName;
51 }
52 actors.sort(Person.personComparatorByNameSurname);
53 model.addAttribute("actors", actors);
54 model.addAttribute("contentTemplate", "actorsList");
55 return "template";
56 }
57
58 @GetMapping("/directors")
59 public String getDirectorsPage(Model model, @RequestParam(required = false) String nameAndSurname){
60 List<Person> directors = personSerivce.findAllDirectors();
61 if(nameAndSurname != null && !nameAndSurname.isEmpty()){
62 String [] parts = nameAndSurname.split(" ");
63 String name = parts[0];
64 String surname = "";
65 if(parts.length > 1)
66 surname = parts[1];
67 List<Person> directorsByName = personSerivce.findDirectorsByNameLike("%"+name+"%");
68 if(!surname.isEmpty()){
69 List<Person> directorsBySurname = personSerivce.findDirectorsBySurnameLike("%"+surname+"%");
70 for(Person p: directorsBySurname){
71 boolean add = true;
72 for(Person a: directorsByName){
73 if(a.getPerson_id() == p.getPerson_id()){
74 add=false;
75 break;
76 }
77 }
78 if(!add)
79 continue;
80 directorsByName.add(p);
81 }
82 }
83 directors = directorsByName;
84 }
85 directors.sort(Person.personComparatorByNameSurname);
86 model.addAttribute("directors", directors);
87 model.addAttribute("contentTemplate", "directorsList");
88 return "template";
89 }
90
91 @GetMapping("/persons/add")
92 public String getPersonsAddPage(Model model){
93 PersonType [] types = PersonType.values();
94 model.addAttribute("types", types);
95 model.addAttribute("contentTemplate", "personsAdd");
96 return "template";
97 }
98
99 @PostMapping("/persons/add/confirm")
100 public String confirmPersonsAdd(
101 @RequestParam String name,
102 @RequestParam String surname,
103 @RequestParam PersonType type,
104 @RequestParam Date date_of_birth,
105 @RequestParam String image_url,
106 @RequestParam String description){
107 Person person = new Person(name, surname, type, date_of_birth, image_url, description);
108 if(personSerivce.save(person)){
109 String toWhere = "actors";
110 if(type == PersonType.D)
111 toWhere = "directors";
112 return "redirect:/"+toWhere;
113 }
114 else{
115 return "redirect:/persons/add";
116 }
117 }
118
119}
Note: See TracBrowser for help on using the repository browser.