This commit is contained in:
ublue 2020-09-15 23:37:05 +08:00
parent 8375983b27
commit 89de75abf9
2 changed files with 15 additions and 11 deletions

View File

@ -5,14 +5,14 @@ import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
import java.util.Optional;
@Repository @Repository
public interface StudentRepository extends JpaRepository<Student, Integer> { public interface StudentRepository extends JpaRepository<Student, Integer> {
Student findByParentId(int parentId); Optional<Student> findByParentId(int parentId);
Student findByStudentId(int studentId); Optional<Student> findByParentId(int parentId, Sort sort);
Student findByParentId(int parentId, Sort sort);
} }

View File

@ -1,5 +1,6 @@
package com.codesdream.ase.service; package com.codesdream.ase.service;
import com.codesdream.ase.exception.notfound.NotFoundException;
import com.codesdream.ase.model.message.Notification; import com.codesdream.ase.model.message.Notification;
import com.codesdream.ase.model.parent.Parent; import com.codesdream.ase.model.parent.Parent;
import com.codesdream.ase.model.student.Student; import com.codesdream.ase.model.student.Student;
@ -51,7 +52,11 @@ public class ParentService {
*/ */
public Student getStudent(int parentId){ public Student getStudent(int parentId){
return studentRepository.findByParentId(parentId); Optional<Student> s0=studentRepository.findByParentId(parentId);
if(!s0.isPresent()){
throw new NotFoundException("No such student.");
}
return s0.get();
} }
/** /**
* 用于绑定家长与学生账号家长输入学生学号与姓名若都正确返回真 * 用于绑定家长与学生账号家长输入学生学号与姓名若都正确返回真
@ -60,13 +65,12 @@ public class ParentService {
* @return 学号与姓名是否一致 * @return 学号与姓名是否一致
*/ */
public boolean startIdentify(int studentId, String studentName){ public boolean startIdentify(int studentId, String studentName){
Student s0=studentRepository.findByStudentId(studentId); Optional<Student> s0=studentRepository.findById(studentId);
if(s0!=null){ if(!s0.isPresent()){
if(s0.getUsername().equals(studentName)) throw new NotFoundException("No such student.");
return true;
else return false;
} }
else return false; Student student = s0.get();
return student.getUsername().equals(studentName);
} }