def add(self): """ A method to create a car. :return: Http Response """ Car.abort_if_car_registration_is_already_used(self.registration) if Car.capacity_greater_than_zero(self.capacity): connection = connectDB() cursor = connection.cursor( cursor_factory=psycopg2.extras.DictCursor) try: cursor.execute( """INSERT INTO car (id, model, capacity) VALUES (%s, %s, %s);""", (self.registration, self.model, self.capacity)) connection.commit() except (Exception, psycopg2.DatabaseError) as error: connection.rollback() return {'status': 'failed', 'data': error}, 500 cursor.close() connection.close() return { 'status': 'success', 'message': 'Car created successfully' }, 201 return { 'status': 'failed', 'message': 'Car capacity cannot be below one' }, 202
def abort_if_car_doesnt_exist(registration): """ A method to check if a car exists. :param registration: A string, the licence plate of the user's car. :return: Http Response """ if registration is not None: connection = connectDB() cursor = connection.cursor( cursor_factory=psycopg2.extras.DictCursor) try: cursor.execute('SELECT * FROM car WHERE id = %s ;', ([registration])) except (Exception, psycopg2.DatabaseError) as error: connection.rollback() return {'status': 'failed', 'data': error}, 500 results = cursor.fetchone() cursor.close() connection.close() if results is None: abort( 404, message= 'The car with licence plate {} cannot be found in our records' .format(registration)) return results
def add(self): """ A method to create a ride. :return: Http Response """ connection = connectDB() cursor = connection.cursor(cursor_factory=psycopg2.extras.DictCursor) try: cursor.execute( """INSERT INTO ride ( date, time, pickup, dropoff, capacity, seats_available, driver_id, price, status ) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s);""", (self.date, self.time, self.pickup, self.dropoff, self.capacity, self.seats_available, self.driver_id, self.price, self.status)) connection.commit() except (Exception, psycopg2.DatabaseError) as error: connection.rollback() return {'status': 'failed', 'message': error}, 500 cursor.close() connection.close() return { 'status': 'success', 'message': 'Ride created successfully' }, 201
def read(car_registration): """ A method to get the details of a car. :param car_registration: string, The car's licence plate :return: car details """ connection = connectDB() cursor = connection.cursor(cursor_factory=psycopg2.extras.DictCursor) try: cursor.execute('SELECT * FROM car WHERE id = %s ;', ([car_registration])) except (Exception, psycopg2.DatabaseError) as error: connection.rollback() return {'status': 'failed', 'data': error}, 500 results = cursor.fetchone() cursor.close() connection.close() if results is None: abort( 404, message='The car with licence plate {} does not exist'.format( car_registration)) car = { 'registration': results['id'], 'model': results['model'], 'capacity': results['capacity'], } return car
def edit(car_registration, model, capacity): """ A method to edit the details of a car. :param registration: A string, the car's licence plate. :param model: An int, the car's model. :param capacity: An int, the car's passenger capacity. :return: Http Response """ Car.abort_if_car_doesnt_exist(car_registration) if Car.capacity_greater_than_zero(capacity): connection = connectDB() cursor = connection.cursor( cursor_factory=psycopg2.extras.DictCursor) try: cursor.execute( """ UPDATE car SET model = %s, capacity = %s WHERE id = %s; """, (model, capacity, car_registration)) connection.commit() except (Exception, psycopg2.DatabaseError) as error: connection.rollback() return {'status': 'failed', 'data': error}, 500 cursor.close() connection.close() return { 'status': 'success', 'data': 'Car successfully updated' }, 200 return { 'status': 'failed', 'message': 'Car capacity cannot be below one' }, 202
def browse(): """A method to get all cars. :return: A list of dictionaries with all rides """ connection = connectDB() cursor = connection.cursor(cursor_factory=psycopg2.extras.DictCursor) try: cursor.execute('SELECT * FROM car;') except (Exception, psycopg2.DatabaseError) as error: connection.rollback() return {'status': 'failed', 'data': error}, 500 car_list = cursor.fetchall() cursor.close() connection.close() if len(car_list) == 0: return { 'status': 'success', 'message': 'There are no cars here' }, 202 else: data = [] for car in car_list: item = { 'registration': car['id'], 'model': car['model'], 'capacity': car['capacity'] } data.append(item) return { 'status': 'success', 'message': 'Fetch successful', 'data': data }, 200
def read(user_id): """A method to get all details of a user. :param user_id: An int, a unique identifier of the user. :return: user details """ connection = connectDB() cursor = connection.cursor(cursor_factory=psycopg2.extras.DictCursor) try: cursor.execute('SELECT * FROM app_user WHERE id = %s ;', ([user_id])) except (Exception, psycopg2.DatabaseError) as error: connection.rollback() return {'status': 'failed', 'data': error}, 500 results = cursor.fetchone() cursor.close() connection.close() if results is None: abort(404, message='The user with id {} does not exist'.format(user_id)) user = { 'id': results['id'], 'firstname': results['firstname'], 'lastname': results['lastname'], 'fullname': results['fullname'], 'email': results['email'], 'phone_number': results['phone_number'], 'password': results['password'], 'car_registration': results['car_registration'] } return user
def edit(ride_id, request_id, request_status): """ A method to accept/decline a ride request. :param ride_id: An int, the unique identifier of a ride. :param request_id: An int, the unique identifier of the ride request. :param request_status: A string, the status of the ride request. :return: Http Response """ if request_status in RideRequest.STATUS_OPTIONS: connection = connectDB() cursor = connection.cursor( cursor_factory=psycopg2.extras.DictCursor) RideRequest.abort_if_ride_offer_doesnt_exist(ride_id) RideRequest.abort_if_ride_request_doesnt_exist(request_id) try: cursor.execute( 'UPDATE ride_request SET request_status = %s WHERE id = %s;', (request_status, request_id)) connection.commit() except (Exception, psycopg2.DatabaseError) as error: connection.rollback() return {'status': 'failed', 'data': error}, 500 cursor.close() connection.close() return { 'status': 'success', 'data': 'Ride request successfully updated' }, 200 return { 'status': 'failed', 'message': 'You entered an invalid request status' }, 404
def read(ride_id): """ A method to get the details of a ride. :param ride_id: An int, the unique identifier of the ride. :return: ride details """ connection = connectDB() cursor = connection.cursor(cursor_factory=psycopg2.extras.DictCursor) try: cursor.execute('SELECT * FROM ride WHERE id = %s ;', ([ride_id])) except (Exception, psycopg2.DatabaseError) as error: connection.rollback() return {'status': 'failed', 'data': error}, 500 results = cursor.fetchone() cursor.close() connection.close() if results is None: abort(404, message='The ride with id {} does not exist'.format(ride_id)) ride = { 'id': results['id'], 'time': results['time'], 'date': results['date'], 'pickup': results['pickup'], 'dropoff': results['dropoff'], 'capacity': results['capacity'], 'seats_available': results['seats_available'], 'driver': User.read(results['driver_id'])['fullname'], 'price': results['price'], 'status': results['status'] } return ride
def read(ride_id, request_id): """ A method to get the details of a ride request. :param ride_id: An int, the unique identifier of a ride. :param request_id: An int, the unique identifier of a ride request. :return: ride request details """ connection = connectDB() cursor = connection.cursor(cursor_factory=psycopg2.extras.DictCursor) RideRequest.abort_if_ride_offer_doesnt_exist(ride_id) RideRequest.abort_if_ride_request_doesnt_exist(request_id) try: cursor.execute('SELECT * FROM ride_request WHERE id = %s ;', ([request_id])) except (Exception, psycopg2.DatabaseError) as error: connection.rollback() return {'status': 'failed', 'data': error}, 500 results = cursor.fetchone() cursor.close() connection.close() if results is None: abort(404, message='The ride request with id {} does not exist'.format( request_id)) request = { 'id': results['id'], 'ride_id': results['ride_id'], 'requestor': User.read(results['requestor_id'])['fullname'], 'request_status': results['request_status'], } return request
def get_db_rows(query): connection = connectDB() cursor = connection.cursor(cursor_factory=psycopg2.extras.DictCursor) try: cursor.execute(query) except (Exception, psycopg2.DatabaseError) as error: connection.rollback() return {'status': 'failed', 'data': error}, 500 rows = cursor.fetchall() cursor.close() connection.close() return rows
def edit(date, time, pickup, dropoff, driver_id, price, status, ride_id): """ A method to edit a ride's details. :param date: A string, the date the ride is to be taken. :param time: A string, the time the ride is to start. :param pickup: A string, the place where the ride starts. :param dropoff: A string, the destination of the ride. :param driver_id: An int, the unique identifier of the driver of the ride. :param price: A string, the price of the ride. :param status: A string, the status of the ride. :param ride_id: An int, the unique identifier of the ride. :return: Http Response """ if status in Ride.STATUS_OPTIONS: car = User.get_car(driver_id) car_capacity = car['capacity'] connection = connectDB() cursor = connection.cursor( cursor_factory=psycopg2.extras.DictCursor) Ride.abort_if_ride_offer_doesnt_exist(ride_id) try: cursor.execute( """UPDATE ride SET date = %s, time = %s, pickup = %s, dropoff = %s, capacity = %s, driver_id = %s, price = %s, status = %s WHERE id = %s;""", (date, time, pickup, dropoff, car_capacity, driver_id, price, status, ride_id)) connection.commit() except (Exception, psycopg2.DatabaseError) as error: connection.rollback() return {'status': 'failed', 'data': error}, 500 cursor.close() connection.close() return { 'status': 'success', 'data': 'Ride offer successfully updated' }, 200 return { 'status': 'failed', 'message': 'You entered an invalid ride status' }, 404
def delete(car_registration): """ A method to delete a car. :param registration: A string, the car's licence plate. :return: Http Response """ Car.abort_if_car_doesnt_exist(car_registration) connection = connectDB() cursor = connection.cursor(cursor_factory=psycopg2.extras.DictCursor) try: cursor.execute('DELETE FROM car WHERE id = %s ;', ([car_registration])) connection.commit() except (Exception, psycopg2.DatabaseError) as error: connection.rollback() return {'status': 'failed', 'data': error}, 500 cursor.close() connection.close() return {'status': 'success', 'data': 'Car successfully deleted'}, 200
def edit(user_id, firstname, lastname, password, phone_number=None, car_registration=None): """ A method to accept/decline a ride request. :param firstname: A string, the firstname of the user. :param lastname: A string, the lastname of the user. :param password: A string, the password of the user. :param phone_number: A string, the phone number of the user. :param car_registration: A string, the licence plate of the user's car. :return: Http Response """ connection = connectDB() cursor = connection.cursor(cursor_factory=psycopg2.extras.DictCursor) User.abort_if_user_doesnt_exist(user_id) User.abort_if_car_doesnt_exist(car_registration) try: cursor.execute( """UPDATE app_user SET firstname = %s, lastname = %s, fullname = %s, phone_number = %s, password = %s, car_registration = %s WHERE id = %s;""", (firstname, lastname, '{} {}'.format(firstname, lastname), phone_number, generate_password_hash( password, method='sha256'), car_registration, int(user_id))) connection.commit() except (Exception, psycopg2.DatabaseError) as error: connection.rollback() return {'status': 'failed', 'data': error}, 500 cursor.close() connection.close() return { 'status': 'success', 'message': 'User updated successfully' }, 200
def abort_if_ride_offer_doesnt_exist(ride_id): """ A method to check if a ride exists. :param ride_id: An int, the unique identifier of a ride. :return: Http Response """ connection = connectDB() cursor = connection.cursor(cursor_factory=psycopg2.extras.DictCursor) try: cursor.execute('SELECT * FROM ride WHERE id = %s ;', ([ride_id])) except (Exception, psycopg2.DatabaseError) as error: connection.rollback() return {'status': 'failed', 'data': error}, 500 results = cursor.fetchone() cursor.close() connection.close() if results is None: abort(404, message='The ride with id {} does not exist'.format(ride_id)) return results
def abort_if_email_is_already_used(email): """ A method to check if an email address is already used. :param email: A string, the email address of the user. :return: Http Response """ connection = connectDB() cursor = connection.cursor(cursor_factory=psycopg2.extras.DictCursor) try: cursor.execute('SELECT * FROM app_user WHERE email = %s ;', ([email])) except (Exception, psycopg2.DatabaseError) as error: connection.rollback() return {'status': 'failed', 'data': error}, 500 results = cursor.fetchone() cursor.close() connection.close() if results is not None: abort(400, message='The email {} is already taken'.format(email)) return results
def delete(user_id): """ A method to delete a user. :param user_id: An int, the unique identifier of the user. :return: Http Response """ connection = connectDB() cursor = connection.cursor(cursor_factory=psycopg2.extras.DictCursor) User.abort_if_user_doesnt_exist(user_id) try: cursor.execute('DELETE FROM app_user WHERE id = %s ;', ([user_id])) connection.commit() except (Exception, psycopg2.DatabaseError) as error: connection.rollback() return {'status': 'failed', 'message': error}, 200 cursor.close() connection.close() return { 'status': 'success', 'message': 'User successfully deleted' }, 200
def delete_this_rides_requests(ride_id): """ A method to delete a ride's requests. :param ride_id: An int, the unique identifier of the ride. :return: Http Response """ connection = connectDB() cursor = connection.cursor(cursor_factory=psycopg2.extras.DictCursor) try: cursor.execute('DELETE FROM ride_request WHERE ride_id = %s ;', ([ride_id])) connection.commit() except (Exception, psycopg2.DatabaseError) as error: connection.rollback() return {'status': 'failed', 'data': error}, 500 cursor.close() connection.close() return { 'status': 'success', 'data': 'Ride requests successfully deleted' }, 200
def browse(): """A method to get all rides. :return: A list of dictionaries with all rides """ connection = connectDB() cursor = connection.cursor(cursor_factory=psycopg2.extras.DictCursor) try: cursor.execute('SELECT * FROM ride;') except (Exception, psycopg2.DatabaseError) as error: connection.rollback() return {'status': 'failed', 'data': error}, 500 ride_list = cursor.fetchall() cursor.close() connection.close() if len(ride_list) == 0: return { 'status': 'success', 'message': 'There are no rides offers yet' }, 404 else: data = [] for ride in ride_list: item = { 'id': ride['id'], 'time': ride['time'], 'date': ride['date'], 'pickup': ride['pickup'], 'dropoff': ride['dropoff'], 'capacity': ride['capacity'], 'seats_available': ride['seats_available'], 'driver': User.read(ride['driver_id'])['fullname'], 'price': ride['price'], 'status': ride['status'] } data.append(item) return { 'status': 'success', 'message': 'Fetch successful', 'data': data }, 200
def abort_if_car_registration_is_already_used(registration): """ A method to check if a car's liceence plate is unique. :param registration: A string, the car's licence plate. :return: Http Response """ connection = connectDB() cursor = connection.cursor(cursor_factory=psycopg2.extras.DictCursor) try: cursor.execute('SELECT * FROM car WHERE id = %s ;', ([registration])) except (Exception, psycopg2.DatabaseError) as error: connection.rollback() return {'status': 'failed', 'data': error}, 500 results = cursor.fetchone() cursor.close() connection.close() if results is not None: abort(400, message='The licence plate {} is already used'.format( registration)) return results
def browse(ride_id): """A method to get all ride requests. :return: A list of dictionaries with all ride requests """ connection = connectDB() cursor = connection.cursor(cursor_factory=psycopg2.extras.DictCursor) RideRequest.abort_if_ride_offer_doesnt_exist(ride_id) try: cursor.execute( 'SELECT id, ride_id, requestor_id, request_status FROM ride_request WHERE ride_id = %s ;', ([ride_id])) except (Exception, psycopg2.DatabaseError) as error: connection.rollback() return {'status': 'failed', 'data': error}, 500 ride_offer_requests = cursor.fetchall() cursor.close() connection.close() if len(ride_offer_requests) == 0: return { 'status': 'success', 'message': 'No requests available for this ride yet' }, 404 else: data = [] for request in ride_offer_requests: item = { 'id': request['id'], 'ride_id': request['ride_id'], 'requestor': User.read(request['requestor_id'])['fullname'], 'request_status': request['request_status'], } data.append(item) return { 'status': 'success', 'message': 'Fetch successful', 'data': data }
def login(email, password): """ A method to login a user. :param email: A string, the email address of the user. :param password: A string, the password of the user. :return: Http Response """ connection = connectDB() cursor = connection.cursor(cursor_factory=psycopg2.extras.DictCursor) try: cursor.execute('SELECT * FROM app_user WHERE email = %s ;', ([email])) except (Exception, psycopg2.DatabaseError) as error: connection.rollback() return {'status': 'failed', 'data': error}, 500 results = cursor.fetchone() cursor.close() connection.close() if results is not None: if results['email'] == email and check_password_hash( results['password'], password): expires = datetime.timedelta(days=1) access_token = create_access_token(identity=email, expires_delta=expires) return { 'status': 'success', 'message': 'Login successful', 'access_token': access_token, }, 200 return { 'status': 'failed', 'message': 'Wrong password, please try again' }, 202 else: abort( 404, message='The user with email {} does not exist'.format(email))
def add(self): """ A method to create a user. :return: Http Response """ connection = connectDB() cursor = connection.cursor(cursor_factory=psycopg2.extras.DictCursor) User.abort_if_email_is_already_used(self.email) try: cursor.execute( """INSERT INTO app_user ( firstname, lastname, fullname, email, phone_number, password, car_registration ) VALUES (%s, %s, %s, %s, %s, %s, %s);""", (self.firstname, self.lastname, self.fullname, self.email, self.phone_number, self.password, self.car_registration)) connection.commit() except (Exception, psycopg2.DatabaseError) as error: connection.rollback() return {'status': 'failed', 'message': error}, 500 expires = datetime.timedelta(days=1) access_token = create_access_token(identity=self.email, expires_delta=expires) cursor.close() connection.close() return { 'status': 'success', 'message': 'Account creation successful', 'access_token': access_token, }, 201
def delete(ride_id, request_id): """ A method to delete a ride request. :param ride_id: An int, the unique identifier of a ride. :param request_id: An int, the unique identifier of the ride request. :return: Http Response """ connection = connectDB() cursor = connection.cursor(cursor_factory=psycopg2.extras.DictCursor) RideRequest.abort_if_ride_offer_doesnt_exist(ride_id) RideRequest.abort_if_ride_request_doesnt_exist(request_id) try: cursor.execute('DELETE FROM ride_request WHERE id = %s ;', ([request_id])) connection.commit() except (Exception, psycopg2.DatabaseError) as error: connection.rollback() return {'status': 'failed', 'data': error}, 500 cursor.close() connection.close() return { 'status': 'success', 'message': 'Ride request successfully deleted' }, 200
def add(self): """ A method to create a ride request. :return: Http Response """ connection = connectDB() cursor = connection.cursor(cursor_factory=psycopg2.extras.DictCursor) RideRequest.abort_if_ride_offer_doesnt_exist(self.ride_id) RideRequest.abort_if_requestor_doesnt_exist(self.requestor_id) try: cursor.execute( """INSERT INTO ride_request (ride_id, requestor_id, request_status) VALUES (%s, %s, %s);""", (self.ride_id, self.requestor_id, self.request_status)) connection.commit() except (Exception, psycopg2.DatabaseError) as error: connection.rollback() return {'status': 'failed', 'data': error}, 500 cursor.close() connection.close() return { 'status': 'success', 'message': 'Ride requested successfully' }, 201