source: src/main/java/project/educatum/web/HomeController.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: 7.8 KB
Line 
1package project.educatum.web;
2
3import net.bytebuddy.utility.RandomString;
4import org.springframework.security.core.userdetails.UserDetails;
5import org.springframework.security.crypto.password.PasswordEncoder;
6import org.springframework.stereotype.Controller;
7import org.springframework.ui.Model;
8import org.springframework.web.bind.annotation.GetMapping;
9import org.springframework.web.bind.annotation.PostMapping;
10import org.springframework.web.bind.annotation.RequestMapping;
11import org.springframework.web.bind.annotation.RequestParam;
12import org.springframework.web.multipart.MultipartFile;
13import org.springframework.web.servlet.mvc.support.RedirectAttributes;
14import project.educatum.model.Admin;
15import project.educatum.model.Student;
16import project.educatum.model.Teacher;
17import project.educatum.model.Subject;
18import project.educatum.service.*;
19
20import javax.servlet.http.HttpServletRequest;
21import java.io.IOException;
22import java.nio.file.Files;
23import java.nio.file.Path;
24import java.nio.file.Paths;
25import java.util.List;
26
27@Controller
28@RequestMapping("/home")
29public class HomeController {
30
31 private final SubjectService subjectService;
32 private final AdminService adminService;
33 private final TeacherService teacherService;
34 private final QualificationService qualificationService;
35 private final StudentService studentService;
36 private final EmailService emailService;
37 private final PasswordEncoder passwordEncoder;
38 // private static String UPLOADED_FOLDER = "C://Users//Acer//Desktop//kvalifikacii//";
39 private static String UPLOADED_FOLDER = "C://Users//User//OneDrive//Desktop//kvalifikacii//";
40
41 public HomeController(SubjectService subjectService, AdminService adminService, TeacherService teacherService, QualificationService qualificationService, StudentService studentService, EmailService emailService, PasswordEncoder passwordEncoder) {
42 this.subjectService = subjectService;
43 this.adminService = adminService;
44 this.teacherService = teacherService;
45 this.qualificationService = qualificationService;
46 this.studentService = studentService;
47 this.emailService = emailService;
48 this.passwordEncoder = passwordEncoder;
49 }
50
51 @GetMapping
52 public String getHomePage() {
53 return "/home";
54 }
55
56 @PostMapping("/forgotPassword")
57 public String resetPassword(@RequestParam String email, HttpServletRequest request) {
58
59 if (teacherService.findByEmail(email) != null || studentService.findByEmail(email) != null) {
60 String randomPassword = RandomString.make();
61 String text = String.format("Код за ресетирање на Вашата лозинка: " + randomPassword + "\nEDUCATUM");
62 emailService.sendMessage(email, "ЗАБОРАВЕНА ЛОЗИНКА", text);
63
64 if (teacherService.findByEmail(email) != null) {
65 Teacher teacher = teacherService.findByEmail(email);
66 teacher.setPassword(passwordEncoder.encode(randomPassword));
67 teacherService.save(teacher);
68 } else if (studentService.findByEmail(email) != null) {
69 Student student = studentService.findByEmail(email);
70 student.setPassword(passwordEncoder.encode(randomPassword));
71 studentService.save(student);
72 }
73
74 }
75 return "redirect:/home";
76 }
77
78 @GetMapping("/chooseSubject")
79 public String choose(@RequestParam(required = false) String ime, Model model) {
80 List<Subject> subjects;
81 if (ime == null) {
82 subjects = this.subjectService.findAll();
83 } else {
84 subjects = this.subjectService.findAllByNameLike(ime);
85 }
86 model.addAttribute("subjects", subjects);
87 return "chooseSubjects.html";
88 }
89
90 @GetMapping("/document")
91 public String document() {
92 return "prikaziDokument.html";
93 }
94
95 @PostMapping("/upload")
96 public String singleFileUpload(@RequestParam("file") MultipartFile file,
97 RedirectAttributes redirectAttributes, HttpServletRequest request) {
98
99 UserDetails user = (UserDetails) request.getSession().getAttribute("user");
100 Teacher teacher = teacherService.findByEmail(user.getUsername());
101
102 if (file.isEmpty()) {
103 redirectAttributes.addFlashAttribute("message", "Прикачувањето на документ е задолжително!");
104 return "redirect:/home/potvrda";
105 }
106
107 try {
108
109 byte[] bytes = file.getBytes();
110 Path path = Paths.get(UPLOADED_FOLDER + teacher.getId() + "_" + file.getOriginalFilename());
111 Files.write(path, bytes);
112 UserDetails userDetails = (UserDetails) request.getSession().getAttribute("user");
113 Teacher n = teacherService.findByEmail(userDetails.getUsername());
114 qualificationService.insert(String.valueOf(path), n.getId());
115 redirectAttributes.addFlashAttribute("message",
116 "Ви благодариме за регистрацијата!\n Администраторот треба да го провери прикачениот документ. Ќе добиете известување на e-mail кога Вашиот профил ќе биде активиран.");
117
118 } catch (IOException e) {
119 e.printStackTrace();
120 }
121
122 return "redirect:/home/potvrda";
123 }
124
125 @GetMapping("/potvrda")
126 public String potvrdaZaNastavnik() {
127 return "potvrdaNastavnik.html";
128 }
129
130 @GetMapping("/add")
131 public String addSubject(Model model) {
132 List<Admin> adminList = this.adminService.listAll();
133 model.addAttribute("adminList", adminList);
134 return "dodadiForma.html";
135 }
136
137
138 @PostMapping("/chooseSubject")
139 public String createSubject(Model model, @RequestParam(required = false) String name, @RequestParam(required = false) String ime) {
140 this.subjectService.create(name);
141 List<Subject> subjects;
142 if (ime == null) {
143 subjects = this.subjectService.findAll();
144 } else {
145 subjects = this.subjectService.findAllByNameLike(ime);
146 }
147 model.addAttribute("subjects", subjects);
148 return "chooseSubjects.html";
149 }
150
151 @GetMapping("/listenSubject")
152 public String listenSubject(Model model, @RequestParam(required = false) String subject,
153 HttpServletRequest request) {
154 if (subject == null) {
155 return "redirect:/zainteresiran?error=Ве%20молиме%20изберете%20предмет";
156 }
157 UserDetails user = (UserDetails) request.getSession().getAttribute("user");
158 Student ucenik = studentService.findByEmail(user.getUsername());
159 studentService.interestedIn(Integer.valueOf(subject), ucenik.getId());
160 model.addAttribute("subjects", subjectService.findAll());
161 model.addAttribute("teachers", teacherService.getAllTeachersBySubject(Integer.valueOf(subject)));
162
163 return "listenSubject";
164 }
165
166 @GetMapping("/zainteresiran")
167 public String interested(Model model, @RequestParam(required = false) String search) {
168 List<Subject> subjects;
169 if (search == null) {
170 subjects = this.subjectService.findAll();
171 } else {
172 subjects = this.subjectService.findAllByNameLike(search);
173 }
174 model.addAttribute("subjects", subjects);
175 return "interested";
176 }
177
178 @PostMapping("/showSubjects")
179 public String getAllTeachersBySubject(Model model, @RequestParam(required = false) String subjectID) {
180 model.addAttribute("subject", subjectService.findById(Integer.valueOf(subjectID)));
181 model.addAttribute("subjects", subjectService.findAll());
182 model.addAttribute("teachers", teacherService.getAllTeachersBySubject(Integer.valueOf(subjectID)));
183 return "listenSubject";
184 }
185
186}
Note: See TracBrowser for help on using the repository browser.