def api_customer_explore(): if not hasattr(current_user, 'roles') or 'Customer' not in current_user.roles: return ErrorModel('No Privilege: Must be Customer').json() data = json.loads(request.args.get('data') or '{}') buildingName = data.get('buildingName') stationName = data.get('stationName') buildingTag = data.get('buildingTag') foodTruckName = data.get('foodTruckName') food = data.get('food') ret = {} callProcedure('cus_filter_explore', [buildingName, stationName, buildingTag, foodTruckName, food]) ret['table'] = select('cus_filter_explore_result', '1 = 1', '*')['data'] ret['stationList'] = mysqlDB.select( 'SELECT stationName from Station;', () )['data'] ret['buildingList'] = mysqlDB.select( 'SELECT buildingName from Station;', () )['data'] return SuccessModel(ret).json()
def api_manager_getAvailableStaff(): if not hasattr(current_user, 'roles') or 'Manager' not in current_user.roles: return ErrorModel('No Privilege: Must be Manager').json() username = current_user.id callProcedure('mn_view_foodTruck_available_staff', [username, None]) data = select('mn_view_foodTruck_available_staff_result', '1 = 1', '*')['data'] urlData = json.loads(request.args.get('data') or '{}') foodTruckName = urlData.get('foodTruckName') if foodTruckName: callProcedure('mn_view_foodTruck_staff', [foodTruckName]) newData = select('mn_view_foodTruck_staff_result', '1 = 1', '*')['data'] data = tuple(list(data) + list(newData)) for obj in data: staffUsername = mysqlDB.select( 'SELECT username from Staff NATURAL JOIN `User` WHERE CONCAT(firstName, " ", lastName) = %s', (obj.get('availableStaff') or obj.get('assignedStaff')))['data'][0]['username'] obj['username'] = staffUsername return SuccessModel(data).json()
def api_manager_getFoodList(): if not hasattr(current_user, 'roles') or 'Manager' not in current_user.roles: return ErrorModel('No Privilege: Must be Manager').json() return SuccessModel(mysqlDB.select('SELECT * FROM Food;', ())['data']).json()
def api_admin_manageFood(): if not hasattr(current_user, 'roles') or 'Admin' not in current_user.roles: return ErrorModel('No Privilege: Must be Admin').json() data = json.loads(request.args.get('data') or '{}') foodName = data.get('name') sortBy = data.get('sortBy') isAsc = data.get('asc') if sortBy is not None: if isAsc: isAsc = 'ASC' else: isAsc = 'DESC' ret = {} callProcedure('ad_filter_food', [foodName, sortBy, isAsc]) result = select('ad_filter_food_result', '1 = 1', '*') if not result['okay']: return ErrorModel({'errno': 0}).json() ret['table'] = result['data'] result = mysqlDB.select('SELECT * FROM Food;', ()) if not result['okay']: return ErrorModel({'errno': 1}).json() ret['foodList'] = result['data'] return SuccessModel(ret).json()
def api_admin_updateStation(): if not hasattr(current_user, 'roles') or 'Admin' not in current_user.roles: return ErrorModel('No Privilege: Must be Admin').json() data = request.get_json() stationName = data.get('name') capacity = data.get('capacity') sponsoredBuilding = data.get('sponsoredBuilding') if type(stationName) is str and type( capacity) is int and capacity > 0 and type( sponsoredBuilding) is str: if not checkExist('Station', '`stationName` = %s', (stationName)): return ErrorModel({'errno': 0}).json() numFoodTrucks = mysqlDB.select( 'SELECT COUNT(*) AS numFoodTrucks FROM FoodTruck WHERE stationName = %s', (stationName))['data'][0]['numFoodTrucks'] if numFoodTrucks > capacity: return ErrorModel({'errno': 3}).json() callProcedure('ad_update_station', [stationName, capacity, sponsoredBuilding]) if not mysqlDB.tryCommit(): return ErrorModel({'errno': 2}).json() return SuccessModel({}).json() else: return ErrorModel({'errno': 1}).json()
def api_customer_makeOrder(): if not hasattr(current_user, 'roles') or 'Customer' not in current_user.roles: return ErrorModel('No Privilege: Must be Customer').json() username = current_user.id data = request.get_json() foodTruck = data.get('foodTruck') date = data.get('date') menuItems = data.get('menuItems') if type(foodTruck) is not str or type(date) is not str or type(menuItems) is not list: return ErrorModel({'errno': 0}).json() totalPrice = 0 for menuItem in menuItems: thisPrice = mysqlDB.select( 'SELECT price FROM MenuItem WHERE foodTruckName = %s AND foodName = %s;', (foodTruck, menuItem['food']) )['data'][0]['price'] totalPrice = totalPrice + thisPrice * menuItem['quantity'] balance = mysqlDB.select( 'SELECT balance FROM Customer WHERE username = %s;', (username) )['data'][0]['balance'] if totalPrice > balance: return ErrorModel({'errno': 2}).json() callProcedure('cus_order', [date, username]) currOrderIDArr = mysqlDB.select( 'SELECT MAX(orderID) AS currOrderID FROM Orders WHERE orderID NOT IN (SELECT DISTINCT orderID FROM OrderDetail);', () )['data'] if len(currOrderIDArr) == 0: return ErrorModel({'errno': 1}).json() currOrderID = currOrderIDArr[0]['currOrderID'] for menuItem in menuItems: callProcedure('cus_add_item_to_order', [foodTruck, menuItem['food'], menuItem['quantity'], currOrderID]) if not mysqlDB.tryCommit(): return ErrorModel({'errno': 3}).json() return SuccessModel({}).json()
def api_admin_getStation(): if not hasattr(current_user, 'roles') or 'Admin' not in current_user.roles: return ErrorModel('No Privilege: Must be Admin').json() data = json.loads(request.args.get('data') or '{}') buildingName = data.get('buildingName') res = mysqlDB.select( 'SELECT `stationName` FROM `Station` WHERE `buildingName` = %s;', (buildingName))['data'] if len(res) == 0: return ErrorModel({'errno': 1}).json() stationName = res[0]['stationName'] callProcedure('ad_view_station', [stationName]) res = select('ad_view_station_result', '1 = 1', '*') return SuccessModel( select('ad_view_station_result', '1 = 1', '*')['data'][0]).json()
def api_manager_getAvailableStation(): if not hasattr(current_user, 'roles') or 'Manager' not in current_user.roles: return ErrorModel('No Privilege: Must be Manager').json() data = json.loads(request.args.get('data') or '{}') foodTruckName = data.get('foodTruckName') callProcedure('mn_get_available_station', [foodTruckName]) data = select('mn_get_available_station_result', '1 = 1', '*')['data'] if foodTruckName: data = tuple( list( mysqlDB.select( 'SELECT stationName FROM FoodTruck WHERE foodTruckName = %s;', (foodTruckName))['data']) + list(data)) return SuccessModel(data).json()
def api_admin_deleteStation(): if not hasattr(current_user, 'roles') or 'Admin' not in current_user.roles: return ErrorModel('No Privilege: Must be Admin').json() data = request.get_json() buildingName = data.get('building') res = mysqlDB.select( 'SELECT `stationName` FROM `Station` WHERE `buildingName` = %s;', (buildingName))['data'] if len(res) == 0: return ErrorModel({'errno': 2}).json() stationName = res[0]['stationName'] if stationName: callProcedure('ad_delete_station', [stationName]) if not mysqlDB.tryCommit(): return ErrorModel({'errno': 0}).json() else: return ErrorModel({'errno': 1}).json() return SuccessModel({}).json()