async def get_sorts(limit: int = 10, page: int = 1, db: Session = Depends(deps.get_db)): category = db.query(models.Category).order_by( models.Category.create_at.desc()).offset( (page - 1) * 2).limit(limit).all() return Response200(data={"category": category})
async def get_posts(limit: int = 10, page: int = 1, db: Session = Depends(deps.get_db)): posts = db.query(models.Post).order_by( models.Post.create_at.desc()).offset( (page - 1) * 2).limit(limit).all() return Response200(data={"post": posts})
async def get_reviews(post_id: int, limit: int = 10, page: int = 1, db: Session = Depends(deps.get_db)): data = db.query(models.Comment).filter_by(post_id=post_id).order_by( models.Comment.create_at.desc()).offset( (page - 1) * 2).limit(limit).all() return Response200(data={"comment": data})
async def delete_review(comment_id: int, db: Session = Depends(deps.get_db)): """删除评论""" review_obj = db.query(models.Comment).get(comment_id) if review_obj: db.delete(review_obj) db.commit() return Response200(data=review_obj) return Response404(msg="删除失败,评论不存在")
async def delete_post(post_id: int, db: Session = Depends(deps.get_db)): """文章删除""" post_obj = db.query(models.Post).get(post_id) if post_obj: db.delete(post_obj) db.commit() return Response200() return Response404(msg="删除失败,文章不存在")
async def delete_sort(sort_id: int, db: Session = Depends(deps.get_db)): """分类删除""" category_obj = db.query(models.Category).get(sort_id) if category_obj: db.delete(category_obj) db.commit() return Response200(data=category_obj) return Response404(msg="删除失败,分类不存在")
async def admin_login( auth: Auth, db: Session = Depends(deps.get_db) ): user = db.query(Admin).filter(Admin.username == auth.username).first() if not user or not security.verify_password(auth.password, user.password_hash): return Response404(msg="用户名或密码错误") token = Token(token=f"Bearer {create_access_token(user.id)}") return Response200(data=token)
async def create_review(review: ReviewCreate, db: Session = Depends(deps.get_db)): if db.query(models.Post).get(review.post_id): review_obj = models.Comment(**review.dict()) db.add(review_obj) db.commit() db.refresh(review_obj) return Response200(data={"review": review_obj}) return Response404(msg="评论失败,文章不存在")
async def create_post(post: PostCreate, db: Session = Depends(deps.get_db)): """文章创建""" if db.query(models.Category).get(post.category_id): post_obj = models.Post(**post.dict()) db.add(post_obj) db.commit() db.refresh(post_obj) return Response200(data={"post": post_obj}) else: return Response404(msg="新增失败,分类不存在")
async def update_sort(category_id: int, category: CategoryCreate, db: Session = Depends(deps.get_db)): """分类更新""" category_obj = db.query(models.Category).filter_by(id=category_id) if category_obj: category_obj.update({"name": category.name}) db.commit() return Response200(data={"category": category_obj.first()}) return Response404(msg="修改失败,分类不存在")
async def get_sort_posts(category_id: int, limit: int = 10, page: int = 1, db: Session = Depends(deps.get_db)): category = db.query(models.Category).get(category_id) if category is not None: posts = db.query(models.Post). \ filter(models.Post.category_id == category_id).order_by(models.Post.create_at.desc()).offset( (page - 1) * 2).limit(limit).all() return Response200(data={"category": category, "posts": posts}) return Response404
async def create_sort(category: CategoryCreate, db: Session = Depends(deps.get_db)): """分类创建""" category_result = db.query( models.Category).filter_by(name=category.name).first() if category_result: return Response404(msg="新增失败,分类已存在") category_obj = models.Category(name=category.name) db.add(category_obj) db.commit() db.refresh(category_obj) return Response200(data={"category": category_obj})
async def update_info(site: AdminUpdate, db: Session = Depends(deps.get_db)): """博客资料修改""" site_obj = db.query(models.Admin).filter_by(id=1) site_obj.update({ "about": site.about, "blog_sub_title": site.blog_sub_title, "password_hash": get_password_hash(site.password), "username": site.username, "name": site.name, "blog_title": site.blog_title, }) db.commit() return Response200(data=site)
async def update_post(post_id: int, post: PostUpdate, db: Session = Depends(deps.get_db)): """文章更新""" post_obj = db.query(models.Post).filter_by(id=post_id) if post_obj: post_obj.update({ "title": post.title, "body": post.body, "status": post.status, "category_id": post.category_id }) db.commit() return Response200(data={"post": post_obj.first()}) return Response404(msg="更新失败,文章不存在")
async def get_post(post_id: int, db: Session = Depends(deps.get_db)): # 连接查询返回分类名称 post_obj = db.query(models.Post).get(post_id) if post_obj is None: return Response404 post_obj.reading += 1 db.commit() # # 三表查询 # post = db.query(models.Post, models.Category, models.Comment).join(models.Category, models.Post.category_id==models.Category.id).join( # models.Comment, models.Post.id == models.Comment.post_id # ).filter(models.Post.id==post_id).first() # return Response200(data={"post": post}) category = db.query( models.Category).filter_by(id=post_obj.category_id).first() comment = db.query(models.Comment).filter_by(post_id=post_id).all() return Response200(data={ "post": post_obj, "category": category, "comment": comment })
async def get_link(db: Session = Depends(deps.get_db)): data = db.query(models.Link).all() return Response200(data={"link": data})