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

Nanda Achidunnafi #2

Open
wants to merge 36 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
Prev Previous commit
Next Next commit
Solved
dunnaf committed Feb 3, 2020

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit fd1b6b85eee51081878d02b3521a38f1e3803073
Binary file added .app.js.swp
Binary file not shown.
32 changes: 32 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const express = require("express");
const app = express();
const port = 3000;
const createError = require("http-errors");
const Routers = require("./routes");

app.use(express.json());
app.use(express.urlencoded({extended: true}));

app.use("/", Routers);

app.use(function(err, req, res, next) {
if(err) {
if(err.name == "SequelizeValidationError") {
res.status(400).json(err);
} else if(err.name == "NotFound") {
err.msg = "Error not found!"
res.status(404).json(err)
} else {
next();
}
}
});
app.use(function(err, req, res, next) {
if(err) {
res.status(500);
}
});

app.listen(port, function() {
console.log(`Listening on port ${port}`);
});
25 changes: 25 additions & 0 deletions config/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"development": {
"username": "thedunnaf",
"password": "edvokgrate2",
"database": "fancy-todo",
"host": "127.0.0.1",
"dialect": "postgres"
},
"test": {
"username": "root",
"password": null,
"database": "database_test",
"host": "127.0.0.1",
"dialect": "mysql",
"operatorsAliases": false
},
"production": {
"username": "root",
"password": null,
"database": "database_production",
"host": "127.0.0.1",
"dialect": "mysql",
"operatorsAliases": false
}
}
Binary file added controllers/.todo.js.swp
Binary file not shown.
75 changes: 75 additions & 0 deletions controllers/todo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
const {Todo} = require("../models");

class Controller {
static findAll(req, res, next) {
Todo.findAll()
.then(data => {
res.status(200).json(data);
})
.catch(err => {
next(err);
});
}
static create(req, res, next) {
const obj = {
title: req.body.title,
description: req.body.description,
status: req.body.status,
due_date: req.body.due_date
};
Todo.create(obj)
.then(data => {
res.status(201).json(data);
})
.catch(err => {
next(err);
});
}
static update(req, res, next) {
const id = req.params.id;
const obj = {
title: req.body.title,
description: req.body.description,
status: req.body.status,
due_date: req.body.due_date
};
const where = {
where: {
id: id
},
returning: true
};
Todo.update(obj, where)
.then(data => {
if(data[0] == 0) {
next({name:"NotFound"});
} else {
res.status(200).json(data[1]);
}
})
.catch(err => {
next(err);
});
}
static destroy(req, res, next) {
const id = req.params.id;
const where = {
where: {
id: id
}
};
Promise.all([Todo.findByPk(id), Todo.destroy(where)])
.then(data => {
if(data[1] == 0) {
next({name:"NotFound"})
} else {
res.status(200).json(data[0]);
}
})
.catch(err => {
next(err);
});
}
}

module.exports = Controller;
36 changes: 36 additions & 0 deletions migrations/20200203062649-create-todo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('Todos', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
title: {
type: Sequelize.STRING
},
description: {
type: Sequelize.STRING
},
status: {
type: Sequelize.BOOLEAN
},
due_date: {
type: Sequelize.DATE
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('Todos');
}
};
Binary file added models/.todo.js.swp
Binary file not shown.
37 changes: 37 additions & 0 deletions models/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
'use strict';

const fs = require('fs');
const path = require('path');
const Sequelize = require('sequelize');
const basename = path.basename(__filename);
const env = process.env.NODE_ENV || 'development';
const config = require(__dirname + '/../config/config.json')[env];
const db = {};

let sequelize;
if (config.use_env_variable) {
sequelize = new Sequelize(process.env[config.use_env_variable], config);
} else {
sequelize = new Sequelize(config.database, config.username, config.password, config);
}

fs
.readdirSync(__dirname)
.filter(file => {
return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js');
})
.forEach(file => {
const model = sequelize['import'](path.join(__dirname, file));
db[model.name] = model;
});

Object.keys(db).forEach(modelName => {
if (db[modelName].associate) {
db[modelName].associate(db);
}
});

db.sequelize = sequelize;
db.Sequelize = Sequelize;

module.exports = db;
68 changes: 68 additions & 0 deletions models/todo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
'use strict';
module.exports = (sequelize, DataTypes) => {
const Model = sequelize.Sequelize.Model;
class Todo extends Model {};
Todo.init({
title: {
type: DataTypes.STRING,
allowNull: false,
validate: {
notNull: {
args: true,
msg: "Please fill `Title`"
},
notEmpty: {
args: true,
msg: "Please fill `Title`"
}
}
},
description: {
type: DataTypes.STRING,
allowNull: false,
validate: {
notNull: {
args: true,
msg: "Please fill `Description`"
},
notEmpty: {
args: true,
msg: "Please fill `Description`"
}
}
},
status: DataTypes.BOOLEAN,
due_date: {
type: DataTypes.DATE,
allowNull: false,
validate: {
notNull: {
args: true,
msg: "Please fill `Due Date`"
},
notEmpty: {
args: true,
msg: "Please fill `Due Date`"
}
}
}
}, {
hooks: {
beforeCreate: (data) => {
if(data.status == null || !data.status) {
data.status = false;
}
},
beforeUpdate: () => {
if(data.status == null || !data.status) {
data.status = false;
}
}
},
sequelize
});
Todo.associate = function(models) {
// associations can be defined here
};
return Todo;
};
1 change: 1 addition & 0 deletions node_modules/.bin/mime

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions node_modules/.bin/semver

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions node_modules/.bin/uuid

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions node_modules/@types/node/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions node_modules/@types/node/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

53 changes: 53 additions & 0 deletions node_modules/@types/node/assert.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading