diff --git a/backend/app/api/endpoints/base/historical_data.py b/backend/app/api/endpoints/base/historical_data.py index deb65dea5..e3b72c4e9 100644 --- a/backend/app/api/endpoints/base/historical_data.py +++ b/backend/app/api/endpoints/base/historical_data.py @@ -3,6 +3,7 @@ # LICENSE file in the root directory of this source tree. from fastapi import APIRouter +from fastapi.exceptions import HTTPException from app.domain.schemas.base.historical_data import ( GetDeleteHistoricalDataRequest, @@ -24,9 +25,15 @@ async def get_historical_data_by_task_and_user(model: GetHistoricalDataRequest): @router.post("/save_historical_data") async def save_historical_data(model: GetSaveHistoricalDataRequest): - return HistoricalDataService().save_historical_data( + history = HistoricalDataService().save_historical_data( model.task_id, model.user_id, model.data ) + if history: + return history + else: + raise HTTPException( + status_code=400, detail="Historical data already exists for this task" + ) @router.post("/delete_historical_data") diff --git a/backend/app/domain/services/base/historical_data.py b/backend/app/domain/services/base/historical_data.py index b0e676950..b172ed923 100644 --- a/backend/app/domain/services/base/historical_data.py +++ b/backend/app/domain/services/base/historical_data.py @@ -15,9 +15,12 @@ def get_historical_data_by_task_and_user(self, task_id: int, user_id: int): ) def save_historical_data(self, task_id: int, user_id: int, data: str): - return self.historical_data_repository.save_historical_data( + if not self.historical_data_repository.check_if_historical_data_exists( task_id, user_id, data - ) + ): + return self.historical_data_repository.save_historical_data( + task_id, user_id, data + ) def delete_historical_data(self, task_id: int, user_id: int): self.historical_data_repository.delete_historical_data(task_id, user_id) diff --git a/backend/app/infrastructure/repositories/historical_data.py b/backend/app/infrastructure/repositories/historical_data.py index 15d4e48e5..6590d9bd5 100644 --- a/backend/app/infrastructure/repositories/historical_data.py +++ b/backend/app/infrastructure/repositories/historical_data.py @@ -35,3 +35,12 @@ def delete_historical_data(self, task_id: int, user_id: int): ).filter(HistoricalData.user_id == user_id).delete() self.session.flush() self.session.commit() + + def check_if_historical_data_exists(self, task_id: int, user_id: int, data: str): + return ( + self.session.query(HistoricalData) + .filter(HistoricalData.task_id == task_id) + .filter(HistoricalData.user_id == user_id) + .filter(HistoricalData.history == data) + .first() + )