Skip to content
This repository has been archived by the owner on Sep 27, 2024. It is now read-only.

Commit

Permalink
code annotations domain
Browse files Browse the repository at this point in the history
  • Loading branch information
rubben-88 committed Mar 14, 2024
1 parent ef76546 commit 6c2a871
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 1 deletion.
8 changes: 8 additions & 0 deletions backend/domain/logic/basic_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@
from db.errors.database_errors import ItemNotFoundError
from db.models.models import AbstractModel

# Create a generic type variable bound to subclasses of AbstractModel.
T = TypeVar("T", bound=AbstractModel)


def get(session: Session, object_type: type[T], ident: int) -> T:
"""
General function for retrieving a single object from the database.
The type of the object and its id as well a session object has to be provided.
"""
generic_object: T | None = session.get(object_type, ident)

if not generic_object:
Expand All @@ -20,4 +25,7 @@ def get(session: Session, object_type: type[T], ident: int) -> T:


def get_all(session: Session, object_type: type[T]) -> list[T]:
"""
General function for retrieving all objects of a certain type from the database.
"""
return list(session.scalars(select(object_type)).all())
5 changes: 4 additions & 1 deletion backend/domain/logic/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@


def create_group(session: Session, project_id: int) -> GroupDataclass:
"""
Create an empty group for a certain project.
"""
project: Project = get(session, Project, project_id)
new_group: Group = Group(project_id=project_id)
project.groups.append(new_group)
Expand All @@ -34,7 +37,7 @@ def get_groups_of_project(session: Session, project_id: int) -> list[GroupDatacl

def get_groups_of_student(session: Session, student_id: int) -> list[GroupDataclass]:
student: Student = get(session, Student, ident=student_id)
groups: list[Group] = student.groups
groups: list[Group] = student.groupsget_groups_of_student
return [group.to_domain_model() for group in groups]


Expand Down
3 changes: 3 additions & 0 deletions backend/domain/logic/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ def create_project(
visible: bool,
max_students: int,
) -> ProjectDataclass:
"""
Create a project for a certain subject.
"""
subject: Subject = get(session, Subject, subject_id)

new_project: Project = Project(
Expand Down
3 changes: 3 additions & 0 deletions backend/domain/logic/submission.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ def create_submission(
state: SubmissionState,
date_time: datetime,
) -> SubmissionDataclass:
"""
Create a submission for a certain project by a certain group.
"""
student: Student = get(session, Student, ident=student_id)
group: Group = get(session, Group, ident=group_id)

Expand Down
3 changes: 3 additions & 0 deletions backend/domain/logic/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@


def convert_user(session: Session, user: UserDataclass) -> APIUser:
"""
Given a UserDataclass, check what roles that user has and fill those in to convert it to an APIUser.
"""
api_user = APIUser(id=user.id, name=user.name, email=user.email, roles=[])

if is_user_teacher(session, user.id):
Expand Down
3 changes: 3 additions & 0 deletions backend/domain/models/APIUser.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@


class APIUser(BaseModel):
"""
A user with that has is roles specified.
"""
id: int
name: str
email: EmailStr
Expand Down
4 changes: 4 additions & 0 deletions backend/domain/models/UserDataclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@


class UserDataclass(BaseModel):
"""
This user does not have any roles yet.
When the roles become specified, use the almost equivalent APIUser.
"""
id: int
name: str
email: EmailStr

0 comments on commit 6c2a871

Please sign in to comment.