From 8ff647bc75b6568b17b99fd3495d50a7e7804c79 Mon Sep 17 00:00:00 2001 From: Danielle Mayabb Date: Wed, 13 Sep 2023 08:18:31 -0700 Subject: [PATCH] Add API endpoint to GET user templates - Add service to fetch all user templates plus global templates - Add schema --- api/routers/v1/timelog.py | 10 ++++++++-- api/schemas/timelog.py | 18 ++++++++++++++++++ api/services/timelog.py | 11 ++++++++++- 3 files changed, 36 insertions(+), 3 deletions(-) diff --git a/api/routers/v1/timelog.py b/api/routers/v1/timelog.py index 744b88a3e..d95ed74d4 100644 --- a/api/routers/v1/timelog.py +++ b/api/routers/v1/timelog.py @@ -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 @@ -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 diff --git a/api/schemas/timelog.py b/api/schemas/timelog.py index 43c6e6da8..d4ecfb3c7 100644 --- a/api/schemas/timelog.py +++ b/api/schemas/timelog.py @@ -1,4 +1,5 @@ from pydantic import BaseModel +from typing import Optional class TaskTypeItem(BaseModel): @@ -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 diff --git a/api/services/timelog.py b/api/services/timelog.py index 3e26bfaf6..19e461439 100644 --- a/api/services/timelog.py +++ b/api/services/timelog.py @@ -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