def addPromotion(value: str = Body(...)): UserId = '1' code = str(random.randrange(10, 99)) + str(value) + str( random.randrange(10, 99)) q = 'insert into promotion values(\"' + code + '\",\"' + UserId + '\",\"' + value + '\");' db.query_exec(q) return {'code': code, 'value': value}
async def addNewFood(file: str = Body(...), FoodType: str = Body(...), Rating: str = Body(...), Price: str = Body(...), Title: str = Body(...), TagContent: str = Body(...), Content: str = Body(...)): #handel image ih.mk_dir('./api_v1/img') #create dir file = file.split(',')[1] file = b64decode(file, validate=True) img = ih.Iconverse(file) #read image name = FoodType + str(random.randrange(1, 1001)) + '.png' #create new name img.save("./api_v1/img/" + str(name)) #save image with new name ImageUrl = 'http://' + str(settings.IMG_HOST) + ':' + str( settings.PORT) + '/getImage/' + str(name) #creaet image url q = 'insert into foods(FoodType, Rating, Price, Title, Content, ImageUrl) values(\"' + FoodType + '\",\"' + Rating + '\",\"' + Price + '\",\"' + Title + '\",\"' + Content + '\",\"' + ImageUrl + '\");' query_exec(q) #store data into database(foods table) # Tags handling q = 'select FoodId from foods where ImageUrl = \"' + ImageUrl + '\";' FoodId = query_exec(q)[0]['FoodId'] q = 'insert into tags values (\"' + str(FoodId) + '\",\"' + str( TagContent) + '\")' query_exec(q) return ({'status': 'oke'})
def getSomeFoods(FoodType): UserId = '1' q = 'select * from foods where FoodType = \"' + FoodType + '\";' maindata = query_exec(q) q = 'select * from Bag where UserId = \"' + UserId + '\";' tempdata = db_handler.removeUserId(query_exec(q)) result = db_handler.joindata(maindata, tempdata, 'FoodId', 'quantity', 0) return result
def cancellOrder(OrderId:str): UserId = '1' q = 'select count(*) as num from history where UserId = \"'+UserId+'\" and OrderId = \"'+OrderId+'\" and status = \"1\";' num = db.query_exec(q)[0]['num'] if num == 0: raise HTTPException(status_code = 500, detail = 'Order not found') q = 'update history set status = \"-1\" where OrderId = \"'+OrderId+'\" and UserId = \"'+UserId+'\";' db.query_exec(q) return {'status' : 'oke'}
def getAllItem(): UserId = '1' q = 'select * from foods, TopRecent where TopRecent.UserId = \"' + UserId + '\" and foods.FoodId = TopRecent.FoodId ;' maindata = invertData(query_exec(q)) maindata = db_handler.removeUserId(maindata) q = 'select * from Bag where UserId = \"' + UserId + '\";' tempdata = db_handler.removeUserId(query_exec(q)) result = db_handler.joindata(maindata, tempdata, 'FoodId', 'quantity', 0) return result
def removePromotion(code): UserId = '1' if len( db_handler.removeUserId( db.query_exec('select * from promotion where code = \"' + code + '\";'))) == 0: raise HTTPException(status_code=500, detail='Code not found') q = 'delete from promotion where UserId = \"' + UserId + '\" and code = \"' + str( code) + '\"' db.query_exec(q) return {'status': 'oke'}
async def getSomeTag_FoodId(TagId: Optional[List[str]] = Header(None)): q = 'select * from tags where ' result = [] for item in TagId: result.append(await query_exec(q+' FoodId = '+str(item)+';')) result = query_exec(q) return result
async def deleteFood(FoodIds: list = Body(...)): for FoodId in FoodIds: q = 'select ImageUrl from foods where FoodId = \"' + FoodId + '\";' #create get image url query ImageUrl = str(query_exec(q)).split("/") #get image url ImagePath = str(ImageUrl[len(ImageUrl) - 1].strip("\'}]")) #get image name q = 'delete from foods where FoodId = \"' + FoodId + '\";' #create delete query query_exec(q) #delete data in table foods q = 'delete from TopRecent where FoodId = \"' + FoodId + '\";' query_exec(q) #delete data in table TopRecent try: #delete image os.remove('./api_v1/img/' + ImagePath) print("Remove image " + str(ImagePath) + " successfull!") except OSError as error: print(error) return ({'status': 'oke'})
def getOrder(OrderId:str): UserId = '1' q = 'select distinct OrderId, code, UserName, Phone, OrderDate, RecieveDate, Destination, note from history where UserId = \"'+UserId+'\" and OrderId = \"'+OrderId+'\" and status = \"1\";' result = db.query_exec(q) q = 'select o.OrderId, f.*, o.quantity from history o, foods f where o.UserId = \"'+UserId+'\" and o.FoodId = f.FoodId and o.status = \"1\";' data = db.query_exec(q) for r in result: temp1 = [] for d in data: d1 = {} d1.update(d) if r['OrderId'] == d1['OrderId']: d1.pop('OrderId') temp1.append(d1) r.update({'code':r['code'][2:-2]}) r.update({'data' : temp1}) return result
async def editFood(FoodId: str = Body(...), ImageUrl: str = Body(...), FoodType: str = Body(...), Rating: str = Body(...), Price: str = Body(...), Title: str = Body(...), Content: str = Body(...)): file = str(ImageUrl) #check whether FoodId existed or not q = 'select count(*) as num from foods where FoodId = \"' + FoodId + '\";' num = query_exec(q)[0]['num'] if num == 0: raise HTTPException(status_code=500, detail='FoodId not found') else: #update image in folder './api_v1/img' if change if '/png;base64,' in str(file): #delete image q = 'select ImageUrl from foods where FoodId = \"' + FoodId + '\";' #create get image url query ImageUrl = str(query_exec(q)).split("/") #get image url ImagePath = str(ImageUrl[len(ImageUrl) - 1].strip("\'}]")) try: #delete image os.remove('./api_v1/img/' + ImagePath) print("Remove image " + str(ImagePath) + " successfull!") except OSError as error: print(error) #store new image ih.mk_dir('./api_v1/img') #create dir file = file.split(',')[1] file = b64decode(file, validate=True) img = ih.Iconverse(file) #read image name = FoodType + str(random.randrange( 1, 1001)) + '.png' #create new name img.save("./api_v1/img/" + str(name)) #save image with new name ImageUrl = 'http://' + str(settings.IMG_HOST) + ':' + str( settings.PORT) + '/getImage/' + str(name) #creaet image url else: ImageUrl = file #update data in database at table foods q = 'update foods set FoodType = \"' + FoodType + '\", Rating = \"' + FoodType + '\", Price = \"' + Price + '\", Title = \"' + Title + '\", Content = \"' + Content + '\", ImageUrl = \"' + ImageUrl + '\" where FoodId = \"' + FoodId + '\";' query_exec(q) return {'status': 'oke'}
def purchase(data: list = Body(...), code: str = Body(...), UserName: str = Body(...), Phone: str = Body(...), OrderDate: str = Body(...), RecieveDate: str = Body(...), Destination: str = Body(...), note: str = Body(...)): UserId = '1' OrderId = str(random.randrange(0, 999)) + str( data[0]['FoodId']) + str(UserId) + str(code) for item in data: #add new order into order_handler table FoodId = item['FoodId'] quantity = item['quantity'] code = str(code) q = 'insert into history values (\"' + str(UserId) + '\",\"' + str( OrderId ) + '\",\"' + str(FoodId) + '\",\"' + str( quantity ) + '\",\"' + UserName + '\",\"' + Phone + '\",\"' + str( code ) + '\",\"1\",\"' + OrderDate + '\",\"' + RecieveDate + '\",\"' + Destination + '\",\"' + note + '\");' query_exec(q) for item in data: #delete items in bag after purchase q = 'delete from Bag where UserId = \"' + UserId + '\" and FoodId = \"' + item[ 'FoodId'] + '\";' query_exec(q) if code != '': #delete code after use q = 'delete from promotion where UserId = \"' + UserId + '\" and code = \"' + code + '\"' query_exec(q) return {'status': 'oke'}
def getAllItem(): UserId = '1' q = 'select distinct OrderId, code, UserName, Phone, OrderDate, RecieveDate, Destination, status, note from history where UserId = \"' + UserId + '\" and status = \"0\" or status = \"-1\";' result = db.query_exec(q) q = 'select o.status, o.OrderId, f.*, o.quantity from history o, foods f where o.UserId = \"' + UserId + '\" and o.FoodId = f.FoodId;' data = db.query_exec(q) for r in result: temp1 = [] for d in data: d1 = {} d1.update(d) if r['OrderId'] == d1['OrderId'] and r['status'] == d1['status']: d1.pop('OrderId') d1.pop('status') temp1.append(d1) if r['status'] == '0': r.update({'status': 'Successed'}) elif r['status'] == '-1': r.update({'status': 'Cancelled'}) r.update({'code': r['code'][2:-2]}) r.update({'data': temp1}) return result
def addItem(FoodId, quantity): UserId = '1' if int(quantity) == 0: q = 'delete from Bag where UserId = \"' + UserId + '\" and FoodId = \"' + FoodId + '\"' query_exec(q) else: q = 'select count(*) as num from Bag where UserId = \"' + UserId + '\" and FoodId = \"' + FoodId + '\";' num = int(query_exec(q)[0]['num']) if num == 0: q = 'insert into Bag values(\"' + FoodId + '\",\"' + UserId + '\", ' + quantity + ')' query_exec(q) else: q = 'update Bag set quantity = ' + quantity + ' where UserId = \"' + UserId + '\" and FoodId = \"' + FoodId + '\";' query_exec(q) return ({'status': 'oke'})
def updateTopRecent(FoodId): UserId = '1' int(FoodId) limitTopRecent = 5 q = 'delete from TopRecent where FoodId = \"' + FoodId + '\";' query_exec(q) q = "select count(*) as cur_num from TopRecent;" result = query_exec(q) cur_num = int(result[0]['cur_num']) if cur_num >= limitTopRecent: q = 'select * from TopRecent;' last_id = query_exec(q)[0]['FoodId'] q = 'delete from TopRecent where FoodId = \"' + last_id + '\";' query_exec(q) q = 'insert into TopRecent values(\"' + FoodId + '\", \"1\")' result = query_exec(q) q = 'select * from foods, TopRecent where TopRecent.UserId = \"' + UserId + '\" and foods.FoodId = TopRecent.FoodId ;' maindata = invertData(query_exec(q)) maindata = db_handler.removeUserId(maindata) q = 'select * from Bag where UserId = \"' + UserId + '\";' tempdata = db_handler.removeUserId(query_exec(q)) result = db_handler.joindata(maindata, tempdata, 'FoodId', 'quantity', 0) return result
def getAllTags(): q = 'select * from tags;' result = query_exec(q) return result
def getAllItem(): UserId = '1' q = 'select * from promotion where UserId = \"' + UserId + '\";' result = db_handler.removeUserId(db.query_exec(q)) return result
def deleteAllItem(): UserId = '1' q = 'Delete from history where UserId = \"1\" and status = \"0\" or status = \"-1\";' db.query_exec(q) return {'status': 'oke'}
def getItem(code: str): q = 'select * from promotion where code = \"' + code + '\";' result = db_handler.removeUserId(db.query_exec(q)) if len(result) == 0: raise HTTPException(status_code=500, detail='Code not found') return result[0]
def updateTags(FoodId: str = Body(...), TagContent: str = Body(...)): q = 'update table tags set TagContent = \"'+TagContent+'\" where FoodId = \"'+FoodId+'\";' query_exec(q) return ({'status':'oke'})
def getAllItem(): UserId = '1' q = 'select * from foods, Bag where Bag.UserId = \"' + UserId + '\" and Bag.FoodId = foods.FoodId;' result = query_exec(q) result = db_handler.removeUserId(result) return result