Skip to content

Commit

Permalink
add save validation
Browse files Browse the repository at this point in the history
  • Loading branch information
Ciroye committed Aug 9, 2023
1 parent 195abcc commit d16a6d5
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
9 changes: 8 additions & 1 deletion backend/app/api/endpoints/base/historical_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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")
Expand Down
7 changes: 5 additions & 2 deletions backend/app/domain/services/base/historical_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
9 changes: 9 additions & 0 deletions backend/app/infrastructure/repositories/historical_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
)

0 comments on commit d16a6d5

Please sign in to comment.