File size: 3,167 Bytes
03549e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package com.cs102.attendance.service;

import com.cs102.attendance.entity.Student;
import com.cs102.attendance.entity.FaceData;
import com.cs102.attendance.repository.StudentRepository;
import com.cs102.attendance.repository.FaceDataRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;
import java.util.Optional;

@Service
@Transactional
public class StudentService {
    
    @Autowired
    private StudentRepository studentRepository;
    
    @Autowired
    private FaceDataRepository faceDataRepository;
    
    public Student enrol(String code, String name, String className, String studentGroup, String email, String phone) {
        Student student = new Student();
        student.setCode(code);
        student.setName(name);
        student.setClassName(className);
        student.setStudentGroup(studentGroup);
        student.setEmail(email);
        student.setPhone(phone);
        
        return studentRepository.save(student);
    }
    
    public Student updateProfile(UUID studentId, String name, String className, String studentGroup, String email, String phone) {
        Optional<Student> optionalStudent = studentRepository.findById(studentId);
        if (optionalStudent.isPresent()) {
            Student student = optionalStudent.get();
            if (name != null) student.setName(name);
            if (className != null) student.setClassName(className);
            if (studentGroup != null) student.setStudentGroup(studentGroup);
            if (email != null) student.setEmail(email);
            if (phone != null) student.setPhone(phone);
            
            return studentRepository.save(student);
        }
        throw new RuntimeException("Student not found with id: " + studentId);
    }
    
    public FaceData uploadFaceImage(UUID studentId, String imageUrl) {
        Optional<Student> optionalStudent = studentRepository.findById(studentId);
        if (optionalStudent.isPresent()) {
            Student student = optionalStudent.get();
            FaceData faceData = new FaceData(student, imageUrl);
            return faceDataRepository.save(faceData);
        }
        throw new RuntimeException("Student not found with id: " + studentId);
    }
    
    public FaceData uploadFaceImage(UUID studentId, byte[] imageData) {
        Optional<Student> optionalStudent = studentRepository.findById(studentId);
        if (optionalStudent.isPresent()) {
            Student student = optionalStudent.get();
            FaceData faceData = new FaceData(student, imageData);
            return faceDataRepository.save(faceData);
        }
        throw new RuntimeException("Student not found with id: " + studentId);
    }
    
    public List<Student> getAllStudents() {
        return studentRepository.findAll();
    }
    
    public Optional<Student> getStudentById(UUID id) {
        return studentRepository.findById(id);
    }
    
    public void deleteStudent(UUID id) {
        studentRepository.deleteById(id);
    }
}