Skip to content

Commit

Permalink
Backend optimizations
Browse files Browse the repository at this point in the history
- Added constants file
- Added api version to metadata on api responses
- Fixed bug that some routes raised an exception when date and datetime
columns where set in the database and not properly parsed before JSON
Response
  • Loading branch information
joaovitoriasilva committed Jan 8, 2024
1 parent 51ec61c commit d197c6a
Show file tree
Hide file tree
Showing 6 changed files with 184 additions and 96 deletions.
6 changes: 6 additions & 0 deletions backend/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Constant related to version
API_VERSION="v0.1.2"

# Constants related to user access types
ADMIN_ACCESS = 2
REGULAR_ACCESS = 1
77 changes: 41 additions & 36 deletions backend/controllers/activityController.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
from dependencies import get_db_session, create_error_response, get_current_user
from sqlalchemy.orm import Session
from fastapi.responses import JSONResponse
from constants import API_VERSION

# Define the API router
router = APIRouter()
Expand Down Expand Up @@ -220,7 +221,7 @@ async def read_activities_all(
]

# Include metadata in the response
metadata = {"total_records": len(activity_records)}
metadata = {"total_records": len(activity_records), "api_version": API_VERSION}

# Return the queried values using JSONResponse
return JSONResponse(
Expand Down Expand Up @@ -272,7 +273,7 @@ async def read_activities_useractivities(
]

# Include metadata in the response
metadata = {"total_records": len(activity_records)}
metadata = {"total_records": len(activity_records), "api_version": API_VERSION}

# Return the queried values using JSONResponse
return JSONResponse(
Expand Down Expand Up @@ -347,7 +348,12 @@ async def read_activities_useractivities_thisweek_number(
]

# Include metadata in the response
metadata = {"total_records": len(activity_records)}
metadata = {
"total_records": len(activity_records),
"user_id": user_id,
"week_number": week_number,
"api_version": API_VERSION,
}

# Return the queried values using JSONResponse
return JSONResponse(
Expand Down Expand Up @@ -416,15 +422,13 @@ async def read_activities_useractivities_thisweek_distances(
distances = calculate_activity_distances(activity_records)

# Return the queried values using JSONResponse
#return JSONResponse(content=distances)
# return JSONResponse(content=distances)

# Include metadata in the response
metadata = {"total_records": 1}
metadata = {"total_records": 1, "user_id": user_id, "api_version": API_VERSION}

# Return the queried values using JSONResponse
return JSONResponse(
content={"metadata": metadata, "content": distances}
)
return JSONResponse(content={"metadata": metadata, "content": distances})
except JWTError:
# Return an error response if the user is not authenticated
return create_error_response("UNAUTHORIZED", "Unauthorized", 401)
Expand Down Expand Up @@ -487,15 +491,13 @@ async def read_activities_useractivities_thismonth_distances(
distances = calculate_activity_distances(activity_records)

# Return the queried values using JSONResponse
#return JSONResponse(content=distances)
# return JSONResponse(content=distances)

# Include metadata in the response
metadata = {"total_records": 1}
metadata = {"total_records": 1, "user_id": user_id, "api_version": API_VERSION}

# Return the queried values using JSONResponse
return JSONResponse(
content={"metadata": metadata, "content": distances}
)
return JSONResponse(content={"metadata": metadata, "content": distances})
except JWTError:
# Return an error response if the user is not authenticated
return create_error_response("UNAUTHORIZED", "Unauthorized", 401)
Expand Down Expand Up @@ -554,12 +556,10 @@ async def read_activities_useractivities_thismonth_number(
)

# Include metadata in the response
metadata = {"total_records": 1}
metadata = {"total_records": 1, "user_id": user_id, "api_version": API_VERSION}

# Return the queried values using JSONResponse
return JSONResponse(
content={"metadata": metadata, "content": activity_count}
)
return JSONResponse(content={"metadata": metadata, "content": activity_count})

except JWTError:
# Return an error response if the user is not authenticated
Expand All @@ -575,9 +575,9 @@ async def read_activities_useractivities_thismonth_number(
)


@router.get("/activities/gear/{gearID}", response_model=List[dict])
@router.get("/activities/gear/{gear_id}", response_model=List[dict])
async def read_activities_gearactivities(
gearID=int,
gear_id=int,
user_id: int = Depends(get_current_user),
db_session: Session = Depends(get_db_session),
):
Expand All @@ -600,7 +600,7 @@ async def read_activities_gearactivities(
# Query the activities records using SQLAlchemy
activity_records = (
db_session.query(Activity)
.filter(Activity.user_id == user_id, Activity.gear_id == gearID)
.filter(Activity.user_id == user_id, Activity.gear_id == gear_id)
.order_by(desc(Activity.start_time))
.all()
)
Expand All @@ -611,7 +611,11 @@ async def read_activities_gearactivities(
]

# Include metadata in the response
metadata = {"total_records": len(activity_records)}
metadata = {
"total_records": len(activity_records),
"gear_id": gear_id,
"api_version": API_VERSION,
}

# Return the queried values using JSONResponse
return JSONResponse(
Expand Down Expand Up @@ -655,12 +659,10 @@ async def read_activities_all_number(
activity_count = db_session.query(func.count(Activity.id)).scalar()

# Include metadata in the response
metadata = {"total_records": 1}
metadata = {"total_records": 1, "api_version": API_VERSION}

# Return the queried values using JSONResponse
return JSONResponse(
content={"metadata": metadata, "content": activity_count}
)
return JSONResponse(content={"metadata": metadata, "content": activity_count})

except JWTError:
# Return an error response if the user is not authenticated
Expand Down Expand Up @@ -701,12 +703,10 @@ async def read_activities_useractivities_number(
)

# Include metadata in the response
metadata = {"total_records": 1}
metadata = {"total_records": 1, "api_version": API_VERSION}

# Return the queried values using JSONResponse
return JSONResponse(
content={"metadata": metadata, "content": activity_count}
)
return JSONResponse(content={"metadata": metadata, "content": activity_count})

except JWTError:
# Return an error response if the user is not authenticated
Expand Down Expand Up @@ -756,12 +756,10 @@ async def read_activities_followed_useractivities_number(
)

# Include metadata in the response
metadata = {"total_records": 1}
metadata = {"total_records": 1, "api_version": API_VERSION}

# Return the queried values using JSONResponse
return JSONResponse(
content={"metadata": metadata, "content": activity_count}
)
return JSONResponse(content={"metadata": metadata, "content": activity_count})

except JWTError:
# Return an error response if the user is not authenticated
Expand Down Expand Up @@ -826,6 +824,7 @@ async def read_activities_all_pagination(
"total_records": len(activity_records),
"page_number": pageNumber,
"num_records": numRecords,
"api_version": API_VERSION,
}

# Return the queried values using JSONResponse
Expand Down Expand Up @@ -891,6 +890,7 @@ async def read_activities_useractivities_pagination(
"total_records": len(activity_records),
"page_number": pageNumber,
"num_records": numRecords,
"api_version": API_VERSION,
}

# Return the queried values using JSONResponse
Expand Down Expand Up @@ -966,6 +966,7 @@ async def read_activities_followed_user_activities_pagination(
"total_records": len(activity_records),
"page_number": pageNumber,
"num_records": numRecords,
"api_version": API_VERSION,
}

# Return the queried values using JSONResponse
Expand Down Expand Up @@ -1032,7 +1033,11 @@ async def read_activities_activityFromId(
]

# Include metadata in the response
metadata = {"total_records": len(activity_records)}
metadata = {
"total_records": len(activity_records),
"id": id,
"api_version": API_VERSION,
}

# Return the queried values using JSONResponse
return JSONResponse(
Expand Down
65 changes: 36 additions & 29 deletions backend/controllers/followerController.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
)
from dependencies import get_db_session, create_error_response
from sqlalchemy.orm import Session
from constants import API_VERSION

# Define the API router
router = APIRouter()
Expand Down Expand Up @@ -97,7 +98,12 @@ async def read_followers_user_specific_user(

if follower:
# Include metadata in the response
metadata = {"total_records": 1}
metadata = {
"total_records": 1,
"user_id": user_id,
"target_user_id": target_user_id,
"api_version": API_VERSION,
}

# User follows target_user_id or vice versa
response_data = {
Expand All @@ -112,14 +118,18 @@ async def read_followers_user_specific_user(
)

# Users are not following each other
return create_error_response("NOT_FOUND", "Users are not following each other.", 404)
return create_error_response(
"NOT_FOUND", "Users are not following each other.", 404
)

except JWTError:
# Return an error response if the user is not authenticated
return create_error_response("UNAUTHORIZED", "Unauthorized", 401)
except Exception as err:
# Log the error and return an error response
logger.error(f"Error in read_followers_user_specific_user: {err}", exc_info=True)
logger.error(
f"Error in read_followers_user_specific_user: {err}", exc_info=True
)
return create_error_response(
"INTERNAL_SERVER_ERROR", "Internal Server Error", 500
)
Expand Down Expand Up @@ -156,12 +166,10 @@ async def get_user_follower_count_all(
)

# Include metadata in the response
metadata = {"total_records": 1}
metadata = {"total_records": 1, "user_id": user_id, "api_version": API_VERSION}

# Return the queried values using JSONResponse
return JSONResponse(
content={"metadata": metadata, "content": follower_count}
)
return JSONResponse(content={"metadata": metadata, "content": follower_count})

except JWTError:
# Return an error response if the user is not authenticated
Expand All @@ -174,7 +182,6 @@ async def get_user_follower_count_all(
)



@router.get("/followers/user/{user_id}/followers/count")
async def get_user_follower_count(
user_id: int,
Expand Down Expand Up @@ -208,12 +215,10 @@ async def get_user_follower_count(
)

# Include metadata in the response
metadata = {"total_records": 1}
metadata = {"total_records": 1, "user_id": user_id, "api_version": API_VERSION}

# Return the queried values using JSONResponse
return JSONResponse(
content={"metadata": metadata, "content": follower_count}
)
return JSONResponse(content={"metadata": metadata, "content": follower_count})

except JWTError:
# Return an error response if the user is not authenticated
Expand Down Expand Up @@ -263,12 +268,14 @@ async def get_user_follower_all(
]

# Include metadata in the response
metadata = {"total_records": len(followers_list)}
metadata = {
"total_records": len(followers_list),
"user_id": user_id,
"api_version": API_VERSION,
}

# Return the queried values using JSONResponse
return JSONResponse(
content={"metadata": metadata, "content": followers_list}
)
return JSONResponse(content={"metadata": metadata, "content": followers_list})

except JWTError:
# Return an error response if the user is not authenticated
Expand Down Expand Up @@ -312,12 +319,10 @@ async def get_user_following_count_all(
)

# Include metadata in the response
metadata = {"total_records": 1}
metadata = {"total_records": 1, "user_id": user_id, "api_version": API_VERSION}

# Return the queried values using JSONResponse
return JSONResponse(
content={"metadata": metadata, "content": following_count}
)
return JSONResponse(content={"metadata": metadata, "content": following_count})

except JWTError:
# Return an error response if the user is not authenticated
Expand Down Expand Up @@ -363,12 +368,10 @@ async def get_user_following_count(
)

# Include metadata in the response
metadata = {"total_records": 1}
metadata = {"total_records": 1, "user_id": user_id, "api_version": API_VERSION}

# Return the queried values using JSONResponse
return JSONResponse(
content={"metadata": metadata, "content": following_count}
)
return JSONResponse(content={"metadata": metadata, "content": following_count})

except JWTError:
# Return an error response if the user is not authenticated
Expand Down Expand Up @@ -421,12 +424,14 @@ async def get_user_following_all(
]

# Include metadata in the response
metadata = {"total_records": len(following_list)}
metadata = {
"total_records": len(following_list),
"user_id": user_id,
"api_version": API_VERSION,
}

# Return the queried values using JSONResponse
return JSONResponse(
content={"metadata": metadata, "content": following_list}
)
return JSONResponse(content={"metadata": metadata, "content": following_list})

except JWTError:
# Return an error response if the user is not authenticated
Expand Down Expand Up @@ -543,7 +548,9 @@ async def create_follow(

if existing_follow:
# Follow relationship already exists
return create_error_response("BAD_REQUEST", "Follow relationship already exists.", 400)
return create_error_response(
"BAD_REQUEST", "Follow relationship already exists.", 400
)

# Create a new follow relationship
new_follow = Follower(
Expand Down
Loading

0 comments on commit d197c6a

Please sign in to comment.