Exemplo n.º 1
0
 def post(self):
     if self.id != 0:
         try:
             sqlString = "SELECT 1 FROM `table_user` WHERE `id`=%s"
             cur = yield db.connPool().execute(sqlString, (self.id,))
             result = cur.fetchone()
             if result == None: 
                 self.make_reserror_json(400, "user not exist")
             else:
                 self.write({
                     "status": "success",
                     "data":{"code":200, "message": "user exist"}
                     })
         except Exception as e:
             self.make_internalerror()
     elif len(self.name) > 0:
         try:
             sqlString = "SELECT 1 FROM `table_user` WHERE `name`=%s"
             cur = yield db.connPool().execute(sqlString, (self.name,))
             result = cur.fetchone()
             if result == None: 
                 self.make_reserror_json(400, "user not exist")
             else:
                 self.write({
                     "status": "success",
                     "data":{"code":200, "message": "user exist"}
                     })
         except Exception as e:
             print(e)
             self.make_internalerror()
     else:
         self.make_badrequest()
Exemplo n.º 2
0
    def post(self):
        if not ("postalCodeList" in self.jsonBody) or len(self.jsonBody["postalCodeList"]) == 0:
            self.make_badrequest()
        else:
            try:
                sqlString = "SELECT `id`, `user`, `price`, `listing_type`,  `postal_code`, `status` FROM `table_listing`"
                filterString = ""
                objectList = []
                if "postalCodeList" in self.jsonBody:
                    for item in self.jsonBody["postalCodeList"]:
                        filterString += item + ","
                    filterString = filterString[:len(filterString) -1]
                    filterString = " WHERE `postal_code` in (" + filterString + ")"
                
                sqlString += filterString
                print(sqlString)

                cur = yield db.connPool().execute(sqlString)
                result = cur.fetchall()
                for row in result:
                    objectList.append(row)
                objects = json.dumps(objectList)
                self.write({
                    "status": "sucess",
                    "data": {"code": 200, "message":"", "listings": objects}
                })
            except Exception as e:
                print(e)
                self.make_internalerror()
Exemplo n.º 3
0
    def post(self):
        if not ("postalCodeList" in self.jsonBody) or len(
                self.jsonBody["postalCodeList"]) == 0:
            self.make_badrequest()
        else:
            try:
                sqlString = "SELECT `id`, `user`, `price`, `listing_type`,  `postal_code`, `status` FROM `table_listing`"
                filterString = ""
                objectList = []
                if "postalCodeList" in self.jsonBody:
                    for item in self.jsonBody["postalCodeList"]:
                        filterString += item + ","
                    filterString = filterString[:len(filterString) - 1]
                    filterString = " WHERE `postal_code` in (" + filterString + ")"

                sqlString += filterString
                print(sqlString)

                cur = yield db.connPool().execute(sqlString)
                result = cur.fetchall()
                for row in result:
                    objectList.append(row)
                objects = json.dumps(objectList)
                self.write({
                    "status": "sucess",
                    "data": {
                        "code": 200,
                        "message": "",
                        "listings": objects
                    }
                })
            except Exception as e:
                print(e)
                self.make_internalerror()
Exemplo n.º 4
0
 def post(self):
     if self.id == 0:
         self.make_badrequest()
     else:
         verified = self.verifyUserId(self.user)
         if not verified:
             self.make_notfound()
         else:
             try:
                 sqlString = "DELETE FROM `table_listing` WHERE `id`=%s"
                 cur = yield db.connPool().execute(sqlString,
                                                   (str(self.id), ))
                 affected_rows = cur.rowcount
                 if affected_rows == 0:
                     self.make_reserror_json(400, "id not exist")
                 else:
                     self.write({
                         "status": "sucess",
                         "data": {
                             "code": 200,
                             "message": ""
                         }
                     })
             except Exception as e:
                 print(e)
                 self.make_internalerror()
Exemplo n.º 5
0
 def post(self):
     if self.id == 0 or self.user == 0 or self.price <= 0:
         self.make_badrequest()
     else:
         verified = self.verifyUserId(self.user)
         if not verified:
             self.make_notfound()
         else:
             try:
                 sqlString = "UPDATE `table_listing` SET `price`=%s WHERE `id`=%s"
                 cur = yield db.connPool().execute(sqlString, (
                     str(self.price),
                     str(self.id),
                 ))
                 affectedRows = cur.rowcount
                 print(affectedRows)
                 if affectedRows == 0:
                     self.make_badrequest()
                 else:
                     self.write({
                         "status": "sucess",
                         "data": {
                             "code": 200,
                             "message": ""
                         }
                     })
             except Exception as e:
                 print(e)
                 self.make_internalerror()
Exemplo n.º 6
0
 def post(self):
     if self.id == 0 or self.price <= 0 or len(
             self.postalCode) == 0 or getListingTypeName(
                 self.listingType) == None or getListingStatusName(
                     self.status) == None:
         self.make_badrequest()
     else:
         verified = self.verifyUserId(self.user)
         if not verified:
             self.make_notfound()
         else:
             try:
                 sqlString = "UPDATE `table_listing` SET `user`=%s,`price`=%s, `listing_type`=%s, `postal_code`=%s, `status`=%s WHERE `id`=%s"
                 cur = yield db.connPool().execute(sqlString, (
                     str(self.user),
                     str(self.price),
                     getListingTypeName(self.listingType),
                     self.postalCode,
                     getListingStatusName(self.status),
                     str(self.id),
                 ))
                 affectedRows = cur.rowcount
                 if affectedRows == 0:
                     self.make_badrequest()
                 else:
                     self.write({
                         "status": "sucess",
                         "data": {
                             "code": 200,
                             "message": ""
                         }
                     })
             except Exception as e:
                 print(e)
                 self.make_internalerror()
Exemplo n.º 7
0
 def post(self):
     if self.id == 0:
         self.make_badrequest()
     else:
         try:
             sqlString = "UPDATE `table_user` SET `name`=%s,`address`=%s WHERE `id`=%s"
             cur = yield db.connPool().execute(sqlString, (self.name, self.address, str(self.id),))
             self.write({
                 "status": "sucess",
                 "data": {"code": 200, "message":""}
             })
         except Exception as e:
             print (e)
             self.make_internalerror()
Exemplo n.º 8
0
 def get(self):
     try:
         sqlString = "SELECT l.`id`, u.`name`, `price`, `listing_type`,  `postal_code`, `status` FROM `table_listing` l JOIN `table_user` u ON (l.`user`=u.`id`)"
         cur = yield db.connPool().execute(sqlString)
         result = cur.fetchall()
         objectList = []
         for row in result:
             objectList.append(row)
         objects = json.dumps(objectList)
         self.write({
             "status": "sucess",
             "data": {"code": 200, "message":"", "listings": objects}
         })
     except Exception as e:
         print(e)
         self.make_internalerror()
Exemplo n.º 9
0
 def post(self):
     if self.id == 0:
         self.make_badrequest()
     else:
         try:
             sqlString = "DELETE FROM `table_user` WHERE `id`=%s"
             cur = yield db.connPool().execute(sqlString, (str(self.id),))
             affected_rows = cur.rowcount
             if affected_rows == 0:
                 self.make_reserror_json(400, "user not exist")
             else:
                 self.write({
                     "status": "sucess",
                     "data": {"code": 200, "message":""}
                 })
         except Exception as e:
             print(e)
             self.make_internalerror()
Exemplo n.º 10
0
 def post(self):
     # data validation
     # {"name": "string", "address":"string"}
     if len(self.name) == 0: 
         self.make_badrequest()
     else:
         try:
             sqlString = "INSERT INTO `table_user` (`name`, `address`) VALUES (%s, %s)"
             cur = yield db.connPool().execute(sqlString, (self.name, self.address))
             user_id = cur.lastrowid
             self.write({
                     "status":"success",
                     "data":{"code":200, "user": {"id": user_id, "name": self.name, "address": self.address}}
                 }
             )
         except err.IntegrityError as e:
             self.make_reserror_json(400, "user exist already!")
         except:
             self.make_internalerror()
             raise
Exemplo n.º 11
0
 def get(self):
     try:
         sqlString = "SELECT l.`id`, u.`name`, `price`, `listing_type`,  `postal_code`, `status` FROM `table_listing` l JOIN `table_user` u ON (l.`user`=u.`id`)"
         cur = yield db.connPool().execute(sqlString)
         result = cur.fetchall()
         objectList = []
         for row in result:
             objectList.append(row)
         objects = json.dumps(objectList)
         self.write({
             "status": "sucess",
             "data": {
                 "code": 200,
                 "message": "",
                 "listings": objects
             }
         })
     except Exception as e:
         print(e)
         self.make_internalerror()
Exemplo n.º 12
0
 def post(self):
     if self.id == 0 or self.price <= 0 or len(self.postalCode) == 0 or getListingTypeName(self.listingType) == None or getListingStatusName(self.status) == None:
         self.make_badrequest()
     else:
         verified = self.verifyUserId(self.user)
         if not verified:
             self.make_notfound()
         else:
             try:
                 sqlString = "UPDATE `table_listing` SET `user`=%s,`price`=%s, `listing_type`=%s, `postal_code`=%s, `status`=%s WHERE `id`=%s"
                 cur = yield db.connPool().execute(sqlString, (str(self.user), str(self.price), getListingTypeName(self.listingType), self.postalCode, getListingStatusName(self.status), str(self.id),))
                 affectedRows = cur.rowcount
                 if affectedRows == 0:
                     self.make_badrequest()
                 else:
                     self.write({
                         "status": "sucess",
                         "data": {"code": 200, "message":""}
                     })
             except Exception as e:
                 print (e)
                 self.make_internalerror()
Exemplo n.º 13
0
 def post(self):
     # data validation
     if self.user == 0 or self.price <= 0.0 or len(
             self.postalCode) == 0 or getListingTypeName(
                 self.listingType) == None or getListingStatusName(
                     self.status) == None:
         self.make_badrequest()
     else:
         verified = yield self.verifyUserId(self.user)
         if not verified:
             self.make_notfound()
         else:
             try:
                 sqlString = "INSERT INTO `table_listing` (`user`, `price`, `listing_type`, `postal_code`, `status`) VALUES (%s, %s, %s, %s, %s)"
                 cur = yield db.connPool().execute(sqlString, (
                     str(self.user),
                     str(self.price),
                     getListingTypeName(self.listingType),
                     self.postalCode,
                     getListingStatusName(self.status),
                 ))
                 id = cur.lastrowid
                 self.write({
                     "status": "success",
                     "data": {
                         "code": 200,
                         "listing": {
                             "id": id,
                             "user": self.user,
                             "price": str(self.price),
                             "listingType": self.listingType,
                             "postalCode": self.postalCode,
                             "status": self.status
                         }
                     }
                 })
             except Exception as e:
                 print(e)
                 self.make_internalerror()
Exemplo n.º 14
0
 def post(self):
     if self.id == 0 or self.user == 0 or self.price <= 0:
         self.make_badrequest()
     else:
         verified = self.verifyUserId(self.user)
         if not verified:
             self.make_notfound()
         else:
             try:
                 sqlString = "UPDATE `table_listing` SET `price`=%s WHERE `id`=%s"
                 cur = yield db.connPool().execute(sqlString, (str(self.price), str(self.id),))
                 affectedRows = cur.rowcount
                 print(affectedRows)
                 if affectedRows == 0:
                     self.make_badrequest()
                 else:
                     self.write({
                         "status": "sucess",
                         "data": {"code": 200, "message":""}
                     })
             except Exception as e:
                 print (e)
                 self.make_internalerror()
Exemplo n.º 15
0
 def post(self):
     # data validation
     if self.user == 0 or self.price <= 0.0 or len(self.postalCode) == 0 or getListingTypeName(self.listingType) == None or getListingStatusName(self.status) == None:
         self.make_badrequest()
     else:
         verified = yield self.verifyUserId(self.user)
         if not verified:
             self.make_notfound()
         else:
             try:
                 sqlString = "INSERT INTO `table_listing` (`user`, `price`, `listing_type`, `postal_code`, `status`) VALUES (%s, %s, %s, %s, %s)"
                 cur = yield db.connPool().execute(sqlString, (
                     str(self.user), str(self.price), getListingTypeName(self.listingType), self.postalCode, getListingStatusName(self.status),) )
                 id = cur.lastrowid
                 self.write({
                         "status":"success",
                         "data":{"code":200, "listing": {"id": id, "user": self.user, "price": str(self.price), "listingType": self.listingType,
                             "postalCode": self.postalCode, "status": self.status}}
                     }
                 )
             except Exception as e:
                 print (e)
                 self.make_internalerror()