2022-10-06 07:40:03 +00:00
|
|
|
from typing import cast, Optional
|
|
|
|
|
|
|
|
from sqlalchemy import select
|
|
|
|
from sqlmodel.ext.asyncio.session import AsyncSession
|
|
|
|
|
|
|
|
from init import sqlite
|
|
|
|
from models.models.lofter import Lofter
|
|
|
|
|
|
|
|
|
|
|
|
class LofterPost:
|
|
|
|
@staticmethod
|
|
|
|
async def get_by_post_and_user_id(user_id: str, post_id: str) -> Optional[Lofter]:
|
2023-08-18 13:28:16 +00:00
|
|
|
async with sqlite.session() as session:
|
2022-10-06 07:40:03 +00:00
|
|
|
session = cast(AsyncSession, session)
|
|
|
|
if user_id != "0":
|
|
|
|
check = Lofter.post_id == post_id and Lofter.user_id == user_id
|
|
|
|
else:
|
|
|
|
check = Lofter.post_id == post_id
|
|
|
|
statement = select(Lofter).where(check)
|
|
|
|
results = await session.exec(statement)
|
2022-11-04 15:54:19 +00:00
|
|
|
return post[0] if (post := results.first()) else None
|
2022-10-06 07:40:03 +00:00
|
|
|
|
2022-10-06 08:00:46 +00:00
|
|
|
@staticmethod
|
|
|
|
async def get_by_post_id(post_id: str) -> Optional[Lofter]:
|
|
|
|
return await LofterPost.get_by_post_and_user_id("0", post_id)
|
|
|
|
|
2022-10-06 07:40:03 +00:00
|
|
|
@staticmethod
|
|
|
|
async def add_post(post: Lofter):
|
2023-08-18 13:28:16 +00:00
|
|
|
async with sqlite.session() as session:
|
2022-10-06 07:40:03 +00:00
|
|
|
session = cast(AsyncSession, session)
|
|
|
|
session.add(post)
|
|
|
|
await session.commit()
|