Spring 3 Review Blog
Backend Learning for Sprint 3 Project
Backend Startup
Firstly, we go to Main,py and start debugging it to start up the API.

Using Groups.py we find our Section.id in order to create a group that works when we use Posts.

We then registered our channels in channel.py

Code for Backend implemented in Frontend
<script type="module">
// Import server URI and standard fetch options
import { pythonURI, fetchOptions } from '/zafeer_2025/assets/js/api/config.js';
/**
* Fetch groups for dropdown selection
* User picks from dropdown
*/
async function fetchGroups() {
try {
const response = await fetch(`${pythonURI}/api/groups/filter`, {
...fetchOptions,
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ section_name: "Vote for the GOAT" }) // Adjust the section name as needed
});
if (!response.ok) {
throw new Error('Failed to fetch groups: ' + response.statusText);
}
const groups = await response.json();
const groupSelect = document.getElementById('group_id');
groups.forEach(group => {
const option = document.createElement('option');
option.value = group.name; // Use group name for payload
option.textContent = group.name;
groupSelect.appendChild(option);
});
} catch (error) {
console.error('Error fetching groups:', error);
}
}
Gets Groups from the Server: It sends a request to the server asking for a list of groups using an API endpoint (/api/groups/filter).
Handles the Response: If the server responds successfully, it gets a list of groups (e.g., “Team A,” “Team B”). Adds Groups to a Dropdown:
It finds a dropdown menu on your page (with an ID of group_id). For each group it received, it creates an option in the dropdown, so you can select one.
/**
* Fetch channels based on selected group
* User picks from dropdown
*/
async function fetchChannels(groupName) {
try {
const response = await fetch(`${pythonURI}/api/channels/filter`, {
...fetchOptions,
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ group_name: groupName })
});
if (!response.ok) {
throw new Error('Failed to fetch channels: ' + response.statusText);
}
const channels = await response.json();
const channelSelect = document.getElementById('channel_id');
channelSelect.innerHTML = '<option value="">Select a channel</option>'; // Reset channels
channels.forEach(channel => {
const option = document.createElement('option');
option.value = channel.id;
option.textContent = channel.name;
channelSelect.appendChild(option);
});
} catch (error) {
console.error('Error fetching channels:', error);
}
}
/**
* Handle group selection change
* Channel Dropdown refresh to match group_id change
*/
document.getElementById('group_id').addEventListener('change', function() {
const groupName = this.value;
if (groupName) {
fetchChannels(groupName);
} else {
document.getElementById('channel_id').innerHTML = '<option value="">Select a channel</option>'; // Reset channels
}
});
/**
* Handle form submission for selection
* Select Button: Computer fetches and displays posts
*/
document.getElementById('selectionForm').addEventListener('submit', function(event) {
event.preventDefault();
const groupId = document.getElementById('group_id').value;
const channelId = document.getElementById('channel_id').value;
if (groupId && channelId) {
fetchData(channelId);
} else {
alert('Please select both group and channel.');
}
});
/**
* Handle form submission for adding a post
* Add Form Button: Computer handles form submission with request
*/
document.getElementById('postForm').addEventListener('submit', async function(event) {
event.preventDefault();
// Extract data from form
const title = document.getElementById('title').value;
const comment = document.getElementById('comment').value;
const channelId = document.getElementById('channel_id').value;
// Create API payload
const postData = {
title: title,
comment: comment,
channel_id: channelId
};
async function fetchData(channelId) {
try {
const response = await fetch(`${pythonURI}/api/posts/filter`, {
...fetchOptions,
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ channel_id: channelId })
});
if (!response.ok) {
throw new Error('Failed to fetch posts: ' + response.statusText);
}
// Parse the JSON data
const postData = await response.json();
// Extract posts count
const postCount = postData.length || 0;
// Update the HTML elements with the data
document.getElementById('count').innerHTML = `<h2>Count ${postCount}</h2>`;
// Get the details div
const detailsDiv = document.getElementById('details');
detailsDiv.innerHTML = ''; // Clear previous posts
// Iterate over the postData and create HTML elements for each item
postData.forEach(postItem => {
const postElement = document.createElement('div');
postElement.className = 'post-item';
postElement.innerHTML = `
<h3>${postItem.title}</h3>
<p><strong>Channel:</strong> ${postItem.channel_name}</p>
<p><strong>User:</strong> ${postItem.user_name}</p>
<p>${postItem.comment}</p>
`;
detailsDiv.appendChild(postElement);
});
} catch (error) {
console.error('Error fetching data:', error);
}
}
// Fetch groups when the page loads
fetchGroups();
</script>
-
Update Channels When a Group is Selected:
-
When you pick a group from the first dropdown (group_id), the script fetches and updates the channels in the second dropdown (channel_id) to match the selected group. If no group is selected, it resets the channel dropdown to its default “Select a channel.”
Show Posts for Selected Channel:
-
When you submit the selection form (selectionForm), the script checks if you’ve selected both a group and a channel. If both are selected, it fetches posts for that channel and displays them. If not, it shows an alert asking you to select both.
-
The fetchData() function gets posts for a selected channel. It updates the webpage to show: The total number of posts. A list of posts with their title, channel name, user name, and comments.
Simple Flow: Pick a group → See related channels. Pick a channel → See related posts. Add a post → Update the post list. Everything updates dynamically based on what you choose or submit.
Final user_management
