Skip to content

Commit

Permalink
make it work with database
Browse files Browse the repository at this point in the history
  • Loading branch information
xerbalind committed Mar 4, 2024
1 parent 2821001 commit 2de4c57
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 13 deletions.
8 changes: 3 additions & 5 deletions backend/src/subject/models.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
from typing import Set

from sqlalchemy import Column, ForeignKey, Table
from sqlalchemy.orm import Mapped, mapped_column, relationship
from sqlalchemy.orm import Mapped, mapped_column
from src.database import Base

StudentSubject = Table(
"student_subject",
Base.metadata,
Column("student_id", ForeignKey("user.uid")),
Column("uid", ForeignKey("user.uid")),
Column("subject_id", ForeignKey("subject.id")),
)

TeacherSubject = Table(
"teacher_subject",
Base.metadata,
Column("teacher_id", ForeignKey("user.uid")),
Column("uid", ForeignKey("user.uid")),
Column("subject_id", ForeignKey("subject.id")),
)

Expand Down
5 changes: 3 additions & 2 deletions backend/src/subject/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ async def create_subject(subject: SubjectCreate, db: Session = Depends(get_db)):

@router.delete("/{subject_id}",
dependencies=[Depends(admin_user_validation)], status_code=200)
async def delete_subject(subject: Subject = Depends(get_subject), db: Session = Depends(get_db)):
await service.delete_subject(db, subject)
async def delete_subject(subject_id: int, db: Session = Depends(get_db)):
await service.delete_subject(db, subject_id)
return "Successfully deleted"


Expand All @@ -59,6 +59,7 @@ async def get_subject_teachers(subject_id: int, db: Session = Depends(get_db)):
status_code=201)
async def create_subject_teacher(subject_id: int, user_id: str, db: Session = Depends(get_db)):
await service.create_subject_teacher(db, subject_id, user_id)
return "Successfully added"


@router.delete("/{subject_id}/teachers/{user_id}", dependencies=[Depends(user_permission_validation)])
Expand Down
12 changes: 6 additions & 6 deletions backend/src/subject/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ async def get_subject(db: Session, subject_id: int) -> models.Subject:
async def get_subjects(db: Session, user_id: str) -> tuple[Sequence[models.Subject],
Sequence[models.Subject]]:
return (db.query(models.Subject).join(models.TeacherSubject).
filter(models.TeacherSubject.c.teacher_id == user_id).all(),
filter(models.TeacherSubject.c.uid == user_id).all(),

db.query(models.Subject).join(models.StudentSubject).
filter(models.StudentSubject.c.student_id == user_id).all()
filter(models.StudentSubject.c.uid == user_id).all()
)


Expand All @@ -34,18 +34,18 @@ async def create_subject(db: Session, subject: schemas.SubjectCreate) -> models.

async def create_subject_teacher(db: Session, subject_id: int, user_id: str):
insert_stmnt = models.TeacherSubject.insert().values(
subject_id=subject_id, teacher_id=user_id)
subject_id=subject_id, uid=user_id)
db.execute(insert_stmnt)
db.commit()


async def delete_subject_teacher(db: Session, subject_id: int, user_id: str):
db.query(models.TeacherSubject).filter_by(
subject_id=subject_id, teacher_id=user_id).delete()
subject_id=subject_id, uid=user_id).delete()
db.commit()


async def delete_subject(db: Session, subject: schemas.Subject):
async def delete_subject(db: Session, subject_id: int):
"""Remove a subject"""
db.delete(subject)
db.query(models.Subject).filter_by(id=subject_id).delete()
db.commit()

0 comments on commit 2de4c57

Please sign in to comment.