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

Feature/tasks #12

Merged
merged 5 commits into from
Dec 16, 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
18 changes: 18 additions & 0 deletions data/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[
{
"id": "_uaszbz8",
"userId": "_xjslaun",
"title": "Testing",
"description": "works",
"dueDate": "2023-12-03",
"status": "pending"
},
{
"id": "_ziyuo0z",
"userId": "_islqrzy",
"title": "MackPro 2023",
"description": "Will work had to get one some day",
"dueDate": "2023-11-24",
"status": "completed"
}
]
30 changes: 29 additions & 1 deletion data/users.json
Original file line number Diff line number Diff line change
@@ -1 +1,29 @@
[{"id":"1","username":"user1","password":"password1"},{"id":"2","username":"user2","password":"password2"},{"id":"_zwsde6n","username":"james","password":"pass"},{"id":"_xjslaun","username":"bobby","password":"pass"}]

[
{
"id": "1",
"username": "user1",
"password": "password1"
},
{
"id": "2",
"username": "user2",
"password": "password2"
},
{
"id": "_zwsde6n",
"username": "james",
"password": "pass"
},
{
"id": "_xjslaun",
"username": "bobby",
"password": "pass"
},
{
"id": "_islqrzy",
"username": "macbook",
"password": "oneday"
}
]

7 changes: 5 additions & 2 deletions helpers/middlewares.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
const usersData = require('../data/users.json');
let loggedInUser = null;

function authenticate(username, password) {
// Simulating basic authentication
const user = usersData.find(user => user.username === username && user.password === password);
const user = usersData.find(
user => user.username === username && user.password === password
);
if (user) {
loggedInUser = user;
return true;
Expand All @@ -21,4 +24,4 @@ function requireAuth(req, res, next) {
module.exports = {
authenticate,
requireAuth,
};
};
9 changes: 6 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@
const express = require('express');
const app = express();
const indexRoutes = require('./routes/index.routes');
// const taskRoutes = require('./routes/task.routes');
const taskRoutes = require('./routes/task.routes');

app.use(express.json());

app.use((req, res, next) => {
console.log(`Request: ${req.method} ${req.url}`);
next();
});
// Routes setup
app.use('/', indexRoutes);
// app.use('/tasks', taskRoutes);
app.use('/tasks', taskRoutes);

const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
Expand Down
98 changes: 59 additions & 39 deletions models/task.model.js
Original file line number Diff line number Diff line change
@@ -1,53 +1,73 @@
const fs = require('fs');
const { generateId } = require('../helpers/helper');

class Task {
constructor(userId, title, description, dueDate) {
this.id = generateId();
this.userId = userId;
this.title = title;
this.description = description;
this.dueDate = dueDate;
this.status = 'pending';
}
function getAllTasks(tasksData, userId) {
return tasksData.filter(task => task.userId === userId);
}

static getAllTasks(tasksData, userId) {
return tasksData.filter(task => task.userId === userId);
}
function getTaskById(taskId, tasksData, userId) {
return tasksData.find(task => task.id === taskId && task.userId === userId);
}

static getTaskById(taskId, tasksData, userId) {
return tasksData.find(task => task.id === taskId && task.userId === userId);
}
function createTask(userId, title, description, dueDate, tasksData) {
const newTask = {
id: generateId(),
userId,
title,
description,
dueDate,
status: 'pending',
};
tasksData.push(newTask);
saveTasksToFile(tasksData);
return newTask;
}

static createTask(newTask, tasksData) {
tasksData.push(newTask);
function updateTaskById(taskId, updatedTask, tasksData, userId) {
const index = tasksData.findIndex(task => task.id === taskId && task.userId === userId);
if (index !== -1) {
tasksData[index] = { ...tasksData[index], ...updatedTask };
saveTasksToFile(tasksData);
return tasksData[index];
}
return null;
}

static updateTaskById(taskId, updatedTask, tasksData, userId) {
const index = tasksData.findIndex(task => task.id === taskId && task.userId === userId);
if (index !== -1) {
tasksData[index] = { ...tasksData[index], ...updatedTask };
return tasksData[index];
}
return null;
function deleteTaskById(taskId, tasksData, userId) {
const index = tasksData.findIndex(task => task.id === taskId && task.userId === userId);
if (index !== -1) {
const deletedTask = tasksData.splice(index, 1);
saveTasksToFile(tasksData);
return deletedTask[0];
}
return null;
}

static deleteTaskById(taskId, tasksData, userId) {
const index = tasksData.findIndex(task => task.id === taskId && task.userId === userId);
if (index !== -1) {
const deletedTask = tasksData.splice(index, 1);
return deletedTask[0];
}
return null;
function updateTaskStatus(taskId, status, tasksData, userId) {
const task = tasksData.find(task => task.id === taskId && task.userId === userId);
if (task) {
task.status = status;
saveTasksToFile(tasksData);
return task;
}
return null;
}

static updateTaskStatus(taskId, status, tasksData, userId) {
const task = tasksData.find(task => task.id === taskId && task.userId === userId);
if (task) {
task.status = status;
return task;
function saveTasksToFile(tasksData) {
fs.writeFile('data/tasks.json', JSON.stringify(tasksData, null, 2), 'utf8', err => {
if (err) {
console.error(err);
return;
}
return null;
}
console.log('Tasks saved to file successfully!');
});
}

module.exports = Task;
module.exports = {
getAllTasks,
getTaskById,
createTask,
updateTaskById,
deleteTaskById,
updateTaskStatus,
};
39 changes: 26 additions & 13 deletions models/user.model.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,32 @@
const fs = require('fs');
const { generateId } = require('../helpers/helper');

class User {
constructor(username, password) {
this.id = generateId();
this.username = username;
this.password = password;
}
function createUser(username, password, usersData) {
const newUser = {
id: generateId(),
username,
password,
};
usersData.push(newUser);
return newUser; // Return the new user without saving to file here
}

static createUser(newUser, usersData) {
usersData.push(newUser);
}
function getUserByUsername(username, usersData) {
return usersData.find(user => user.username === username);
}

static getUserByUsername(username, usersData) {
return usersData.find(user => user.username === username);
}
function saveUsersToFile(usersData) {
fs.writeFile('data/users.json', JSON.stringify(usersData, null, 2), 'utf8', err => {
if (err) {
console.error(err);
return;
}
console.log('Users saved to file successfully!');
});
}

module.exports = User;
module.exports = {
createUser,
getUserByUsername,
saveUsersToFile,
};
19 changes: 6 additions & 13 deletions routes/index.routes.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
const express = require('express');
const fs = require('fs');
const router = express.Router();
const { authenticate } = require('../helpers/middlewares');
const User = require('../models/user.model');
const usersData = require('../data/users.json');

// Simulated login route
router.post('/login', (req, res) => {
const { username, password } = req.body;
const isAuthenticated = authenticate(username, password);
Expand All @@ -16,23 +14,18 @@ router.post('/login', (req, res) => {
}
});

// Signup route for user registration
router.post('/signup', (req, res) => {
const { username, password } = req.body;
const existingUser = User.getUserByUsername(username, usersData);
if (existingUser) {
return res.status(400).send('Username already exists');
}
const newUser = new User(username, password);
User.createUser(newUser, usersData);
const newUser = User.createUser(username, password, usersData);

// Save updated usersData back to users.json
fs.writeFile('./data/users.json', JSON.stringify(usersData), err => {
if (err) {
return res.status(500).send('Error saving user data');
}
res.send('User registered successfully');
});
// Save your usersData back to users.json
User.saveUsersToFile(usersData);

res.send('User registered successfully');
});

module.exports = router;
module.exports = router;
52 changes: 52 additions & 0 deletions routes/task.routes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
const express = require('express');
const router = express.Router();
const Task = require('../models/task.model');
const tasksData = require('../data/tasks.json');
const { requireAuth } = require('../helpers/middlewares');

router.use(requireAuth);

router.get('/', (req, res) => {
const userTasks = Task.getAllTasks(tasksData, req.user.id);
res.json(userTasks);
});

router.post('/', (req, res) => {
const { title, description, dueDate } = req.body;
const newTask = Task.createTask(req.user.id, title, description, dueDate, tasksData);
res.json(newTask);
});

router.put('/:id', (req, res) => {
const taskId = req.params.id;
const updatedTask = req.body;
const result = Task.updateTaskById(taskId, updatedTask, tasksData, req.user.id);
if (result) {
res.json(result);
} else {
res.status(404).send('Task not found');
}
});

router.delete('/:id', (req, res) => {
const taskId = req.params.id;
const deletedTask = Task.deleteTaskById(taskId, tasksData, req.user.id);
if (deletedTask) {
res.json(deletedTask);
} else {
res.status(404).send('Task not found');
}
});

router.patch('/:id/status', (req, res) => {
const taskId = req.params.id;
const { status } = req.body;
const updatedTask = Task.updateTaskStatus(taskId, status, tasksData, req.user.id);
if (updatedTask) {
res.json(updatedTask);
} else {
res.status(404).send('Task not found');
}
});

module.exports = router;