From 1e9405bad053763e49b8e226b704b0556ec34d04 Mon Sep 17 00:00:00 2001 From: Nikolai Date: Sat, 29 May 2021 17:45:03 -0700 Subject: [PATCH] added post object --- amanuensis/backend/post.py | 42 ++++++++++++++++++++++++++++++++++++++ tests/test_post.py | 0 2 files changed, 42 insertions(+) create mode 100644 amanuensis/backend/post.py create mode 100644 tests/test_post.py diff --git a/amanuensis/backend/post.py b/amanuensis/backend/post.py new file mode 100644 index 0000000..9b88138 --- /dev/null +++ b/amanuensis/backend/post.py @@ -0,0 +1,42 @@ +""" +Post query interface +""" + +import re + +from sqlalchemy import select, func + +from amanuensis.db import DbContext, Post +from amanuensis.errors import ArgumentError + +def create( + db: DbContext, + lexicon_id: int, + user_id: int, + body: str) -> Post: + """ + Create a new post + """ + + # Verify lexicon id + if not isinstance(lexicon_id, int): + raise ArgumentError('Lexicon id must be an integer.') + + # Verify user_id + if not isinstance(user_id, int): + raise ArgumentError('Lexicon id must ne an integer.') + + # Verify body + if not isinstance(body, str): + raise ArgumentError('Post body must be a string.') + if not body.strip(): + raise ArgumentError('Post body cannot be empty.') + + new_post = Post( + lexicon_id=lexicon_id, + user_id=user_id, + body=body + ) + db.session.add(new_post) + db.session.commit() + return new_post \ No newline at end of file diff --git a/tests/test_post.py b/tests/test_post.py new file mode 100644 index 0000000..e69de29