Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Completed Database Integration #269

Merged
merged 1 commit into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 6 additions & 55 deletions back-end/src/controllers/studentIssueUpdateHandler.js
Original file line number Diff line number Diff line change
@@ -1,76 +1,27 @@
import { publicpath } from "../../app.js";
import { promises as fs } from 'fs';
import axios from 'axios';
import Issue from '../../models/issueModel.js';

// The function updates the issue related to this student
export async function studentIssueUpdateHandler(req, res) {

const { studentNetID,
paramName } = req.params;

const issueindex = req.body.issueindex;
const newcomment = req.body.comments;
const currentStatus = req.body.currentStatus;
const filePath = publicpath + "/json/mockapi.json";
const fileContent = await fs.readFile(filePath, 'utf8');
const jsonData = JSON.parse(fileContent);


// const specificIssue = jsonData.filter(
// (item) => String(item.index) === String(issueindex)
// );

// if (newcomment !== undefined) {
// specificIssue[0].comments.unshift(newcomment);
// }
// if (currentStatus !== undefined) {
// specificIssue[0].currentStatus = currentStatus;
// }

// fs.writeFile(filePath, JSON.stringify(jsonData, null, '\t'), (err) => {
// if (err) {
// console.error(err);
// } else {
// console.log('File written successfully.');
// }
// });

// const updateData = req.body; // This should contain the data you want to update

try {
// Make a POST request to the backend API to update the student issue
// const response = await axios.post(
// `${process.env.BACKEND_URL}/api/actions/student/${studentNetID}`,
// updateData
// );

const response = await axios.post(
`${process.env.BACKEND_URL}/json/mockapi.json`,
updateData // will be replaced with db call
);

const specificIssue = jsonData.filter(
(item) => String(item.index) === String(paramName)
);
const specificIssue = await Issue.findOne({ index: issueindex });

if (newcomment !== undefined) {
specificIssue[0].comments.unshift(newcomment);
specificIssue.comments.unshift(newcomment);
}
if (currentStatus !== undefined) {
specificIssue[0].currentStatus = currentStatus;
specificIssue.currentStatus = currentStatus;
}

fs.writeFile(filePath, JSON.stringify(jsonData, null, '\t'), (err) => {
if (err) {
console.error(err);
} else {
console.log('File written successfully.');
}
});

const updatedIssue = await specificIssue.save();

// Send a response back to the client indicating success
res.json({ message: 'Issue updated successfully', updatedIssue: response.data });
res.json({ message: 'Issue updated successfully', updatedIssue: updatedIssue });
} catch (error) {
// Log the error and send an appropriate response
console.error('Error updating data:', error.message);
Expand Down
19 changes: 6 additions & 13 deletions back-end/src/controllers/studentIssueViewDetailsHandler.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import axios from "axios";
import Issue from '../../models/issueModel.js';

// The function retrieves all the issues related to this student
export async function studentIssueViewDetailsHandler(req, res) {
Expand All @@ -14,27 +14,20 @@ export async function studentIssueViewDetailsHandler(req, res) {
}

try {
// Assuming the data you want is at the response.data property
const response = await axios.get(
`${process.env.BACKEND_URL}/json/mockapi.json` // will be replaced with db call
);

const response = await Issue.find({ index: paramName });

// Check if any data is returned for the student
if (!response.data || response.data.length === 0) {
if (!response || response.length === 0) {
return res.status(500).send("No issues found for the given studentNetID.");
}

// Assuming response.data is an array of items and each item has a index
const filteredData = response.data.filter(
(item) => String(item.index) === String(paramName)
);

// Check if the specific issue index exists
if (filteredData.length === 0) {
if (response.length === 0) {
return res.status(500).send("Issue with the given index not found.");
}

res.json(filteredData); // Send only the data that matches the specific issue index
res.json(response); // Send only the data that matches the specific issue index
} catch (error) {
// Log the error and send an appropriate response
console.error("Error retrieving data:", error.message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,15 @@ const DesktopIssueDetails = ({ index }) => {
// every time the `index` changes.
}, [index, changeOccured]);

const reopenIssue = () => {
const reopenIssue = async () => {
// setIssue({ ...issue, currentStatus: 'Open' });
postReopen();
await postReopen();
setChangeOccured(!changeOccured);
setIssue({ ...issue, currentStatus: issue.currentStatus });
};

const acceptResolution = () => {
postMarkAsResolved();
const acceptResolution = async () => {
await postMarkAsResolved();
setChangeOccured(!changeOccured);
setIssue({ ...issue, currentStatus: issue.currentStatus });
// Since the issue status is already 'Resolved', no need to change it.
Expand Down