def test_find_by_user(self): """ Find a Cart by UserId """ ShoppingCart(state="activated", userId=4).save() ShoppingCart(state="deactivated", userId=5).save() shopcarts = ShoppingCart.find_by_user(5) self.assertEqual(shopcarts[0].state, "deactivated") self.assertEqual(shopcarts[0].userId, 5)
def list_carts(): """ Returns all of the Carts """ if request.args.get('userId'): user_id = request.args.get('userId') app.logger.info('Getting Cart for user with id: {}'.format(user_id)) results = ShoppingCart.find_by_user(user_id) if not results: raise NotFound( 'Cart with user id: {} was not found'.format(user_id)) return jsonify([cart.serialize() for cart in results]), status.HTTP_200_OK elif request.args.get('state'): state = request.args.get('state') app.logger.info('Getting Carts with state: {}'.format(state)) results = ShoppingCart.find_by_state(state) if not results: raise NotFound('Carts with state: {} was not found'.format(state)) return jsonify([cart.serialize() for cart in results]), status.HTTP_200_OK else: results = [] app.logger.info('Getting all Carts') results = ShoppingCart.all() return jsonify([cart.serialize() for cart in results]), status.HTTP_200_OK