From 84be0c715452e79d73f7d8a564fd7a4823892ffd Mon Sep 17 00:00:00 2001 From: profitroll Date: Thu, 19 Jan 2023 13:58:18 +0100 Subject: [PATCH] Versions added --- extensions/saves.py | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/extensions/saves.py b/extensions/saves.py index 76d1ba4..d3eb693 100644 --- a/extensions/saves.py +++ b/extensions/saves.py @@ -15,10 +15,19 @@ from modules.utils import zip_saves @app.get("/saves", response_class=UJSONResponse, response_model=Dict[str, StardewSave], description="Get all available game saves") -async def saves_get(device: Union[str, None] = None, apikey: APIKey = Depends(get_api_key)): +async def saves_get(device: Union[str, None] = None, version: Union[str, None] = None, apikey: APIKey = Depends(get_api_key)): user = user_by_key(apikey) - saves_entries = list(col_saves.find({"user": user})) if device is None else list(col_saves.find({"user": user_by_key(apikey), "user": user})) + + query = {"user": user} + + if device is not None: + query["device"] = device + + if version is not None: + query["data.game_version"] = version + + saves_entries = list(col_saves.find(query)) if len(saves_entries) == 0: raise HTTPException(HTTP_404_NOT_FOUND, detail="Could not find any saves.") @@ -36,9 +45,17 @@ async def saves_get(device: Union[str, None] = None, apikey: APIKey = Depends(ge @app.get("/saves/{id}", response_class=UJSONResponse, response_model=List[StardewSave], description="Get game saves by name") -async def saves_get_by_id(id: int, device: Union[str, None] = None, apikey: APIKey = Depends(get_api_key)): +async def saves_get_by_id(id: int, device: Union[str, None] = None, version: Union[str, None] = None, apikey: APIKey = Depends(get_api_key)): - saves_entries = list(col_saves.find({"id": id})) if device is None else list(col_saves.find({"id": id, "device": device})) + query = {"id": id} + + if device is not None: + query["device"] = device + + if version is not None: + query["data.game_version"] = version + + saves_entries = list(col_saves.find(query)) if len(saves_entries) == 0: raise HTTPException(HTTP_404_NOT_FOUND, detail="Could not find save with such id.") @@ -57,7 +74,14 @@ async def saves_get_by_id(id: int, device: Union[str, None] = None, apikey: APIK @app.get("/saves/{id}/{save_date}", response_class=UJSONResponse, response_model=List[StardewSave], description="Get game saves by name") async def saves_get_by_both_ids(id: int, save_date: int, device: Union[str, None] = None, apikey: APIKey = Depends(get_api_key)): - saves_entry = col_saves.find_one({"user": user_by_key(apikey), "id": id, "date": save_date}) if device is None else col_saves.find_one({"user": user_by_key(apikey), "id": id, "device": device, "date": save_date}) + + query = {"user": user_by_key(apikey), "id": id, "date": save_date} + + if device is not None: + query["device"] = device + + saves_entry = col_saves.find_one(query) + if saves_entry is not None: del saves_entry["_id"] del saves_entry["file"]