source: src/main/java/com/example/moviezone/web/HomeController.java@ b5ce654

Last change on this file since b5ce654 was b5ce654, checked in by DenicaKj <dkorvezir@…>, 22 months ago

Style updates

  • Property mode set to 100644
File size: 7.8 KB
Line 
1package com.example.moviezone.web;
2
3
4import com.example.moviezone.model.Customer;
5import com.example.moviezone.model.Film;
6import com.example.moviezone.model.Role;
7import com.example.moviezone.model.User;
8import com.example.moviezone.model.exceptions.PasswordsDoNotMatchException;
9import com.example.moviezone.model.exceptions.UserNotFoundException;
10import com.example.moviezone.service.*;
11import org.springframework.format.annotation.DateTimeFormat;
12import org.springframework.stereotype.Controller;
13import org.springframework.ui.Model;
14import org.springframework.web.bind.annotation.*;
15
16import javax.servlet.http.HttpSession;
17import java.time.LocalDate;
18import java.util.List;
19import java.util.stream.Collectors;
20
21@Controller
22@RequestMapping({"/","/home"})
23public class HomeController {
24
25private final FilmService filmService;
26private final UserService userService;
27private final ProjectionService projectionService;
28private final EventService eventService;
29private final TicketService ticketService;
30private final WorkerService workerService;
31private final CustomerRatesFilmService customerRatesFilmService;
32
33 public HomeController(FilmService filmService, UserService userService, ProjectionService projectionService, EventService eventService, TicketService ticketService, WorkerService workerService, CustomerRatesFilmService customerRatesFilmService) {
34 this.filmService = filmService;
35 this.userService = userService;
36 this.projectionService = projectionService;
37 this.eventService = eventService;
38 this.ticketService = ticketService;
39 this.workerService = workerService;
40 this.customerRatesFilmService = customerRatesFilmService;
41 }
42
43 @GetMapping
44 public String getHomePage(Model model) {
45 List<Film> films=filmService.findAllFilms();
46 films=films.stream().limit(5).collect(Collectors.toList());
47 model.addAttribute("films", films);
48 model.addAttribute("bodyContent", "home");
49
50 return "master-template";
51 }
52 @GetMapping("/getFilm/{id}")
53 public String getFilm(@PathVariable Long id, Model model) {
54 Film film=filmService.getFilmById(id).get();
55 model.addAttribute("film", film);
56 List<String> genres= List.of(film.getGenre().split(","));
57 double r=customerRatesFilmService.avg_rating(film.getId_film());
58 model.addAttribute("rating",r);
59 model.addAttribute("genres", genres);
60 model.addAttribute("bodyContent", "film");
61
62 return "master-template";
63 }
64
65 @GetMapping("/login")
66 public String getLoginPage(Model model)
67 {
68 model.addAttribute("bodyContent", "login");
69 return "master-template";
70 }
71
72 @GetMapping("/register")
73 public String getRegisterPage(Model model)
74 {
75 model.addAttribute("bodyContent", "register");
76 return "master-template";
77 }
78
79 @PostMapping("/login")
80 public String login(@RequestParam String username,@RequestParam String password,Model model, HttpSession session)
81 {
82 User user = null;
83 try {
84 user=userService.login(username,password);
85 session.setAttribute("sessionUser",user);
86 model.addAttribute("user",user);
87 return "redirect:/home";
88
89 }catch (UserNotFoundException e)
90 {
91 model.addAttribute("hasError", true);
92 model.addAttribute("error", e.getMessage());
93 return "login";
94 }
95
96 }
97
98 @PostMapping()
99 public String register(@RequestParam String username,
100 @RequestParam String first_name,
101 @RequestParam String last_name,
102 @RequestParam String password,
103 @RequestParam String repeatedPassword,
104 @RequestParam String email,
105 @RequestParam String number,
106 @RequestParam Role role)
107 {
108 try {
109 User user=userService.register(first_name,last_name,username,email,number,password,repeatedPassword,role);
110 return "redirect:/login";
111 }catch (PasswordsDoNotMatchException exception)
112 {
113 return "redirect:/register?error=" + exception.getMessage();
114 }
115
116 }
117
118 @GetMapping("/films")
119 public String getFilmsPage(Model model){
120 model.addAttribute("films",filmService.findAllFilms());
121 model.addAttribute("bodyContent","films");
122 return "master-template";
123 }
124
125 @GetMapping("/projections")
126 public String getProjectionsPage(Model model)
127 {
128 model.addAttribute("projections",projectionService.findAllProjections());
129 model.addAttribute("bodyContent","projections");
130 return "master-template";
131 }
132 @GetMapping("/events")
133 public String getEventsPage(Model model)
134 {
135 model.addAttribute("events",eventService.findAllEvents());
136 model.addAttribute("bodyContent","events");
137 return "master-template";
138 }
139 @GetMapping("/myTickets")
140 public String getMyTicketsPage(Model model,HttpSession session)
141 {
142 model.addAttribute("tickets",ticketService.findAllByCustomer((Customer) session.getAttribute("user")));
143 model.addAttribute("bodyContent","myTickets");
144 return "master-template";
145 }
146 @GetMapping("/addProjection")
147 public String getAddProjectionPage(Model model)
148 {
149 model.addAttribute("films",filmService.findAllFilms());
150 model.addAttribute("bodyContent","addProjection");
151 return "master-template";
152 }
153 @GetMapping("/addEvent")
154 public String getAddEventPage(Model model)
155 {
156 model.addAttribute("bodyContent","addEvent");
157 return "master-template";
158 }
159 @GetMapping("/addFilm")
160 public String getAddFilmPage(Model model)
161 {
162 model.addAttribute("bodyContent","addFilm");
163 return "master-template";
164 }
165
166 @PostMapping("/addP")
167 public String saveProjection(@RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate date_time_start,
168 @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate date_time_end,
169 @RequestParam String type_of_technology,
170 @RequestParam Integer id_film)
171 {
172 projectionService.save(date_time_start,date_time_end,type_of_technology,id_film);
173 return "redirect:/home";
174 }
175 @PostMapping("/addE")
176 public String saveEvent(@RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate start_date,
177 @RequestParam String theme,
178 @RequestParam String duration,
179 @RequestParam String repeating)
180 {
181 eventService.save(start_date,theme,duration,repeating);
182 return "redirect:/home";
183 }
184 @PostMapping("/addF")
185 public String saveFilm(
186 @RequestParam String name,
187 @RequestParam Integer duration,
188 @RequestParam String actors,
189 @RequestParam String genre,
190 @RequestParam String age_category,
191 @RequestParam String url,
192 @RequestParam String director,
193 @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate start_date,
194 @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate end_date
195 )
196 {
197 filmService.save(name,duration,actors,genre,age_category,url,director,start_date,end_date);
198 return "redirect:/home";
199 }
200
201 @GetMapping("/workers")
202 public String getWorkersPage(Model model)
203 {
204 model.addAttribute("workers",workerService.findAllWorkers());
205 model.addAttribute("bodyContent", "workers");
206 return "master-template";
207 }
208}
Note: See TracBrowser for help on using the repository browser.