-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #260 from visharma14/main
Task Management api
- Loading branch information
Showing
13 changed files
with
228 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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,3 @@ | ||
node_modules | ||
.env | ||
package-lock.json |
This file contains 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,14 @@ | ||
const express = require('express'); | ||
const bodyParser = require('body-parser'); | ||
const authRoutes = require('./routes/authRoutes'); | ||
const taskRoutes = require('./routes/taskRoutes'); | ||
const app = express(); | ||
|
||
app.use(bodyParser.json()); | ||
app.use('/api/auth', authRoutes); | ||
app.use('/api/tasks', taskRoutes); | ||
|
||
const PORT = process.env.PORT || 3000; | ||
app.listen(PORT, () => { | ||
console.log(`Server is running on port ${PORT}`); | ||
}); |
This file contains 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,18 @@ | ||
const mysql = require('mysql2'); | ||
const dotenv = require('dotenv'); | ||
|
||
dotenv.config(); | ||
|
||
const db = mysql.createConnection({ | ||
host: process.env.DB_HOST, | ||
user: process.env.DB_USER, | ||
password: process.env.DB_PASSWORD, | ||
database: process.env.DB_DATABASE, | ||
}); | ||
|
||
db.connect(err => { | ||
if (err) throw err; | ||
console.log('Database connected!'); | ||
}); | ||
|
||
module.exports = db; |
This file contains 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,29 @@ | ||
const bcrypt = require('bcrypt'); | ||
const jwt = require('jsonwebtoken'); | ||
const User = require('../models/user'); | ||
|
||
const register = (req, res) => { | ||
const { username, password } = req.body; | ||
const hashedPassword = bcrypt.hashSync(password, 10); | ||
|
||
User.create(username, hashedPassword, (err, result) => { | ||
if (err) return res.status(500).json(err); | ||
res.status(201).json({ message: 'User created successfully!',result }); | ||
}); | ||
}; | ||
|
||
const login = (req, res) => { | ||
const { username, password } = req.body; | ||
|
||
User.findByUsername(username, (err, user) => { | ||
if (err || !user) return res.status(404).json({ message: 'User not found!' }); | ||
|
||
const validPassword = bcrypt.compareSync(password, user.password); | ||
if (!validPassword) return res.status(401).json({ message: 'Invalid password!' }); | ||
|
||
const token = jwt.sign({ id: user.id }, process.env.JWT_SECRET); | ||
res.json({ token }); | ||
}); | ||
}; | ||
|
||
module.exports = { register, login }; |
This file contains 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,52 @@ | ||
const Task = require('../models/task'); | ||
|
||
const createTask = (req, res) => { | ||
const { title, description, userId } = req.body; | ||
|
||
Task.create(title, description, userId, (err, result) => { | ||
if (err) return res.status(500).json(err); | ||
res.status(201).json({ message: 'Task created successfully!' }); | ||
}); | ||
}; | ||
|
||
const getTasks = (req, res) => { | ||
const userId = req.user.id; // Assume user ID is added to req by auth middleware | ||
|
||
Task.findAll(userId, (err, tasks) => { | ||
if (err) return res.status(500).json(err); | ||
res.json(tasks); | ||
}); | ||
}; | ||
|
||
// Add updateTask and deleteTask similarly | ||
|
||
const updateTask = (req, res) => { | ||
const taskId = req.params.id; // Get the task ID from the request parameters | ||
const { title, description } = req.body; // Extract title and description from the request body | ||
|
||
// Validate if the taskId and required fields are provided | ||
if (!taskId) { | ||
return res.status(400).json({ message: "Task ID is required." }); | ||
} | ||
|
||
Task.update(taskId, title, description, (err, result) => { | ||
if (err) return res.status(500).json(err); | ||
|
||
if (result.affectedRows === 0) { | ||
return res.status(404).json({ message: "Task not found." }); | ||
} | ||
|
||
res.json({ message: "Task updated successfully!" }); | ||
}); | ||
}; | ||
|
||
const deleteTasks = (req,res)=>{ | ||
const taskId = req.user.id; | ||
|
||
Task.delete(taskId, (err, tasks) => { | ||
if(err) return res.status(500).json(err); | ||
res.json(tasks); | ||
}); | ||
} | ||
|
||
module.exports = { createTask, getTasks,updateTasks,deleteTasks }; |
This file contains 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,15 @@ | ||
const jwt = require('jsonwebtoken'); | ||
|
||
const authenticate = (req, res, next) => { | ||
const token = req.headers['authorization']; | ||
|
||
if (!token) return res.status(403).json({ message: 'No token provided!' }); | ||
|
||
jwt.verify(token, process.env.JWT_SECRET, (err, decoded) => { | ||
if (err) return res.status(401).json({ message: 'Unauthorized!' }); | ||
req.user = decoded; | ||
next(); | ||
}); | ||
}; | ||
|
||
module.exports = { authenticate }; |
This file contains 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 @@ | ||
const db = require('../config/db'); | ||
|
||
const Task = { | ||
create: (title, description, userId, callback) => { | ||
const sql = 'INSERT INTO tasks (title, description, user_id) VALUES (?, ?, ?)'; | ||
db.query(sql, [title, description, userId], callback); | ||
}, | ||
findAll: (userId, callback) => { | ||
const sql = 'SELECT * FROM tasks WHERE user_id = ?'; | ||
db.query(sql, [userId], callback); | ||
}, | ||
update: (id, title, description, callback) => { | ||
const sql = 'UPDATE tasks SET title = ?, description = ? WHERE id = ?'; | ||
db.query(sql, [title, description, id], callback); | ||
}, | ||
delete: (id, callback) => { | ||
const sql = 'DELETE FROM tasks WHERE id = ?'; | ||
db.query(sql, [id], callback); | ||
}, | ||
}; | ||
|
||
module.exports = Task; |
This file contains 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,14 @@ | ||
const db = require('../config/db'); | ||
|
||
const User = { | ||
create: (username, password, callback) => { | ||
const sql = 'INSERT INTO users (username, password) VALUES (?, ?)'; | ||
db.query(sql, [username, password], callback); | ||
}, | ||
findByUsername: (username, callback) => { | ||
const sql = 'SELECT * FROM users WHERE username = ?'; | ||
db.query(sql, [username], callback); | ||
}, | ||
}; | ||
|
||
module.exports = User; |
This file contains 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,23 @@ | ||
{ | ||
"name": "task-management-api", | ||
"version": "1.0.0", | ||
"main": "app.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1", | ||
"start": "node app.js", | ||
"dev": "nodemon app.js" | ||
|
||
}, | ||
"keywords": [], | ||
"author": "", | ||
"license": "ISC", | ||
"description": "", | ||
"dependencies": { | ||
"bcrypt": "^5.1.1", | ||
"body-parser": "^1.20.3", | ||
"dotenv": "^16.4.5", | ||
"express": "^4.21.1", | ||
"jsonwebtoken": "^9.0.2", | ||
"mysql2": "^3.11.3" | ||
} | ||
} |
This file contains 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,8 @@ | ||
const express = require('express'); | ||
const { register, login } = require('../controllers/authcontroller'); | ||
const router = express.Router(); | ||
|
||
router.post('/register', register); | ||
router.post('/login', login); | ||
|
||
module.exports = router; |
This file contains 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,11 @@ | ||
const express = require('express'); | ||
const { createTask, getTasks, updateTasks, deleteTasks } = require('../controllers/taskController'); | ||
const { authenticate } = require('../middlewares/authMiddleware'); | ||
const router = express.Router(); | ||
|
||
router.post('/', authenticate, createTask); | ||
router.get('/', authenticate, getTasks); | ||
router.post('/update', authenticate, updateTasks); | ||
router.post('/delete',authenticate,deleteTasks); | ||
|
||
module.exports = router; |
This file contains 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,14 @@ | ||
const db = require('../config/db'); | ||
|
||
const User = { | ||
create: (username, password, callback) => { | ||
const sql = 'INSERT INTO users (username, password) VALUES (?, ?)'; | ||
db.query(sql, [username, password], callback); | ||
}, | ||
findByUsername: (username, callback) => { | ||
const sql = 'SELECT * FROM users WHERE username = ?'; | ||
db.query(sql, [username], callback); | ||
}, | ||
}; | ||
|
||
module.exports = User; |