You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
53 lines
1.9 KiB
53 lines
1.9 KiB
package com.example.classroomattendance.service;
|
|
|
|
import com.example.classroomattendance.model.Student;
|
|
import com.example.classroomattendance.repository.StudentRepository;
|
|
import org.apache.poi.ss.usermodel.*;
|
|
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
import java.io.IOException;
|
|
import java.io.InputStream;
|
|
import java.util.*;
|
|
|
|
@Service
|
|
public class AttendanceService {
|
|
@Autowired
|
|
private StudentRepository studentRepository;
|
|
|
|
public List<Student> getAllStudents() {
|
|
return studentRepository.findAll();
|
|
}
|
|
|
|
public void addPoints(Long studentId, double points) {
|
|
Optional<Student> student = studentRepository.findById(studentId);
|
|
student.ifPresent(s -> {
|
|
s.setPoints(s.getPoints() + points);
|
|
studentRepository.save(s);
|
|
});
|
|
}
|
|
|
|
public Student pickRandomStudent() {
|
|
List<Student> students = getAllStudents();
|
|
Collections.shuffle(students, new Random(System.nanoTime()));
|
|
return students.get(0);
|
|
}
|
|
|
|
public void loadStudentsFromExcel(InputStream inputStream) throws IOException {
|
|
List<Student> students = new ArrayList<>();
|
|
try (Workbook workbook = new XSSFWorkbook(inputStream)) {
|
|
Sheet sheet = workbook.getSheetAt(0);
|
|
for (Row row : sheet) {
|
|
if (row.getRowNum() == 0) {
|
|
continue;
|
|
}
|
|
String name = row.getCell(0).getStringCellValue();
|
|
String studentId = row.getCell(1).getStringCellValue();
|
|
double points = row.getCell(2).getNumericCellValue();
|
|
students.add(new Student(name, studentId, points));
|
|
}
|
|
}
|
|
studentRepository.saveAll(students);
|
|
}
|
|
} |