File size: 1,428 Bytes
b7a7f32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from cruds.base import CRUDBase
from models.group import Group
from schemas.group import GroupCreate, GroupUpdate
from schemas.group import Group as GroupSchema
from sqlalchemy.orm import Session
from cruds.course import crud_course
from fastapi.encoders import jsonable_encoder


class CRUDGroup(CRUDBase[Group, GroupCreate, GroupUpdate]):
    def get_by_program_and_sem(self, db: Session, *, program: int, sem: int):
        return (
            db.query(self.model)
            .filter_by(program_id=program)
            .filter_by(sem=sem)
            .first()
        )

    def update(
        self,
        db: Session,
        *,
        db_obj: Group,
        obj_in: GroupCreate,
    ) -> Group:
        if obj_in.course:
            course = [crud_course.get(db=db, id=id) for id in obj_in.course]
        else:
            course = []
        db_obj.course = course
        db.add(db_obj)
        db.commit()
        db.refresh(db_obj)
        return db_obj

    def create(
        self,
        db: Session,
        *,
        obj_in: GroupCreate,
    ) -> Group:
        if obj_in.course:
            course = [crud_course.get(db=db, id=id) for id in obj_in.course]
        else:
            course = []

        db_obj = Group(program_id=obj_in.program_id, sem=obj_in.sem, course=course)
        db.add(db_obj)
        db.commit()
        db.refresh(db_obj)
        return db_obj


crud_group = CRUDGroup(Group)