Skip to content

Commit

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

from __future__ import annotations

from app.common.database.objects import DBForumTopic, 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) -> DBForumTopic | None:
return session.query(DBForumTopic) \
.filter(DBForumTopic.id == id) \
.first()

@session_wrapper
def fetch_all(session: Session | None = None) -> List[DBForumTopic]:
return session.query(DBForumTopic) \
.filter(DBForumTopic.hidden == False) \
.order_by(DBForumTopic.id.desc()) \
.all()

@session_wrapper
def fetch_by_forum(
forum_id: int,
session: Session | None = None
) -> List[DBForumTopic]:
return session.query(DBForumTopic) \
.filter(DBForumTopic.forum_id == forum_id) \
.filter(DBForumTopic.hidden == False) \
.order_by(DBForumTopic.id.desc()) \
.all()

@session_wrapper
def fetch_range(
limit: int,
offset: int,
session: Session | None = None
) -> List[DBForumTopic]:
return session.query(DBForumTopic) \
.filter(DBForumTopic.hidden == False) \
.order_by(DBForumTopic.id.desc()) \
.limit(limit) \
.offset(offset) \
.all()

@session_wrapper
def fetch_announcements(
limit: int,
offset: int,
session: Session | None = None
) -> List[DBForumTopic]:
return session.query(DBForumTopic) \
.filter(DBForumTopic.announcement == True) \
.filter(DBForumTopic.hidden == False) \
.order_by(DBForumTopic.id.desc()) \
.limit(limit) \
.offset(offset) \
.all()

@session_wrapper
def fetch_post_count(topic_id: int, session: Session | None = None) -> int:
return session.query(DBForumPost) \
.filter(DBForumPost.topic_id == topic_id) \
.filter(DBForumPost.hidden == False) \
.count()

0 comments on commit 51428fd

Please sign in to comment.