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.
50 lines
2.1 KiB
50 lines
2.1 KiB
from datetime import date
|
|
from typing import Optional, List
|
|
from dal.student_dal_json import StudentDAL
|
|
from model import Student
|
|
|
|
|
|
class StudentBLL:
|
|
def __init__(self, dal: StudentDAL):
|
|
self.dal = dal
|
|
|
|
def add_student(self, student: Student) -> bool:
|
|
if self.dal.check_student_exists(student.sid):
|
|
raise ValueError(f"学生 ID {student.sid} 已存在。")
|
|
if not student.is_valid:
|
|
raise ValueError(f"学生数据校验不通过。错误信息:{student.get_errors()}")
|
|
return self.dal.add_student(student)
|
|
# 其他方法同理
|
|
|
|
def delete_student(self, sid: str) ->bool:
|
|
if not self.dal.check_student_exists(sid):
|
|
raise ValueError(f"学生 ID {sid} 不存在。")
|
|
return self.dal.delete_student(sid)
|
|
|
|
def update_student(self, sid: str, student: Student) -> bool:
|
|
if not self.dal.check_student_exists(sid):
|
|
raise ValueError(f"学生 ID {sid} 不存在。")
|
|
if not student.is_valid:
|
|
raise ValueError(f"学生数据校验不通过。错误信息:{student.get_errors()}")
|
|
return self.dal.update_student(sid, student)
|
|
|
|
def update_student_partial(self, sid: str, name: Optional[str] = None, height: Optional[int] = None,
|
|
birth_date: Optional[date] = None, enrollment_date: Optional[date] = None,
|
|
class_name: Optional[str] = None) -> bool:
|
|
if not self.dal.check_student_exists(sid):
|
|
raise ValueError(f"学生 ID {sid} 不存在。")
|
|
return self.dal.update_student_partial(sid, name, height, birth_date, enrollment_date, class_name)
|
|
|
|
def get_student_by_id(self, sid: str) -> Optional[Student]:
|
|
return self.dal.find_student_by_sid(sid)
|
|
|
|
def get_students_by_name(self, name: str) -> List[Student]:
|
|
return self.dal.find_students_by_name(name)
|
|
|
|
def get_students_by_class(self, class_name: str) -> List[Student]:
|
|
return self.dal.find_students_by_class_name(class_name)
|
|
|
|
def get_all_students(self) -> List[Student]:
|
|
return self.dal.get_all_students()
|
|
|