async def post(book: BookItem): book_obj = session.query(Books).filter_by(id=book.id).first() if not book: return return_data(msg='没有找到该书籍', code=404) book_obj.title = book.title book_obj.new_percent = book.new_percent book_obj.author = book.author book_obj.pub_com = book.pub_com session.commit() session.close() return return_data(data={"title": book.title})
def get_article_list(status: ArticleStatusEnum, article_type: str = 'public', free: bool = True, user_id: int = Query(default=0)): article_obj_list = session.query(Article).filter_by(type=article_type, free=free) if user_id: article_obj_list = article_obj_list.filter(Article.user_id == user_id) return return_data(data_list=article_obj_list.all())
async def pulish_book(title: str, new_percent: float, author: str = Query('', max_length=50), pub_com: str = None, can_email: bool = True): """ 发布书籍 :param title: 书籍名称 :param new_percent: 新旧程度 :param author: 作者 :param pub_com: 出版社 :return: """ book = Books(title=title, new_percent=new_percent, author=author, pub_com=pub_com) session.add(book) session.commit() return return_data()
async def user_profile(user_id: int): user_obj = session.query(User).get(user_id) return return_data(data=user_obj)
async def path_test(file_path): return return_data(data={'file_path': file_path})
async def login_by_third_paty(name: LoginNameEnum): return return_data(data={'name': name})
async def book_list(user_id: int = Form(None)): books = session.query(Books).all() return return_data(data_list=books)
async def article_detail(article_id: int): article_obj = session.query(Article).get(article_id) return return_data(data=article_obj)
async def newst_comment_list(user_id: int): comments_list = session.query(Comment).filter() return return_data(data_list=comments_list)
async def updata_article_status(status: ArticleStatusEnum, article_ids: List[int] = Query(...)): print(status.value) session.query(Article).filter(Article.id.in_(article_ids)).update({'status': status.value}) session.commit() return return_data(data_list=article_ids)
async def comment_operate_detail(id: int): comment_operate = session.query(CommentOperate).filter(CommentOperate.id == id).first() return return_data(data=comment_operate)
async def comment_detail(comment_id: int): comment = session.query(Comment).filter(Comment.id == comment_id).first() return return_data(data=comment)
async def add_comment(article_id: int, comment: AddCommentItem): comment_obj = Comment(detail=comment.detail, img_url=comment.img_url, article_id=article_id) session.add(comment_obj) session.commit() return return_data()
async def like_article(article_id: int): article_obj = session.query(Article).get(article_id) article_obj.like_num += 1 session.commit() # todo 生成一条谁点赞谁的文章记录 return return_data()
async def user_articles(user_id: int): articles = session.query(Article).filter(Article.user_id == user_id).all() return return_data(data_list=articles)
async def test(cookie: str = Cookie(None)): print(cookie) return return_data(data=cookie)
async def public_article(article: ArticleItem): article_obj = Article(**article.dict()) session.add(article_obj) session.commit() return return_data()