jiachenlong/backend/app/routers/news.py

263 lines
6.0 KiB
Python

# news - 新闻路由
# Version: 0.0.1
# 更新:
from fastapi import APIRouter, Depends, HTTPException, Query
# 更新:
# Version: 1.2.x
# 更新:
from sqlalchemy import Table, MetaData
# 更新:
from sqlalchemy.orm import Session
# 更新:
from pydantic import BaseModel
# 更新:
from typing import Optional, List
# 更新:
from datetime import datetime, date
# 更新:
from app.core.database import get_db, engine
# 更新:
from app.models.models import User
# 更新:
from app.routers.auth import get_current_user
# 更新:
# 更新:
router = APIRouter(prefix="/api/news", tags=["资讯"])
# 更新:
metadata = MetaData()
# 更新:
# 更新:
# 分类表
# 更新:
categories_table = Table('news_categories', metadata, autoload_with=engine)
# 更新:
news_table = Table('news', metadata, autoload_with=engine)
# 更新:
user_posts_table = Table('user_posts', metadata, autoload_with=engine)
# 更新:
users_table = Table('users', metadata, autoload_with=engine)
# 更新:
deals_table = Table('deals', metadata, autoload_with=engine)
# 更新:
notifications_table = Table('notifications', metadata, autoload_with=engine)
# 更新:
# 更新:
# ============ 获取分类 ============
# 更新:
@router.get("/categories")
# 更新:
def get_categories(db: Session = Depends(get_db)):
# 更新:
results = db.query(categories_table).order_by(categories_table.c.sort_order).all()
# 更新:
return [dict(r._mapping) for r in results]
# 更新:
# 更新:
# ============ 获取资讯 ============
# 更新:
@router.get("")
# 更新:
def get_news(
# 更新:
category_id: Optional[int] = None,
# 更新:
page: int = 1,
# 更新:
limit: int = 20,
# 更新:
db: Session = Depends(get_db)
# 更新:
):
# 更新:
query = db.query(news_table)
# 更新:
if category_id:
# 更新:
query = query.filter(news_table.c.category_id == category_id)
# 更新:
offset = (page - 1) * limit
# 更新:
results = query.order_by(news_table.c.created_at.desc()).offset(offset).limit(limit).all()
# 更新:
return [dict(r._mapping) for r in results]
# 更新:
# 更新:
# ============ 获取用户发布 ============
# 更新:
@router.get("/posts")
# 更新:
def get_posts(
# 更新:
post_type: Optional[str] = None,
# 更新:
status: str = "active",
# 更新:
page: int = 1,
# 更新:
limit: int = 20,
# 更新:
db: Session = Depends(get_db)
# 更新:
):
# 更新:
query = db.query(user_posts_table).filter(user_posts_table.c.status == status)
# 更新:
if post_type:
# 更新:
query = query.filter(user_posts_table.c.post_type == post_type)
# 更新:
offset = (page - 1) * limit
# 更新:
results = query.order_by(user_posts_table.c.created_at.desc()).offset(offset).limit(limit).all()
# 更新:
return [dict(r._mapping) for r in results]
# 更新:
# 更新:
# ============ 创建发布 ============
# 更新:
class PostCreate(BaseModel):
# 更新:
post_type: str
# 更新:
title: str
# 更新:
content: Optional[str] = None
# 更新:
zodiac_type: Optional[str] = None
# 更新:
packaging: Optional[str] = None
# 更新:
# 更新:
@router.post("/posts")
# 更新:
def create_post(
# 更新:
post: PostCreate,
# 更新:
current_user: User = Depends(get_current_user),
# 更新:
db: Session = Depends(get_db)
# 更新:
):
# 更新:
result = db.execute(user_posts_table.insert().values(
# 更新:
user_id=current_user.f99_90_id,
# 更新:
post_type=post.post_type,
# 更新:
title=post.title,
# 更新:
content=post.content,
# 更新:
zodiac_type=post.zodiac_type,
# 更新:
packaging=post.packaging,
# 更新:
status="pending"
# 更新:
))
# 更新:
db.commit()
# 更新:
return {"success": True, "id": result.inserted_primary_key[0]}
# 更新:
# 更新:
# ============ 成交数据 ============
# 更新:
@router.get("/deals")
# 更新:
def get_deals(
# 更新:
zodiac_type: Optional[str] = None,
# 更新:
limit: int = 20,
# 更新:
db: Session = Depends(get_db)
# 更新:
):
# 更新:
query = db.query(deals_table)
# 更新:
if zodiac_type:
# 更新:
query = query.filter(deals_table.c.zodiac_type == zodiac_type)
# 更新:
results = query.order_by(deals_table.c.deal_date.desc()).limit(limit).all()
# 更新:
return [dict(r._mapping) for r in results]
# 更新:
# 更新:
# ============ 通知 ============
# 更新:
@router.get("/notifications")
# 更新:
def get_notifications(limit: int = 10, db: Session = Depends(get_db)):
# 更新:
results = db.query(notifications_table).filter(
# 更新:
notifications_table.c.is_published == True
# 更新:
).order_by(notifications_table.c.created_at.desc()).limit(limit).all()
# 更新:
return [dict(r._mapping) for r in results]
# 更新:
# 更新:
# ============ 首页数据 ============
# 更新:
@router.get("/home")
# 更新:
def get_home(db: Session = Depends(get_db)):
# 更新:
# 推荐发布
# 更新:
posts = db.query(user_posts_table).filter(
# 更新:
user_posts_table.c.status == "active"
# 更新:
).order_by(user_posts_table.c.created_at.desc()).limit(10).all()
# 更新:
# 更新:
# 成交
# 更新:
deals = db.query(deals_table).order_by(
# 更新:
deals_table.c.deal_date.desc()
# 更新:
).limit(10).all()
# 更新:
# 更新:
# 通知
# 更新:
notices = db.query(notifications_table).filter(
# 更新:
notifications_table.c.is_published == True
# 更新:
).order_by(notifications_table.c.created_at.desc()).limit(5).all()
# 更新:
# 更新:
return {
# 更新:
"posts": [dict(p._mapping) for p in posts],
# 更新:
"deals": [dict(d._mapping) for d in deals],
# 更新:
"notices": [dict(n._mapping) for n in notices]
# 更新:
}
# 更新: