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