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

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

Trying To Call A procedure from databasee

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