diff --git a/backend/app/health_data/crud.py b/backend/app/health_data/crud.py index eca172e29..217bb8a8f 100644 --- a/backend/app/health_data/crud.py +++ b/backend/app/health_data/crud.py @@ -173,14 +173,16 @@ def create_health_weight_data( status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail="Internal Server Error", ) from err - -def edit_health_weight_data( - health_data: health_data_schema.HealthData, db: Session -): + +def edit_health_weight_data(health_data: health_data_schema.HealthData, db: Session): try: # Get the health_data from the database - db_health_data = db.query(models.HealthData).filter(models.HealthData.id == health_data.id).first() + db_health_data = ( + db.query(models.HealthData) + .filter(models.HealthData.id == health_data.id) + .first() + ) if db_health_data is None: raise HTTPException( @@ -191,9 +193,9 @@ def edit_health_weight_data( # Update the user if health_data.created_at is not None: - db_health_data.created_at = health_data.created_at - if health_data.weight is not None: - db_health_data.weight = health_data.weight + db_health_data.created_at = health_data.created_at + if health_data.weight is not None: + db_health_data.weight = health_data.weight # Commit the transaction db.commit() @@ -218,16 +220,19 @@ def edit_health_weight_data( status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail="Internal Server Error", ) from err - -def delete_health_weight_data( - health_data_id: int, user_id: int, db: Session -): + + +def delete_health_weight_data(health_data_id: int, user_id: int, db: Session): try: # Delete the gear - num_deleted = db.query(models.HealthData).filter( + num_deleted = ( + db.query(models.HealthData) + .filter( models.HealthData.id == health_data_id, models.HealthData.user_id == user_id, - ).delete() + ) + .delete() + ) # Check if the gear was found and deleted if num_deleted == 0: @@ -249,4 +254,4 @@ def delete_health_weight_data( raise HTTPException( status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail="Internal Server Error", - ) from err \ No newline at end of file + ) from err diff --git a/backend/app/health_data/router.py b/backend/app/health_data/router.py index c7319fa68..e223217b1 100644 --- a/backend/app/health_data/router.py +++ b/backend/app/health_data/router.py @@ -103,7 +103,9 @@ async def create_health_weight_data( Depends(database.get_db), ], ): - health_for_date = health_data_crud.get_health_data_by_created_at(token_user_id, health_data.created_at, db) + health_for_date = health_data_crud.get_health_data_by_created_at( + token_user_id, health_data.created_at, db + ) if health_for_date: if health_for_date.weight is None: # Edits the health_data in the database and returns it @@ -115,8 +117,37 @@ async def create_health_weight_data( ) else: # Creates the health_data in the database and returns it - return health_data_crud.create_health_weight_data(health_data, token_user_id, db) - + return health_data_crud.create_health_weight_data( + health_data, token_user_id, db + ) + + +@router.put("/weight/{health_data_id}") +async def edit_health_weight_data( + health_data_id: int, + health_data: health_data_schema.HealthData, + check_scopes: Annotated[ + Callable, Security(session_security.check_scopes, scopes=["health:write"]) + ], + token_user_id: Annotated[ + int, + Depends(session_security.get_user_id_from_access_token), + ], + db: Annotated[ + Session, + Depends(database.get_db), + ], +): + # Check if the user_id in the token is the same as the user_id in the health_data + if token_user_id != health_data.user_id: + raise HTTPException( + status_code=status.HTTP_403_FORBIDDEN, + detail="Forbidden, user_id in token is different from user_id in health_data", + ) + + # Edits the health_data in the database and returns it + return health_data_crud.edit_health_weight_data(health_data, db) + @router.post("/weight/{health_data_id}") async def delete_health_weight_data( @@ -134,4 +165,4 @@ async def delete_health_weight_data( ], ): # Deletes entry from database - return health_data_crud.delete_health_weight_data(health_data_id, token_user_id, db) \ No newline at end of file + return health_data_crud.delete_health_weight_data(health_data_id, token_user_id, db) diff --git a/frontend/app/src/components/Health/HealthWeightZone.vue b/frontend/app/src/components/Health/HealthWeightZone.vue index 939d49b83..dfc5f0fc7 100644 --- a/frontend/app/src/components/Health/HealthWeightZone.vue +++ b/frontend/app/src/components/Health/HealthWeightZone.vue @@ -21,7 +21,7 @@ @@ -76,7 +76,7 @@ export default { required: true, }, }, - emits: ["createdWeight", "deletedWeight"], + emits: ["createdWeight", "deletedWeight", "editedWeight"], setup(props, { emit }) { const dataWithWeight = ref([]); @@ -105,6 +105,10 @@ export default { emit("deletedWeight", deletedWeight); } + function updateWeightListEdited(editedWeight){ + emit("editedWeight", editedWeight); + } + watchEffect(() => { if (props.userHealthData) { updatedDataWithWeightArray(); @@ -121,6 +125,7 @@ export default { updateIsLoadingNewWeight, updateWeightListAdded, updateWeightListDeleted, + updateWeightListEdited, }; }, }; diff --git a/frontend/app/src/components/Health/HealthWeightZone/HealthWeightAddEditModalComponent.vue b/frontend/app/src/components/Health/HealthWeightZone/HealthWeightAddEditModalComponent.vue index 8f8c1fed3..4484a4951 100644 --- a/frontend/app/src/components/Health/HealthWeightZone/HealthWeightAddEditModalComponent.vue +++ b/frontend/app/src/components/Health/HealthWeightZone/HealthWeightAddEditModalComponent.vue @@ -98,7 +98,8 @@ export default { function submitEditWeight(){ emit("editedWeight", { id: props.data.id, - weight: newEditWeightDate.value, + user_id: props.data.user_id, + weight: newEditWeightWeight.value, created_at: newEditWeightDate.value, }); } diff --git a/frontend/app/src/components/Health/HealthWeightZone/HealthWeightLineChartComponent.vue b/frontend/app/src/components/Health/HealthWeightZone/HealthWeightLineChartComponent.vue index 08a67af27..df2b9b666 100644 --- a/frontend/app/src/components/Health/HealthWeightZone/HealthWeightLineChartComponent.vue +++ b/frontend/app/src/components/Health/HealthWeightZone/HealthWeightLineChartComponent.vue @@ -1,6 +1,6 @@ \ No newline at end of file + + + \ No newline at end of file diff --git a/frontend/app/src/components/Health/HealthWeightZone/HealthWeightListComponent.vue b/frontend/app/src/components/Health/HealthWeightZone/HealthWeightListComponent.vue index 0dbc323d7..90b2896fe 100644 --- a/frontend/app/src/components/Health/HealthWeightZone/HealthWeightListComponent.vue +++ b/frontend/app/src/components/Health/HealthWeightZone/HealthWeightListComponent.vue @@ -52,8 +52,16 @@ export default { return `${date.getDate().toString().padStart(2, '0')}-${(date.getMonth() + 1).toString().padStart(2, '0')}-${date.getFullYear()}`; }; - function updateWeightListEdited(editedWeight){ - emit("editedWeight", editedWeight); + async function updateWeightListEdited(editedWeight){ + try { + await health_data.editWeight(editedWeight); + + emit("editedWeight", editedWeight); + + push.success(t("healthWeightListComponent.successEditWeight")); + } catch (error) { + push.error(`${t("healthWeightListComponent.errorEditWeight")} - ${error.toString()}`); + } } async function submitDeleteWeight(){ diff --git a/frontend/app/src/i18n/us/components/health/healthWeightZone/healthWeightListComponent.json b/frontend/app/src/i18n/us/components/health/healthWeightZone/healthWeightListComponent.json index 90efb77b9..44a97a270 100644 --- a/frontend/app/src/i18n/us/components/health/healthWeightZone/healthWeightListComponent.json +++ b/frontend/app/src/i18n/us/components/health/healthWeightZone/healthWeightListComponent.json @@ -2,5 +2,7 @@ "modalDeleteWeightTitle": "Delete weight", "modalDeleteWeightBody": "Are you sure you want to delete weight entry for ", "successDeleteWeight": "Weight deleted", - "errorDeleteWeight": "It was not possible to delete weight entry" + "errorDeleteWeight": "It was not possible to delete weight entry", + "successEditWeight": "Weight edited", + "errorEditWeight": "It was not possible to edit weight entry" } \ No newline at end of file diff --git a/frontend/app/src/services/health_dataService.js b/frontend/app/src/services/health_dataService.js index 28c6881df..fcd510bc8 100644 --- a/frontend/app/src/services/health_dataService.js +++ b/frontend/app/src/services/health_dataService.js @@ -13,6 +13,9 @@ export const health_data = { createWeight(data) { return fetchPostRequest('health/weight', data) }, + editWeight(data) { + return fetchPutRequest(`health/weight/${data.id}`, data) + }, deleteWeight(id) { return fetchPostRequest(`health/weight/${id}`) } diff --git a/frontend/app/src/views/HealthView.vue b/frontend/app/src/views/HealthView.vue index 63357c319..9897861c4 100644 --- a/frontend/app/src/views/HealthView.vue +++ b/frontend/app/src/views/HealthView.vue @@ -11,7 +11,7 @@ - + @@ -121,6 +121,15 @@ export default { } } + function updateWeightListEdited(editedWeight) { + for(const data of userHealthData.value){ + if(data.id === editedWeight.id){ + data.weight = editedWeight.weight; + data.created_at = editedWeight.created_at; + } + } + } + onMounted(async () => { // Fetch health_data and health_targets await fetchHealthData(); @@ -142,6 +151,7 @@ export default { updateActiveSection, updateWeightListAdded, updateWeightListDeleted, + updateWeightListEdited, }; }, };