added post object

This commit is contained in:
Nikolai 2021-05-29 17:45:03 -07:00
parent 92849d8396
commit 1e9405bad0
2 changed files with 42 additions and 0 deletions

View File

@ -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

0
tests/test_post.py Normal file
View File