Added edit weight functionality

[backend] added route for edit weight functionality
[frontend] added edit weight functionality
[frontend] fixed height chart on 300px max
This commit is contained in:
João Vitória Silva
2024-10-15 16:14:18 +01:00
parent 9ec0a069f4
commit d376bd7c2a
9 changed files with 102 additions and 30 deletions

View File

@@ -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
) from err

View File

@@ -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)
return health_data_crud.delete_health_weight_data(health_data_id, token_user_id, db)

View File

@@ -21,7 +21,7 @@
<!-- list zone -->
<ul class="mt-3 list-group list-group-flush" v-for="data in dataWithWeight" :key="data.id" :data="data">
<HealthWeightListComponent :data="data" @deletedWeight="updateWeightListDeleted" />
<HealthWeightListComponent :data="data" @deletedWeight="updateWeightListDeleted" @editedWeight="updateWeightListEdited" />
</ul>
<!-- pagination area -->
@@ -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,
};
},
};

View File

@@ -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,
});
}

View File

@@ -1,6 +1,6 @@
<template>
<LoadingComponent v-if="isLoading" />
<canvas ref="chartCanvas" v-else></canvas>
<canvas ref="chartCanvas" class="chart-canvas" v-else></canvas>
</template>
<script>
@@ -41,12 +41,12 @@ export default {
computedChartData.value = computed(() => {
const data = [];
const labels = [];
for (let healthData of sortedHealthDataArray.value){
for (const healthData of sortedHealthDataArray.value){
data.push(healthData.weight)
const createdAt = new Date(healthData.created_at);
labels.push(`${createdAt.getDate()}/${createdAt.getMonth()+1}/${createdAt.getFullYear()}`)
}
let label = "Weight in kgs";
const label = "Weight in kgs";
return {
datasets: [{
@@ -102,4 +102,11 @@ export default {
};
}
}
</script>
</script>
<style scoped>
.chart-canvas {
max-height: 300px;
width: 100%; /* Ensures the canvas stretches across the available width */
}
</style>

View File

@@ -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(){

View File

@@ -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"
}

View File

@@ -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}`)
}

View File

@@ -11,7 +11,7 @@
<HealthDashboardZone :userHealthData="userHealthData[0]" :userHealthTargets="userHealthTargets" v-if="activeSection === 'dashboard' && !isLoading && userHealthData"/>
<!-- Include the SettingsUserProfileZone -->
<HealthWeightZone :userHealthData="userHealthData" :userHealthTargets="userHealthTargets" :isLoading="isLoading" :totalPages="totalPages" :pageNumber="pageNumber" @createdWeight="updateWeightListAdded" @deletedWeight="updateWeightListDeleted" v-if="activeSection === 'weight' && !isLoading" />
<HealthWeightZone :userHealthData="userHealthData" :userHealthTargets="userHealthTargets" :isLoading="isLoading" :totalPages="totalPages" :pageNumber="pageNumber" @createdWeight="updateWeightListAdded" @deletedWeight="updateWeightListDeleted" @editedWeight="updateWeightListEdited" v-if="activeSection === 'weight' && !isLoading" />
</div>
<!-- back button -->
<BackButtonComponent />
@@ -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,
};
},
};