PPR
PPR
1
The first program code segment must be a student-developed procedure that:
□ Defines the procedure’s name and return type (if necessary)
□ Contains and uses one or more parameters that have an effect on
the functionality of the procedure
□ Implements an algorithm that includes sequencing, selection, and
iteration
Procedure name: get_comments_for_book
Parameters: book_id, returns a list
- Sequencing: checking if book_id is provided, then querying the database, fetching users, and appending the data into a list.
- Selection: Yes, the if book_id: statement is a selection (conditional statement). If a book_id is provided, it queries for comments specific to that book; otherwise, it queries for all comments.
- Iteration: Yes, the for comment in comments_query: loop is an iteration. It goes through each comment in the comments_query and processes it, fetching associated user information and appending it to the comments_data list.
def get_comments_for_book(book_id=None):
if book_id:
comments_query = Comments.query.filter_by(book_id=book_id).all()
else:
comments_query = Comments.query.all()
comments_data = []
for comment in comments_query:
user = User.query.get(comment.user_id)
user_name = user._name if user else "Unknown User"
comments_data.append({
"id": comment.id,
"book_id": comment.book_id,
"user_id": comment.user_id,
"user_name": user_name,
"comment_text": comment.comment_text
})
return comments_data
2
The second program code segment must show where your student-developed procedure is being called in your program.
function fetchComments() {
fetch(`${pythonURI}/api/comments?book_id=${currentBook.id}`, fetchOptions)
.then(response => response.json())
.then(data => {
if (data.comments) {
displayComments(data.comments);
} else {
console.error('No comments found for this book.');
}
})
.catch(error => {
console.error('Error fetching comments:', error);
alert('Failed to fetch comments.');
});
}
In this code segment, the API being called returns the JSONified lists for the comments associated with a specific book id, which utlizes the ‘'’get_comments_for_book’’’ function.
3
The first program code segment must show how data have been stored in the list.
comments_data = []
for comment in comments_query:
user = User.query.get(comment.user_id)
user_name = user._name if user else "Unknown User"
comments_data.append({
"id": comment.id,
"book_id": comment.book_id,
"user_id": comment.user_id,
"user_name": user_name,
"comment_text": comment.comment_text
})
In this section of code, each comment is stored as a dictionary in a list of comments, with keys like ‘'’id’’’, ‘'’book_id’’’, ‘'’user_id’’’, ‘'’user_name’’’, and ‘'’comment_text’’’, demonstrating how the comments list contains comments in the form of dictionaries.
Purpose: This stores the comments associated with a book, including the user name, in a structured format (a list of dictionaries) for further use (returning the comments as a JSON response).
4
The second program code segment must show the data in the same list being used, such as creating new data from the existing data or accessing multiple elements in the list, as part of fulfilling the program’s purpose.
new_comment = Comments(
book_id=book_id,
user_id=user.id,
comment_text=comment_text
)
db.session.add(new_comment)
db.session.commit()
return jsonify({
'id': new_comment.id,
'book_id': new_comment.book_id,
'user_id': new_comment.user_id,
'comment_text': new_comment.comment_text
}), 201
In this code, new data is created when a new Comments instance is instantiated and added to the database using the provided book_id, user_id, and comment_text. Multiple parts of the request data are accessed, such as book_id, user_id, and comment_text, and used to validate and create the new comment. Also, related data is fetched from the Book and User model relationships to associate the comment with the correct book and user.