-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
39 lines (35 loc) · 1.4 KB
/
main.py
File metadata and controls
39 lines (35 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
from fastapi import FastAPI, HTTPException, Depends
from database import *
from sqlalchemy.future import select
from models import Article, Base
from schemas import ArticleLikeRequest
import asyncio
# FastAPI app Initialization
app = FastAPI()
# Async function to get the database session
async def get_db():
async with AsyncSessionLocal() as session:
yield session
# Endpoint to get like count for an article
@app.get("/api/article/like-count")
async def get_like_count(articleId: str, db: AsyncSession = Depends(get_db)):
result = await db.execute(select(Article).where(Article.article_id == articleId))
article = result.scalars().first()
if not article:
return {"likes": 0}
return {"likes": article.like_count}
# Endpoint to increment the like count for an article
@app.post("/api/article/like")
async def like_article(request: ArticleLikeRequest, db: AsyncSession = Depends(get_db)):
result = await db.execute(select(Article).where(Article.article_id == request.articleId))
article = result.scalars().first()
if not article:
# If the article does not exist, create it with a starting like count of 1
article = Article(article_id=request.articleId, like_count=1)
db.add(article)
else:
# Increment the existing like count
article.like_count += 1
await db.commit()
await db.refresh(article)
return {"likes": article.like_count}