Beispiel #1
0
 def PUT(self, userID=None):
     ''' Add a new order if no unplaced order exist and return the orderID'''
     try:
         userID = int(cherrypy.request.json['userID'])
         print "userID received: %s" % userID
     except:
         print "userID was not received"
         return errorJSON(code=8888, message="")
     cnx = mysql.connector.connect(user='******',
                                   host='127.0.0.1',
                                   database='feedND',
                                   charset='utf8mb4')
     cursor = cnx.cursor()
     q = "select orderID from orders where userId = %s and placed = 0" % (
         userID)
     cursor.execute(q)
     orderID = cursor.fetchone()[0]
     if orderID is None:
         cursor.execute(
             "insert into orders (userId, lastUpdated, placed) values (%s, now(), 0)"
             % (userID))
         cursor.execute("select orderId from orders where userId = %s" %
                        (userID))
         orderID = cursor.fetchone()[0]
     else:
         cursor.execute(
             "update orders set lastUpdated = now() where orderID = %s" %
             (orderID))
     cnx.commit()
     cnx.close()
     result = {"orderID": orderID}
     return json.dumps(result)
Beispiel #2
0
    def PUT(self, orderID, itemID):
        ''' Add or update an item to an order 
        quantity is received in a JSON dictionary
        output is also returned in a JSON dictionary'''
        print orderID, itemID
	try:
            quantity = int(cherrypy.request.json["quantity"])
            print "quantity received: %s" % quantity
        except:
            print "quantity was not received"
            return errorJSON(code=9003, message="Expected integer 'quantity' of items in order as JSON input")
        cnx = mysql.connector.connect(user='******',host='127.0.0.1',database='feedND',charset='utf8mb4')        
        cursor = cnx.cursor()
        q="set sql_safe_updates=0;"  
        cursor.execute(q)
        # does orderID exist?
        q="select exists(select 1 from orders where orderID=%s)" % orderID
        cursor.execute(q)
        if not cursor.fetchall()[0][0]:
            #orderID does not exist
            return errorJSON(code=9000, message="Order with OrderID %s Does Not Exist") % orderID
        # does itemID exist?
        q="select exists(select 1 from items where itemID=%s)" % itemID
        cursor.execute(q)
        if not cursor.fetchall()[0][0]:
            #itemID does not exist
            return errorJSON(code=9001, message="Item with ItemID %s Does Not Exist") % itemID
        q="insert into orderItems (orderID, itemID, quantity) values (%s, %s, %s) on duplicate key update quantity=%s;" \
            % (orderID, itemID, quantity, quantity)
        cursor.execute(q)
        q="select orderItemID from orderItems where orderID=%s and itemID=%s" % (orderID,itemID)
        orderItemID=0
        try:
            cursor.execute(q)
            orderItemID=cursor.fetchall()[0][0]
            cnx.commit()
            cnx.close()
        except Error as e:
            #Failed to insert orderItem
            print "mysql error: %s" % e
            return errorJSON(code=9002, message="Failed to add order item to shopping cart")
        result = {'orderItemID':orderItemID, 'orderID':orderID, 'itemID':itemID, 'quantity':quantity, 'errors':[]}
        return json.dumps(result)
Beispiel #3
0
 def DELETE(self, orderID, itemID):
     ''' Delete orderItem with orderID and itemID'''
     cnx = mysql.connector.connect(user='******',host='127.0.0.1',database='feedND',charset='utf8mb4')        
     cursor = cnx.cursor()
     q="set sql_safe_updates=0;"  
     cursor.execute(q)
     q="select exists(select 1 from orderItems where orderID=%s and itemID=%s)" % (orderID, itemID)
     cursor.execute(q)
     if not cursor.fetchall()[0][0]:
         #no orderItemID for this orderID / itemID combination
         return errorJSON(code=9004, message="OrderItemID for OrderID %s  and ItemID %s Does Not Exist" % (orderID,itemID))
     try:
         q="delete from orderItems where orderID=%s and itemID=%s" % (orderID, itemID)
         cursor.execute(q)
         cnx.commit()
         cnx.close()
     except Error as e:
         #Failed to insert orderItem
         print "mysql error: %s" % e
         return errorJSON(code=9005, message="Failed to delete order item from shopping cart")
     result={"errors":[]}
     return json.dumps(result)
    def DELETE(self, orderID):
        ''' If the orderID exists, delete the order. If the orderID does not exist, return error information. Error code 3000'''
	cnx = mysql.connector.connect(user='******',host='127.0.0.1',database='foodND',charset='utf8mb4')
	q="select exists(select 1 from OrderDetail where orderID=%s)" % orderID
	cursor = cnx.cursor()
	cursor.execute(q)
	if not cursor.fetchall()[0][0]:
	    # orderID does not exist
	    return errorJSON(code=3000, message="orderID for %s does not exist" % orderID)
	q = "delete from OrderDetail where orderID=%s" % orderID
	cursor.execute(q)
	result = "Order with orderID=%s has been sucessfully deleted \n" % orderID
	result += "{'error' : []}"
	return json.dumps(result)
Beispiel #5
0
 def GET(self, orderID, itemID):
     ''' GET orderItemID,quantity for orderID and itemID or error if no entry with orderID or itemID'''
     cnx = mysql.connector.connect(user='******',host='127.0.0.1',database='feedND',charset='utf8mb4')        
     cursor = cnx.cursor()
     q="select exists(select 1 from orderItems where orderID=%s and itemID=%s)" % (orderID, itemID)
     cursor.execute(q)
     if not cursor.fetchall()[0][0]:
         #no orderItemID for this orderID / itemID combination
         return errorJSON(code=9004, message="OrderItemID for OrderID %s  and ItemID %s Does Not Exist" % (orderID,itemID))
     q="select orderItemID,quantity from orderItems where orderID=%s and itemID=%s" % (orderID, itemID)
     cursor.execute(q)
     tmp=cursor.fetchall()
     result={"orderItemID":tmp[0][0],"quantity":tmp[0][1],"errors":[]}
     return json.dumps(result)
    def GET(self, orderID):
	''' Return a list of items for a given orderID'''
	cnx = mysql.connector.connect(user='******',host='127.0.0.1',database='foodND',charset='utf8mb4')
        q="select exists(select 1 from OrderDetail where orderID=%s)" % orderID
        cursor = cnx.cursor()
        cursor.execute(q)
	if not cursor.fetchall()[0][0]:
	    # If the orderID does not exist, return error message
	    return errorJSON(code=3000, message="orderID for %s does Not Exist" % orderID) 
	q = "select itemID, quantity from OrderDetail where orderID=%s" % orderID
	cursor.execute(q)
	result = [{
		    'itemID' : itemID,
		    'quantity' : quantity
		} for itemID, quantity in cursor.fetchall()]
	result.append({'error' : []})
	return json.dumps(result)   
Beispiel #7
0
    def PUT(self, userID=None):
        ''' Add a new order if no unplaced order exist and return the orderID'''	
	try:
		userID = int (cherrypy.request.json['userID'])
		print "userID received: %s" % userID
	except:
		print "userID was not received"
		return errorJSON(code=8888, message = "")
	cnx = mysql.connector.connect(user='******',host='127.0.0.1',database='feedND',charset='utf8mb4')
        cursor = cnx.cursor()
        q ="select orderID from orders where userId = %s and placed = 0" % (userID)
        cursor.execute(q)
	orderID = cursor.fetchone()[0]
	if orderID is None:
		cursor.execute("insert into orders (userId, lastUpdated, placed) values (%s, now(), 0)" % (userID))
		cursor.execute("select orderId from orders where userId = %s" % (userID))
		orderID = cursor.fetchone()[0]	
	else:
		cursor.execute("update orders set lastUpdated = now() where orderID = %s" % (orderID))
	cnx.commit()
	cnx.close()
	result = {"orderID": orderID}
	return json.dumps(result)
    def POST(self, name=None, email=None, password=None, phone=None):
        ''' Add a new user '''
        if not name:
            try:
                name = cherrypy.request.json["name"]
                print "name received: %s" % name
            except:
                print "name was not received"
                return errorJSON(
                    code=9003,
                    message="Expected text 'name' for user as JSON input")
        if not email:
            try:
                email = cherrypy.request.json["email"]
                print "email received: %s" % email
            except:
                print "email was not received"
                return errorJSON(
                    code=9003,
                    message="Expected email 'email' for user as JSON input")
        if not password:
            try:
                password = cherrypy.request.json["password"]
                print "password received: %s" % password
            except:
                print "password was not received"
                return errorJSON(
                    code=9003,
                    message=
                    "Expected password 'password' for user as JSON input")
        if not phone:
            try:
                phone = cherrypy.request.json["phone"]
                print "phone received: %s" % phone
            except:
                print "phone was not received"
                return errorJSON(
                    code=9003,
                    message="Expected tel 'phone' for user as JSON input")

        try:
            password = password.pop(0)
            self.check_params(name=name,
                              email=email,
                              password=password,
                              phone=phone)
        except ValidationException as ex:
            print ex.message
            return errorJSON(code=9003, message=str(ex.message))

        cnx = mysql.connector.connect(user=self.db['user'],
                                      host=self.db['host'],
                                      database=self.db['name'])
        cursor = cnx.cursor()

        q = "SELECT EXISTS(SELECT 1 FROM users WHERE email='%s')" % email
        cursor.execute(q)
        if cursor.fetchall()[0][0]:
            #email already exists
            print "User with email %s Already Exists" % email
            return errorJSON(
                code=9000, message="User with email %s Already Exists") % email

        hash = pwd_context.encrypt(password)

        # WARNING: Need to do validation
        q="INSERT INTO users (name, email, password, phone) VALUES ('%s', '%s', '%s', '%s');" \
            % (name, email, hash, phone)
        try:
            cursor.execute(q)
            #userID=cursor.fetchall()[0][0]
            cnx.commit()
            cnx.close()
        except Error as e:
            #Failed to insert user
            print "mysql error: %s" % e
            return errorJSON(code=9002, message="Failed to add user")
        result = {
            'name': name,
            'email': email,
            'password': hash,
            'phone': phone,
            'errors': []
        }
        return json.dumps(result)
    def POST(self,name=None,email=None,password=None,phone=None):
        ''' Add a new user '''
        if not name:
          try:
            name = cherrypy.request.json["name"]
            print "name received: %s" % name
          except:
            print "name was not received"
            return errorJSON(code=9003, message="Expected text 'name' for user as JSON input")
        if not email:
          try:
            email = cherrypy.request.json["email"]
            print "email received: %s" % email
          except:
            print "email was not received"
            return errorJSON(code=9003, message="Expected email 'email' for user as JSON input")
        if not password:
          try:
            password = cherrypy.request.json["password"]
            print "password received: %s" % password
          except:
            print "password was not received"
            return errorJSON(code=9003, message="Expected password 'password' for user as JSON input")
        if not phone:
          try:
            phone = cherrypy.request.json["phone"]
            print "phone received: %s" % phone
          except:
            print "phone was not received"
            return errorJSON(code=9003, message="Expected tel 'phone' for user as JSON input")
        
	try:
            password = password.pop(0)
            self.check_params(name=name, email=email, password=password, phone=phone)
        except ValidationException as ex:
            print ex.message
            return errorJSON(code=9003, message=str(ex.message))
	
	cnx = mysql.connector.connect(user=self.db['user'],host=self.db['host'],database=self.db['name'])
        cursor = cnx.cursor()
        
	q="SELECT EXISTS(SELECT 1 FROM users WHERE email='%s')" % email
        cursor.execute(q)
        if cursor.fetchall()[0][0]:
            #email already exists
            print "User with email %s Already Exists" % email
            return errorJSON(code=9000, message="User with email %s Already Exists") % email

	hash = pwd_context.encrypt(password)

	# WARNING: Need to do validation
        q="INSERT INTO users (name, email, password, phone) VALUES ('%s', '%s', '%s', '%s');" \
            % (name, email, hash, phone)
        try:
            cursor.execute(q)
            #userID=cursor.fetchall()[0][0]
            cnx.commit()
            cnx.close()
        except Error as e:
            #Failed to insert user
            print "mysql error: %s" % e
            return errorJSON(code=9002, message="Failed to add user")
        result = {'name':name, 'email':email, 'password':hash, 'phone':phone, 'errors':[]}
        return json.dumps(result)
Beispiel #10
0
    def POST(self,
             firstName=None,
             lastName=None,
             email=None,
             address=None,
             password=None,
             confirmPassword=None,
             phone=None):
        ''' Add a new user '''
        if not firstName:
            try:
                firstName = cherrypy.request.json["firstName"]
                print "firstName received: %s" % firstName
            except:
                print "firstName was not received"
                return errorJSON(
                    code=8002,
                    message="Expected text 'firstName' for user as JSON input")
        if not lastName:
            try:
                lastName = cherrypy.request.json["lastName"]
                print "lastName received: %s" % lastName
            except:
                print "lastName was not received"
                return errorJSON(
                    code=8002,
                    message="Expected text 'lastName' for user as JSON input")

        if not email:
            try:
                email = cherrypy.request.json["email"]
                print "email received: %s" % email
            except:
                print "email was not received"
                return errorJSON(
                    code=8002,
                    message="Expected email 'email' for user as JSON input")

        if not address:
            try:
                address = cherrypy.request.json["address"]
                print "address received: %s" % address
            except:
                print "address was not received"
                return errorJSON(
                    code=8002,
                    message="Expected email 'address' for user as JSON input")

        if not password:
            try:
                password = cherrypy.request.json["password"]
                print "password received: %s" % password
            except:
                print "password was not received"
                return errorJSON(
                    code=8002,
                    message=
                    "Expected password 'password' for user as JSON input")

        if not phone:
            try:
                phone = cherrypy.request.json["phone"]
                print "phone received: %s" % phone
            except:
                print "phone was not received"
                return errorJSON(
                    code=8002,
                    message=
                    "Expected phone 'phone number' for user as JSON input")

        try:
            self.check_params(firstName=firstName,
                              lastName=lastName,
                              email=email,
                              address=address,
                              password=password,
                              phone=phone)
        except ValidationException as ex:
            print ex.message
            return errorJSON(code=8002, message=ex.message)

        cnx = mysql.connector.connect(user=self.db['user'],
                                      host=self.db['host'],
                                      database=self.db['name'])
        cursor = cnx.cursor()
        # WARNING: Need to do validation

        #Add in def POST: after connecting to DB and before INSERT statement:
        # Check if email already exists
        q = "SELECT EXISTS(SELECT 1 FROM User WHERE email='%s')" % email
        cursor.execute(q)
        if cursor.fetchall()[0][0]:
            #email already exists
            print "User with email %s Already Exists" % email
            return errorJSON(
                code=8000, message="User with email %s Already Exists") % email

        hash = pwd_context.encrypt(password)


        q="INSERT INTO User (firstName, lastName, email, address, password, phone) VALUES ('%s', '%s', '%s', '%s', '%s', '%s');" \
            % (firstName, lastName, email, address, hash, phone)
        try:
            cursor.execute(q)
            #userID=cursor.fetchall()[0][0]
            cnx.commit()
            cnx.close()
        except Error as e:
            #Failed to insert user
            print "mysql error: %s" % e
            return errorJSON(code=8001, message="Failed to add user")
        result = {
            'firstName': firstName,
            'lastName': lastName,
            'email': email,
            'address': address,
            'password': hash,
            'phone': phone,
            'errors': []
        }
        return json.dumps(result)