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,
|
---|
80 | @RequestParam String password,Model model, HttpSession session)
|
---|
81 | {
|
---|
82 | // User user = null;
|
---|
83 | // try {
|
---|
84 | User user=userService.login(username,password);
|
---|
85 | System.out.println(user.getFirst_name());
|
---|
86 | // session.setAttribute("sessionUser",user);
|
---|
87 | // model.addAttribute("user",user);
|
---|
88 | return "redirect:/home";
|
---|
89 | //
|
---|
90 | // }catch (UserNotFoundException e)
|
---|
91 | // {
|
---|
92 | // model.addAttribute("hasError", true);
|
---|
93 | // model.addAttribute("error", e.getMessage());
|
---|
94 | // return "login";
|
---|
95 | // }
|
---|
96 |
|
---|
97 | }
|
---|
98 |
|
---|
99 | @PostMapping()
|
---|
100 | public String register(@RequestParam String username,
|
---|
101 | @RequestParam String first_name,
|
---|
102 | @RequestParam String last_name,
|
---|
103 | @RequestParam String password,
|
---|
104 | @RequestParam String repeatedPassword,
|
---|
105 | @RequestParam String email,
|
---|
106 | @RequestParam String number,
|
---|
107 | @RequestParam Role role)
|
---|
108 | {
|
---|
109 | try {
|
---|
110 | userService.register(first_name,last_name,username,email,number,password,role);
|
---|
111 | return "redirect:/login";
|
---|
112 | }catch (PasswordsDoNotMatchException exception)
|
---|
113 | {
|
---|
114 | return "redirect:/register?error=" + exception.getMessage();
|
---|
115 | }
|
---|
116 |
|
---|
117 | }
|
---|
118 |
|
---|
119 | @GetMapping("/films")
|
---|
120 | public String getFilmsPage(Model model){
|
---|
121 | model.addAttribute("films",filmService.findAllFilms());
|
---|
122 | model.addAttribute("bodyContent","films");
|
---|
123 | return "master-template";
|
---|
124 | }
|
---|
125 |
|
---|
126 | @GetMapping("/projections")
|
---|
127 | public String getProjectionsPage(Model model)
|
---|
128 | {
|
---|
129 | model.addAttribute("projections",projectionService.findAllProjections());
|
---|
130 | model.addAttribute("bodyContent","projections");
|
---|
131 | return "master-template";
|
---|
132 | }
|
---|
133 | @GetMapping("/events")
|
---|
134 | public String getEventsPage(Model model)
|
---|
135 | {
|
---|
136 | model.addAttribute("events",eventService.findAllEvents());
|
---|
137 | model.addAttribute("bodyContent","events");
|
---|
138 | return "master-template";
|
---|
139 | }
|
---|
140 | @GetMapping("/myTickets")
|
---|
141 | public String getMyTicketsPage(Model model,HttpSession session)
|
---|
142 | {
|
---|
143 | model.addAttribute("tickets",ticketService.findAllByCustomer((Customer) session.getAttribute("user")));
|
---|
144 | model.addAttribute("bodyContent","myTickets");
|
---|
145 | return "master-template";
|
---|
146 | }
|
---|
147 | @GetMapping("/addProjection")
|
---|
148 | public String getAddProjectionPage(Model model)
|
---|
149 | {
|
---|
150 | model.addAttribute("films",filmService.findAllFilms());
|
---|
151 | model.addAttribute("bodyContent","addProjection");
|
---|
152 | return "master-template";
|
---|
153 | }
|
---|
154 | @GetMapping("/addEvent")
|
---|
155 | public String getAddEventPage(Model model)
|
---|
156 | {
|
---|
157 | model.addAttribute("bodyContent","addEvent");
|
---|
158 | return "master-template";
|
---|
159 | }
|
---|
160 | @GetMapping("/addFilm")
|
---|
161 | public String getAddFilmPage(Model model)
|
---|
162 | {
|
---|
163 | model.addAttribute("bodyContent","addFilm");
|
---|
164 | return "master-template";
|
---|
165 | }
|
---|
166 |
|
---|
167 | @PostMapping("/addP")
|
---|
168 | public String saveProjection(@RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate date_time_start,
|
---|
169 | @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate date_time_end,
|
---|
170 | @RequestParam String type_of_technology,
|
---|
171 | @RequestParam Integer id_film)
|
---|
172 | {
|
---|
173 | projectionService.save(date_time_start,date_time_end,type_of_technology,id_film);
|
---|
174 | return "redirect:/home";
|
---|
175 | }
|
---|
176 | @PostMapping("/addE")
|
---|
177 | public String saveEvent(@RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate start_date,
|
---|
178 | @RequestParam String theme,
|
---|
179 | @RequestParam String duration,
|
---|
180 | @RequestParam String img_url,
|
---|
181 | @RequestParam String repeating)
|
---|
182 | {
|
---|
183 | eventService.save(start_date,theme,duration,repeating,img_url);
|
---|
184 | return "redirect:/home";
|
---|
185 | }
|
---|
186 | @PostMapping("/addF")
|
---|
187 | public String saveFilm(
|
---|
188 | @RequestParam String name,
|
---|
189 | @RequestParam Integer duration,
|
---|
190 | @RequestParam String actors,
|
---|
191 | @RequestParam String genre,
|
---|
192 | @RequestParam String age_category,
|
---|
193 | @RequestParam String url,
|
---|
194 | @RequestParam String director,
|
---|
195 | @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate start_date,
|
---|
196 | @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate end_date
|
---|
197 | )
|
---|
198 | {
|
---|
199 | filmService.save(name,duration,actors,genre,age_category,url,director,start_date,end_date);
|
---|
200 | return "redirect:/home";
|
---|
201 | }
|
---|
202 |
|
---|
203 | @GetMapping("/workers")
|
---|
204 | public String getWorkersPage(Model model)
|
---|
205 | {
|
---|
206 | model.addAttribute("workers",workerService.findAllWorkers());
|
---|
207 | model.addAttribute("bodyContent", "workers");
|
---|
208 | return "master-template";
|
---|
209 | }
|
---|
210 | }
|
---|