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

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

added listing workers

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