def get(self, id_all): article_data = Article.find_by_id(id_all) if article_data: return article_data.json() else: return {'message': 'Article not found'}, 404
def get(self, article_id=None): article = Article.find_by_id(article_id) res = { 'id': article.id, 'title': article.title, 'body': article.body, 'front_pic': article.front_pic } return max_res(res)
def delete(self, id_all): article_data = Article.find_by_id(id_all) if article_data: try: article_data.delete_to_db() except: return { "message": "An error occurred update the Article." }, 500 return {'message': 'Article succes delete'}, 200 else: return {'message': 'Article succes delete'}, 404
def put(self, id_all): data = Articles.parse.parse_args() article_data = Article.find_by_id(id_all) if article_data: article_data.title = data['title'] article_data.content = data['content'] try: article_data.save_to_db() except: return { "message": "An error occurred update the Article." }, 500 return {'message': 'Article succes update'}, 200 else: return {'message': 'Article not found'}, 404
def downvote_article(article_id, type): article = Article.find_by_id(article_id) if not article: return JSONUtil.generate_error_json(VoteArticleError.ArticleNotExist) upvote = ArticleVote.query \ .filter_by(enable=True, article_id=article_id, voter_id=current_user.id, type=VoteType.UP) \ .first() if upvote: return JSONUtil.generate_error_json(VoteArticleError.AlreadyDownVoted) article_vote = ArticleVote.query\ .filter_by(article_id=article_id, voter_id=current_user.id, type=VoteType.DOWN)\ .first() if type == 'add': if not article_vote: article_vote = ArticleVote(article_id=article_id, voter_id=current_user.id, type=VoteType.DOWN) db.session.add(article_vote) db.session.commit() elif not article_vote.enable: article_vote.update(enable=True) else: return JSONUtil.generate_error_json( VoteArticleError.AlreadyDownVoted) elif type == 'remove': if not article_vote or not article.enable: return JSONUtil.generate_error_json(VoteArticleError.NotDownVoted) else: article_vote.update(enable=False) else: return JSONUtil.generate_error_json(VoteArticleError.TypeError) downvote_count = ArticleVote.query\ .filter_by(enable=True, article_id=article_id, type=VoteType.DOWN).count() return JSONUtil.generate_result_json({'downvote_count': downvote_count})