Added pagination to get notes endpoint
authorAdam Dullage <redacted>
Mon, 13 Jun 2022 18:16:11 +0000 (19:16 +0100)
committerAdam Dullage <redacted>
Mon, 13 Jun 2022 18:16:11 +0000 (19:16 +0100)
flatnotes/main.py

index 409db0b5c008f3e8aec737a3450abb3964da0b1c..2a8badff504f0bbd5edf33c7e57396ba51949e47 100644 (file)
@@ -1,6 +1,6 @@
 import logging
 import os
-from typing import List
+from typing import List, Literal
 
 from auth import (
     FLATNOTES_PASSWORD,
@@ -56,12 +56,24 @@ async def root(filename: str = ""):
 
 @app.get("/api/notes", response_model=List[NoteModel])
 async def get_notes(
-    include_content: bool = False, _: str = Depends(validate_token)
+    start: int = 0,
+    limit: int = None,
+    sort: Literal["filename", "lastModified"] = "filename",
+    order: Literal["asc", "desc"] = "asc",
+    include_content: bool = False,
+    _: str = Depends(validate_token),
 ):
     """Get all notes."""
+    notes = flatnotes.get_notes()
+    notes.sort(
+        key=lambda note: note.last_modified
+        if sort == "lastModified"
+        else note.filename,
+        reverse=order == "desc",
+    )
     return [
         NoteModel.dump(note, include_content=include_content)
-        for note in flatnotes.get_notes()
+        for note in notes[start : None if limit is None else start + limit]
     ]
 
 
git clone https://git.99rst.org/PROJECT