diff --git a/modules/Archive/database.py b/modules/Archive/database.py index fb46146..d01baad 100644 --- a/modules/Archive/database.py +++ b/modules/Archive/database.py @@ -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) diff --git a/modules/Archive/endpoints/get_posts.py b/modules/Archive/endpoints/get_posts.py index ceaf330..9fe0fbf 100644 --- a/modules/Archive/endpoints/get_posts.py +++ b/modules/Archive/endpoints/get_posts.py @@ -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)