Performance Task Blog
Jan 25, 2025 • 5 min read
Individual Feature: Notification System
My feature focuses on a notification system where users can:
- Send notifications with details such as content, sender, and recipient.
- View notifications sent to them, displayed in an organized and user-friendly list.
This feature ensures users are kept informed about important updates or messages in real-time.
Lists, Dictionaries, and Database
- Lists (Rows): Queries from the database retrieve rows of notification data as Python objects. These rows are used to populate the notification list. A third-party library, SQLAlchemy, is used to perform the queries.
- Dictionaries (Columns): Each row (notification) is converted into a dictionary with keys matching database column names using the
read()
method.
Formatting Response Data (JSON) from API into DOM
The API returns notification data in JSON format. After receiving the JSON data, it is formatted and displayed in the DOM using JavaScript.
Methods in the “Class” to Work with Columns (CRUD Operations)
- Create: Add a new notification to the database.
- Read: Retrieve notification data from the database (already covered above with the query).
- Update: Modify notification content in the database.
- Delete: Remove a notification from the database.
Algorithmic Code
API Class for CRUD Operations
The API class in the code defines the necessary HTTP methods (POST
, GET
, PUT
, DELETE
) to handle requests for the notifications data.
- POST: Creates a new notification and saves it to the database. It uses the request body data (
content
,sender
, andrecipient IDs
) to generate the notification and store it in the database. - GET: Retrieves a single notification by its ID, returning the notification data as a JSON response.
- PUT: Updates an existing notification’s content. The request data specifies which fields (e.g.,
content
) should be updated. - DELETE: Deletes a notification by its ID.
Method/Procedure with Sequencing, Selection, and Iteration
Sequencing
The steps are executed in a specific order:
- Check for Required Fields: The method first checks if the required fields (
content
,user_id
,recipient_id
) are present in the incoming request. If any are missing, it returns an error response. - Create Notification Object: If all fields are valid, a
Notification
object is instantiated using the data from the request. - Save Notification to Database: The
create()
method is then called on theNotification
object to store it in the database.
Selection
Selection is used to determine what action to take based on the input:
- If any required fields are missing or invalid, the method returns an error message.
- If all data is valid, it proceeds to create and store the notification.
Iteration
The get()
method for retrieving all notifications demonstrates iteration:
Notification.query.all()
fetches all notifications from the database.- The method then iterates through the fetched notifications, using a list comprehension, to generate a JSON response containing the data for each notification.
Parameters (Request Body) and Return Type (JSONify)
Parameters
The parameters for the POST
, PUT
, and DELETE
methods are obtained from the request body using request.get_json()
.
For example, in the POST
method, the expected parameters are:
- content: The content of the notification.
- user_id: The ID of the user sending the notification.
- recipient_id: The ID of the user receiving the notification.
Return Type
The methods return a JSON response using jsonify()
:
- POST: After creating a notification, the
post()
method returns the newly created notification’s data as JSON. - GET: The
get()
method returns the notification data as JSON. - PUT: After updating the notification, the
put()
method returns the updated notification data as JSON. - DELETE: The
delete()
method returns a JSON message confirming the deletion of the notification.
Call to Algorithm Request
Call/Request to the Method with Algorithm (Fetch to Endpoint)
To make a request to the backend (such as creating, retrieving, or updating a notification), you can use the fetch()
method. Below is an example of sending a POST
request to create a notification:
Return/Response from the Method with Algorithm (Fetch) and How You Handle Data
Normal Conditions
If the notification is successfully created:
- The
response.ok
will betrue
. - The JSON response data is parsed and used as needed by the front-end application.
Error Conditions
If the response is not OK (i.e., response.ok
is false
):
- An error is thrown.
- The error message is logged to the console.
- The error can be handled appropriately, such as displaying an error message to the user.
How Changing Data or Method Triggers Different Responses
Data Changes (Normal Condition)
If the notification data is correct (e.g., all required fields are present):
- The backend will return a success message or the notification data with a
200
status code. - The returned data is processed and displayed by the front-end application.
Error Conditions (Invalid Data)
If the notification data is missing required fields (e.g., content
or recipient_id
is missing):
- The server may respond with an error message (e.g.,
400 Bad Request
). - The error response will indicate the issue, and the front-end can handle it accordingly.