source: Git/src/main/java/com/wediscussmovies/project/web/controller/PersonsController.java@ 3ded84d

main
Last change on this file since 3ded84d was 3ded84d, checked in by Test <matonikolov77@…>, 2 years ago

Model-database resolved bugs for mapping

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