Skip to content

Commit

Permalink
Add posts repository
Browse files Browse the repository at this point in the history
  • Loading branch information
Lekuruu committed Feb 23, 2024
1 parent 51428fd commit 0ca787e
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions database/repositories/posts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@

from __future__ import annotations

from app.common.database.objects import DBForumPost
from .wrapper import session_wrapper

from sqlalchemy.orm import Session
from typing import List

@session_wrapper
def fetch_one(id: int, session: Session | None = None) -> DBForumPost | None:
return session.query(DBForumPost) \
.filter(DBForumPost.id == id) \
.first()

@session_wrapper
def fetch_all_by_topic(
topic_id: int,
session: Session | None = None
) -> List[DBForumPost]:
return session.query(DBForumPost) \
.filter(DBForumPost.topic_id == topic_id) \
.filter(DBForumPost.hidden == False) \
.order_by(DBForumPost.id.asc()) \
.all()

@session_wrapper
def fetch_range_by_topic(
topic_id: int,
range: int,
offset: int,
session: Session | None = None
) -> List[DBForumPost]:
return session.query(DBForumPost) \
.filter(DBForumPost.topic_id == topic_id) \
.filter(DBForumPost.hidden == False) \
.order_by(DBForumPost.id.asc()) \
.limit(range) \
.offset(offset) \
.all()

@session_wrapper
def fetch_initial_post(topic_id: int, session: Session | None = None) -> DBForumPost | None:
return session.query(DBForumPost) \
.filter(DBForumPost.topic_id == topic_id) \
.filter(DBForumPost.hidden == False) \
.order_by(DBForumPost.id.asc()) \
.first()

@session_wrapper
def fetch_last_post(topic_id: int, session: Session | None = None) -> DBForumPost | None:
return session.query(DBForumPost) \
.filter(DBForumPost.topic_id == topic_id) \
.filter(DBForumPost.hidden == False) \
.order_by(DBForumPost.id.desc()) \
.first()

0 comments on commit 0ca787e

Please sign in to comment.