Exemplo n.º 1
0
    def test_add_cart(self):
        db = DAOUtils.get_db()

        response = self.app.delete(
            '/cart',
            data=json.dumps(dict(user_id='', item_id=0)),
            headers={"Content-Type": "application/json"})
        self.assertEqual(response.status_code, 404,
                         'User ID Empty Response Error')

        account = 'leo.li'
        password = '******'
        user = DAOUtils.get_user_dao().get_user(
            db,
            condition="ACCOUNT = '{ACCOUNT}' AND PASSWORD = '******'",
            ACCOUNT=account,
            PASSWORD=md5(password.encode('utf-8')).hexdigest())

        user_id = user.USER_ID
        item_id = 0

        response = self.app.delete(
            '/cart',
            data=json.dumps(dict(user_id=user_id, item_id=item_id)),
            headers={"Content-Type": "application/json"})
        self.assertEqual(response.status_code, 404,
                         'Cart Item Not Existed Response Error')

        item_id = 1
        item = DAOUtils.get_product_dao().get_product(
            db, condition="ITEM_ID = '{ITEM_ID}'", ITEM_ID=item_id)
        cart = DAOUtils.get_cart_dao().get_carts(
            db,
            condition="USER_ID = '{USER_ID}' AND ITEM_ID = '{ITEM_ID}'",
            USER_ID=user_id,
            ITEM_ID=item_id)

        response = self.app.delete(
            '/cart',
            data=json.dumps(dict(user_id=user_id, item_id=item_id)),
            headers={"Content-Type": "application/json"})
        if item:
            if cart and cart[0]:
                self.assertEqual(response.status_code, 200,
                                 'Cart Delete Response Error')
            else:
                self.assertEqual(response.status_code, 404,
                                 'Cart Item  Response Error')
        else:
            self.assertEqual(response.status_code, 404,
                             'Item Not Found Response Error')
Exemplo n.º 2
0
    def __delete_cart():
        """
        Remove Item from Cart
        :return: Response
        """
        db = DAOUtils.get_db()
        input_json = request.get_json()
        user = DAOUtils.get_user_dao().get_user(
            db,
            condition="USER_ID = '{USER_ID}'",
            USER_ID=input_json['user_id'])
        product = DAOUtils.get_product_dao().get_product(
            db, "ITEM_ID = '{ITEM_ID}'", ITEM_ID=input_json['item_id'])

        if user and product:
            try:
                carts = DAOUtils.get_cart_dao().get_carts(
                    db,
                    condition="USER_ID = '{USER_ID}' AND ITEM_ID = '{ITEM_ID}'",
                    USER_ID=input_json['user_id'],
                    ITEM_ID=input_json['item_id'])
                if carts:
                    for cart in carts:
                        DAOUtils.get_cart_dao().delete_cart(db, cart)
                        product.AMOUNT += cart.AMOUNT
                    LogUtils.insert_user_log(db,
                                             user_id=input_json['user_id'],
                                             action='移除商品至購物車',
                                             remark=str(
                                                 json.dumps(input_json)))
                    DAOUtils.commit(db)
                    return Flask(__name__).make_response(('', 200))
                else:
                    return Flask(__name__).make_response(('', 404))
            except:
                DAOUtils.rollback(db)
                error_result = dict()
                error_result['error'] = str(sys.exc_info())
                return Flask(__name__).make_response(
                    (jsonify(error_result), 406))
            finally:
                DAOUtils.close(db)
        else:
            DAOUtils.close(db)
            return Flask(__name__).make_response(('', 404))
Exemplo n.º 3
0
 def __add_cart():
     """
     Add Item to Cart
     :return: Response
     """
     db = DAOUtils.get_db()
     input_json = request.get_json()
     user = DAOUtils.get_user_dao().get_user(
         db,
         condition="USER_ID = '{USER_ID}'",
         USER_ID=input_json['user_id'])
     product = DAOUtils.get_product_dao().get_product(
         db, "ITEM_ID = '{ITEM_ID}'", ITEM_ID=input_json['item_id'])
     if user and product:
         if input_json['amount'] > product.AMOUNT:
             DAOUtils.close(db)
             error_result = dict()
             error_result['error'] = '商品庫存不足'
             return Flask(__name__).make_response(
                 (jsonify(error_result), 403))
         else:
             product.AMOUNT -= input_json['amount']
             cart = Cart(user_id=input_json['user_id'],
                         item_id=input_json['item_id'],
                         amount=input_json['amount'])
             try:
                 DAOUtils.get_cart_dao().insert_cart(db, cart)
                 LogUtils.insert_user_log(db,
                                          user_id=input_json['user_id'],
                                          action='加入商品至購物車',
                                          remark=str(
                                              json.dumps(input_json)))
                 DAOUtils.commit(db)
                 return Flask(__name__).make_response(('', 201))
             except:
                 DAOUtils.rollback(db)
                 error_result = dict()
                 error_result['error'] = str(sys.exc_info())
                 return Flask(__name__).make_response(
                     (jsonify(error_result), 406))
             finally:
                 DAOUtils.close(db)
     else:
         DAOUtils.close(db)
         return Flask(__name__).make_response(('', 404))