[e0ef1b1] | 1 | package com.wediscussmovies.project.web.controller;
|
---|
| 2 |
|
---|
| 3 | import com.wediscussmovies.project.model.Person;
|
---|
| 4 | import com.wediscussmovies.project.service.MovieService;
|
---|
| 5 | import com.wediscussmovies.project.service.PersonService;
|
---|
| 6 | import org.springframework.stereotype.Controller;
|
---|
| 7 | import org.springframework.ui.Model;
|
---|
| 8 | import org.springframework.web.bind.annotation.GetMapping;
|
---|
| 9 | import org.springframework.web.bind.annotation.PathVariable;
|
---|
| 10 | import org.springframework.web.bind.annotation.PostMapping;
|
---|
| 11 | import org.springframework.web.bind.annotation.RequestParam;
|
---|
| 12 |
|
---|
| 13 | import java.sql.Date;
|
---|
| 14 | import java.util.List;
|
---|
| 15 |
|
---|
| 16 | @Controller
|
---|
| 17 | public 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/add")
|
---|
| 51 | public String addingFormForPerson( Model model){
|
---|
| 52 |
|
---|
| 53 | addModelPropertiesForForm(model);
|
---|
| 54 | return "template";
|
---|
| 55 | }
|
---|
| 56 | @GetMapping("persons/edit/{personId}")
|
---|
| 57 | public String editPerson(@PathVariable Integer personId, Model model){
|
---|
| 58 |
|
---|
| 59 | try {
|
---|
| 60 | Person person = this.personService.findById(personId);
|
---|
| 61 | addModelPropertiesForForm(model);
|
---|
| 62 | model.addAttribute("person",person);
|
---|
| 63 | model.addAttribute("movieActors",this.personService.findAllMoviesByPerson(person));
|
---|
| 64 | return "template";
|
---|
| 65 | }
|
---|
| 66 | catch (RuntimeException exc){
|
---|
| 67 | return "redirect:/actors?erorr="+exc.getMessage();
|
---|
| 68 | }
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | @PostMapping("/persons/save")
|
---|
| 72 | public String savePerson(
|
---|
| 73 | @RequestParam String name,
|
---|
| 74 | @RequestParam String surname,
|
---|
| 75 | @RequestParam Character type,
|
---|
| 76 | @RequestParam Date birthDate,
|
---|
| 77 | @RequestParam String imageUrl,
|
---|
| 78 | @RequestParam String description,
|
---|
| 79 | @RequestParam(required = false) List<Integer> movieIds){
|
---|
| 80 |
|
---|
| 81 | String returnedUrl = getReturnedUrl(type);
|
---|
| 82 |
|
---|
| 83 | try {
|
---|
| 84 |
|
---|
| 85 | Person person = this.personService.save(name, surname, type, birthDate, imageUrl, description, movieIds);
|
---|
| 86 | return returnedUrl;
|
---|
| 87 | }
|
---|
| 88 | catch (RuntimeException exc){
|
---|
| 89 | return returnedUrl + "?error" + exc.getMessage();
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | }
|
---|
| 93 | @PostMapping("/persons/save/{personId}")
|
---|
| 94 | public String editPerson(
|
---|
| 95 | @PathVariable Integer personId,
|
---|
| 96 | @RequestParam String name,
|
---|
| 97 | @RequestParam String surname,
|
---|
| 98 | @RequestParam Character type,
|
---|
| 99 | @RequestParam Date birthDate,
|
---|
| 100 | @RequestParam String imageUrl,
|
---|
| 101 | @RequestParam String description,
|
---|
| 102 | @RequestParam(required = false) List<Integer> movieIds){
|
---|
| 103 |
|
---|
| 104 | String returnedUrl = getReturnedUrl(type);
|
---|
| 105 | try {
|
---|
| 106 |
|
---|
| 107 | Person person = this.personService.edit(personId,name, surname, type, birthDate, imageUrl, description, movieIds);
|
---|
| 108 | return returnedUrl;
|
---|
| 109 | }
|
---|
| 110 | catch (RuntimeException exc){
|
---|
| 111 | return returnedUrl + "?error" + exc.getMessage();
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | }
|
---|
| 115 | private String getReturnedUrl(Character type){
|
---|
| 116 | if (type.equals('A'))
|
---|
| 117 | return "redirect:/actors";
|
---|
| 118 | return "redirect:/directors";
|
---|
| 119 | }
|
---|
| 120 | private void addModelPropertiesForForm(Model model){
|
---|
| 121 | model.addAttribute("contentTemplate", "personsAdd");
|
---|
| 122 | model.addAttribute("moviesActors",this.movieService.listAllByType('A'));
|
---|
| 123 | model.addAttribute("moviesDirectors",this.movieService.listAllByType('D'));
|
---|
| 124 |
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 |
|
---|
| 128 | }
|
---|