summaryrefslogtreecommitdiff
path: root/src/main/java/com/mavlushechka/studentdatabase/controller/StudentController.java
blob: 76bc0f215a36b26364082c5355477943b1a99375 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package com.mavlushechka.studentdatabase.controller;

import com.mavlushechka.studentdatabase.domain.Student;
import com.mavlushechka.studentdatabase.domain.User;
import com.mavlushechka.studentdatabase.repository.StudentRepository;
import com.mavlushechka.studentdatabase.repository.UserRepository;
import org.bson.BsonBinarySubType;
import org.bson.types.Binary;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

@Controller
public class StudentController {
    @Autowired
    private UserRepository userRepository;
    @Autowired
    private StudentRepository studentRepository;

    @GetMapping("/information/student")
    public String getUserAndStudent(Authentication authentication, Map<String, Object> model) {
        User user = userRepository.findByUsername(authentication.getName());
        Student student = studentRepository.findById(user.getId()).orElse(new Student());
        model.put("user", user);
        model.put("student", student);
        return "information/student";
    }

    @PostMapping("/information/student/save")
    public String saveStudent(String id, String diploma, long telephoneNumber, String religion, String car, String house, MultipartFile photo) throws IOException {
        Student student = new Student(id, diploma, telephoneNumber, religion, car, house, new Binary(BsonBinarySubType.BINARY, photo.getBytes()));
        studentRepository.save(student);
        return "redirect:/";
    }
}