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

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

changes to register

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