Skip to content

Commit 64b1e75

Browse files
authored
Merge pull request #108 from BUAA-SE-coders007/fix/107
[fix]: 更新点击量设置
2 parents 1aab435 + d738ca3 commit 64b1e75

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

app/curd/articleDB.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ async def get_article_in_db(db: AsyncSession, get_article: GetArticle):
1919
if get_article.id:
2020
result = await db.execute(select(ArticleDB).where(ArticleDB.id == get_article.id))
2121
articles = result.scalars().first()
22+
if not articles:
23+
return [], 0
24+
articles.clicks += 1 # 增加点击量
25+
await db.commit()
26+
await db.refresh(articles) # 刷新以获取最新数据
2227
total_count = 1
2328
articles = [articles] if articles else []
2429
elif get_article.page and get_article.page_size:
@@ -61,6 +66,11 @@ async def search_article_in_db(db: AsyncSession, search_article: SearchArticle):
6166
result = await db.execute(select(ArticleDB).where(ArticleDB.title.like(f"%{search_article.query}%")))
6267
articles = result.scalars().all()
6368
total_count = len(articles)
69+
# 更新所有搜索到文章的点击量
70+
for article in articles:
71+
article.clicks += 1 # 增加点击量
72+
await db.commit()
73+
await db.refresh(article)
6474
return [GetResponse.model_validate(article) for article in articles], total_count
6575

6676
async def get_article_in_db_by_id(db: AsyncSession, article_id: int):

0 commit comments

Comments
 (0)