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

Main #196

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

Main #196

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
21 changes: 21 additions & 0 deletions badges_engagement/controller/badgeController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const db = require('../config/db');

// Get all badges
exports.getBadges = (req, res) => {
const sql = 'SELECT * FROM badges';
db.query(sql, (err, result) => {
if (err) throw err;
res.json(result);
});
};

// Award a badge to a user
exports.awardBadge = (req, res) => {
const { user_id, badge_id } = req.body;

const sql = 'INSERT INTO user_badges (user_id, badge_id) VALUES (?, ?)';
db.query(sql, [user_id, badge_id], (err, result) => {
if (err) throw err;
res.json({ message: 'Badge awarded successfully!' });
});
};
43 changes: 43 additions & 0 deletions badges_engagement/models/badge.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const { DataTypes } = require('sequelize');
const sequelize = require('../config/db'); // Adjust the path as necessary

const Badge = sequelize.define('Badge', {
id: {
type: DataTypes.INTEGER,
autoIncrement: true,
primaryKey: true,
},
name: {
type: DataTypes.STRING,
allowNull: false,
unique: true,
},
criteria: {
type: DataTypes.STRING,
allowNull: false,
},
createdAt: {
type: DataTypes.DATE,
defaultValue: DataTypes.NOW,
},
updatedAt: {
type: DataTypes.DATE,
defaultValue: DataTypes.NOW,
}
}, {
tableName: 'badges', // The name of the table in the database
});

// Sync the model with the database
const syncBadges = async () => {
try {
await Badge.sync(); // Creates the table if it doesn't exist
console.log("Badge table has been synced.");
} catch (error) {
console.error("Error syncing Badge table: ", error);
}
};

syncBadges();

module.exports = Badge;
25 changes: 25 additions & 0 deletions badges_engagement/public/scripts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Fetch user's badges
function fetchBadges(userId) {
fetch(`/badges/${userId}`)
.then(response => response.json())
.then(badges => {
const badgeContainer = document.getElementById('badge-container');
badgeContainer.innerHTML = '';

badges.forEach(badge => {
const badgeElement = document.createElement('div');
badgeElement.classList.add('badge');
badgeElement.innerHTML = `
<h3>${badge.name}</h3>
<p>${badge.description}</p>
<small>Earned on: ${new Date(badge.earned_at).toLocaleDateString()}</small>
`;
badgeContainer.appendChild(badgeElement);
});
})
.catch(error => console.error('Error fetching badges:', error));
}

// Replace with actual userId
fetchBadges(1);

39 changes: 39 additions & 0 deletions badges_engagement/public/styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
}

h1, h2 {
text-align: center;
}

#badge-container {
display: flex;
flex-wrap: wrap;
justify-content: center;
}

.badge {
background-color: #fff;
border: 1px solid #ddd;
border-radius: 5px;
padding: 10px;
margin: 10px;
width: 200px;
text-align: center;
}

.badge h3 {
margin: 10px 0;
}

.badge p {
font-size: 0.9em;
}

.badge small {
display: block;
margin-top: 5px;
color: #888;
}

31 changes: 31 additions & 0 deletions badges_engagement/routes/badges.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const express = require('express');
const router = express.Router();
const db = require('../db'); // Assuming db.js has your MySQL config

// Get all badges for a user
router.get('/:userId', (req, res) => {
const { userId } = req.params;
const sql = `
SELECT badges.name, badges.description, user_badges.earned_at
FROM badges
JOIN user_badges ON badges.id = user_badges.badge_id
WHERE user_badges.user_id = ?`;

db.query(sql, [userId], (err, results) => {
if (err) throw err;
res.json(results);
});
});

// Award a new badge to a user
router.post('/award', (req, res) => {
const { userId, badgeId } = req.body;
const sql = `INSERT INTO user_badges (user_id, badge_id) VALUES (?, ?)`;

db.query(sql, [userId, badgeId], (err, result) => {
if (err) throw err;
res.json({ message: 'Badge awarded successfully' });
});
});

module.exports = router;
43 changes: 43 additions & 0 deletions badges_engagement/sql script
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
-- Create the database
CREATE DATABASE research_nexas;

-- Select the database to use
USE research_nexas;

-- Create the 'student' table
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Delete this table its already created

CREATE TABLE student (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL,
badge_count INT DEFAULT 0
);

-- Create the 'badges' table
CREATE TABLE badges (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
description TEXT NOT NULL,
criteria VARCHAR(255)
);

-- Create the 'student_badges' table (stores which student earned which badge)
CREATE TABLE student_badges (
id INT AUTO_INCREMENT PRIMARY KEY,
student_id INT NOT NULL,
badge_id INT NOT NULL,
awarded_at DATETIME DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (student_id) REFERENCES students(id) ON DELETE CASCADE,
FOREIGN KEY (badge_id) REFERENCES badges(id) ON DELETE CASCADE
);


-- Insert some example data into 'student' table
INSERT INTO student (name, email) VALUES ('Alice', 'alice@example.com');

-- Insert example badges into 'badges' table
INSERT INTO badges (name, description, criteria) VALUES
('Research Contributor', 'Awarded for submitting 5 research papers', 'Submit 5 papers'),
('Peer Reviewer', 'Awarded for completing 10 peer reviews', 'Complete 10 reviews');

-- Award badges to student (student_badges table)
INSERT INTO student_badges (user_id, badge_id) VALUES (1, 1), (1, 2);
23 changes: 23 additions & 0 deletions login-system/dbServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,29 @@ const { allot, DisplayPapers } = require("../stakeholder/allotment");
const { Dis_fac_papers, fac_signup, fac_login, dis_mail, giverating } = require("../stakeholder/faculty");
const app = express();

const mysql = require('mysql2');
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of importing mysql module you can import the function in the config folder of main branch

const dotenv = require('dotenv');
dotenv.config();

const db = mysql.createConnection({
host: process.env.DB_HOST,
user: process.env.DB_USER,
password: process.env.DB_PASS,
database: process.env.DB_NAME
});

db.connect((err) => {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the function in the config folder will connect to the database remove these lines of code to connect the db

if (err) throw err;
console.log('MySQL connected...');
});

// Importing and setting up the badgesRouter to handle all badge-related routes
// This allows endpoints like '/badges/create' or '/badges/view' to be managed
// within the badgesRouter module, keeping route organization modular and clean
const badgesRouter = require('./routes/badges');
app.use('/badges', badgesRouter);


const globalLimit = rateLimiter({
windowMs: 30 * 60 * 1000,
max: 100,
Expand Down