def get(self, comment_id): if comment_id: comment = db_session.query(Comment).get(comment_id) if comment: return jsonify(_parse_comment(comment)) else: return make_response( jsonify({ 'type': 'error', 'text': 'not found' }), 404) comments = db_session.query(Comment).all() comments[:] = [_parse_comment(comment) for comment in comments] return jsonify({'comments': comments})
def delete(self, comment_id): comment = db_session.query(Comment).get(comment_id) if comment: db_session.delete(comment) db_session.commit() return jsonify(_parse_comment(comment, detailed=False)) return make_response(jsonify({ 'type': 'error', 'text': 'not found' }), 404)
def get_details(): user_id = g.user.id user = db_session.query(User).get(user_id) comments = db_session.query(Comment).filter_by(user_id=user_id).order_by( Comment.timestamp_created.desc()).limit(50).all() orders = db_session.query(Order).filter_by(user_id=user_id).order_by( Order.order_date.desc()).limit(50).all() user_dict = _parse_user(user) user_dict['orders'] = [_parse_order(order) for order in orders] user_dict['comments'] = [_parse_comment(comment) for comment in comments] return jsonify(user_dict)
def post(self): check_comment = db_session.query(Comment).filter_by( user_id=g.user.id, meal_id=self.json.get('meal_id')).first() if check_comment: return make_response( jsonify({ 'type': 'error', 'text': 'you have already commented this meal' })) new_comment = Comment(user_id=g.user.id, meal_id=self.json.get('meal_id'), content=self.json.get('content', '')) db_session.add(new_comment) db_session.commit() return jsonify(_parse_comment(new_comment, detailed=False))
def get_user_comments(user_id): comments = db_session.query(Comment).filter_by(user_id=user_id).all() return jsonify( {'comments': [_parse_comment(comment) for comment in comments]})