def get_ride_requests(ride_id, user_id): all_requests_on_given_ride = [] sql = "SELECT * FROM requests WHERE ride_id = %s" """ Check if user exists """ if check_user(user_id): try: with DatabaseManager() as cursor: if check_ride(ride_id): cursor.execute(sql, [ride_id]) results = cursor.fetchall() if results: for result in results: all_requests_on_given_ride.append(request_json(result[0], result[1], result[2], result[3])) return { "Ride's requests": all_requests_on_given_ride } return { "message": "Ride has no requests" } return { "Message": "Ride not Found" } except Exception as e: logging.error(e) return { "message": "You are not registered, Register to request ride" }
def create_ride(self): sql = "INSERT INTO rides (ref_no, source, destination, date, creator_id, time, requests_no)" \ " VALUES (%s, %s, %s, %s, %s, %s, %s) RETURNING id" with DatabaseManager() as cursor: if check_user(self.creator_id): try: cursor.execute( sql, (self.ref_no, self.source, self.destination, self.date, self.creator_id, self.time, self.requests_no)) cursor.execute("SELECT * FROM rides WHERE ref_no = '%s'" % self.ref_no) result_ride = cursor.fetchone() return ride_json(result_ride[0], result_ride[1], result_ride[2], result_ride[3], result_ride[4], result_ride[5], result_ride[6], result_ride[7]) except Exception as e: return e else: return { "message": "You are not registered, Register to create ride" }
def create_request(self): sql = "INSERT INTO requests (ride_id, requestor_id, status) VALUES (%s, %s, %s) RETURNING id" """ Check if user exists """ if check_user(self.user_id): try: with DatabaseManager() as cursor: if check_ride(self.ride_id): cursor.execute( sql, (self.ride_id, self.user_id, self.status)) if cursor.fetchone(): return { "message": "request made successfully", "code": 201 } return { "Message": "Failed to make request" } return { "message": "Ride not found", "code": 400 } except Exception as e: return e else: return { "message": "You are not registered, Register to request ride", "code": 401 }
def approve_request(request_id, user_id, status): if check_user(user_id): if check_request(request_id): try: with DatabaseManager() as cursor: cursor.execute( "SELECT ride_id FROM requests WHERE id = '%s'" % request_id) ride_id = cursor.fetchone() # This sql determines whether the user to approve request is owner of ride offer sql = """SELECT creator_id, ref_no FROM rides WHERE id = %s AND creator_id = %s""" cursor.execute(sql, (ride_id, user_id)) results = cursor.fetchone() if results: creator_id = str(results[0]) ride_ref_no = results[1] cursor.execute( "SELECT l_name FROM users WHERE id = %s", creator_id) driver = cursor.fetchone() update_sql = "UPDATE requests SET status = '%s' WHERE id = '%s'" % ( status.title(), request_id) if status.title() == "Y": message = "%s Accepted you to join ride %s" % ( driver[0], ride_ref_no) else: message = "%s Rejected you to join ride %s" % ( driver[0], ride_ref_no) notification = Notification( user_id, request_id, message) Notification.create_notification(notification) cursor.execute(update_sql) return {"Message": "Approval action was successful"} else: return {"Message": "Access Denied"} except Exception as e: logging.error(e) else: return {"Message": "Request not found"} else: return {"message": "You are not registered, Register to request ride"}
def delete_ride(ride_id, user_id): """ This method deletes a ride which has a provided id """ if check_user(user_id): if check_user_ride(ride_id, user_id): with DatabaseManager() as cursor: try: sql = "DELETE FROM rides WHERE id = %s AND creator_id = %s" cursor.execute(sql, [ride_id, user_id]) return {"message": "Ride offer deleted successfully"} except Exception as e: return e if not check_user_ride(ride_id, user_id): return {"Access Denied": "You can not delete this ride"} return {"message": "You are not registered, Register to continue"}
def get_ride(user_id, ride_id): """ This method returns a particular ride from the database """ if check_user(user_id): if check_user_ride(ride_id, user_id) == "Not Found": return {"ride": "ride not found"} with DatabaseManager() as cursor: try: cursor.execute("SELECT * FROM rides WHERE id = %s", [ride_id]) ride = cursor.fetchone() if ride: return ride_json(ride[0], ride[1], ride[2], ride[3], ride[4], ride[5], ride[6], ride[7]) return {"Message": "Requested ride is not found"} except Exception as e: return e return {"Message": "Login (create account) to view the offers"}
def update( user_id, ride_id, source, destination, date, time, ): if check_user_ride(ride_id, user_id) == "Not Found": return {"ride": "ride not found"} if check_user(user_id): if check_user_ride(ride_id, user_id): with DatabaseManager() as cursor: try: update = """UPDATE rides SET source = %s, destination = %s, date = %s, time = %s WHERE id = %s RETURNING * """ cursor.execute( update, (source, destination, date, time, ride_id)) ride = cursor.fetchone() if ride: return { "updated ride": ride_json(ride[0], ride[1], ride[2], ride[3], ride[4], ride[5], ride[6], ride[7]) } except Exception as e: return e if not check_user_ride(ride_id, user_id): return {"Access Denied": "You can not edit this ride"} return {"message": "You are not registered, Register to create ride"}
def get_rides(user_id): """ This method returns all ride created in our database """ all_rides = [] if check_user(user_id): with DatabaseManager() as cursor: try: cursor.execute("SELECT * FROM rides") rides = cursor.fetchall() if rides: for ride in rides: all_rides.append( ride_json(ride[0], ride[1], ride[2], ride[3], ride[4], ride[5], ride[6], ride[7])) return {"Ride offers": all_rides} return {"Message": "No ride Found"} except Exception as e: return e return {"Message": "Login (create account) to view the offers"}