49 lines
1.3 KiB
Python
49 lines
1.3 KiB
Python
from fastapi import APIRouter, Depends, HTTPException
|
|
from core.database import fake_users_db
|
|
import uuid
|
|
from typing import Optional
|
|
from pydantic import BaseModel
|
|
|
|
router = APIRouter()
|
|
|
|
class BlogPost(BaseModel):
|
|
title: str
|
|
content: str
|
|
author_id: Optional[str] = None
|
|
|
|
@router.post("/posts")
|
|
async def create_blog_post(
|
|
post: BlogPost,
|
|
username: str = Depends(get_current_user_dummy)
|
|
):
|
|
"""Create a new blog post"""
|
|
post_id = str(uuid.uuid4())
|
|
|
|
if username not in fake_users_db:
|
|
raise HTTPException(status_code=404, detail="User not found")
|
|
|
|
# Simulate storing post in database
|
|
if "posts" not in fake_users_db:
|
|
fake_users_db["posts"] = {}
|
|
|
|
fake_users_db["posts"][post_id] = {
|
|
"id": post_id,
|
|
"title": post.title,
|
|
"content": post.content,
|
|
"author": username,
|
|
"created_at": "2024-01-01T00:00:00Z" # Demo timestamp
|
|
}
|
|
|
|
return {
|
|
"message": "Blog post created successfully",
|
|
"post_id": post_id,
|
|
"data": {
|
|
"title": post.title,
|
|
"author": username,
|
|
"created_at": "2024-01-01T00:00:00Z"
|
|
},
|
|
"metadata": {
|
|
"word_count": len(post.content.split()),
|
|
"status": "published"
|
|
}
|
|
} |