source: src/main/java/com/example/moviezone/web/HomeController.java@ 3242ef4

Last change on this file since 3242ef4 was 03fd098, checked in by milamihajlovska <mila.mihajlovska01@…>, 22 months ago

cinemaPlaysFilm implemented

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