added post order modes

This commit is contained in:
katboi01 2025-01-11 18:32:03 +01:00
parent 155394ab75
commit d7dba19b8e
2 changed files with 12 additions and 3 deletions

View File

@ -75,9 +75,11 @@ class Database:
result = self.db.run_query(query)
return result[0][0]
def get_posts(self, num_posts, artist, actions_taken = [0, 1, 2, 3], last_id = -1, offset = 0, include_ratings = ["SFW", "NSFW"]):
def get_posts(self, num_posts, artist, actions_taken = [0, 1, 2, 3], last_id = -1, offset = 0, include_ratings = ["SFW", "NSFW"], order = "DESC"):
num_posts = max(1, min(num_posts, 100))
order_by = "RANDOM()" if order == "RAND" else "x_posts.id ASC" if order == "ASC" else "x_posts.id DESC"
where_query = self.build_where_query(artist, actions_taken, last_id, include_ratings)
query = f'''
@ -87,7 +89,7 @@ class Database:
LEFT JOIN accounts
ON x_posts.account_id = accounts.id
{where_query}
ORDER BY x_posts.id DESC
ORDER BY {order_by}
LIMIT {num_posts} OFFSET {offset}'''
result = self.db.run_query(query)

View File

@ -42,10 +42,17 @@ class Archive_GetPosts(Resource):
if ratings == []:
ratings = ["SFW", "NSFW"]
if "random" in request.args:
order = "RAND"
elif "ascending" in request.args:
order = "ASC"
else:
order = "DESC"
real_page = page-1
offset = real_page * count
result = db.get_posts(count, artist, actions_taken=actions, last_id= -1, offset= offset, include_ratings= ratings)
result = db.get_posts(count, artist, actions_taken=actions, last_id= -1, offset= offset, include_ratings= ratings, order=order)
if result is None:
response = app.response_class(status=400)