Commit 8126aaa1 authored by james's avatar james

[ADD] init

parent dcdf1ab6
module.exports = {
HOST: "192.168.1.10",
PORT: 3306,
USER: "jameschiu",
PASSWORD: "Essen99",
DB: "testDB",
dialect: "mysql",
pool: {
max: 5,
min: 0,
acquire: 30000,
idle: 10000
}
};
const db = require("../models");
const UserModel = db.users;
const Op = db.Sequelize.Op;
// Create and Save a new User
exports.create = (req, res) => {
// Validate request
if (!req.body.email) {
res.status(400).send({
message: "Content can not be empty!"
});
return;
}
// Create a User
const user = {
name: req.body.name,
ename: req.body.ename,
email: req.body.email
};
// Save Tutorial in the database
UserModel.create(user)
.then(data => {
res.send(data);
})
.catch(err => {
res.status(500).send({
message:
err.message || "Some error occurred while creating the User."
});
});
};
// Retrieve all User from the database.
exports.findAll = (req, res) => {
const email = req.query.email;
var condition = email ? { email: { [Op.like]: `%${email}%` } } : null;
UserModel.findAll({ where: condition })
.then(data => {
res.send(data);
})
.catch(err => {
res.status(500).send({
message:
err.message || "Some error occurred while retrieving User."
});
});
};
// Find a single User with an id
exports.findOne = (req, res) => {
const id = req.params.id;
UserModel.findByPk(id)
.then(data => {
if (data) {
res.send(data);
} else {
res.status(404).send({
message: `Cannot find User with id=${id}.`
});
}
})
.catch(err => {
res.status(500).send({
message: "Error retrieving User with id=" + id
});
});
};
// Update a User by the id in the request
exports.update = (req, res) => {
const id = req.params.id;
UserModel.update(req.body, {
where: { id: id }
})
.then(num => {
if (num == 1) {
res.send({
message: "User was updated successfully."
});
} else {
res.send({
message: `Cannot update User with id=${id}. Maybe User was not found or req.body is empty!`
});
}
})
.catch(err => {
res.status(500).send({
message: "Error updating User with id=" + id
});
});
};
// Delete a User with the specified id in the request
exports.delete = (req, res) => {
const id = req.params.id;
UserModel.destroy({
where: { id: id }
})
.then(num => {
if (num == 1) {
res.send({
message: "User was deleted successfully!"
});
} else {
res.send({
message: `Cannot delete User with id=${id}. Maybe User was not found!`
});
}
})
.catch(err => {
res.status(500).send({
message: "Could not delete User with id=" + id
});
});
};
// Delete all User from the database.
exports.deleteAll = (req, res) => {
UserModel.destroy({
where: {},
truncate: false
})
.then(nums => {
res.send({ message: `${nums} User were deleted successfully!` });
})
.catch(err => {
res.status(500).send({
message:
err.message || "Some error occurred while removing all users."
});
});
};
const dbConfig = require("../config/db.config.js");
const Sequelize = require("sequelize");
const sequelize = new Sequelize(dbConfig.DB, dbConfig.USER, dbConfig.PASSWORD, {
logging: false,
host: dbConfig.HOST,
dialect: dbConfig.dialect,
port: dbConfig.PORT,
define: {
charset: 'utf8',
collate: 'utf8_general_ci',
timestamps:true,
},
timezone: '+08:00', // -->Add this line. for writing to database
});
const db = {};
db.Sequelize = Sequelize;
db.sequelize = sequelize;
db.users = require("./user.model.js")(sequelize, Sequelize);
module.exports = db;
module.exports = (sequelize, DataTypes) => {
const User = sequelize.define("user", {
id: {
type: DataTypes.INTEGER,
autoIncrement: true,
allowNull: false,
primaryKey: true
},
name: DataTypes.STRING,
ename: DataTypes.STRING,
email: {
type: DataTypes.STRING,
allowNull: false,
unique: true,
},
});
return User;
};
module.exports = app => {
const userController = require("../controllers/user.controller.js");
var router = require("express").Router();
// Create a new User
router.post("/", userController.create);
// Retrieve all Users
router.get("/", userController.findAll);
// Retrieve a single User with id
router.get("/:id", userController.findOne);
// Update a User with id
router.put("/:id", userController.update);
// Delete a User with id
router.delete("/:id", userController.delete);
// Delete all Users
router.delete("/", userController.deleteAll);
app.use('/api/user', router);
};
const express = require('express') const express = require("express");
const app = express() // const bodyParser = require("body-parser"); /* deprecated */
const port = 3000
app.get('/', (req, res) => { const app = express();
res.send('Hello World! (get')
})
app.post('/', (req, res) => { // parse requests of content-type - application/json
res.send('Hello World! (post') app.use(express.json()); /* bodyParser.json() is deprecated */
})
app.put('/', (req, res) => { // parse requests of content-type - application/x-www-form-urlencoded
res.send('Hello World! (put') app.use(express.urlencoded({ extended: true })); /* bodyParser.urlencoded() is deprecated */
})
app.delete('/', (req, res) => { const db = require("./app/models");
res.send('Hello World! (delete')
})
app.listen(port, () => { db.sequelize.sync();
console.log(`Example app listening on port ${port}`) // // drop the table if it already exists
}) // db.sequelize.sync({ force: true }).then(() => {
\ No newline at end of file // console.log("Drop and re-sync db.");
// });
// simple route
app.get("/", (req, res) => {
res.json({ message: "Welcome to bezkoder application." });
});
require("./app/routes/turorial.routes")(app);
// set port, listen for requests
const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}.`);
});
\ No newline at end of file
This diff is collapsed.
...@@ -14,6 +14,10 @@ ...@@ -14,6 +14,10 @@
"author": "", "author": "",
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"express": "^4.18.1" "express": "^4.18.1",
"body-parser": "^1.19.0",
"cors": "^2.8.5",
"mysql2": "^2.0.2",
"sequelize": "^5.21.2"
} }
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment