Skip to content

Commit

Permalink
Add API endpoint to GET user templates
Browse files Browse the repository at this point in the history
- Add service to fetch all user templates plus global templates
- Add schema
  • Loading branch information
dmtrek14 committed Sep 13, 2023
1 parent 13c29fe commit 8ff647b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
10 changes: 8 additions & 2 deletions api/routers/v1/timelog.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
from fastapi import APIRouter, Depends
from sqlalchemy.orm import Session

from schemas.timelog import TaskTypeItem
from services.timelog import TaskTypeService
from schemas.timelog import TaskTypeItem, Template as TemplateSchema
from services.timelog import TaskTypeService, TemplateService

from db.db_connection import get_db
from auth.auth_bearer import BearerToken
Expand All @@ -15,3 +15,9 @@
async def get_task_types(db: Session = Depends(get_db), skip: int = 0, limit: int = 100):
items = TaskTypeService(db).get_items()
return items


@router.get("/templates/{user_id}", dependencies=[Depends(BearerToken())], response_model=List[TemplateSchema])
async def get_user_templates(user_id: int, db: Session = Depends(get_db)):
templates = TemplateService(db).get_user_templates(user_id)
return templates
18 changes: 18 additions & 0 deletions api/schemas/timelog.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from pydantic import BaseModel
from typing import Optional


class TaskTypeItem(BaseModel):
Expand All @@ -8,3 +9,20 @@ class TaskTypeItem(BaseModel):

class Config:
orm_mode = True


class Template(BaseModel):
id: int
name: str
story: Optional[str]
description: Optional[str]
task_type: Optional[str]
init_time: Optional[int]
end_time: Optional[int]
customer_id: Optional[int]
user_id: Optional[int]
project_id: Optional[int]
is_global: bool

class Config:
orm_mode = True
11 changes: 10 additions & 1 deletion api/services/timelog.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
from typing import List
from sqlalchemy import or_

from services.main import AppService
from models.timelog import TaskType
from models.timelog import TaskType, Template


class TaskTypeService(AppService):
def get_items(self) -> List[TaskType]:
task_types = self.db.query(TaskType).all() or []
return task_types


class TemplateService(AppService):
def get_user_templates(self, user_id: int) -> List[Template]:
templates = (
self.db.query(Template).filter(or_(Template.user_id == user_id, Template.is_global is True)).all() or []
)
return templates

0 comments on commit 8ff647b

Please sign in to comment.