Skip to content

Commit

Permalink
endpoint workering
Browse files Browse the repository at this point in the history
  • Loading branch information
junidy committed Apr 13, 2024
1 parent b728ff4 commit ff83759
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 11 deletions.
3 changes: 2 additions & 1 deletion backend/rest-test.http
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@base=http://localhost:3000
@jwtToken=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOjEsImlhdCI6MTcxMjk2MzI5MywiZXhwIjoxNzEzMDQ5NjkzfQ.TFlQor5AbxFNAQe6SLFK0Dv8y1tIDQW6RmZj0pe4mpA
@jwtToken=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOjUsImlhdCI6MTcxMjk2OTUyNCwiZXhwIjoxNzEzMDU1OTI0fQ.-3fzq2KmhBwMngqE7ZCw504D0Q7CCiWlGp4IWVtWrD0
@eventId=1

@rsoId=8
Expand Down Expand Up @@ -103,6 +103,7 @@ Content-Type: application/json

### Get all RSOs's names and ids.
GET {{base}}/rsos
Authorization: Bearer {{jwtToken}}

### Create a new RSO, after verifying that the user is an admin.
POST {{base}}/rsos
Expand Down
43 changes: 36 additions & 7 deletions backend/src/endpoints/rsos.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const router = express.Router();
const db = mysql.createPool({
host: 'localhost',
user: 'root',
password: '',
password: 'password',
database: 'cop4710'
});
const query = promisify(db.query).bind(db);
Expand Down Expand Up @@ -42,17 +42,46 @@ const isAdmin = async (req, res, next) => {
}
};

// Get all RSOs
router.get('/', async (req, res) => {
router.get('/', verifyToken, async (req, res) => {
const userId = jwt.verify(req.token, process.env.JWT_SECRET_KEY).userId;
try {
const rsos = await query('SELECT name, rso_id FROM rsos');
res.status(200).json(rsos);

// First, get the user's university_id
const [users] = await query(`
SELECT university_id FROM users WHERE user_id = ?
`, [userId]);

if (users.length === 0) {
res.status(404).json({ message: "User not found" });
await conn.end();
return;
}

const universityId = users.university_id;

// Next, fetch all RSOs from the user's university and whether the user is a member
const rsos = await query(`
SELECT rsos.rso_id, rsos.name,
EXISTS(SELECT 1 FROM rso_members WHERE rso_members.rso_id = rsos.rso_id AND rso_members.member_id = ?) AS is_member
FROM rsos
WHERE EXISTS (SELECT 1 FROM users WHERE users.user_id = rsos.admin_id AND users.university_id = ?)
`, [userId, universityId]);

// Format the response as JSON
const formattedRsos = rsos.map(rso => ({
rso_id: rso.rso_id,
name: rso.name,
is_member: !!rso.is_member // convert the number to a boolean
}));

res.json(formattedRsos);
} catch (error) {
console.error(error);
res.status(500).json({ error: 'Internal server error' });
console.error('Failed to query RSOs:', error);
res.status(500).json({ message: "Failed to retrieve RSO information" });
}
});


// Create a new RSO
router.post('/', verifyToken, isAdmin, async (req, res) => {
const { name } = req.body;
Expand Down
2 changes: 1 addition & 1 deletion database/TestData.sql
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ INSERT INTO `users` (`name`, `password`, `phone`, `email`, `university_id`) VALU

-- Insert Admins into `admins` table
INSERT INTO `admins` (`user_id`) VALUES
(1), (2), (3), (4);
(1), (2), (3), (4), (5), (6);

-- RSOs for UCF and Harvard
INSERT INTO `rsos` (`admin_id`, `name`) VALUES
Expand Down
16 changes: 14 additions & 2 deletions frontend/src/utils/endpoints.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import axios from 'axios';
const token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOjUsImlhdCI6MTcxMjk1MTc1NiwiZXhwIjoxNzEzMDM4MTU2fQ.GwdLOOgSYOUPRrOyu5aIev38YYpkeeqgNBd9qsdR4wM';
const base = import.meta.env.PROD
? ''
: 'http://localhost:3000'
const url = import.meta.env.PROD
? ''
: 'http://localhost:3000'

const postLogin = (creds) => {
return axios.post(`${base}/auth/login`, creds)
Expand Down Expand Up @@ -120,4 +122,14 @@ const fetchEvents = () => {
export {
postLogin,
postRegister,
};
getEvents,
postEvent,
getRSOs,
createRSO,
joinRSO,
leaveRSO,
getComments,
addComment,
editComment,
removeComment
};

0 comments on commit ff83759

Please sign in to comment.