예제 #1
0
    def create_user(self):

        sql = "INSERT INTO users (f_name, l_name, email, city, phone_no, password)" \
              " VALUES (%s, %s, %s, %s, %s, %s) RETURNING id"
        try:
            with DatabaseManager() as cursor:
                # check if email already exists
                cursor.execute(
                    "SELECT email FROM users WHERE email = ('%s')" % self.email)
                results = cursor.fetchone()

                if results:
                    return {"message": "Email already registered", "code": 400}

                else:
                    cursor.execute(sql, (self.f_name, self.l_name, self.email,
                                         self.city, self.phone_no, hash_password(self.password)))

                    cursor.execute(
                        "SELECT * FROM users WHERE email = '%s'" % self.email)
                    result_user = cursor.fetchone()

                    return self.user_json(result_user[0], result_user[1], result_user[2],
                                          result_user[3], result_user[4], result_user[5], result_user[6])
        except Exception as e:
            return e
예제 #2
0
 def tearDown(self):
     with app.app_context():
         with DatabaseManager() as cursor:
             drop_tables_file = drop_schema
             with open(drop_tables_file, 'r') as file:
                 sql = file.read()
                 cursor.execute(sql)
예제 #3
0
    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"
                }
예제 #4
0
 def logout(user_id):
     try:
         with DatabaseManager() as cursor:
             print(user_id)
             logged_out = "update users set logged_in = FALSE where id = %s returning id"
             cursor.execute(logged_out, [user_id])
             results = cursor.fetchone()
             if results:
                 return {"message": "You are logged out successfully"}
     except Exception as e:
         return e
예제 #5
0
    def generate_ref_no():
        sql = "SELECT id FROM rides WHERE id = (select max(id) from rides)"

        with DatabaseManager() as cursor:
            try:
                cursor.execute(sql)
                results = cursor.fetchone()
                if results:
                    return "RF00" + str(results[0] + 1)
                else:
                    return "RF001"
            except Exception as e:
                return e
예제 #6
0
def check_user_ride(ride_id, user_id):
    """This method returns false when ride is not found and true when user created it"""
    try:
        with DatabaseManager() as cursor:
            cursor.execute("SELECT ref_no FROM rides WHERE id = %s", [ride_id])
            if cursor.fetchone():

                cursor.execute(
                    "SELECT ref_no FROM rides WHERE creator_id = %s AND id = %s",
                    [user_id, ride_id])
                if cursor.fetchone():
                    return True
                return False
            return "Ride Not Found"

    except Exception as e:
        return e
예제 #7
0
    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"}
예제 #8
0
    def login_user(email, password):
        try:
            with DatabaseManager() as cursor:

                sql = "select id,email, password from users where email = %s and password = %s"
                logged_in = "update users set logged_in = TRUE where email = %s and password = %s"

                cursor.execute(sql, (email, hash_password(password)))
                results = cursor.fetchone()
                if results:

                    token = jwt.encode(
                        {'email': email, 'user_id': results[0]}, secret, algorithm='HS256').decode()

                    cursor.execute(logged_in, (email, hash_password(password)))
                    return {"message": "You are logged in", "token": token}
                else:
                    return {
                        "message": "Email and password don't match",
                        "code": 401}
        except Exception as e:
            return e
예제 #9
0
    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"}
예제 #10
0
    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"}
예제 #11
0
    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"}