mirror of
https://gitverse.ru/kpa39l/telepost.git
synced 2026-09-29 09:55:09 +00:00
182 lines
5.4 KiB
Python
182 lines
5.4 KiB
Python
from typing import List
|
|
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from sqlalchemy.orm import Session
|
|
from ....database.database import get_db
|
|
from ....auth.jwt import get_current_user
|
|
from ....models.user import User
|
|
from ....schemas.post import Post, PostCreate, PostUpdate
|
|
from ....services.post_service import PostService
|
|
|
|
router = APIRouter(prefix="/posts", tags=["posts"])
|
|
|
|
|
|
@router.get("/", response_model=List[Post])
|
|
def get_posts(
|
|
skip: int = 0,
|
|
limit: int = 100,
|
|
current_user: User = Depends(get_current_user),
|
|
db: Session = Depends(get_db)
|
|
):
|
|
"""
|
|
Получить список постов пользователя
|
|
"""
|
|
post_service = PostService(db)
|
|
posts = post_service.get_posts(owner_id=current_user.id, skip=skip, limit=limit)
|
|
return posts
|
|
|
|
|
|
@router.post("/", response_model=Post)
|
|
def create_post(
|
|
post: PostCreate,
|
|
current_user: User = Depends(get_current_user),
|
|
db: Session = Depends(get_db)
|
|
):
|
|
"""
|
|
Создать новый пост
|
|
"""
|
|
post_service = PostService(db)
|
|
db_post = post_service.create_post(post_data=post, owner_id=current_user.id)
|
|
return db_post
|
|
|
|
|
|
@router.get("/{post_id}", response_model=Post)
|
|
def get_post(
|
|
post_id: int,
|
|
current_user: User = Depends(get_current_user),
|
|
db: Session = Depends(get_db)
|
|
):
|
|
"""
|
|
Получить конкретный пост
|
|
"""
|
|
post_service = PostService(db)
|
|
db_post = post_service.get_post(post_id=post_id, owner_id=current_user.id)
|
|
if not db_post:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Post not found"
|
|
)
|
|
return db_post
|
|
|
|
|
|
@router.put("/{post_id}", response_model=Post)
|
|
def update_post(
|
|
post_id: int,
|
|
post_update: PostUpdate,
|
|
current_user: User = Depends(get_current_user),
|
|
db: Session = Depends(get_db)
|
|
):
|
|
"""
|
|
Обновить пост
|
|
"""
|
|
post_service = PostService(db)
|
|
db_post = post_service.update_post(
|
|
post_id=post_id,
|
|
post_data=post_update,
|
|
owner_id=current_user.id
|
|
)
|
|
if not db_post:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Post not found"
|
|
)
|
|
return db_post
|
|
|
|
|
|
@router.delete("/{post_id}")
|
|
def delete_post(
|
|
post_id: int,
|
|
current_user: User = Depends(get_current_user),
|
|
db: Session = Depends(get_db)
|
|
):
|
|
"""
|
|
Удалить пост
|
|
"""
|
|
post_service = PostService(db)
|
|
success = post_service.delete_post(post_id=post_id, owner_id=current_user.id)
|
|
if not success:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Post not found"
|
|
)
|
|
return {"message": "Post deleted successfully"}
|
|
|
|
|
|
@router.post("/{post_id}/publish_telegram")
|
|
def publish_post_to_telegram(
|
|
post_id: int,
|
|
chat_id: str,
|
|
current_user: User = Depends(get_current_user),
|
|
db: Session = Depends(get_db)
|
|
):
|
|
"""
|
|
Опубликовать пост в Telegram
|
|
"""
|
|
post_service = PostService(db)
|
|
|
|
# Проверяем, что пост принадлежит пользователю
|
|
post = post_service.get_post(post_id=post_id, owner_id=current_user.id)
|
|
if not post:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Post not found"
|
|
)
|
|
|
|
try:
|
|
success = post_service.publish_post_to_telegram(post_id=post_id, chat_id=chat_id)
|
|
if success:
|
|
return {"message": "Post published to Telegram successfully", "success": True}
|
|
else:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
|
detail="Failed to publish post to Telegram"
|
|
)
|
|
except ValueError as e:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
|
detail=str(e)
|
|
)
|
|
except Exception as e:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
|
detail=f"Error publishing post to Telegram: {str(e)}"
|
|
)
|
|
|
|
|
|
@router.post("/{post_id}/publish_twitter")
|
|
def publish_post_to_twitter(
|
|
post_id: int,
|
|
current_user: User = Depends(get_current_user),
|
|
db: Session = Depends(get_db)
|
|
):
|
|
"""
|
|
Опубликовать пост в Twitter
|
|
"""
|
|
post_service = PostService(db)
|
|
|
|
# Проверяем, что пост принадлежит пользователю
|
|
post = post_service.get_post(post_id=post_id, owner_id=current_user.id)
|
|
if not post:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Post not found"
|
|
)
|
|
|
|
try:
|
|
success = post_service.publish_post_to_twitter(post_id=post_id)
|
|
if success:
|
|
return {"message": "Post published to Twitter successfully", "success": True}
|
|
else:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
|
detail="Failed to publish post to Twitter"
|
|
)
|
|
except ValueError as e:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
|
detail=str(e)
|
|
)
|
|
except Exception as e:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
|
detail=f"Error publishing post to Twitter: {str(e)}"
|
|
) |