Changed only_ids behavior + sorting added

This commit is contained in:
Profitroll 2023-01-23 16:39:11 +01:00
parent 546600a29e
commit 1294d1a037

View File

@ -3,6 +3,7 @@ from urllib.parse import quote_plus
from os import remove from os import remove
from typing import Dict, List, Literal, Union from typing import Dict, List, Literal, Union
from xmltodict import parse from xmltodict import parse
from pymongo import DESCENDING
from models.saves import StardewSave, StardewSaveBrief from models.saves import StardewSave, StardewSaveBrief
from modules.app import app, get_api_key, user_by_key from modules.app import app, get_api_key, user_by_key
from modules.database import col_devices, col_saves from modules.database import col_devices, col_saves
@ -27,29 +28,25 @@ async def saves_get(device: Union[str, None] = None, version: Union[str, None] =
if version is not None: if version is not None:
query["data.game_version"] = version query["data.game_version"] = version
saves_entries = list(col_saves.find(query)) saves_entries = list(col_saves.find(query).sort("date", DESCENDING))
if len(saves_entries) == 0: if len(saves_entries) == 0:
raise HTTPException(HTTP_404_NOT_FOUND, detail="Could not find any saves.") raise HTTPException(HTTP_404_NOT_FOUND, detail="Could not find any saves.")
output = [] output = []
added = []
if only_ids is True: for entry in saves_entries:
for entry in saves_entries: out_entry = entry
if entry["id"] not in output: del out_entry["_id"]
output.append( del out_entry["user"]
{ del out_entry["file"]
"id": entry["id"], if only_ids is True:
"farmer": entry["data"]["farmer"] if entry["id"] in added:
} continue
) else:
else: added.append(entry["id"])
for entry in saves_entries: output.append(out_entry)
out_entry = entry
del out_entry["_id"]
del out_entry["user"]
del out_entry["file"]
output.append(out_entry)
return UJSONResponse(output) return UJSONResponse(output)
@ -65,7 +62,7 @@ async def saves_get_by_id(id: int, device: Union[str, None] = None, version: Uni
if version is not None: if version is not None:
query["data.game_version"] = version query["data.game_version"] = version
saves_entries = list(col_saves.find(query)) saves_entries = list(col_saves.find(query).sort("date", DESCENDING))
if len(saves_entries) == 0: if len(saves_entries) == 0:
raise HTTPException(HTTP_404_NOT_FOUND, detail="Could not find save with such id.") raise HTTPException(HTTP_404_NOT_FOUND, detail="Could not find save with such id.")