source: src/main/java/project/educatum/web/TeacherController.java

Last change on this file was d3cf3a1, checked in by Marija Micevska <marija_micevska@…>, 2 years ago

Initial commit

  • Property mode set to 100644
File size: 12.7 KB
Line 
1package project.educatum.web;
2
3import org.springframework.format.annotation.DateTimeFormat;
4import org.springframework.http.HttpStatus;
5import org.springframework.security.core.userdetails.UserDetails;
6import org.springframework.stereotype.Controller;
7import org.springframework.ui.Model;
8import org.springframework.web.bind.annotation.*;
9import org.springframework.web.server.ResponseStatusException;
10import project.educatum.model.*;
11import project.educatum.model.Class;
12import project.educatum.model.exceptions.SubjectNotFoundException;
13import project.educatum.service.*;
14
15import javax.servlet.http.HttpServletRequest;
16import java.time.LocalDateTime;
17import java.util.ArrayList;
18import java.util.List;
19import java.util.Objects;
20import java.util.Optional;
21
22@Controller
23@RequestMapping(path = "/teachers", method = {RequestMethod.POST, RequestMethod.DELETE, RequestMethod.GET})
24public class TeacherController {
25
26 private final TeacherService teacherService;
27 private final SubjectService subjectService;
28 private final TeacherSubjectService teacherSubjectService;
29 private final StudentService studentService;
30 private final PaymentService paymentService;
31 private final TeacherStudentService teacherStudentService;
32 private final ClassService classService;
33
34 public TeacherController(TeacherService teacherService, SubjectService subjectService, TeacherSubjectService teacherSubjectService, StudentService studentService, PaymentService paymentService, TeacherStudentService teacherStudentService, ClassService classService) {
35 this.teacherService = teacherService;
36 this.subjectService = subjectService;
37 this.teacherSubjectService = teacherSubjectService;
38 this.studentService = studentService;
39 this.paymentService = paymentService;
40 this.teacherStudentService = teacherStudentService;
41 this.classService = classService;
42 }
43
44 @GetMapping
45 public String getAllTeachers(Model model) {
46 model.addAttribute("teachers", teacherService.findAll());
47 return "teachers";
48 }
49
50 @GetMapping("/allStudents")
51 public String getAllStudentsByTeacher(@RequestParam(required = false) String ime, Model model, HttpServletRequest request) {
52 List<Student> students = new ArrayList<>();
53 UserDetails user = (UserDetails) request.getSession().getAttribute("user");
54 if (user != null) {
55 Teacher teacher = teacherService.findByEmail(user.getUsername());
56 if (ime == null) {
57 students = teacherService.getStudentsByTeacher(teacher.getId());
58 } else {
59 students = this.studentService.findAllByNameLike(ime, teacherService.getStudentsByTeacher(teacher.getId()));
60 }
61 model.addAttribute("teacher", teacherService.findById(teacher.getId()));
62 model.addAttribute("students", students);
63 return "evidencija";
64 } else return "redirect:/home";
65 }
66
67 @PostMapping("/delete/{id}")
68 public String deleteTeacher(@PathVariable String id) {
69 teacherService.delete(Integer.parseInt(id));
70 return "redirect:/admin/allTeachers";
71 }
72
73
74 @PostMapping("/payments/{id}")
75 public String paymentForStudent(@PathVariable String id, Model model, HttpServletRequest request) {
76 UserDetails user = (UserDetails) request.getSession().getAttribute("user");
77 if (user != null) {
78 Teacher teacher = teacherService.findByEmail(user.getUsername());
79 Integer owes = paymentService.studentTeacherLoan(Integer.valueOf(id), teacher.getId());
80 model.addAttribute("owes", owes);
81 Integer numScheduledClasses = teacherStudentService.find(teacher.getId(), Integer.valueOf(id)).getNumScheduledClasses();
82 model.addAttribute("numScheduledClasses", numScheduledClasses);
83 Integer priceByClass = teacherStudentService.find(teacher.getId(), Integer.valueOf(id)).getPriceByClass();
84 model.addAttribute("priceByClass", priceByClass);
85 model.addAttribute("classes", teacherService.getClassesByTeacher(teacher.getId()));
86
87 Integer numListenedClasses = paymentService.numListenedClasses(Integer.valueOf(id), teacher.getId());
88 model.addAttribute("numListenedClasses", numListenedClasses);
89 model.addAttribute("student", studentService.findById(Integer.valueOf(id)));
90 return "payment";
91 } else return "redirect:/home";
92 }
93
94 @PostMapping("/updatePayment")
95 public String updatePaymentForStudent(@RequestParam String studentID, Model model, HttpServletRequest request,
96 @RequestParam(required = false) String price, @RequestParam String classID) {
97
98 UserDetails user = (UserDetails) request.getSession().getAttribute("user");
99 if (user != null) {
100 Teacher teacher = teacherService.findByEmail(user.getUsername());
101
102
103 if (Integer.parseInt(price) != 0) {
104 paymentService.addPayment(teacher.getId(), Integer.valueOf(price), Integer.valueOf(classID), Integer.valueOf(studentID));
105 }
106
107 Integer owes = paymentService.studentTeacherLoan(Integer.valueOf(studentID), teacher.getId());
108 model.addAttribute("owes", owes);
109 Integer numScheduledClasses = teacherStudentService.find(teacher.getId(), Integer.valueOf(studentID)).getNumScheduledClasses();
110 model.addAttribute("numScheduledClasses", numScheduledClasses);
111 Integer priceByClass = teacherStudentService.find(teacher.getId(), Integer.valueOf(studentID)).getPriceByClass();
112 model.addAttribute("priceByClass", priceByClass);
113 Integer numListenedClasses = paymentService.numListenedClasses(Integer.valueOf(studentID), teacher.getId());
114 model.addAttribute("numListenedClasses", numListenedClasses);
115 model.addAttribute("classes", teacherService.getClassesByTeacher(teacher.getId()));
116 model.addAttribute("student", studentService.findById(Integer.valueOf(studentID)));
117 return "payment";
118 } else return "redirect:/home";
119 }
120
121
122 @GetMapping("/allClasses")
123 public String timetable(Model model, HttpServletRequest request) {
124 UserDetails user = (UserDetails) request.getSession().getAttribute("user");
125 if (user != null) {
126 Teacher teacher = teacherService.findByEmail(user.getUsername());
127 List<Class> classes = teacherService.getClassesByTeacher(teacher.getId());
128 model.addAttribute("teacher", teacherService.findById(teacher.getId()));
129 model.addAttribute("classes", classes);
130 return "raspored";
131 } else return "redirect:/home";
132 }
133
134
135 @GetMapping("/allSubjects")
136 public String getAllSubjectsByTeacher(@RequestParam(required = false) String ime, Model model, HttpServletRequest request) {
137 List<Subject> subjects = new ArrayList<>();
138 UserDetails user = (UserDetails) request.getSession().getAttribute("user");
139 if (user != null) {
140 Teacher teacher = teacherService.findByEmail(user.getUsername());
141 if (ime == null) {
142 subjects = teacherService.getSubjectsByTeacher(teacher.getId());
143 } else {
144 subjects = this.subjectService.findAllByNameAndTeacherLike(ime, teacherService.getSubjectsByTeacher(teacher.getId()));
145 }
146 model.addAttribute("teacher", teacherService.findById(teacher.getId()));
147 model.addAttribute("subjects", subjects);
148 model.addAttribute("allSubjects", subjectService.findAll());
149 return "subjectsByTeacher";
150 } else return "redirect:/home";
151 }
152
153
154 @PostMapping("/addStudentForm")
155 public String addStudentForm() {
156 return "addNewStudent";
157 }
158
159 @PostMapping("/addSubjectForm")
160 public String addSubjectForm(Model model) {
161 model.addAttribute("subjects", subjectService.findAll());
162 return "addNewSubject";
163 }
164
165 @PostMapping("/addStudent")
166 public String addStudent(@RequestParam String price, @RequestParam String numClasses,
167 @RequestParam String email, HttpServletRequest request) {
168 Student ucenik = studentService.findByEmail(email);
169 UserDetails user = (UserDetails) request.getSession().getAttribute("user");
170 if (user != null) {
171 Teacher teacher = teacherService.findByEmail(user.getUsername());
172
173 teacherService.addStudent(teacher.getId(), ucenik.getId(), Integer.valueOf(price), Integer.valueOf(numClasses));
174 return "redirect:/teachers/allStudents";
175 } else return "redirect:/home";
176 }
177
178
179 @PostMapping("/addSubject")
180 public String addSubject(Model model, HttpServletRequest request,
181 @RequestParam String subjectId,
182 @RequestParam String desc) {
183
184 UserDetails user = (UserDetails) request.getSession().getAttribute("user");
185 if (user != null) {
186 Teacher teacher = teacherService.findByEmail(user.getUsername());
187 teacherService.addSubject(teacher.getId(), Integer.valueOf(subjectId), desc);
188
189 return "redirect:/teachers/allSubjects";
190 } else return "redirect:/home";
191 }
192
193 @PostMapping("/teachesSubject")
194 public String teachesSubject(@RequestParam String tema, @RequestParam String subjectID, HttpServletRequest request) {
195 Optional<Subject> p = subjectService.findById(Integer.valueOf(subjectID));
196 UserDetails user = (UserDetails) request.getSession().getAttribute("user");
197 if (user != null) {
198 String username = user.getUsername();
199 Teacher n = teacherService.findByEmail(username);
200 teacherSubjectService.addSubject(n.getId(), Integer.valueOf(subjectID), tema);
201 return "redirect:/home/document";
202 } else return "redirect:/home";
203 }
204
205
206 @PostMapping("/addClass")
207 public String addClass(
208 @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime date,
209 @RequestParam String desc,
210 @RequestParam String ime,
211 HttpServletRequest request) {
212 UserDetails user = (UserDetails) request.getSession().getAttribute("user");
213 if (user != null) {
214 Teacher t = teacherService.findByEmail(user.getUsername());
215 Subject s = subjectService.findByName(ime);
216 if (s != null) {
217 classService.addClass(date, desc, t.getId(), s.getId());
218 return "redirect:/teachers/allClasses";
219 } else
220 return "redirect:/teachers/allClasses";
221 } else
222 return "redirect:/home";
223
224 }
225
226 @PostMapping("/showProfileTeacher/{id}")
227 public String showProfileTeacher(@PathVariable String id, Model model) {
228 Teacher teacher = teacherService.findById(Integer.valueOf(id));
229 model.addAttribute("teacher", teacher);
230 model.addAttribute("teacherRating",teacherService.getRatingForTeacher(Long.valueOf(teacher.getId())));
231
232 return "showProfileTeacher.html";
233 }
234
235 @PostMapping("/showProfileTeacher2/{id}")
236 public String showProfileTeacher2(@PathVariable String id, Model model) {
237 Teacher teacher = teacherService.findById(Integer.valueOf(id));
238 model.addAttribute("teacher", teacher);
239 model.addAttribute("teacherRating",teacherService.getRatingForTeacher(Long.valueOf(teacher.getId())));
240 return "showProfileTeacher2.html";
241 }
242
243 @PostMapping("/showProfile")
244 public String showProfile(HttpServletRequest request, Model model) {
245 UserDetails user = (UserDetails) request.getSession().getAttribute("user");
246 Teacher teacher = teacherService.findByEmail(user.getUsername());
247 model.addAttribute("teacher", teacher);
248 return "showProfile.html";
249 }
250
251 @GetMapping("/{id}/edit")
252 public String showEdit(@PathVariable Integer id, Model model) {
253 Teacher teacher = this.teacherService.findById(id);
254 model.addAttribute("teacher", teacher);
255 return "showProfile";
256 }
257
258 @PostMapping("/{id}")
259 public String editTeacher(@PathVariable Integer id,
260 @RequestParam String name,
261 @RequestParam String surname,
262 @RequestParam String description,
263 @RequestParam String email,
264 @RequestParam String telephoneNumber,
265 HttpServletRequest request) {
266 this.teacherService.edit(id, name, surname,description,email,telephoneNumber);
267 UserDetails user = (UserDetails) request.getSession().getAttribute("user");
268 if(!Objects.equals(email, user.getUsername())){
269 return "redirect:/login";
270 }
271 return "redirect:/teachers/showProfile";
272 }
273}
Note: See TracBrowser for help on using the repository browser.