-
Notifications
You must be signed in to change notification settings - Fork 91
Main #196
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
Open
snehas-05
wants to merge
4
commits into
Research-Nexas:main
Choose a base branch
from
snehas-05:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Main #196
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| const express = require('express'); | ||
| const mysql = require('mysql2'); | ||
| const path = require('path'); | ||
| const dotenv = require('dotenv'); | ||
|
|
||
| dotenv.config(); | ||
|
|
||
| const app = express(); | ||
|
|
||
| // Set view engine to EJS | ||
| app.set('view engine', 'ejs'); | ||
| app.use(express.static(path.join(__dirname, 'public'))); | ||
| app.use(express.json()); | ||
| app.use(express.urlencoded({ extended: true })); | ||
|
|
||
| // MySQL database connection | ||
| 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) => { | ||
| if (err) throw err; | ||
| console.log('MySQL connected...'); | ||
| }); | ||
|
|
||
| // Routes | ||
| const badgesRouter = require('./routes/badges'); | ||
| app.use('/badges', badgesRouter); | ||
|
|
||
| // Home route | ||
| app.get('/', (req, res) => { | ||
| res.render('layout', { page: 'profile' }); | ||
| }); | ||
|
|
||
| const PORT = process.env.PORT || 3000; | ||
| app.listen(PORT, () => console.log(`Server running on port ${PORT}`)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| const mysql = require('mysql'); | ||
|
|
||
| // MySQL connection pool setup | ||
| const db = mysql.createConnection({ | ||
| host: 'localhost', // or your DB host if remote | ||
| user: 'root', // MySQL username | ||
| password: '', // MySQL password | ||
| database: 'research_nexas' // Database name you created | ||
| }); | ||
|
|
||
| // Connect to MySQL | ||
| db.connect((err) => { | ||
| if (err) { | ||
| console.error('Error connecting to the database:', err.stack); | ||
| return; | ||
| } | ||
| console.log('Connected to the MySQL database'); | ||
| }); | ||
|
|
||
| module.exports = db; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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!' }); | ||
| }); | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| { | ||
| "name": "research-nexas", | ||
| "version": "1.0.0", | ||
| "description": "Profile Badges and Achievements system for Research-Nexas", | ||
| "main": "app.js", | ||
| "scripts": { | ||
| "start": "node app.js", | ||
| "dev": "nodemon app.js" | ||
| }, | ||
| "dependencies": { | ||
| "express": "^4.17.1", | ||
| "mysql": "^2.18.1", | ||
| "dotenv": "^16.0.0", | ||
| "body-parser": "^1.19.0" | ||
| }, | ||
| "devDependencies": { | ||
| "nodemon": "^2.0.20", | ||
| "eslint": "^8.0.0", | ||
| "jest": "^29.0.0", | ||
| "prettier": "^2.5.0" | ||
| }, | ||
| "author": "Your Name", | ||
| "license": "MIT" | ||
| } | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| 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); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="UTF-8"> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
| <title>Research-Nexas | Profile Badges</title> | ||
| <link rel="stylesheet" href="styles.css"> <!-- Link to your CSS file --> | ||
| <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css"> <!-- Font Awesome for icons --> | ||
| <script defer src="script.js"></script> <!-- Link to your JavaScript file --> | ||
| </head> | ||
| <body> | ||
| <header> | ||
| <nav> | ||
| <div class="logo"> | ||
| <h1>Research-Nexas</h1> | ||
| </div> | ||
| <ul> | ||
| <li><a href="/">Home</a></li> | ||
| <li><a href="/badges">Badges</a></li> | ||
| <li><a href="/about">About</a></li> | ||
| <li><a href="/contact">Contact</a></li> | ||
| </ul> | ||
| </nav> | ||
| </header> | ||
|
|
||
| <main> | ||
| <h2>Profile Badges and Achievements</h2> | ||
| <div class="content"> | ||
| <%- body %> <!-- This is where the content of the specific page will be rendered --> | ||
| </div> | ||
| </main> | ||
|
|
||
| <footer> | ||
| <p>© 2024 Research-Nexas. All rights reserved.</p> | ||
| </footer> | ||
| </body> | ||
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| <!-- views/profile.ejs --> | ||
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="UTF-8"> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
| <link rel="stylesheet" href="styles.css"> | ||
| <title>User Profile</title> | ||
| </head> | ||
| <body> | ||
| <header> | ||
| <h1>User Profile</h1> | ||
| </header> | ||
|
|
||
| <section> | ||
| <h2>Your Achievements</h2> | ||
| <div id="badge-container"></div> | ||
| </section> | ||
|
|
||
| <script src="scripts.js"></script> | ||
| </body> | ||
| </html> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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