Skip to content

Commit

Permalink
updated pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
LLkaia committed Dec 23, 2023
1 parent 510c59b commit a7855fb
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 45 deletions.
2 changes: 0 additions & 2 deletions server/app.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
from fastapi import FastAPI
from fastapi_pagination import add_pagination

from server.routes.search_result import router as SearchResultRouter


app = FastAPI()
add_pagination(app)
app.include_router(SearchResultRouter, tags=["News"], prefix="/news")


Expand Down
29 changes: 18 additions & 11 deletions server/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,17 @@ async def retrieve_search_result_by_id(id_: str):
return


async def retrieve_search_results_by_tags(tags: list[str]):
async def retrieve_search_results_by_tags(tags: list[str], page: int, limit: int):
"""Find articles by tags
Take search words and check if database contain articles,
which have more than :percentage: of words in 'tags' fields matches
with words in search query. If database have them, return
this articles.
paginated articles and total amount of them.
:param limit: Page size
:param page: Number of page
:param tags: List of search words
:return: List of articles
:return: Count and List of articles
"""
percentage = 0.75
tags = list(set(tags))
Expand All @@ -84,16 +86,21 @@ async def retrieve_search_results_by_tags(tags: list[str]):
}
}
}
documents = search_results_collection.find(filter_expression)
return [search_results_helper(result) async for result in documents]
results = search_results_collection.find(filter_expression).skip((page - 1) * limit).limit(limit)
count = await search_results_collection.count_documents(filter_expression)
return count, [search_results_helper(result) async for result in results]


async def retrieve_newest_search_results():
"""Get 20 newest articles from database"""
results = []
async for result in search_results_collection.find().sort('date', -1).limit(20):
results.append(search_results_helper(result))
return results
async def retrieve_newest_search_results(page: int, limit: int):
"""Get the newest articles from database
:param limit: Page size
:param page: Number of page
:return: Count and List of articles
"""
results = search_results_collection.find().sort('date', -1).skip((page - 1) * limit).limit(limit)
count = await search_results_collection.count_documents({})
return count, [search_results_helper(result) async for result in results]


async def update_content_of_article(id_: str, content: list[list]):
Expand Down
21 changes: 4 additions & 17 deletions server/models/search_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,10 @@ class ArticleModel(BaseModel):
description: str = ""
tags: set[str] = set()

model_config = {
"json_schema_extra": {
"examples": [
{
"id": "657b4a8d9e6d5419e28aa3e1",
"link": "https://www.laptopmag.com/best-picks/tips-to-improve-macbook-sound",
"tags": ["acer", "aspire", "nvidia"],
"image": "https://cdn.mos.cms.futurecdn.net/vzWy7ZzZy4rfZUESfUw4Lg.jpg",
"title": "7 ways to improve sound on your MacBook",
"author": "Alex Bracetti",
"date": "2023-05-20T07:00:53Z",
"description": "Unhappy with the MacBook’s sound quality? Here are some tips and tricks to enhance "
"the audio performance on your Apple laptop."
},
]
}
}

class SearchResponseModel(BaseModel):
count: int
results: list[ArticleModel]


class ExtendArticleModel(ArticleModel):
Expand Down
27 changes: 12 additions & 15 deletions server/routes/search_result.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from fastapi import APIRouter, status, HTTPException, Query
from fastapi_pagination import Page, paginate
from fastapi import APIRouter, status, HTTPException

from server.scraper import scrap_content
from server.models.search_result import ArticleModel, ExtendArticleModel
from server.models.search_result import SearchResponseModel, ExtendArticleModel
from server.database import (
update_search_results,
retrieve_search_result_by_id,
Expand All @@ -13,13 +12,10 @@


router = APIRouter()
Page = Page.with_custom_options(
size=Query(5, ge=1, le=10),
)


@router.get("/search", status_code=status.HTTP_200_OK, response_model=Page[ArticleModel])
async def get_search_results(find: str | None = None) -> Page[ArticleModel]:
@router.get("/search", status_code=status.HTTP_200_OK, response_model=SearchResponseModel)
async def get_search_results(find: str | None = None, page: int = 1, limit: int = 5):
"""Find articles by search query
Get list of articles which match with search query from database.
Expand All @@ -28,16 +24,17 @@ async def get_search_results(find: str | None = None) -> Page[ArticleModel]:
articles.
"""
if find:
results = await retrieve_search_results_by_tags(find.split())
# if len(results) < 5:
# await update_search_results(find)
# results = await retrieve_search_results_by_tags(find.split())
return paginate(results)
return paginate(await retrieve_newest_search_results())
count, results = await retrieve_search_results_by_tags(find.split(), page, limit)
if count < 5:
await update_search_results(find)
count, results = await retrieve_search_results_by_tags(find.split(), page, limit)
return {'count': count, 'results': results}
count, results = await retrieve_newest_search_results(page, limit)
return {'count': count, 'results': results}


@router.get("/{id}", status_code=status.HTTP_200_OK, response_model=ExtendArticleModel)
async def get_article(id: str) -> ExtendArticleModel:
async def get_article(id: str):
"""Get concrete article with content
Find article by ID in database and if it exists, check if it
Expand Down

0 comments on commit a7855fb

Please sign in to comment.