diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ccb2c80 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +node_modules/ +package-lock.json \ No newline at end of file diff --git a/app.js b/app.js new file mode 100644 index 0000000..62dff0d --- /dev/null +++ b/app.js @@ -0,0 +1,41 @@ +var createError = require('http-errors'); +var express = require('express'); +var path = require('path'); +var cookieParser = require('cookie-parser'); +var logger = require('morgan'); + +var indexRouter = require('./routes/index'); +var usersRouter = require('./routes/users'); + +var app = express(); + +// view engine setup +app.set('views', path.join(__dirname, 'views')); +app.set('view engine', 'ejs'); + +app.use(logger('dev')); +app.use(express.json()); +app.use(express.urlencoded({ extended: false })); +app.use(cookieParser()); +app.use(express.static(path.join(__dirname, 'public'))); + +app.use('/', indexRouter); +app.use('/users', usersRouter); + +// catch 404 and forward to error handler +app.use(function(req, res, next) { + next(createError(404)); +}); + +// error handler +app.use(function(err, req, res, next) { + // set locals, only providing error in development + res.locals.message = err.message; + res.locals.error = req.app.get('env') === 'development' ? err : {}; + + // render the error page + res.status(err.status || 500); + res.render('error'); +}); + +module.exports = app; diff --git a/bin/www b/bin/www new file mode 100644 index 0000000..1250e7b --- /dev/null +++ b/bin/www @@ -0,0 +1,90 @@ +#!/usr/bin/env node + +/** + * Module dependencies. + */ + +var app = require('../app'); +var debug = require('debug')('word-template:server'); +var http = require('http'); + +/** + * Get port from environment and store in Express. + */ + +var port = normalizePort(process.env.PORT || '3000'); +app.set('port', port); + +/** + * Create HTTP server. + */ + +var server = http.createServer(app); + +/** + * Listen on provided port, on all network interfaces. + */ + +server.listen(port); +server.on('error', onError); +server.on('listening', onListening); + +/** + * Normalize a port into a number, string, or false. + */ + +function normalizePort(val) { + var port = parseInt(val, 10); + + if (isNaN(port)) { + // named pipe + return val; + } + + if (port >= 0) { + // port number + return port; + } + + return false; +} + +/** + * Event listener for HTTP server "error" event. + */ + +function onError(error) { + if (error.syscall !== 'listen') { + throw error; + } + + var bind = typeof port === 'string' + ? 'Pipe ' + port + : 'Port ' + port; + + // handle specific listen errors with friendly messages + switch (error.code) { + case 'EACCES': + console.error(bind + ' requires elevated privileges'); + process.exit(1); + break; + case 'EADDRINUSE': + console.error(bind + ' is already in use'); + process.exit(1); + break; + default: + throw error; + } +} + +/** + * Event listener for HTTP server "listening" event. + */ + +function onListening() { + var addr = server.address(); + var bind = typeof addr === 'string' + ? 'pipe ' + addr + : 'port ' + addr.port; + debug('Listening on ' + bind); +} diff --git a/config/config.json b/config/config.json new file mode 100644 index 0000000..7cb7058 --- /dev/null +++ b/config/config.json @@ -0,0 +1,23 @@ +{ + "development": { + "username": "postgres", + "password": "shoaib", + "database": "word_db", + "host": "127.0.0.1", + "dialect": "postgres" + }, + "test": { + "username": "root", + "password": null, + "database": "database_test", + "host": "127.0.0.1", + "dialect": "postgres" + }, + "production": { + "username": "root", + "password": null, + "database": "database_production", + "host": "127.0.0.1", + "dialect": "postgres" + } +} diff --git a/migrations/20210314175509-create-user.js b/migrations/20210314175509-create-user.js new file mode 100644 index 0000000..45538f3 --- /dev/null +++ b/migrations/20210314175509-create-user.js @@ -0,0 +1,66 @@ +'use strict'; +module.exports = { + up: async (queryInterface, Sequelize) => { + await queryInterface.createTable('Users', { + id: { + allowNull: false, + autoIncrement: true, + primaryKey: true, + type: Sequelize.INTEGER + }, + firstName: { + type: Sequelize.STRING + }, + lastName: { + type: Sequelize.STRING + }, + fatherName: { + type: Sequelize.STRING + }, + codeMelli: { + type: Sequelize.INTEGER + }, + workUnit: { + type: Sequelize.STRING + }, + workUnitCode: { + type: Sequelize.INTEGER + }, + address: { + type: Sequelize.TEXT + }, + obligationNumber: { + type: Sequelize.INTEGER + }, + obligationDate: { + type: Sequelize.DATE + }, + requestNumber: { + type: Sequelize.INTEGER + }, + requestDate: { + type: Sequelize.DATE + }, + writtenOrderNumber: { + type: Sequelize.INTEGER + }, + writtenOrderDate: { + type: Sequelize.DATE + }, + deadline: { + type: Sequelize.INTEGER + }, + createdAt: { + allowNull: false, + type: Sequelize.DATE + }, + updatedAt: { + allowNull: false, + type: Sequelize.DATE + } + }); + }, + down: async (queryInterface, Sequelize) => { + await queryInterface.dropTable('Users'); + } +}; \ No newline at end of file diff --git a/models/index.js b/models/index.js new file mode 100644 index 0000000..33f09e7 --- /dev/null +++ b/models/index.js @@ -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; diff --git a/models/user.js b/models/user.js new file mode 100644 index 0000000..07dd9d6 --- /dev/null +++ b/models/user.js @@ -0,0 +1,36 @@ +'use strict'; +const { + Model +} = require('sequelize'); +module.exports = (sequelize, DataTypes) => { + class User 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 + } + }; + User.init({ + firstName: DataTypes.STRING, + lastName: DataTypes.STRING, + fatherName: DataTypes.STRING, + codeMelli: DataTypes.INTEGER, + workUnit: DataTypes.STRING, + workUnitCode: DataTypes.INTEGER, + address: DataTypes.TEXT, + obligationNumber: DataTypes.INTEGER, + obligationDate: DataTypes.DATE, + requestNumber: DataTypes.INTEGER, + requestDate: DataTypes.DATE, + writtenOrderNumber: DataTypes.INTEGER, + writtenOrderDate: DataTypes.DATE, + deadline: DataTypes.INTEGER + }, { + sequelize, + modelName: 'User', + }); + return User; +}; \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..3084c8f --- /dev/null +++ b/package.json @@ -0,0 +1,18 @@ +{ + "name": "word-template", + "version": "0.0.0", + "private": true, + "scripts": { + "start": "node ./bin/www" + }, + "dependencies": { + "cookie-parser": "~1.4.4", + "debug": "~2.6.9", + "ejs": "~2.6.1", + "express": "~4.16.1", + "http-errors": "~1.6.3", + "morgan": "~1.9.1", + "pg": "^8.5.1", + "sequelize": "^6.5.0" + } +} diff --git a/public/stylesheets/style.css b/public/stylesheets/style.css new file mode 100644 index 0000000..9453385 --- /dev/null +++ b/public/stylesheets/style.css @@ -0,0 +1,8 @@ +body { + padding: 50px; + font: 14px "Lucida Grande", Helvetica, Arial, sans-serif; +} + +a { + color: #00B7FF; +} diff --git a/routes/index.js b/routes/index.js new file mode 100644 index 0000000..ecca96a --- /dev/null +++ b/routes/index.js @@ -0,0 +1,9 @@ +var express = require('express'); +var router = express.Router(); + +/* GET home page. */ +router.get('/', function(req, res, next) { + res.render('index', { title: 'Express' }); +}); + +module.exports = router; diff --git a/routes/users.js b/routes/users.js new file mode 100644 index 0000000..69b757f --- /dev/null +++ b/routes/users.js @@ -0,0 +1,50 @@ +var express = require('express'); +var router = express.Router(); +const userServeice = require('../services/user') + +/* GET users listing. */ +router.post('/add', function(req, res, next) { + console.log('hereeeeeeeeee'); + const userInfo ={ + firstName : req.body.firstName, + lastName : req.body.lastName, + fatherName : req.body.fatherName, + codeMelli : req.body.codeMelli, + workUnit : req.body.workUnit, + workUnitCode : req.body.workUnitCode, + address : req.body.address, + obligationNumber : req.body.obligationNumber, + obligationDate : req.body.obligationDate, + requestNumber : req.body.requestNumber, + requestDate : req.body.requestDate, + writtenOrderNumber : req.body.writtenOrderNumber, + writtenOrderDate : req.body.writtenOrderDate, + deadline : req.body.deadline + } + userServeice.addUser(userInfo) + .then((result) => { + res.status(200).send({ + success: true, + user: result + }) + }) + .catch(err => { + if (err.eText) { + if (typeof err.eText !== 'string') { + err.eText = err.eText.toString() + } + res.status(err.eCode).send({ + success: false, + error: err.eText + }) + } else { + res.status(500).send({ + success: false, + error: err.toString() + }) + } + + }) +}); + +module.exports = router; diff --git a/services/user.js b/services/user.js new file mode 100644 index 0000000..a1ffb1f --- /dev/null +++ b/services/user.js @@ -0,0 +1,22 @@ +let models = require('../models') + +let methods = {} + +methods.addUser = (userInfo) => { + return new Promise((resolve, reject) => { + models.User.create(userInfo).then((user) => { + console.log(user); + resolve(user) + + }).catch(err => { + reject(err) + }) + + }) +} + + + + + +module.exports = methods \ No newline at end of file diff --git a/views/error.ejs b/views/error.ejs new file mode 100644 index 0000000..7cf94ed --- /dev/null +++ b/views/error.ejs @@ -0,0 +1,3 @@ +
<%= error.stack %>diff --git a/views/index.ejs b/views/index.ejs new file mode 100644 index 0000000..7b7a1d6 --- /dev/null +++ b/views/index.ejs @@ -0,0 +1,11 @@ + + + +
Welcome to <%= title %>
+ +