source: src/main/java/project/educatum/web/StudentController.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: 9.6 KB
Line 
1package project.educatum.web;
2
3import org.springframework.security.core.userdetails.UserDetails;
4import org.springframework.stereotype.Controller;
5import org.springframework.ui.Model;
6import org.springframework.web.bind.annotation.*;
7import project.educatum.model.Class;
8import project.educatum.model.Homework;
9import project.educatum.model.Student;
10import project.educatum.model.Teacher;
11import project.educatum.model.relations.StudentHomeworkRelation;
12import project.educatum.model.relations.TeacherStudentRelation;
13import project.educatum.service.*;
14
15import javax.servlet.http.HttpServletRequest;
16import java.time.LocalDate;
17import java.util.ArrayList;
18import java.util.List;
19import java.util.Objects;
20
21@Controller
22@RequestMapping(path = "/students", method = {RequestMethod.POST, RequestMethod.DELETE, RequestMethod.GET})
23public class StudentController {
24
25 private final StudentService studentService;
26 private final SubjectService subjectService;
27 private final InterestService interestService;
28 private final TeacherService teacherService;
29 private final PaymentService paymentService;
30 private final TeacherStudentService teacherStudentService;
31 private final HomeworkService homeworkService;
32 private final StudentHomeworkService studentHomeworkService;
33 private final ClassService classService;
34
35 public StudentController(StudentService studentService, SubjectService subjectService, InterestService interestService, TeacherService teacherService, PaymentService paymentService, TeacherStudentService teacherStudentService, HomeworkService homeworkService, StudentHomeworkService studentHomeworkService, ClassService classService) {
36 this.studentService = studentService;
37 this.subjectService = subjectService;
38 this.interestService = interestService;
39 this.teacherService = teacherService;
40 this.paymentService = paymentService;
41 this.teacherStudentService = teacherStudentService;
42 this.homeworkService = homeworkService;
43 this.studentHomeworkService = studentHomeworkService;
44 this.classService = classService;
45 }
46
47 @GetMapping("/listSubjectsTeachers")
48 public String listSubjectsTeachers(Model model, @RequestParam(required = false) String subjectID) {
49 model.addAttribute("subject", subjectService.findById(Integer.valueOf(subjectID)));
50 model.addAttribute("subjects", subjectService.findAll());
51 model.addAttribute("teachers", teacherService.getAllTeachersBySubject(Integer.valueOf(subjectID)));
52 return "listSubTeachStudents";
53 }
54
55 @PostMapping("/teachers")
56 public String getAllTeachersBySubject(Model model, @RequestParam String subjectID) {
57 model.addAttribute("subjects", subjectService.findAll());
58 model.addAttribute("teachers", teacherService.getAllTeachersBySubject(Integer.valueOf(subjectID)));
59 return "listSubTeachStudents";
60 }
61
62 @PostMapping("/delete/{id}")
63 public String deleteStudent(@PathVariable String id) {
64 studentService.delete(Integer.parseInt(id));
65 return "redirect:/admin/allStudents";
66 }
67
68 @PostMapping("/slusanje")
69 public String slusanje(Model model, HttpServletRequest request) {
70 return "";
71 }
72
73 @PostMapping("/zaintesesiranZaPredmet")
74 public String zaintesesiranZaPredmet(@RequestParam String subjectID,
75 HttpServletRequest request) {
76 UserDetails user = (UserDetails) request.getSession().getAttribute("user");
77 LocalDate localDate = LocalDate.now();
78 String username = user.getUsername();
79 Student u = studentService.findByEmail(username);
80 interestService.addSubjectStudent(Integer.valueOf(subjectID), u.getId(), localDate);
81 return "redirect:/zaintesesiranZaPredmet";
82 }
83
84 @PostMapping("/addListening")
85 public String addListening(Model model, @RequestParam String studentID,
86 @RequestParam(required = false) String price,
87 @RequestParam String classID, HttpServletRequest request) {
88 UserDetails user = (UserDetails) request.getSession().getAttribute("user");
89 Teacher teacher = teacherService.findByEmail(user.getUsername());
90 studentService.addListening(Integer.valueOf(studentID), Integer.valueOf(classID), teacher.getId());
91
92 if (price != null)
93 paymentService.addPayment(teacher.getId(), Integer.valueOf(price), Integer.valueOf(classID), Integer.valueOf(studentID));
94
95
96 Integer owes = paymentService.studentTeacherLoan(Integer.valueOf(studentID), teacher.getId());
97 model.addAttribute("owes", owes);
98 Integer numScheduledClasses = teacherStudentService.find(teacher.getId(), Integer.valueOf(studentID)).getNumScheduledClasses();
99 model.addAttribute("numScheduledClasses", numScheduledClasses);
100 Integer priceByClass = teacherStudentService.find(teacher.getId(), Integer.valueOf(studentID)).getPriceByClass();
101 model.addAttribute("priceByClass", priceByClass);
102 Integer numListenedClasses = paymentService.numListenedClasses(Integer.valueOf(studentID), teacher.getId());
103 model.addAttribute("numListenedClasses", numListenedClasses);
104 model.addAttribute("classes", teacherService.getClassesByTeacher(teacher.getId()));
105 model.addAttribute("student", studentService.findById(Integer.valueOf(studentID)));
106 return "payment";
107 }
108
109
110 @PostMapping("/showProfileStudent")
111 public String showProfileStudent(HttpServletRequest request, Model model) {
112 UserDetails user = (UserDetails) request.getSession().getAttribute("user");
113 Student student = studentService.findByEmail(user.getUsername());
114 model.addAttribute("student", student);
115 return "showProfileStudent.html";
116 }
117
118 @PostMapping("/homeWork")
119 public String getAllHomeworks(@RequestParam(required = false) String desc, Model model, HttpServletRequest request) {
120 List<Homework> homeworks;
121 if (desc == null) {
122 homeworks = homeworkService.findAll();
123 } else {
124 homeworks = homeworkService.findAllByDescriptionLike(desc);
125 }
126 UserDetails user = (UserDetails) request.getSession().getAttribute("user");
127 Student student = studentService.findByEmail(user.getUsername());
128 model.addAttribute("homeworks", homeworks);
129 List<TeacherStudentRelation> teacherStudentRelationList = teacherStudentService.findAll();
130 List<Teacher> teachers = new ArrayList<>();
131 for (TeacherStudentRelation t : teacherStudentRelationList) {
132 if (t.getId().getStudentID().equals(student.getId())) {
133 teachers.add(teacherService.findById(t.getId().getTeacherID()));
134 }
135 }
136 model.addAttribute("teachersByStudent", teachers);
137 return "homeworksList";
138 }
139
140 @PostMapping("/add/homework")
141 public String create(@RequestParam String desc, @RequestParam String topic, @RequestParam String teacher) {
142 Class aClass = classService.findByTopic(topic);
143 homeworkService.create(desc, teacherService.findById(Integer.valueOf(teacher)).getId(), aClass.getId());
144 return "redirect:/students/homeWork";
145 }
146
147 @PostMapping("/done/{id}")
148 public String doneHomework(@PathVariable String id) {
149 StudentHomeworkRelation h = studentHomeworkService.findById(Integer.valueOf(id));
150 if (h != null) {
151 studentHomeworkService.updateDone(Integer.valueOf(id));
152 }
153
154 return "redirect:/students/homeWork";
155 }
156
157 @PostMapping("/rateTeacher/{id}")
158 public String rateTeacher(@PathVariable String id, Model model, @RequestParam String rating, HttpServletRequest request) {
159 Float r = Float.valueOf(rating);
160 Teacher t = teacherService.findById(Integer.valueOf(id));
161 UserDetails user = (UserDetails) request.getSession().getAttribute("user");
162 Student s = studentService.findByEmail(user.getUsername());
163 if (studentService.rateTeacher(t, s, r)) {
164 model.addAttribute("hasError", false);
165 model.addAttribute("message", "Thank you for the rating!");
166 model.addAttribute("teacher", t);
167 model.addAttribute("teacherRating", teacherService.getRatingForTeacher(Long.valueOf(t.getId())));
168
169 return "showProfileTeacher2.html";
170 } else {
171 model.addAttribute("hasError", true);
172 model.addAttribute("message", "You don't have a permission to do this action!");
173 model.addAttribute("teacher", t);
174 model.addAttribute("teacherRating", teacherService.getRatingForTeacher(Long.valueOf(t.getId())));
175
176 return "showProfileTeacher2.html";
177 }
178 }
179
180 @GetMapping("/{id}/edit")
181 public String showEdit(@PathVariable Integer id, Model model) {
182 Student student = this.studentService.findById(id);
183 model.addAttribute("student", student);
184 return "showProfileStudent";
185 }
186
187 @PostMapping("/{id}")
188 public String editStudent(@PathVariable Integer id,
189 @RequestParam String name,
190 @RequestParam String surname,
191 @RequestParam String description,
192 @RequestParam String email,
193 @RequestParam String telephoneNumber,
194 HttpServletRequest request) {
195 this.studentService.edit(id, name, surname,description,email,telephoneNumber);
196 UserDetails user = (UserDetails) request.getSession().getAttribute("user");
197 if(!Objects.equals(email, user.getUsername())){
198 return "redirect:/login";
199 }
200 return "redirect:/students/showProfileStudent";
201 }
202
203}
Note: See TracBrowser for help on using the repository browser.