From: Adam Dullage Date: Mon, 13 Jun 2022 18:16:11 +0000 (+0100) Subject: Added pagination to get notes endpoint X-Git-Url: http://git.99rst.org/?a=commitdiff_plain;h=6247c3ac52d49f8ed55f751372ab93e3f556be26;p=flatnotes.git Added pagination to get notes endpoint --- diff --git a/flatnotes/main.py b/flatnotes/main.py index 409db0b..2a8badf 100644 --- a/flatnotes/main.py +++ b/flatnotes/main.py @@ -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] ]