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

Jeplind Morico #3

Open
wants to merge 25 commits into
base: master
Choose a base branch
from
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
1 change: 0 additions & 1 deletion README.md

This file was deleted.

15 changes: 15 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const express = require('express');
const app = express();
const port = 3000;
const routes = require('./routes/index');

app.use(express.urlencoded({extended: false}))

app.use(express.json())

app.use(routes)


app.listen(port, ()=> {
console.log(`Listen to this http://localhost:${port}`);
})
23 changes: 23 additions & 0 deletions config/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"development": {
"username": "postgres",
"password": "saragih21!",
"database": "database_development_todoapps",
"host": "127.0.0.1",
"dialect": "postgres"
},
"test": {
"username": "postgres",
"password": "saragih21!",
"database": "database_test_todoapps",
"host": "127.0.0.1",
"dialect": "postgres"
},
"production": {
"username": "root",
"password": null,
"database": "database_production",
"host": "127.0.0.1",
"dialect": "mysql"
}
}
93 changes: 93 additions & 0 deletions controllers/controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
const { Todo } = require('../models')

class Controller {
static postTodo (req,res) {
const value = {
title: req.body.title,
description: req.body.description,
due_date: req.body.due_date
}

Todo
.create(value)
.then((todo) => {
res.status(201).json(todo)
}).catch((err) => {
res.status(500).json({error: err.errors[0]})
});
}

static findAll(req,res) {
Todo
.findAll()
.then((todo) => {
res.status(200).json(todo)
}).catch((err) => {
res.status(500).json(err)
});
}

static putEditTodo(req,res) {
const id = +req.params.id;
const {title, description, status, due_date} = req.body

Todo
.update({
title,
description,
status,
due_date
},
{
where: {
id
},
returning:true
})
.then((update) => {
res.status(200).json(update[1][0])
}).catch((err) => {
res.status(500).json(err)
});
}

static patchIdTodo(req,res) {
const id = +req.params.id
const { status } = req.body;

Todo
.update({
status
},
{
where: {
id
},
returning: true
})
.then((todo) => {
res.status(200).json(todo[1][0])
}).catch((err) => {
res.status(500).json(err)
});
}

static deleteById(req,res) {
const id = +req.params.id

Todo
.destroy({
where: {
id
}
})
.then((data) => {
res.status(200).json("message: todo success to delete")
}).catch((err) => {
res.status(500).json(err)
});
}
}


module.exports = Controller;
36 changes: 36 additions & 0 deletions migrations/20201026063854-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.STRING
},
due_date: {
type: Sequelize.DATE
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('Todos');
}
};
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 = require(path.join(__dirname, file))(sequelize, Sequelize.DataTypes);
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;
41 changes: 41 additions & 0 deletions models/todo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
'use strict';
const {
Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
class Todo extends Model {
/**
* Helper method for defining associations.
* This method is not a part of Sequelize lifecycle.
* The `models/index` file will call this method automatically.
*/
static associate(models) {
// define association here
}
};
Todo.init({
title: DataTypes.STRING,
description: DataTypes.STRING,
status: DataTypes.STRING,
due_date: {
type: DataTypes.DATE,
validate: {
isDate: {
args: true,
msg: "must be in date format"
},
isAfter: {
args: `${new Date()}`,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

untuk validasi tanggal tidak bisa menggunakan isAfter karena argsnya jalan ketika server dijalankan, misal server nyala di 17 agustus , di 19 agustus kita masih bisa bikin todo untuk 18 agustus (karena new date nya dibuat saat 17 agustus)

msg: "must be greater than today"
}
}
}
}, {
sequelize,
modelName: 'Todo',
});
Todo.addHook('beforeCreate', (instance) => {
instance.status = "not finished"
})
return Todo;
};
Loading