File size: 2,157 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
from typing import Any, List

from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy.orm import Session
from starlette.responses import Response
from core.cache import cache

from cruds.program import crud_program
from cruds.group import crud_group
from schemas import Program, ProgramCreate, ProgramUpdate, GroupCreate
from schemas import program
from utils import deps

router = APIRouter()


@router.get("/", response_model=List[Program])
async def get_programs(
    db: Session = Depends(deps.get_db),
    skip: int = 0,
    limit: int = 500,
) -> Any:
    programs = crud_program.get_multi(db, skip=skip, limit=limit)
    return programs


@router.post("/", response_model=Program)
async def create_program(
    db: Session = Depends(deps.get_db),
    user=Depends(deps.get_current_admin_or_above),
    *,
    program_in: ProgramCreate,
) -> Any:
    program = crud_program.create(db, obj_in=Program(**program_in.dict()))
    for sem_iter in range(program_in.max_sems):
        group = GroupCreate(
            program_id=program.id,
            sem=sem_iter + 1,
        )
        print(group.dict())
        crud_group.create(db=db, obj_in=group)
    return program


@router.get("/{program_id}/", response_model=Program)
@router.get("/{program_id}/group", response_model=program.ProgramGroupReturn)
async def get_program(
    db: Session = Depends(deps.get_db),
    user=Depends(deps.get_current_active_user),
    *,
    program_id: int,
) -> Any:
    program = crud_program.get(db, program_id)
    return program


@router.put("/{program_id}/")
def update_program(
    db: Session = Depends(deps.get_db),
    *,
    program_id: int,
    obj_in: ProgramUpdate,
    current_user=Depends(deps.get_current_admin_or_above),
) -> Any:
    department = crud_program.get(db, program_id)
    crud_program.update(db, db_obj=department, obj_in=obj_in)
    return {"status": "success"}


@router.delete("/{program_id}/")
async def delete_program(
    db: Session = Depends(deps.get_db),
    user=Depends(deps.get_current_admin_or_above),
    *,
    program_id: int,
):
    crud_program.remove(db=db, id=program_id)
    return {"msg": "success"}