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

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

Procedure Returns Table Fix

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