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