Skip to content

Commit

Permalink
Merge pull request #59 from studio-recoding/feat/crud
Browse files Browse the repository at this point in the history
feat: delete, update schedule
  • Loading branch information
uommou authored May 25, 2024
2 parents 1929e7a + 285fbb1 commit 0a76039
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 5 deletions.
27 changes: 26 additions & 1 deletion app/database/chroma_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import os
import datetime
from dotenv import load_dotenv
from app.dto.db_dto import AddScheduleDTO, DeleteScheduleDTO, RecommendationMainRequestDTO, ReportTagsRequestDTO
from app.dto.db_dto import AddScheduleDTO, DeleteScheduleDTO, UpdateScheduleDTO, RecommendationMainRequestDTO, ReportTagsRequestDTO

load_dotenv()
CHROMA_DB_IP_ADDRESS = os.getenv("CHROMA_DB_IP_ADDRESS")
Expand Down Expand Up @@ -70,6 +70,31 @@ async def delete_db_data(schedule_data: DeleteScheduleDTO):
)
return True

# 데이터베이스 업데이트 함수 정의
async def update_db_data(schedule_data: UpdateScheduleDTO):
schedule_date = schedule_data.schedule_datetime_start.split("T")[0]
year = int(schedule_date.split("-")[0])
month = int(schedule_date.split("-")[1])
date = int(schedule_date.split("-")[2])

# 기존 스케줄 업데이트 로직
schedules.update(
documents=[schedule_data.data],
ids=[str(schedule_data.schedule_id)],
metadatas=[{
"year": year,
"month": month,
"date": date,
"datetime_start": schedule_data.schedule_datetime_start,
"datetime_end": schedule_data.schedule_datetime_end,
"member": schedule_data.member_id,
"category": schedule_data.category,
"location": schedule_data.location,
"person": schedule_data.person
}]
)
return True

# 유저의 id, 해당 날짜로 필터링
async def db_daily_schedule(user_data: RecommendationMainRequestDTO):
member = user_data.member_id
Expand Down
12 changes: 11 additions & 1 deletion app/dto/db_dto.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,24 @@ class AddScheduleDTO(BaseModel):
schedule_datetime_end: str
schedule_id: int
member_id: int
category: int
category: str
location: str
person: str

class DeleteScheduleDTO(BaseModel):
schedule_id: int
member_id: int

class UpdateScheduleDTO(BaseModel):
data: str
schedule_datetime_start: str
schedule_datetime_end: str
schedule_id: int
member_id: int
category: str
location: str
person: str

class RecommendationMainRequestDTO(BaseModel):
member_id: int
user_persona: str
Expand Down
16 changes: 13 additions & 3 deletions app/routers/chromadb.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
from dotenv import load_dotenv
from fastapi import APIRouter, HTTPException, Depends, status

from app.dto.db_dto import AddScheduleDTO, DeleteScheduleDTO
from app.database.chroma_db import add_db_data, delete_db_data, get_chroma_client
from app.dto.db_dto import AddScheduleDTO, DeleteScheduleDTO, UpdateScheduleDTO
from app.database.chroma_db import add_db_data, delete_db_data, update_db_data, get_chroma_client

router = APIRouter(
prefix="/chromadb",
Expand All @@ -31,11 +31,21 @@ async def add_schedule_endpoint(schedule_data: AddScheduleDTO, chroma_client=Dep
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))

@router.post("/delete_schedule", status_code=status.HTTP_204_NO_CONTENT)
@router.delete("/delete_schedule", status_code=status.HTTP_204_NO_CONTENT)
async def delete_schedule_endpoint(schedule_data: DeleteScheduleDTO, chroma_client=Depends(get_chroma_client)):
try:
# 직접 `add_db_data` 함수를 비동기적으로 호출합니다.
await delete_db_data(schedule_data)
return {"message": "Schedule delete successfully"}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))


@router.put("/update_schedule", status_code=status.HTTP_200_OK)
async def update_schedule_endpoint(schedule_data: UpdateScheduleDTO, chroma_client=Depends(get_chroma_client)):
try:
# 데이터베이스 업데이트 함수 호출
await update_db_data(schedule_data)
return {"message": "Schedule updated successfully"}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))

0 comments on commit 0a76039

Please sign in to comment.