Skip to the content.

Sprint 5 Blog

Blog with details for sprint 5

Purpose

  • The Purpose of my overall group’s program is for people to have a designated place to explore and review books as well as post their favorite books for people to find. We accomplish this through a variety of pages such as the Moderator’s Picks page and the pages inside of it, like the Book Gallery.

  • My individual features involve the preferences page as well as the usersDb database. The preferences page allows users to enhance their visit to the website by changing the colors of different elements, such as the text and the menu of the website. The usersDb database allows users to post their name as well as their favorite book to allow people to find more new books that others enjoy.

Input/Output requests

(Shown in Live Review)

List Requests

  • Firstly, in the database, lists are used to store the user records that are taken from the database using “users = usersDb.query.all()”. The user information is stored inside a list that is filled with dictionaries and returned in JSON format. As mentioned, each user is stored inside its own dictionary, with each piece of information being stored as a key/value pair.

  • Second, to convert JSON into DOM format, the JSON data (in this case, the list of users) is fetched and then systematically (each user) converted into javascript objects, such as a <div> object.

  • In order to extract python lists, we use queries from the database that use a 3rd party library; more specifically, they use SQLAlchemy. We use usersDb.query.all() in order to extract these lists.

users.forEach((user) => {
                    const div = document.createElement('div');
                    div.classList.add('user');
                    div.innerHTML = `
                        <p>Name: <span>${user.name}</span></p>
                        <p>Fav_Book: <span>${user.fav_book}</span></p>
                        <p>User ID: <span>${user.user_id}</span></p>
                        <button onclick="deleteUser(${user.user_id})">Delete</button>
                    `;
                    usersContainer.appendChild(div);
@token_required
        def get(self):
            """
            Retrieve all users.
            """
            try:
                users = usersDb.query.all()
                json_ready = [user.read() for user in users]
                return jsonify(json_ready), 200
            except Exception as e:
                return {'message': f'Error fetching users: {str(e)}'}, 500

CRUD Operations

  • Create: creates a new user with data for name, favorite_book, and user_id
  • Read: the program reads the JSON and converts it into DOM format for people to view on the frontend
  • Update: updates a user’s columns, useful if their favorite book changes
  • Delete: allows for a user to be deleted from the database (need to add admin so that not anyone can use this)

Algorithmic Code Request

Firstly, the get method is used to get the user information. This pairs with the post method that then posts the user information to the frontend for people to view. In case there is incorrect information, a put method is used to replace parts or all of a user’s information. Finally, the delete method is placed to completely delete a user in case they don’t want their profile up their anymore.

Next, a method with sequencing, selection, and iteration is the GET method. This uses sequencing by retrieving the database’s data and converting the rows from the database into dictionaries. Next, _____. Finally, the method uses a loop to process each row of data that is obtained from the database to convert them into dictionaries.

When looking at the GET method, the parameters are the JSON objects that the database sends. Then, the method cycles through the data and returns it in DOM format for the frontend. When trying to insert data into the database, this is reversed and the parameters is the DOM data and the outputs are the JSON objects.

@app.route('/usersDb', methods=['GET', 'POST'])
def manage_users():
    if request.method == 'GET':
        # Fetch all users from the database
        try:
            conn = sqlite3.connect(DB_PATH)
            cursor = conn.cursor()
            cursor.execute("SELECT * FROM usersDb")
            users = [
                {"table_id": row[0], "name": row[1], "fav_book": row[2], "user_id": row[3]} 
                for row in cursor.fetchall()
            ]
            conn.close()
            return jsonify(users), 200
        except Exception as e:
            return jsonify({"error": str(e)}), 500

Call to Algorithm Request

  • Call to method: uses fetch to get the data.

  • This method uses the data to create a user and returns it in JSON. This response is used in the frontend to update its UI and add the new user.

  • If the method finds an issue, it will return an error message based on the type of issue. If the information is missing required parts, like the user age, then it will return a message saying that all fields are required, for example. If you were to change the POST method to a GET method, then there would be an error with finding the user in the database, as it does not exist.

if not name or fav_book is None or user_id is None:
            return jsonify({"error": "Name, fav_book, and user_id are required"}), 400
except sqlite3.IntegrityError:
            return jsonify({"error": "User ID must be unique"}), 400
        except Exception as e:
            return jsonify({"error": str(e)}), 500
if rows_deleted == 0:
                return jsonify({"error": "User not found"}), 404