Component A: Program Code

from flask import request, jsonify, Blueprint
from model.factsbase import db, Facts

# Blueprint for handling user-submitted facts via API endpoints
userfacts = Blueprint('userfacts', __name__)

@userfacts.route('/api/userfacts', methods=['POST'])
def add_user():
    """
    Algorithm: Adds a new fact to the database.
    - Extracts JSON data from the request.
    - Validates that required fields ('name' and 'fact') are provided.
    - Creates a new database entry and commits it.
    - Returns a success message if the operation is successful.
    """
    data = request.get_json()
    name = data.get('name')
    fact = data.get('fact')

    if not all([name, fact]):  # Ensuring necessary data is provided
        return jsonify({'error': 'Missing data'}), 400

    new_user = Facts(name=name, fact=fact)  # Data abstraction: Storing user fact
    db.session.add(new_user)
    db.session.commit()

    return jsonify({'message': 'Fact added successfully'}), 201

@userfacts.route('/api/userfacts', methods=['GET'])
def get_fact():
    """
    Algorithm: Retrieves all stored facts.
    - Queries the database for all facts.
    - Formats the data into a structured JSON response.
    - Returns the facts list with a success status.
    """
    facts = Facts.query.all()
    result = [
        {
            'id': fact.id,
            'name': fact._name,  # Potential issue: attribute name might be incorrect
            'fact': fact._fact,
        }
        for fact in facts
    ]
    return jsonify(result), 200  # Returning structured data abstraction

@userfacts.route('/api/userfacts/<int:id>', methods=['DELETE'])
def delete_facts(id):
    """
    Algorithm: Deletes a fact from the database.
    - Searches for a fact using the provided ID.
    - If found, deletes the fact and commits changes.
    - If not found, returns an error message.
    """
    fact = Facts.query.get(id)
    if not fact:
        return jsonify({'error': 'Fact not found'}), 404

    db.session.delete(fact)
    db.session.commit()
    return jsonify({'message': 'Fact deleted successfully'}), 200

@userfacts.route('/api/userfacts/<int:id>', methods=['PUT'])
def update_fact(id):
    """
    Algorithm: Updates an existing fact.
    - Extracts JSON data from the request.
    - Ensures 'name' and 'fact' fields are provided.
    - Finds the existing fact and updates its values.
    - Commits the changes and returns a success message.
    """
    data = request.get_json()
    name = data.get('name')
    fact = data.get('fact')

    if not all([name, fact]):  # Validation to ensure all necessary data is present
        return jsonify({'error': 'Missing data'}), 400

    existing_fact = Facts.query.get(id)
    if not existing_fact:
        return jsonify({'error': 'Fact not found'}), 404

    existing_fact._name = name  # Potential issue: Attribute naming convention
    existing_fact._fact = fact
    db.session.commit()
    return jsonify({'message': 'Fact updated successfully'}), 200


    



How This Code Represents Component A

Program Functionality

  • The code defines a RESTful API using Flask to handle user-submitted “facts.”
  • It supports CRUD (Create, Read, Update, Delete) operations on a database using SQLAlchemy.

Algorithm Implementation

  • The add_user() function processes user input, validates it, and stores it in a database.
  • The get_fact() function retrieves and formats database entries.
  • The update_fact() function modifies stored data.
  • The delete_facts() function removes a record.

Data Abstraction

  • The Facts class (presumably in model/factsbase.py) represents a database table, abstracting the underlying data structure.
  • The request.get_json() method extracts and processes structured JSON data.
  • The SQLAlchemy ORM handles data persistence, abstracting direct SQL operations.

1. Input from User



data = request.get_json()  # The user sends input as a JSON object.
name = data.get('name')
fact = data.get('fact')


2. Use of list to Represent Data



facts = Facts.query.all()  # Querying all facts from the database
result = [
    {
        'id': fact.id,
        'name': fact._name,
        'fact': fact._fact,
    }
    for fact in facts
]


Component B: Video

3. Student-Developed Procedure


def add_user():
    data = request.get_json()
    name = data.get('name')
    fact = data.get('fact')

    if not all([name, fact]):
        return jsonify({'error': 'Missing data'}), 400

    new_user = Facts(name=name, fact=fact)
    db.session.add(new_user)
    db.session.commit()

    return jsonify({'message': 'Fact added successfully'}), 201
    

4. Algorith with sequencing, selection and iteration



def add_user():
    data = request.get_json()
    name = data.get('name')
    fact = data.get('fact')

    if not all([name, fact]):
        return jsonify({'error': 'Missing data'}), 400

    new_user = Facts(name=name, fact=fact)
    db.session.add(new_user)
    db.session.commit()

    return jsonify({'message': 'Fact added successfully'}), 201
    

5. Calls to procedure


@userfacts.route('/api/userfacts', methods=['POST'])
def add_user_route():
    return add_user()  # Calling the procedure defined above
    

6. Output Based on Input



return jsonify({'message': 'Fact added successfully'}), 201


Component B - Video

Demonstrates:

  • Input to your program
  • At least one aspect of the functionality of your program
  • Output produced by your program

Requirements Followed:

  • Either .webm, .mp4, .wmv, .avi, or .mov format
  • No more than 1 minute in length
  • No more than 30MB in file size

Procedure Example

## Component C - Personalized Project Reference

### 1. **Student-Developed Procedure:**
This procedure implements an algorithm used in your program. It contains:
- A **procedure name** and **return type** (if necessary).
- One or more **parameters** that affect the functionality of the procedure.
- An **algorithm** that includes **sequencing**, **selection**, and **iteration**.



# Student-developed procedure
def add_user(data):
    """
    This procedure adds a user and their fact to the database.
    It checks if all data is provided, then adds it to the database.
    """
    name = data.get('name')
    fact = data.get('fact')
    
    # Selection (if-check to ensure data exists)
    if not all([name, fact]):
        return {'error': 'Missing data'}, 400
    
    # Sequencing (creating new user and committing to DB)
    new_user = Facts(name=name, fact=fact)
    db.session.add(new_user)
    db.session.commit()
    
    # Return success message after addition
    return {'message': 'Fact added successfully'}, 201
   

Key Aspects:

  • Procedure Name: add_user
  • Parameters: data (this parameter is a dictionary containing name and fact)
  • Algorithm:
    • sequencing: Check if data exists, create new user, and commit to the database.
    • Selection: Ensure both name and fact are provided before adding to the database.
    • Iteration: Though this specific method doesn’t have a loop, you could imagine an iteration process in other functions where multiple users/facts are handled.

Calling the add_user Procedure:

2. Call the add_user function when a POST request is made to ‘/api/userfacts’

  • The second code segment shows where the add_user procedure is being called in the program.

    


Key Aspects:

  • Procedure Name: add_user is being called inside the route handler for POST /api/userfacts.
  • Parameters: The data parameter is passed to the add_user procedure, which contains the user’s input.
  • Return: The add_user function returns a response that is sent back as JSON to the client.

3. Using a List to Manage Complexity:

In this section, we store multiple insights in a list, which allows us to handle and manage the insights efficiently.

  • Storing Data(in this case insights) in a List:


facts = [
    {'id': 1, 'name': 'Alice', 'fact': 'Apples are green or red'},
    {'id': 2, 'name': 'Bob', 'fact': 'Honey never spoils'},
    {'id': 3, 'name': 'Charlie', 'fact': 'The moon is made of cheese'},
    {'id': 4, 'name': 'Diana', 'fact': 'The earth is flat'}
]


Key Aspects:

  • List Type: A list of dictionaries is used to store facts with their id, name, and fact.
  • Purpose: The list allows us to efficiently handle a collection of facts for further processing (e.g., retrieving, updating, deleting).

4. Using the List to Fulfill Program’s Purpose:

Now, we use the stored facts from the list to retrieve and manipulate data, such as fetching all facts from the database.

  • Accessing and Using List Data:

 
@userfacts.route('/api/userfacts', methods=['GET'])
def get_fact():
    # Iterating through the facts list and returning each fact
    result = [
        {'id': fact['id'], 'name': fact['name'], 'fact': fact['fact']}
        for fact in facts
    ]
    return jsonify(result), 200
    

Key Aspects:

  • Iteration: We iterate over the facts list to retrieve each fact and prepare it for output.
  • Purpose: This is a part of the program’s goal to allow the user to retrieve all facts stored in the system.
  • Data Manipulation: The facts are accessed from the list, transformed into a suitable format (JSON), and then returned to the user.

Summary of Component C

  1. Student-Developed Procedure:
    • Definition: A procedure with a name, return type (if needed), and parameters affecting its functionality.
    • Algorithm Features: Should include sequencing, selection, and iteration.
    • Example: The add_user procedure checks if data is provided, adds the data to the database, and returns a success or error message.
  2. Calling the Procedure:
    • Definition: The procedure is invoked in the program using the relevant parameters.
    • Example: The add_user_route function calls add_user with data from the request and returns the response.
  3. Using a List for Data Management:
    • Definition: A list (or other collection) is used to manage multiple items efficiently.
    • Example: Facts are stored in a list of dictionaries, allowing easy management of different user facts.
  4. Accessing Data in the List:
    • Definition: The stored data is accessed and used as part of the program’s functionality.
    • Example: A GET request retrieves facts from the list and sends them as a JSON response, utilizing iteration to process the list.