def updateVehicleDoc(vehicleId, vehicleUpdate): client = mongoConnect() # 3. write data from the database db = client.team12_supply vehicle = db.vehicle vehicle.update_one( {'_id': vehicleId}, {"$set": vehicleUpdate})
def registerVehicle(path, postBody): response = {} try: fleet_id = path.split("/")[1] if not(validFleet(ObjectId(fleet_id))): raise PyMongoError(f"Fleet with ID: {fleet_id} does not exist") vehicleClass = Vehicle(postBody['vehicle_model'], postBody['license_plate'], postBody['vehicle_status'], ObjectId(fleet_id) ) vehicleObj = vehicleClass.get_register_data() # 2. Open a new database connection client = mongoConnect() # 3. write data from the database db = client.team12_supply # check vehicle = db.vehicle if (vehicle.count_documents({'license_plate': vehicleObj['license_plate']}) == 0): vehicleID = vehicle.insert_one(vehicleObj).inserted_id response = {'status': 'OK', 'data': {'fleet_id': vehicleObj['fleet_id'], 'vehicle_model': vehicleObj['vehicle_model'], 'license_plate': vehicleObj[ 'license_plate'], 'vehicle_status': vehicleObj['vehicle_status'], '_id': vehicleID, "current_location": vehicleObj["current_location"]}} else: response = {'status': 'CONFLICT', 'data': {'msg': 'Vehicle already registered'}} except PyMongoError as err: response = {'status': 'CONFLICT', 'data': { 'msg': err}} except ValueError as err: response = {'status': 'CONFLICT', 'data': { 'msg': err}} except Exception as err: logging.error(err) response = {'status': 'INTERNAL_SERVER_ERROR', 'data': { 'msg': 'Server stopped working, please try again later'}} return response
def updateOrder(postBody): response = {} try: requestObj = { "vehicle_id": str(postBody['vehicle_id']), 'current_location': postBody['current_location'], 'vehicle_status': postBody['vehicle_status'] } # TODO: comment line 64 and uncomment line 65 updateResponse = requests.post("http://localhost:8081/vehicle/update", json.dumps(requestObj)) # updateResponse = requests.post("https://supply.team12.sweispring21.tk/api/vehicle/update", json.dumps(requestObj)) responseObj = updateResponse.json() updateObj = responseObj["data"] client = mongoConnect() db = client.team12_demand orders = db.orders update = orders.update_one( {"_id": ObjectId(postBody['order_id'])}, {"$set": { "status": postBody['order_status'] }}) response = {'status': responseObj['status'], 'data': updateObj} except Exception as err: logging.error(err) response = { 'status': 'INTERNAL_SERVER_ERROR', 'data': { 'msg': 'Server stopped working, please try again later' } } return response
def getOrders(orderParams): response = {} try: client = mongoConnect() db = client.team12_demand orders = db.orders if ("_id" in orderParams): orderParams['_id'] = ObjectId(orderParams['_id']) docs = orders.find(orderParams) orderList = [] for doc in docs: orderList.append(doc) logging.debug(orderList) response = {'status': "OK", 'data': orderList} except ValueError as err: response = {'status': 'CONFLICT', 'data': {'msg': err}} except Exception as err: logging.error(err) response = { 'status': "INTERNAL_SERVER_ERROR", 'data': { 'msg': 'Server stopped working, refresh orders again later' } } return response
def requestOrder(postBody): response = {} try: order = Order(postBody["serviceType"], postBody["pickupAddress"], postBody["dropoffAddress"], postBody["customerId"]) orderObj = order.__dict__ client = mongoConnect() db = client.team12_demand orders = db.orders publicId = orders.count_documents({}) + 1000 orderObj["publicId"] = publicId orderId = orders.insert_one(orderObj).inserted_id address1 = (orderObj["pickupAddress"]).replace(" ", "+") address2 = (orderObj["dropoffAddress"]).replace(" ", "+") #### Uncomment this block when supply is up on server #### routeResponse = requests.get( f"http://localhost:8081/vehicles/req?service_type={orderObj['serviceType']}&order_id={orderId}&customer_id={orderObj['customerId']}&destination={address1}" ) # routeResponse = requests.get(f"https://supply.team12.sweispring21.tk/api/vehicles/req?service_type={orderObj['serviceType']}&order_id={orderId}&customer_id={orderObj['customerId']}&destination={address1}") responseObj = routeResponse.json() routeObj = responseObj["data"] if responseObj['status'] == "OK": order.setStatus("vehicle on route") response = { 'status': responseObj['status'], 'data': { "_id": orderId, "publicId": publicId, "status": order.getStatus(), "routeObj": routeObj } } dbRouteObj = routeObj else: response = { 'status': responseObj['status'], 'data': responseObj['data'] } order.setStatus("unfulfilled") dbRouteObj = routeObj orders.update_one( {"_id": orderId}, {"$set": { "status": order.getStatus(), "routeObj": dbRouteObj }}) except ValueError as err: response = {'status': 'CONFLICT', 'data': {'msg': err}} except Exception as err: logging.error(err) response = { 'status': 'INTERNAL_SERVER_ERROR', 'data': { 'msg': 'Server stopped working, please try again later' } } return response
def getFleetId(service_type): client = mongoConnect() fleet = client.team12_supply.fleet doc = fleet.find_one({"service_type": service_type}) if doc != None: fleetId = doc['_id'] return fleetId else: raise PyMongoError( f"Fleet with service type, {service_type}, does not exist")
def getVehicles(query): client = mongoConnect() db = client.team12_supply vehicle = db.vehicle print(query) docs = vehicle.find(query) vehicleList = [] for doc in docs: vehicleList.append(doc) print("This is the list of vehicles") logging.debug(vehicleList) return vehicleList
def registerUser(postBody): response = {} try: # 1. Data validation in FleetManager class fleetManager = FleetManager(postBody['email'], first_name=postBody['firstName'], last_name=postBody['lastName'], password=postBody['password']) fleetManagerObj = fleetManager.get_register_data() # 2. Open a new database connection client = mongoConnect() # 3. write data from the database db = client.team12_supply # check fleetManager = db.fleetManager if (fleetManager.count_documents({'email': fleetManagerObj['email']}) == 0): fleetManagerObj['password'] = hashPassword( fleetManagerObj['password']) fleetManagerID = fleetManager.insert_one( fleetManagerObj).inserted_id response = { 'status': 'OK', 'data': { 'email': fleetManagerObj['email'], 'firstName': fleetManagerObj['first_name'], 'lastName': fleetManagerObj['last_name'], "id": fleetManagerID } } else: response = { 'status': 'CONFLICT', 'data': { 'msg': 'Email is already registered' } } except ValueError as err: response = {'status': 'CONFLICT', 'data': {'msg': err}} except Exception as err: response = { 'status': 'INTERNAL_SERVER_ERROR', 'data': { 'msg': 'Server stopped working, please try again later' } } return response
def getFleetList(): response = {} try: client = mongoConnect() db = client.team12_supply fleet = db.fleet fleetCursor = fleet.find({}) fleetList = [] for fleet in fleetCursor: fleetList.append(fleet) response = {"status": "OK", "data": {"fleetList": fleetList}} except Exception: response = { "status": "INTERNAL_SERVER_ERROR", "data": { "msg": "Server stopped working, please try again later" } } return response
def registerUser(customerData): response = {} try: # 2. Open a new database connection client = mongoConnect() # 3. write data from the database db = client.team12_demand customer = db.customer if (customer.count_documents({'email': customerData['email']}) == 0): customerObj = customerData customerObj['password'] = hashPassword(customerData['password']) customerID = customer.insert_one(customerObj).inserted_id response = { 'status': 'OK', 'data': { 'email': customerObj['email'], 'firstName': customerObj['first_name'], 'lastName': customerObj['last_name'], "id": customerID } } else: response = { 'status': 'CONFLICT', 'data': { 'msg': 'Email is already registered' } } except ValueError as err: response = {'status': 'CONFLICT', 'data': {'msg': err}} except Exception as err: logging.error(err) response = { 'status': 'INTERNAL_SERVER_ERROR', 'data': { 'msg': 'Server stopped working, please try again later' } } return response
def registerFleet(postBody): response = {} try: fleetClass = Fleet(postBody["service_type"], postBody["fleet_name"]) client = mongoConnect() db = client.team12_supply fleet = db.fleet fleetObj = fleetClass.__dict__ service_type = fleetObj["service_type"] if (fleet.count_documents({"service_type": service_type}) == 0): fleet_id = fleet.insert_one({ "service_type": service_type, "fleet_name": fleetObj["fleet_name"] }).inserted_id fleetClass.setId(fleet_id) response = { "status": "OK", "data": { "service_type": fleetObj["service_type"], "fleet_name": fleetObj["fleet_name"], "_id": fleet_id } } else: response = { "status": "CONFLICT", "data": { "msg": f"Fleet for {service_type} already exists" } } except Exception as err: response = { "status": "INTERNAL_SERVER_ERROR", "data": { "msg": "Server stopped working, please try again later" } } return response
def loginUser(postBody): response = {} try: fleetManager = FleetManager(postBody["email"], password=postBody["password"]) email, password = fleetManager.get_login_data() client = mongoConnect() db = client.team12_supply fleetManager = db.fleetManager user = fleetManager.find_one({'email': email}) # checkPassword() will return T/F if (user != None and checkPassword(password, user['password'])): response = { 'status': 'OK', 'data': { 'email': user['email'], 'firstName': user['first_name'], 'lastName': user['last_name'], "id": user["_id"] } } else: response = { 'status': 'CONFLICT', 'data': { 'msg': 'Credentials incorrect' } } except ValueError as err: response = {'status': 'CONFLICT', 'data': {'msg': err}} except Exception as err: response = { 'status': 'INTERNAL_SERVER_ERROR', 'data': { 'msg': 'Server stopped working, please try again later' } } return response
def loginUser(email, password): response = {} try: client = mongoConnect() db = client.team12_demand customer = db.customer user = customer.find_one({'email': email}) # checkPassword() will return T/F if (user != None and checkPassword(password, user['password'])): response = { 'status': 'OK', 'data': { 'email': user['email'], 'firstName': user['first_name'], 'lastName': user['last_name'], "id": user["_id"] } } else: response = { 'status': 'CONFLICT', 'data': { 'msg': 'Credentials incorrect' } } except ValueError as err: response = {'status': 'CONFLICT', 'data': {'msg': err}} except Exception as err: logging.error(err) response = { 'status': 'INTERNAL_SERVER_ERROR', 'data': { 'msg': 'Server stopped working, please try again later' } } return response
def validFleet(_id): client = mongoConnect() fleet = client.team12_supply.fleet return fleet.count_documents({"_id": _id}) != 0