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

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

trying to call a function from database

  • Property mode set to 100644
File size: 11.6 KB
Line 
1package com.example.moviezone.web;
2
3
4import com.example.moviezone.model.*;
5import com.example.moviezone.model.exceptions.PasswordsDoNotMatchException;
6
7import com.example.moviezone.model.manytomany.ProjectionIsPlayedInRoom;
8import com.example.moviezone.repository.ProjectionIsPlayedInRoomRepository;
9
10import com.example.moviezone.model.procedures.FilmsReturnTable;
11
12import com.example.moviezone.service.*;
13import org.springframework.format.annotation.DateTimeFormat;
14import org.springframework.stereotype.Controller;
15import org.springframework.ui.Model;
16import org.springframework.web.bind.annotation.*;
17
18import javax.servlet.http.HttpSession;
19import java.time.LocalDate;
20import java.util.LinkedList;
21import java.util.List;
22import java.util.stream.Collectors;
23
24@Controller
25@RequestMapping({"/","/home"})
26public class HomeController {
27
28private final FilmService filmService;
29private final UserService userService;
30private final ProjectionService projectionService;
31private final EventService eventService;
32private final TicketService ticketService;
33private final WorkerService workerService;
34private final CustomerRatesFilmService customerRatesFilmService;
35private final CinemaService cinemaService;
36private final CinemaOrganizesEventService cinemaOrganizesEventService;
37private final CinemaPlaysFilmService cinemaPlaysFilmService;
38private final ProjectionIsPlayedInRoomRepository projectionIsPlayedInRoomRepository;
39
40
41 public HomeController(FilmService filmService, UserService userService, ProjectionService projectionService, EventService eventService, TicketService ticketService, WorkerService workerService, CustomerRatesFilmService customerRatesFilmService, CinemaService cinemaService, CinemaOrganizesEventService cinemaOrganizesEventService, CinemaPlaysFilmService cinemaPlaysFilmService, ProjectionIsPlayedInRoomRepository projectionIsPlayedInRoomRepository) {
42
43 this.filmService = filmService;
44 this.userService = userService;
45 this.projectionService = projectionService;
46 this.eventService = eventService;
47 this.ticketService = ticketService;
48 this.workerService = workerService;
49 this.customerRatesFilmService = customerRatesFilmService;
50 this.cinemaService = cinemaService;
51 this.cinemaOrganizesEventService = cinemaOrganizesEventService;
52 this.cinemaPlaysFilmService = cinemaPlaysFilmService;
53 this.projectionIsPlayedInRoomRepository = projectionIsPlayedInRoomRepository;
54 }
55
56 @GetMapping
57 public String getHomePage(Model model) {
58 List<Film> films=filmService.findAllFilms();
59 films=films.stream().limit(5).collect(Collectors.toList());
60 List <Event> events=eventService.findAllEvents().stream().limit(5).collect(Collectors.toList());
61 model.addAttribute("films", films);
62 model.addAttribute("events",events);
63 model.addAttribute("bodyContent", "home");
64
65 return "master-template";
66 }
67 @GetMapping("/getFilm/{id}")
68 public String getFilm(@PathVariable Long id, Model model) {
69 Film film=filmService.getFilmById(id).get();
70 model.addAttribute("film", film);
71 List<String> genres= List.of(film.getGenre().split(","));
72 double r=customerRatesFilmService.avg_rating(film.getId_film());
73 model.addAttribute("rating",r);
74 model.addAttribute("genres", genres);
75 model.addAttribute("bodyContent", "film");
76
77 return "master-template";
78 }
79
80 @GetMapping("/login")
81 public String getLoginPage(Model model)
82 {
83 model.addAttribute("bodyContent", "login");
84 return "master-template";
85 }
86
87 @GetMapping("/register")
88 public String getRegisterPage(Model model)
89 {
90 model.addAttribute("bodyContent", "register");
91 return "master-template";
92 }
93
94 @PostMapping("/login")
95 public String login(@RequestParam String username,
96 @RequestParam String password,Model model, HttpSession session)
97 {
98// User user = null;
99// try {
100 User user=userService.login(username,password);
101 System.out.println(user.getFirst_name());
102// session.setAttribute("sessionUser",user);
103// model.addAttribute("user",user);
104 return "redirect:/home";
105//
106// }catch (UserNotFoundException e)
107// {
108// model.addAttribute("hasError", true);
109// model.addAttribute("error", e.getMessage());
110// return "login";
111// }
112
113 }
114
115 @PostMapping()
116 public String register(@RequestParam String username,
117 @RequestParam String first_name,
118 @RequestParam String last_name,
119 @RequestParam String password,
120 @RequestParam String repeatedPassword,
121 @RequestParam String email,
122 @RequestParam String number,
123 @RequestParam Role role)
124 {
125 try {
126 userService.register(first_name,last_name,username,email,number,password,role);
127 return "redirect:/login";
128 }catch (PasswordsDoNotMatchException exception)
129 {
130 return "redirect:/register?error=" + exception.getMessage();
131 }
132
133 }
134 @GetMapping("/films")
135 public String getFilmsPage(Model model){
136 model.addAttribute("cinemas",cinemaService.findAllCinemas());
137 List<FilmsReturnTable> pom=new LinkedList<>();
138 model.addAttribute("films",pom);
139 boolean h=pom.isEmpty();
140 List<FilmsReturnTable> help=filmService.getFilmsFromCinema(2);
141 model.addAttribute("bodyContent","films");
142 return "master-template";
143 }
144
145 public String getFilmsPage1(Model model,Integer id_cinema){
146 model.addAttribute("cinemas",cinemaService.findAllCinemas());
147 if (id_cinema!=null) {
148 model.addAttribute("films",filmService.getFilmsFromCinema(id_cinema.intValue()));
149 }else{
150 List<FilmsReturnTable> pom=new LinkedList<>();
151 model.addAttribute("films",pom);
152 }
153
154 model.addAttribute("bodyContent","films");
155 return "master-template";
156 }
157 @PostMapping("/getFilmsFromCinema")
158 public String getFilmsFromCinema(@RequestParam Integer cinema, Model model){
159 return getFilmsPage1(model,cinema);
160 }
161 @GetMapping("/projections")
162 public String getProjectionsPage(Model model)
163 {
164 model.addAttribute("projections",projectionService.findAllProjections());
165 model.addAttribute("bodyContent","projections");
166 return "master-template";
167 }
168 @GetMapping("/events")
169 public String getEventsPage(Model model)
170 {
171 model.addAttribute("events",eventService.findAllEvents());
172 model.addAttribute("bodyContent","events");
173 return "master-template";
174 }
175 @GetMapping("/myTickets")
176 public String getMyTicketsPage(Model model,HttpSession session)
177 {
178 model.addAttribute("tickets",ticketService.findAllByCustomer((Customer) session.getAttribute("user")));
179 model.addAttribute("bodyContent","myTickets");
180 return "master-template";
181 }
182 @GetMapping("/addProjection")
183 public String getAddProjectionPage(Model model)
184 {
185 model.addAttribute("films",filmService.findAllFilms());
186 model.addAttribute("bodyContent","addProjection");
187 return "master-template";
188 }
189
190
191 @GetMapping("/addEvent")
192 public String getAddEventPage(Model model)
193 {
194 model.addAttribute("bodyContent","addEvent");
195 return "master-template";
196 }
197 @GetMapping("/addFilm")
198 public String getAddFilmPage(Model model)
199 {
200 model.addAttribute("bodyContent","addFilm");
201 return "master-template";
202 }
203
204 @PostMapping("/addP")
205 public String saveProjection(@RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate date_time_start,
206 @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate date_time_end,
207 @RequestParam String type_of_technology,
208 @RequestParam Integer id_film)
209 {
210 projectionService.save(date_time_start,date_time_end,type_of_technology,id_film);
211 return "redirect:/home";
212 }
213 @PostMapping("/addE")
214 public String saveEvent(@RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate start_date,
215 @RequestParam String theme,
216 @RequestParam String duration,
217 @RequestParam String img_url,
218 @RequestParam String repeating)
219 {
220 eventService.save(start_date,theme,duration,repeating,img_url);
221 return "redirect:/home";
222 }
223 @PostMapping("/addF")
224 public String saveFilm(
225 @RequestParam String name,
226 @RequestParam Integer duration,
227 @RequestParam String actors,
228 @RequestParam String genre,
229 @RequestParam String age_category,
230 @RequestParam String url,
231 @RequestParam String director,
232 @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate start_date,
233 @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate end_date
234 )
235 {
236 filmService.save(name,duration,actors,genre,age_category,url,director,start_date,end_date);
237 return "redirect:/home";
238 }
239
240 @GetMapping("/workers")
241 public String getWorkersPage(Model model)
242 {
243 model.addAttribute("workers",workerService.findAllWorkers());
244 model.addAttribute("bodyContent", "workers");
245 return "master-template";
246 }
247
248 @GetMapping("/addEventToCinema")
249 public String getCinemaOrganizesEventPage(Model model)
250 {
251 model.addAttribute("cinemas",cinemaService.findAllCinemas());
252 model.addAttribute("events",eventService.findAllEvents());
253 model.addAttribute("bodyContent","addEventToCinema");
254 return "master-template";
255 }
256 @PostMapping("/addCinemaOrganizesEvent")
257 public String saveCinemaOrganizesEvent(@RequestParam Integer id_cinema,
258 @RequestParam Integer id_event)
259 {
260
261 cinemaOrganizesEventService.save(id_cinema,id_event);
262 return "redirect:/home";
263 }
264 @GetMapping("/addFilmToCinema")
265 public String getCinemaPlaysFilmPage(Model model)
266 {
267 model.addAttribute("cinemas",cinemaService.findAllCinemas());
268 model.addAttribute("films",filmService.findAllFilms());
269 model.addAttribute("bodyContent","addFilmToCinema");
270 return "master-template";
271 }
272 @PostMapping("/addCinemaPlaysFilm")
273 public String saveCinemaPlaysFilm(@RequestParam Integer id_cinema,
274 @RequestParam Integer id_film)
275 {
276 cinemaPlaysFilmService.save(id_cinema,id_film);
277 return "redirect:/home";
278 }
279
280 @GetMapping("/getProjection/{id}")
281 public String getProjection(@PathVariable Integer id_projection,Model model)
282 {
283 List<Projection_Room> projectionRooms = null;
284 Projection projection=projectionService.findById(id_projection);
285
286
287 List<ProjectionIsPlayedInRoom> p= projectionIsPlayedInRoomRepository.findAllById_projection(id_projection);
288
289 model.addAttribute("projection",projection);
290 model.addAttribute("p_rooms",projectionRooms);
291 model.addAttribute("bodyContent","projectionDetails");
292 return "master-template";
293 }
294
295 @PostMapping("/makeReservation")
296 public String createTicketForReservation()
297 {
298 return "redirect:/myTickets";
299 }
300
301}
Note: See TracBrowser for help on using the repository browser.