Skip to content
Open
Show file tree
Hide file tree
Changes from 6 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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.env
node_modules
103 changes: 103 additions & 0 deletions Controller/TodoController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
const { Todo } = require('../models/');
const mailHelper = require('../helper/mailHelper.js');

class TodoController {
static viewAll(req, res, next)
{
Todo.findAll({order: [['id', 'ASC']]})
.then(data => {
return res.status(200).json(data);
})
.catch(err => {
return next(err)
})
}

static async post(req, res, next)
{
try
{
const obj = {
"title": req.body.title,
"description": req.body.description,
"status": req.body.status,
"due_date": new Date(req.body.due_date),
"UsersId": +req.body.UsersId
}
const data = await Todo.create(obj);
const email = await mailHelper(req.userLogin.email, data, 'Create');
return res.status(201).json({data, email: "email has been sent"});
}
catch (err)
{
next(err);
}
}

static viewOne(req, res, next)
{
Todo.findByPk(+req.params.id)
.then(data => {
if (data)
return res.status(200).json(data);
return next({ errorCode: "NOT_FOUND", message: `Todo list with id ${+req.params.id} not found`});
})
.catch(err => {
return next(err);
});
}

static async update(req, res, next)
{
try
{
const dateNow = new Date();
const obj = {
"title": req.body.title,
"description": req.body.description,
"status": req.body.status,
"due_date": new Date(req.body.due_date),
"UsersId": +req.body.UsersId
};

if (dateNow > obj.due_date)
return next();

const data = await Todo.update(obj, {where: {id: +req.params.id}, returning: true})

if (data)
{
const email = await mailHelper(req.userLogin.email, data[1][0], 'Update');
return res.status(200).json({data: data[1][0], email: "email has been sent"});
}

return next({ errorCode: "NOT_FOUND", message: `Todo list with id ${+req.params.id} not found`});
}
catch (err)
{
return next(err);
}

}

static destroy(req, res, next)
{
let tempData;

Todo.findByPk(+req.params.id)
.then(data => {
if (!data)
return next({ errorCode: "NOT_FOUND", message: `Todo list with id ${+req.params.id} not found`});
tempData = data;
return Todo.destroy({where: {id: +req.params.id}})
})
.then(data => {
return res.status(200).json(tempData);
})
.catch(err => {
return next(err);
})
}
}

module.exports = TodoController;
46 changes: 46 additions & 0 deletions Controller/UserController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const { User, Todo } = require('../models');
const bcrypt = require('bcryptjs');
const jwt = require('jsonwebtoken');

class UserController {
static register(req, res, next)
{
const { email, password } = req.body;

User.create({ email, password })
.then(data => {
return res.status(201).json(data);
})
.catch(err => {
return next(err)
})
}

static login(req, res, next)
{
const { email, password } = req.body;

User.findOne({where: {email: email}})
.then(data => {
if (!data)
return next({ errorCode: "NOT_FOUND", message: `email ${email} is not registered`});

const check = bcrypt.compareSync(password, data.password);
if (check)
{
const token = jwt.sign({ id: data.id, email: data.email }, process.env.JWT_SECRET)
req.userLogin = token;
return res.status(200).json({accesstoken: token})
}
else
{
return next({ errorCode: "INVALID_ACCOUNT", message: `email or password wrong`});
}
})
.catch(err => {
return next(err)
})
}
}

module.exports = UserController;
Loading