diff --git a/src/main/java/com/codesdream/ase/repository/parent/ExerciseRepository.java b/src/main/java/com/codesdream/ase/repository/parent/ExerciseRepository.java index bd1ef7f..a6c4820 100644 --- a/src/main/java/com/codesdream/ase/repository/parent/ExerciseRepository.java +++ b/src/main/java/com/codesdream/ase/repository/parent/ExerciseRepository.java @@ -5,8 +5,11 @@ import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; import java.util.List; +import java.util.Optional; @Repository public interface ExerciseRepository extends JpaRepository { List findByStudentId(int studentId); + Optional findById(int id); + } diff --git a/src/main/java/com/codesdream/ase/repository/student/HonorRepository.java b/src/main/java/com/codesdream/ase/repository/student/HonorRepository.java index ae08c8b..21eb332 100644 --- a/src/main/java/com/codesdream/ase/repository/student/HonorRepository.java +++ b/src/main/java/com/codesdream/ase/repository/student/HonorRepository.java @@ -1,15 +1,18 @@ package com.codesdream.ase.repository.student; +import com.codesdream.ase.model.parent.Exercise; import com.codesdream.ase.model.student.Honor; import org.springframework.data.domain.Sort; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; import java.util.List; +import java.util.Optional; @Repository public interface HonorRepository extends JpaRepository { List findByStudentId(int studentId); List findByStudentId(int studentId, Sort sort); + Optional findById(int id); } diff --git a/src/main/java/com/codesdream/ase/service/StaticsService.java b/src/main/java/com/codesdream/ase/service/StaticsService.java index 8fe5b90..4545d81 100644 --- a/src/main/java/com/codesdream/ase/service/StaticsService.java +++ b/src/main/java/com/codesdream/ase/service/StaticsService.java @@ -112,6 +112,22 @@ public class StaticsService { return honorRepository.findByStudentId(studentId, Sort.by(Sort.Direction.DESC, "creationDate")); } + /** + * 获取指定的一条荣誉信息 + * @author gzq + * @exception NotFoundException 如果荣誉id不存在则抛出此异常 + * @param honorId 荣誉id + * @return 荣誉 + */ + public Honor getHonor(int honorId) { + Optional h0 = honorRepository.findById(honorId); + + if (!h0.isPresent()) { + throw new NotFoundException("No such honor."); + } + return h0.get(); + } + /** * 显示所有公告,默认按照公告发布时间排序 * @return 公告列表 @@ -132,7 +148,21 @@ public class StaticsService { } return exerciseRepository.findByStudentId(studentId); } + /** + * 获取指定的一条锻炼信息 + * @author gzq + * @exception NotFoundException 如果锻炼id不存在则抛出此异常 + * @param exerciseId 锻炼id + * @return 锻炼 + */ + public Exercise getExercise(int exerciseId) { + Optional e0 = exerciseRepository.findById(exerciseId); + if (!e0.isPresent()) { + throw new NotFoundException("No such exercise."); + } + return e0.get(); + } /** * 显示学生详细信息 * @exception NotFoundException 如果学生id不存在则抛出此异常