def reply_comment(create_comment: CreateComment, article_id: str = Path(2, description="博客文章id"), comment_id: str = Path(2, description="评论id"), current_user: User = Depends(get_current_user)): content = create_comment.content article = Article.filter(Article.id == article_id).first() if not article: return fail_response('此文章不存在') if article.user_id == current_user.uuid: # 博主的情况 comment = Comment.filter(Comment.id == comment_id, Comment.article_id == article_id, Comment.user_id != current_user.uuid).first() else: # 用户 comment = Comment.filter(Comment.id == comment_id, Comment.article_id == article_id, Comment.user_id != current_user.uuid, Comment.parent_id != '').first() if not comment: return fail_response('此评论不存在或原评论你不能进行回复!') try: Comment.create(article_id=article_id, user_id=current_user.uuid, content=content, parent_id=comment.id) except Exception as e: db.rollback() logger.error(f'评论回复失败,失败原因:{e}') return fail_response('评论回复失败') return success_response('评论回复成功!')
def delete_article(article_id: str, current_user: User = Depends(get_current_user)): article = Article.filter(Article.id == article_id, Article.user_id == current_user.uuid).first() if not article: return fail_response('此文章不存在') title = article.title try: # 之前这个是 物理删除 # result = article.delete_instance() # 删除改为逻辑删除 query = Article.update(is_delete=True).where( Article.id == article_id, Article.user_id == current_user.uuid) result = query.execute() # print(result, "运行后的结果") if result: res = redis_client.exists("article_rank_list") if res: # 存在了但是这个文章 res_score = redis_client.zscore("article_rank_list", "article_%s_%s" % (article_id, title)) if res_score: # 删除之前的分数记录 redis_client.zrem("article_rank_list", "article_%s_%s" % (article_id, title)) return success_response('删除博客文章成功') return fail_response('删除博客文章失败') except Exception as e: db.rollback() logger.error(f'删除博客文章失败,失败原因:{e}') return fail_response('删除博客文章失败')
def update_blog_site(site_id: int = Path(None, title="站点ID"), the_blog_site: UpdateBlogSite = None, current_user: User = Depends(get_current_user)): title = the_blog_site.title site_name = the_blog_site.site_name theme = the_blog_site.theme if current_user.user_type != 0: fail_response("用户权限不足") blog_site = BlogSite.filter(BlogSite.id == site_id).first() if blog_site is None: return fail_response('博客站点不存在') try: # 法一: query = BlogSite.update(title=title, site_name=site_name, theme=theme, update_time=str(datetime.now())).where( BlogSite.id == site_id) query.execute() # 法二: # blog_site.title = title # blog_site.site_name = site_name # blog_site.theme = theme # blog_site.save() return success_response('更新修改博客站点成功') except Exception as e: db.rollback() logger.error(f'更新修改博客站点失败,失败原因:{e}') return fail_response('更新修改博客站点失败')
def get_article_info(article_id: str): article = Article.filter(Article.id == article_id).first() if not article: return fail_response('此文章不存在') try: up_count = ArticleUpDown.filter(ArticleUpDown.article_id == article.id, ArticleUpDown.is_up == True).count() except Exception as e: db.rollback() logger.error(f'更新博客文章失败,失败原因:{e}') return fail_response('删除博客文章失败') return success_response({"article_id": article_id, "up_count": up_count})
def add_comment(create_comment: CreateComment, article_id: str = Path(2, description="博客文章id"), current_user: User = Depends(get_current_user)): content = create_comment.content article = Article.filter(Article.id == article_id).first() if not article: return fail_response('此文章不存在') try: Comment.create(article_id=article_id, user_id=current_user.uuid, content=content, parent_id=None) except Exception as e: db.rollback() logger.error(f'博客文章评论失败,失败原因:{e}') return fail_response('删除博客文章失败') return success_response('新增评论成功!')
def get_article_up_count(article_id: str, current_user: User = Depends(get_current_user)): article = Article.filter(Article.id == article_id, Article.user_id == current_user).first() if not article: return fail_response('此文章不存在') try: article_likes = ArticleUpDown.filter(ArticleUpDown.article_id == article.id, ArticleUpDown.is_up == True) user_list = [article_like.user_id.username for article_like in article_likes] up_count = article_likes.count() except Exception as e: db.rollback() logger.error(f'更新博客文章失败,失败原因:{e}') return fail_response('删除博客文章失败') return success_response({"user_list": user_list, "up_count": up_count})
def get_category_info(category_id: str, up_category: UpdateCategory, current_user: User = Depends(get_current_user)): blog_site = BlogSite.filter(BlogSite.user_id == current_user.uuid).first() if blog_site is None: return fail_response('此用户未创建博客站点,请先创建') category = Category.filter(Category.id == category_id, Category.blog_id == blog_site.id).first() if not category: return fail_response("博客分类不存在") try: query = Category.update(title=up_category.title, description=up_category.description, update_time=str(datetime.now)).where( Category.id == category_id, Category.blog_id == blog_site.id) query.execute() return success_response('更新博客分类成功') except Exception as e: db.rollback() logger.error(f'更新博客分类失败,失败原因:{e}') return fail_response('更新博客分类失败')
def delete_blog_site(site_id: int = Path(..., title="站点ID"), current_user: User = Depends(get_current_user) ): if current_user.user_type != 0: fail_response("用户权限不足") blog_site = BlogSite.filter(BlogSite.id == site_id).first() if not blog_site: return fail_response('此博客站点不存在') try: result = blog_site.delete_instance() if not result: return fail_response('更新博客文章失败') except Exception as e: db.rollback() logger.error(f'更新博客文章失败,失败原因:{e}') return fail_response('删除博客文章失败') return success_response('删除博客文章成功')
def create_blog_site(*, new_site: CreateBlogSite, current_user: User = Depends(get_current_user)): title = new_site.title site_name = new_site.site_name theme = new_site.theme blog_site = BlogSite.select().where(BlogSite.user_id == current_user.uuid) if blog_site: return fail_response('您的博客站点已存在,不能重复创建') try: BlogSite.create(title=title, site_name=site_name, theme=theme, user_id=current_user.uuid) logger.info(f'创建博客站点name={site_name}站点成功') return success_response('添加成功') except Exception as e: db.rollback() logger.info(f'创建博客站点name={site_name}站点成功,失败原因:{e}') return fail_response('创建博客站点失败')
def create_category(*, new_category: CreateCategory, current_user: User = Depends(get_current_user)): title = new_category.title description = new_category.description blog_site = BlogSite.filter(BlogSite.user_id == current_user.uuid).first() if blog_site is None: return fail_response('此用户未创建博客站点,请先创建') category = Category.filter(Category.title == title, Category.blog_id == blog_site.id).first() if category: return fail_response("相同分类已存在!") try: Category.create(title=title, description=description, blog_id=blog_site.id) except Exception as e: db.rollback() logger.error(f'创建博客文章失败,失败原因:{e}') return fail_response('创建博客文章失败') return success_response('创建博客文章成功')
def delete_comment(article_id: str, comment_id: str, current_user: User = Depends(get_current_user)): article = Article.filter(Article.id == article_id).first() if not article: return fail_response('此文章不存在') comment = Comment.filter(Comment.article_id == article_id, Comment.id == comment_id, Comment.user_id == current_user.uuid).first() if not comment: return fail_response('此评论不存在或你没有删除权限') try: result = comment.delete_instance() if not result: return fail_response('删除评论失败') except Exception as e: db.rollback() logger.error(f'删除评论失败失败,失败原因:{e}') return fail_response('删除评论失败!') return success_response('删除评论成功!')
def update_comment(article_id: str, comment_id: str, create_comment: CreateComment, current_user: User = Depends(get_current_user)): content = create_comment.content article = Article.filter(Article.id == article_id).first() if not article: return fail_response('此文章不存在') comment = Comment.filter(Comment.article_id == article_id, Comment.id == comment_id, Comment.user_id == current_user.uuid).first() if not comment: return fail_response('此评论不存在') try: comment.content = content comment.save() except Exception as e: db.rollback() logger.error(f'博客文章评论修改失败,失败原因:{e}') return fail_response('博客文章评论修改失败!') return success_response('博客文章评论修改成功!')
def update_article(article_id: str, new_article: UpdateArticle, current_user: User = Depends(get_current_user)): title = new_article.title desc = new_article.desc content = new_article.content category_id = new_article.category_id article = Article.filter(Article.id == article_id, Article.user_id == current_user.uuid, Article.is_delete == 0).first() if not article: return fail_response('此文章不存在') try: query = Article.update(title=title, desc=desc, content=content, category_id=category_id, update_time=str(datetime.now)).where( Article.id == article_id, Article.user_id == current_user.uuid) query.execute() # 修改排行榜的标题 res = redis_client.exists("article_rank_list") if not res: # 连有序集合都没有的情况 初始化分数 0 redis_client.zadd("article_rank_list", {"article_%s_%s" % (article_id, title): 0}) # print(redis_client.zscore("article_rank_list", "article_id_1")) else: # 存在了但是这个文章 res_score = redis_client.zscore("article_rank_list", "article_%s_%s" % (article_id, title)) if res_score is None: # 初始化分数 redis_client.zadd("article_rank_list", {"article_%s_%s" % (article_id, title): 0}) else: # 删除之前的分数记录 redis_client.zrem("article_rank_list", "article_%s_%s" % (article_id, title)) # 重新建立一个并将之前的分数给这个 redis_client.zadd("article_rank_list", {"article_%s_%s" % (article_id, title): res_score}) # 存在这个key的时候就分值增加 1 redis_client.zincrby("article_rank_list", 1, "article_%s_%s" % (article_id, title)) return success_response('更新博客文章成功') except Exception as e: db.rollback() logger.error(f'更新博客文章失败,失败原因:{e}') return fail_response('更新博客文章失败')
def create_article(*, new_article: CreateArticle, current_user: User = Depends(get_current_user)): title = new_article.title desc = new_article.desc content = new_article.content category_id = new_article.category_id article = Article.filter(Article.user_id == current_user.uuid).first() if article: fail_response("您的博客文章已创建,不能重复创建") try: new_article = Article.create(title=title, desc=desc, content=content, user_id=current_user.uuid, category_id=category_id) print(new_article.title) except Exception as e: db.rollback() logger.error(f'创建博客文章失败,失败原因:{e}') return fail_response('创建博客文章失败') return success_response('创建博客文章成功')
def article_up(article_id: str, up_down: UpDown, current_user: User = Depends(get_current_user)): is_up = up_down.is_up article = Article.filter(Article.id == article_id).first() if not article: return fail_response('此文章不存在') article_like = ArticleUpDown.filter(ArticleUpDown.user_id == current_user.uuid, ArticleUpDown.article_id == article_id).first() if article_like and article_like.is_up == is_up: pass elif article_like and article_like.is_up != is_up: article_like.is_up = is_up article_like.save() else: try: ArticleUpDown.create(user_id=current_user, article_id=article, is_up=is_up) except Exception as e: db.rollback() logger.error(f'点赞操作失败,失败原因:{e}') return fail_response('点赞操作失败') return success_response('操作成功成功')
def delete_category(category_id: str, current_user: User = Depends(get_current_user)): # 法一: # blog_site = BlogSite.filter(BlogSite.user_id == current_user.uuid).first() # if blog_site is None: # return fail_response('此用户未创建博客站点,请先创建') # category = Category.filter(Category.id == category_id, Category.blog_id == blog_site.id).first() # if not category: # return fail_response("博客分类不存在") # try: # result = category.delete_instance() # if result: # return success_response('删除博客文章成功') # return fail_response('更新博客文章失败') # except Exception as e: # db.rollback() # logger.error(f'更新博客文章失败,失败原因:{e}') # return fail_response('删除博客文章失败') # 法二:使用联表查询的方式 category = Category.select().join(BlogSite, on=(Category.blog_id == BlogSite.id)).where( BlogSite.user_id == current_user.uuid, Category.id == category_id) if not category: return fail_response("博客分类不存在") try: # 先将这个用户下这个分类的文章的分类改为空值 articles = Article.filter(Article.category_id == category_id) for article in articles: article.category_id = None article.save() # 再将这个文章分类删掉 query = Category.delete().where(Category.id == category_id) result = query.execute() # 删除成功返回 1 所以不等于1为失败 if result != 1: return fail_response('更新博客文章失败') except Exception as e: db.rollback() logger.error(f'更新博客文章失败,失败原因:{e}') return fail_response('删除博客文章失败') return success_response('删除博客文章成功')