diff --git a/server/database.py b/server/database.py index c4f5fc6..f47b37e 100644 --- a/server/database.py +++ b/server/database.py @@ -57,6 +57,27 @@ async def retrieve_search_result_by_id(id_: str): return +async def delete_search_result_by_id(id_: str): + """Delete concrete article in a database by ID""" + try: + result = await search_results_collection.delete_one({"_id": ObjectId(id_)}) + if result: + return True + except InvalidId: + pass + return False + + +async def retrieve_authors_count_of_articles(): + """Retrieve count of articles for each author""" + group_by_author = {"$group": {"_id": "$author", "count_of_articles": {"$sum": 1}}} + sort_by_count_desc = {"$sort": {"count_of_articles": -1}} + pipeline = [group_by_author, sort_by_count_desc] + articles_count = search_results_collection.aggregate(pipeline) + articles_count = await articles_count.to_list(length=None) + return articles_count + + async def retrieve_search_results_by_tags(tags: list[str], page: int, limit: int, period: Period): """Find articles by tags