From b83ab56459d58281b49152835e066d32cf5b02c4 Mon Sep 17 00:00:00 2001 From: Only-bottle Date: Fri, 6 Dec 2024 22:38:56 +0900 Subject: [PATCH] Add create_project function to project service --- app/api/v1/endpoints/project.py | 15 +++++++++------ app/services/project.py | 19 +++++++++++++++++++ 2 files changed, 28 insertions(+), 6 deletions(-) create mode 100644 app/services/project.py diff --git a/app/api/v1/endpoints/project.py b/app/api/v1/endpoints/project.py index b033164f..5ccfd2b3 100644 --- a/app/api/v1/endpoints/project.py +++ b/app/api/v1/endpoints/project.py @@ -1,8 +1,10 @@ from typing import Optional from uuid import uuid4 -from fastapi import APIRouter +from fastapi import APIRouter, Depends +from sqlalchemy.orm import Session +from app.api.deps import api_key_header from app.api.v1.schemas.project import ( ExperimentStatus, ModelSummary, @@ -15,6 +17,8 @@ ProjectsResponse, ProjectSummaryPayload, ) +from app.services.project import project_service +from netspresso.utils.db.session import get_db router = APIRouter() @@ -22,14 +26,13 @@ @router.post("", response_model=ProjectResponse) def create_project( *, + db: Session = Depends(get_db), + api_key: str = Depends(api_key_header), request_body: ProjectCreate, ) -> ProjectResponse: + project = project_service.create_project(db=db, project_name=request_body.project_name, api_key=api_key) - project = ProjectSummaryPayload( - project_id=str(uuid4()), - project_name=request_body.project_name, - user_id=str(uuid4()), - ) + project = ProjectSummaryPayload.model_validate(project) return ProjectResponse(data=project) diff --git a/app/services/project.py b/app/services/project.py new file mode 100644 index 00000000..79f0014c --- /dev/null +++ b/app/services/project.py @@ -0,0 +1,19 @@ +from sqlalchemy.orm import Session + +from app.services.user import user_service +from netspresso.netspresso import NetsPresso +from netspresso.utils.db.models.project import Project + + +class ProjectService: + def create_project(self, db: Session, project_name: str, api_key: str) -> Project: + user = user_service.get_user_by_api_key(db=db, api_key=api_key) + + netspresso = NetsPresso(email=user.email, password=user.password) + + project = netspresso.create_project(project_name=project_name) + + return project + + +project_service = ProjectService()