Beispiel #1
0
 def get(self):
     """
     Get all products
     """
     current_user = get_jwt_identity()
     if current_user is None:
         msg = 'Please login to access to access this resource'
         return {"status": "Failed!", "message": msg}, 400
     store_id = get_store_id(get_jwt_identity())
     cur.execute(
         "SELECT * FROM products WHERE store_id={};".format(store_id))
     products = cur.fetchall()
     all_products = []
     for p in products:
         format_p = {
             'product_id': p[0],
             'name': p[2],
             'inventory': p[3],
             'price': p[4],
             'category': p[5],
             'added_at': p[6]
         }
         all_products.append(format_p)
     if len(all_products) < 1:
         res = {
             "status": "Failed!",
             "message": "There are no products at the moment"
         }, 404
         return res
     return {"status": "Success!", "products": all_products}, 200
Beispiel #2
0
 def get(self, id):
     """
     Get all questions for a specific meetup
     """
     cur.execute("SELECT * FROM questions WHERE meetup_id={};".format(id))
     questions = cur.fetchall()
     mtup = get_meetup_by_id(id)
     all_questions = []
     for item in questions:
         format_quiz = {
             'question_id': item[0],
             'username': item[1],
             'meetup_id': item[2],
             'votes': item[3],
             'title': item[4],
             'body': item[5],
             'time_added': str(item[6])
         }
         all_questions.append(format_quiz)
     if not mtup or mtup[0] != id:
         msg = 'Meetup with that id does not exist'
         return {"Status": 404, "Message": msg}, 404
     if len(all_questions) < 1:
         res = {
             "Status": 404,
             "Message": "There are no Questions at the moment"
         }, 404
         return res
     return {"Status": 200, "data": all_questions}, 200
Beispiel #3
0
 def post(self):
     """
     Add a product to the manager
     """
     current_user = get_jwt_identity()
     if current_user is None:
         msg = 'Please login to access to access this resource'
         return {"status": "Failed!", "message": msg}, 400
     json_data = request.get_json(force=True)
     res = product_validator(json_data)
     if not res:
         store_id = get_store_id(get_jwt_identity())
         cur.execute("SELECT * FROM products WHERE name='{}';".format(
             json_data['name'].lower()))
         product = cur.fetchone()
         if product and product[1] == store_id:
             msg = 'Product already exists.Update product inventory instead'
             return {"status": "Failed!", "message": msg}, 409
         cat_name = 'Category-not-set'
         new_pro = Product(store_id, json_data['name'].lower(),
                           json_data['inventory'], json_data['price'],
                           cat_name)
         new_pro.add_product()
         res = new_pro.json_dump()
         res = {
             "status": "Success!",
             "message": "Product successfully added",
             "data": res
         }, 201
     return res
Beispiel #4
0
    def delete(self):
        """
        Delete an entire cart
        """
        current_user = get_jwt_identity()
        if current_user is None:
            msg = 'Please login to access to access this resource'
            return {"status": "Failed!", "message": msg}, 400
        cart = cart_helper(get_jwt_identity())
        if not cart:
            return {
                "status": "Failed!",
                "message": "You don\'t have any cart at the moment"
            }, 404
        seller = get_user_by_email(get_jwt_identity())
        seller_id = seller[0]
        for c in cart:
            inventory = c[3]
            name = c[2]
            cur.execute(
                "UPDATE products SET inventory= inventory + {} WHERE name ='{}'"
                .format(inventory, name))
            conn.commit()

        cur.execute("DELETE FROM carts WHERE seller_id={};".format(seller_id))
        conn.commit()

        return {"status": "Cart Deleted!"}, 200
Beispiel #5
0
 def get(self):
     """
     Get all meetups
     """
     cur.execute("SELECT * FROM meetups")
     meetups = cur.fetchall()
     all_meetups = []
     for item in meetups:
         format_meetup = {
             'meetup_id': item[0],
             'location': item[1],
             'images': item[2],
             'title': item[3],
             'happeningOn': item[4],
             'tags': item[5],
             'time_added': str(item[6])
         }
         all_meetups.append(format_meetup)
     if len(all_meetups) < 1:
         res = {
             "status": 404,
             "message": "There are no meetups at the moment"
         }, 404
         return res
     return {"status": 200, "data": all_meetups}, 200
Beispiel #6
0
 def post(self, id):
     """
     Add a new product to a category
     """
     current_user = get_jwt_identity()
     if current_user is None:
         msg = 'Please login to access to access this resource'
         return {"status": "Failed!", "message": msg}, 400
     store_id = get_store_id(get_jwt_identity())
     cur.execute("SELECT * FROM categories WHERE id='{}';".format(id))
     category = cur.fetchone()
     if not category or category[1] != store_id:
         msg = 'Category does not exist'
         return {"message": msg}, 404
     json_data = request.get_json(force=True)
     product_validator(json_data)
     cur.execute("SELECT * FROM products WHERE name='{}';".format(
         json_data['name']))
     product = cur.fetchone()
     if product and product[1] == store_id:
         msg = 'Product already exists.Update product inventory instead'
         return {"message": msg}, 409
     cat_name = category[2]
     new_pro = Product(store_id, json_data['name'], json_data['inventory'],
                       json_data['price'], cat_name)
     new_pro.add_product()
     res = new_pro.json_dump()
     res = {
         "status": "Success!",
         "message": "Product successfully added",
         "data": res
     }, 201
     return res
Beispiel #7
0
def cart_helper(email):
    """
    Method to get cart that belongs to a specific user
    """
    user = get_user_by_email(email)
    cur.execute("SELECT * FROM carts WHERE seller_id={};".format(user[0]))
    carts = cur.fetchall()
    return carts
Beispiel #8
0
 def put(self, id):
     """
     Update a category
     """
     current_user = get_jwt_identity()
     if current_user is None:
         msg = 'Please login to access to access this resource'
         return {"status": "Failed!", "message": msg}, 400
     json_data = request.get_json(force=True)
     category_validator(json_data)
     cur.execute("SELECT * FROM categories WHERE id={};".format(id))
     category = cur.fetchone()
     store_id = get_store_id(get_jwt_identity())
     if not category or category[1] != store_id:
         msg = {"message": 'Category does not exist'}, 404
         return msg
     name = category[2]
     if 'name' in json_data:
         c_name = json_data['name'].lower()
         name = "".join(c_name.split())
     cur.execute("SELECT * FROM categories WHERE name='{}';".format(name))
     category_check = cur.fetchone()
     if category_check:
         msg = 'That category already exists'
         return {"status": "Failed", "message": msg}, 406
     cur.execute("UPDATE categories SET name='{}' WHERE id ={}".format(
         name, id))
     conn.commit()
     cur.execute("SELECT * FROM categories WHERE id={};".format(id))
     new_c = cur.fetchone()
     format_new_c = {"category_name": new_c[2]}
     return {"status": "Updated!", "category": format_new_c}, 200
Beispiel #9
0
 def post(self, c_id, p_id):
     """
     Add a category to a product
     c_id : the category id
     p_id : the product id
     """
     store_id = get_store_id(get_jwt_identity())
     cur.execute("SELECT * FROM categories WHERE id='{}';".format(c_id))
     category = cur.fetchone()
     if not category or category[1] != store_id:
         msg = 'Category does not exist'
         return {"status": "Failed!", "message": msg}, 404
     cur.execute("SELECT * FROM products WHERE id={};".format(p_id))
     product = cur.fetchone()
     if not product or product[1] != store_id:
         msg = 'Product does not exist'
         return {"message": msg}, 404
     category_name = category[2]
     cur.execute("UPDATE products SET category='{}' WHERE id ='{}'".format(
         category_name, p_id))
     conn.commit()
     cur.execute("SELECT * FROM products WHERE id={};".format(p_id))
     new_p = cur.fetchone()
     format_new_p = {
         "product_name": new_p[2],
         "inventory": new_p[3],
         "price": new_p[4],
         'category': new_p[5],
         'added_at': new_p[6]
     }
     return {"status": "Updated!", "product": format_new_p}, 200
Beispiel #10
0
 def delete(self, id):
     """
     Admin delete a meetup
     """
     user = get_user_by_email(get_jwt_identity())
     if user[7] != True:
         msg = 'access denied! please contact the admin'
         return {'message': msg}, 401
     meetup = get_meetup_by_id(id)
     if not meetup:
         msg = 'meetup with that id does not exist'
         return {"status": 404, "message": msg}, 404
     cur.execute("DELETE FROM meetups WHERE id={};".format(id))
     msg = 'meetup deleted successfully!'
     return {"status": 200, 'message': msg}, 200
Beispiel #11
0
 def put(self, id):
     """
     Update a product on a cart
     """
     current_user = get_jwt_identity()
     if current_user is None:
         msg = 'Please login to access to access this resource'
         return {"status": "Failed!", "message": msg}, 400
     json_data = request.get_json(force=True)
     res = sales_validator(json_data)
     if not res:
         cur.execute("SELECT * FROM carts WHERE id={};".format(id))
         product = cur.fetchone()
         seller = get_user_by_email(get_jwt_identity())
         seller_id = seller[0]
         if not product or product[1] != seller_id:
             return {
                 "status": "Failed!",
                 "message": "That product is not in the cart"
             }, 404
         cur.execute("SELECT * FROM products WHERE name='{}';".format(
             product[2]))
         p = cur.fetchone()
         number = int(json_data['number'])
         total_num = p[3] + product[3]
         if number > int(total_num):
             msg = 'There are only {0} {1} available'.format(
                 total_num, p[2])
             return {"status": "Failed!", "message": msg}, 400
         new_amnt = number * p[4]
         cur.execute(
             "UPDATE carts SET number={0},amount={1} WHERE id ={2}".format(
                 number, new_amnt, id))
         conn.commit()
         new_p_inv = total_num - number
         cur.execute(
             "UPDATE products SET inventory= '{}' WHERE name ='{}'".format(
                 new_p_inv, product[2]))
         conn.commit()
         cur.execute("SELECT * FROM carts WHERE id={};".format(id))
         new_c = cur.fetchone()
         format_new_c = {
             "product": new_c[2],
             "number": new_c[3],
             "amount": new_c[4]
         }
         res = {"status": "Cart Updated", "cart": format_new_c}, 200
     return res
Beispiel #12
0
 def patch(self, id):
     ''' Down votes '''
     user = get_user_by_email(get_jwt_identity())
     if user:
         username = user[5]
     qsn = get_question_by_id(id)
     if not qsn or qsn[0] != id:
         msg = 'Question with that id does not exist'
         return {"Message": msg}, 404
     question_id = qsn[0]
     votes = qsn[3] - 1
     cur.execute("UPDATE questions SET votes = '{}'\
     WHERE id={};".format(votes, id))
     conn.commit()
     votes_count(username, question_id, votes)
     msg = 'You have disliked this question'
     return {'Status': 201, 'Votes': votes, 'Message': msg}
Beispiel #13
0
 def delete(self, id):
     """
     Delete a category
     """
     current_user = get_jwt_identity()
     if current_user is None:
         msg = 'Please login to access to access this resource'
         return {"status": "Failed!", "message": msg}, 400
     cur.execute("SELECT * FROM categories WHERE id={};".format(id))
     category = cur.fetchone()
     store_id = get_store_id(get_jwt_identity())
     if not category or category[1] != store_id:
         msg = {"message": 'Category does not exist'}, 404
         return msg
     cur.execute("DELETE FROM  categories WHERE id={};".format(id))
     conn.commit()
     format_c = {"category_name": category[2]}
     return {"status": "Deleted!", "prpduct": format_c}, 200
Beispiel #14
0
 def get(self):
     """
     Get all get categories
     """
     current_user = get_jwt_identity()
     if current_user is None:
         msg = 'Please login to access to access this resource'
         return {"status": "Failed!", "message": msg}, 400
     store_id = get_store_id(get_jwt_identity())
     cur.execute(
         "SELECT * FROM categories WHERE store_id='{}';".format(store_id))
     categories = cur.fetchall()
     if len(categories) < 1:
         return {"message": "There are no categories at this time"}, 404
     all_categories = []
     for c in categories:
         format_cat = {"id": c[0], "name": c[2], "Added_at": c[3]}
         all_categories.append(format_cat)
     return {"status": "Success!", "categories": all_categories}, 200
Beispiel #15
0
    def put(self, id):
        """
        Update a product
        """
        current_user = get_jwt_identity()
        if current_user is None:
            msg = 'Please login to access to access this resource'
            return {"status": "Failed!", "message": msg}, 400
        json_data = request.get_json(force=True)
        res = product_update_validator(json_data)
        if not res:
            cur.execute("SELECT * FROM products WHERE id={};".format(id))
            product = cur.fetchone()
            store_id = get_store_id(get_jwt_identity())
            if not product or product[1] != store_id:
                return {
                    "status": "Failed!",
                    "message": 'Product does not exist'
                }, 404
            name = product[2]
            inventory = product[3]
            price = product[4]
            if 'name' in json_data:
                name = json_data['name'].lower()
                cur.execute(
                    "SELECT * FROM products WHERE name='{}';".format(name))
                product_check = cur.fetchone()
                if product_check:
                    msg = 'That product already exists'
                    return {"status": "Failed!", "message": msg}, 406
            if 'inventory' in json_data:
                inventory = json_data['inventory']
            if 'price' in json_data:
                price = json_data['price']

            cur.execute(
                "UPDATE products SET name='{}',inventory='{}',price='{}'\
            WHERE id ={}".format(name, inventory, price, id))
            conn.commit()
            cur.execute("SELECT * FROM products WHERE id={};".format(id))
            new_p = cur.fetchone()
            format_new_p = {
                "product_name": new_p[2],
                "inventory": new_p[3],
                "price": new_p[4],
                'category': new_p[5],
                'added_at': new_p[6]
            }
            res = {"status": "Updated!", "product": format_new_p}, 200
        return res
Beispiel #16
0
 def delete(self, id):
     """
     Remove a product from cart
     """
     current_user = get_jwt_identity()
     if current_user is None:
         msg = 'Please login to access to access this resource'
         return {"status": "Failed!", "message": msg}, 400
     cur.execute("SELECT * FROM carts WHERE id={};".format(id))
     product = cur.fetchone()
     seller = get_user_by_email(get_jwt_identity())
     seller_id = seller[0]
     if not product or product[1] != seller_id:
         return {
             "status": "Failed!",
             "message": "That product is not in the cart"
         }, 400
     new_p_inv = product[3]
     cur.execute(
         "UPDATE products SET inventory= inventory + {} WHERE name ='{}'".
         format(new_p_inv, product[2]))
     conn.commit()
     cur.execute("DELETE FROM carts WHERE id='{}';".format(id))
     conn.commit()
     format_c = {
         "product": product[2],
         "number": product[3],
         "amount": product[4]
     }
     return {"status": "Deleted!", "product": format_c}, 200
Beispiel #17
0
 def post(self):
     """
     Sell a cart
     """
     current_user = get_jwt_identity()
     if current_user is None:
         msg = 'Please login to access to access this resource'
         return {"status": "Failed!", "message": msg}, 400
     cart = cart_helper(get_jwt_identity())
     store_id = get_store_id(get_jwt_identity())
     if not cart:
         return {
             "status": "Failed!",
             "message": "You don\'t have any cart at the moment"
         }, 404
     seller = get_user_by_email(get_jwt_identity())
     seller_id = seller[0]
     sale_order = []
     totalamount = 0
     for c in cart:
         product = c[2]
         number = c[3]
         amount = c[4]
         new_sale_record = Sale(store_id, seller_id, product, number,
                                amount)
         new_sale_record.sell()
         format_sale = {
             'product': c[2],
             'number_of_products': c[3],
             'amount': c[4]
         }
         totalamount += c[4]
         sale_order.append(format_sale)
     cur.execute("DELETE FROM carts WHERE seller_id={};".format(seller_id))
     conn.commit()
     return {
         "status": "Sold!",
         "TotalAmount": totalamount,
         "Items": sale_order
     }, 201
Beispiel #18
0
 def post(self):
     """
     Create a category
     """
     json_data = request.get_json(force=True)
     res = category_validator(json_data)
     if res:
         return res
     store_id = get_store_id(get_jwt_identity())
     c_name = json_data['name'].lower()
     category_name = "".join(c_name.split())
     cur.execute(
         "SELECT * FROM categories WHERE name='{}';".format(category_name))
     category = cur.fetchone()
     if category and category[1] == store_id:
         return {
             "status": "Failed!",
             "message": "Category already exists"
         }, 409
     new_cat = Category(store_id, category_name)
     new_cat.add_category()
     return {"status": "Success!", "Category": new_cat.json_dump()}, 201
Beispiel #19
0
 def post(self, id):
     """
     Add a product to cart
     """
     current_user = get_jwt_identity()
     if current_user is None:
         msg = 'Please login to access to access this resource'
         return {"status": "Failed!", "message": msg}, 400
     json_data = request.get_json(force=True)
     res = sales_validator(json_data)
     if not res:
         number = json_data['number']
         cur.execute("SELECT * FROM products WHERE id={};".format(id))
         product = cur.fetchone()
         store_id = get_store_id(get_jwt_identity())
         if not product or product[1] != store_id:
             msg = 'Product does not exist'
             return {"status": "Failed!", "message": msg}, 404
         product_name = product[2]
         if product[3] < int(number):
             msg = 'There are only {0} {1} available'.format(
                 product[3], product_name)
             return {"status": "Failed!", "message": msg}, 400
         amount = number * product[4]
         seller = get_user_by_email(get_jwt_identity())
         seller_id = seller[0]
         new_cart = Cart(seller_id, product_name, number, amount)
         new_cart.add_to_cart()
         res = new_cart.json_dump()
         new_inventory = product[3] - number
         cur.execute(
             "UPDATE products SET inventory={0} WHERE id ={1}".format(
                 new_inventory, id))
         res = {
             "status": "Success!",
             "message": "Added to cart",
             "product": res
         }
     return res
Beispiel #20
0
 def delete(self, id):
     """
     Delete a product
     """
     current_user = get_jwt_identity()
     if current_user is None:
         msg = 'Please login to access to access this resource'
         return {"status": "Failed!", "message": msg}, 400
     cur.execute("SELECT * FROM products WHERE id={};".format(id))
     product = cur.fetchone()
     store_id = get_store_id(get_jwt_identity())
     if not product or product[1] != store_id:
         msg = 'Product does not exist'
         return {"status": "Failed!", "message": msg}, 404
     cur.execute("DELETE FROM products WHERE id={};".format(id))
     conn.commit()
     format_p = {
         "product_name": product[2],
         "inventory": product[3],
         "price": product[4]
     }
     return {"status": "Deleted!", "product": format_p}, 200
Beispiel #21
0
 def get(self, id):
     """
     Get single product
     """
     current_user = get_jwt_identity()
     if current_user is None:
         msg = 'Please login to access to access this resource'
         return {"status": "Failed!", "message": msg}, 400
     cur.execute("SELECT * FROM products WHERE id={};".format(id))
     product = cur.fetchone()
     store_id = get_store_id(get_jwt_identity())
     if not product or product[1] != store_id:
         msg = 'Product does not exist'
         return {"status": "Failed!", "message": msg}, 404
     format_p = {
         "product_name": product[2],
         "inventory": product[3],
         "price": product[4],
         'category': product[5],
         'added_at': product[6]
     }
     return {"status": "Success!", "product": format_p}, 200
Beispiel #22
0
 def get(self, id):
     """
     Get a single question
     """
     cur.execute("SELECT * FROM questions WHERE id={};".format(id))
     quests = cur.fetchall()
     mtup = get_meetup_by_id(id)
     qsn = get_question_by_id(id)
     if not qsn:
         msg = 'Question with that id does not exist'
         return {"Status": 404, "Message": msg}, 404
     for item in quests:
         format_quest = {
             'question_id': item[0],
             'user_id': item[1],
             'meetup_id': item[2],
             'votes': item[3],
             'title': item[4],
             'body': item[5],
             'time_added': str(item[6])
         }
     if not mtup or mtup[0] != id:
         msg = 'Meetup with that id does not exist'
     return {"status": 200, "question": format_quest}, 200