Commit 4a35042b authored by Trai Nguyen's avatar Trai Nguyen

update express-validator

parent f45a28fa
...@@ -37,6 +37,7 @@ ...@@ -37,6 +37,7 @@
"cross-env": "7.0.0", "cross-env": "7.0.0",
"express": "4.17.1", "express": "4.17.1",
"express-boom": "3.0.0", "express-boom": "3.0.0",
"express-validator": "^6.9.2",
"morgan": "1.9.1", "morgan": "1.9.1",
"pg": "^8.5.1", "pg": "^8.5.1",
"pg-hstore": "^2.3.3", "pg-hstore": "^2.3.3",
......
import * as uuid from 'uuid' import * as uuid from 'uuid'
import { AppUser } from './../sqlz/models/appuser' import { AppUsers } from './../sqlz/models/appuser'
import { Language } from '../sqlz/models/language' import { Language } from '../sqlz/models/language'
export function create(appUser: any): Promise<any> { export function createUserDao(appUsers: any): Promise<any> {
return AppUsers
return Language.findOne({ .create({
where: { name: 'fr' } id: uuid.v1(),
}) email: appUsers.email,
.then(language => { pwd: appUsers.pwd
return AppUser
.create({
id: uuid.v1(),
email: appUser.email,
pwd: appUser.pwd,
languageId: language.get('id')
})
}) })
} }
export function findAll(): Promise<any> { export function findAll(): Promise<any> {
return AppUser return AppUsers
.findAll({ include: [{ all: true }] }) .findAll({ include: [{ all: true }] })
} }
export function login(appUser: any): Promise<any> { export function login(appUser: any): Promise<any> {
return AppUser return AppUsers
.findOne({ .findOne({
where: { where: {
email: appUser.email, email: appUser.email,
pwd: appUser.pwd pwd: appUser.pwd
}, },
include: [Language] // include: [Language]
}) })
} }
import { Request, Response } from 'express' import { Request, Response } from 'express'
import { AppUserDao } from '../../dao/_index' import { AppUserDao } from '../../dao/_index'
import { body, validationResult } from 'express-validator'
export function create(req: Request, res: Response) {
const createUserValidationRules = [
req.checkBody('pwd', 'Password is required').notEmpty() body('pwd', 'Password is required').notEmpty(),
req.checkBody('email', 'Email is required').notEmpty() body('email', 'Email is required').notEmpty(),
req.checkBody('email', 'A valid email is required').isEmail() body('email', 'A valid email is required').isEmail()
]
req.getValidationResult()
.then(function(result) { const loginValidationRules = [
if (result.isEmpty()) { body('pwd', 'Password is required').notEmpty(),
return AppUserDao.create(req.body) body('email', 'Email is required').notEmpty(),
.then(appuser => res.status(201).send(appuser)) body('email', 'A valid email is required').isEmail()
.catch(error => res.boom.badRequest(error)) ]
} else {
res.boom.badRequest('Validation errors', result.mapped()) export function getValidationRules(method) {
} switch (method) {
}) case 'CreateUser': {
return createUserValidationRules
}
case 'Login': {
return loginValidationRules
}
}
}
export function createUser(req: Request, res: Response) {
let errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(400).json({ errors: errors.array() });
}
return AppUserDao.createUserDao(req.body).then(user => res.json(user))
} }
export function login(req: Request, res: Response) { export function login(req: Request, res: Response) {
let errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(400).json({ errors: errors.array() });
}
req.checkBody('pwd', 'Password is required').notEmpty() return AppUserDao.login(req.body).then(user => res.json(user))
req.checkBody('email', 'Email is required').notEmpty()
req.checkBody('email', 'A valid email is required').isEmail()
req.getValidationResult()
.then(function(result) {
if (result.isEmpty()) {
return AppUserDao.login(req.body)
} else {
res.boom.badRequest('Validation errors', result.mapped())
}
})
.then(appuser => res.status(200).send(appuser))
.catch(error => res.boom.badRequest(error))
} }
...@@ -2,9 +2,7 @@ import { Express } from 'express' ...@@ -2,9 +2,7 @@ import { Express } from 'express'
import { AppUserController } from '../endpoints/_index' import { AppUserController } from '../endpoints/_index'
export function routes(app: Express) { export function routes(app: Express) {
app.get('/api/appUsers', AppUserController.AppUserGet.list) app.get('/api/appUsers', AppUserController.AppUserGet.list)
app.post('/api/appUsers', AppUserController.AppUserPost.create) app.post('/api/appUsers', AppUserController.AppUserPost.getValidationRules("CreateUser"), AppUserController.AppUserPost.createUser)
app.post('/api/appUsers/login', AppUserController.AppUserPost.login) app.post('/api/appUsers/login', AppUserController.AppUserPost.getValidationRules("Login"), AppUserController.AppUserPost.login)
} }
{ {
"development": { "development": {
"username": "postgres", "username": "postgres",
"password": "chinguyen@123", "password": "meu@sds12@!#gh",
"database": "node_sequelize", "database": "chinguyen_nodejs-practice",
"host": "127.0.0.1", "host": "27.74.255.96",
"port": 5432, "port": 5430,
"dialect": "postgres" "dialect": "postgres"
} }
} }
\ No newline at end of file
...@@ -2,26 +2,23 @@ import { Model, STRING, UUID, Deferrable } from 'sequelize' ...@@ -2,26 +2,23 @@ import { Model, STRING, UUID, Deferrable } from 'sequelize'
import sequelize from './_index' import sequelize from './_index'
import { Language } from './language' import { Language } from './language'
export class AppUser extends Model { export class AppUsers extends Model {
} }
export class AppUserModel { export class AppUsersModel {
id: string id: string
name: string email: string
pwd: string pwd: string
createdAt: Date createdAt: Date
updatedAt: Date updatedAt: Date
} }
AppUser.init( AppUsers.init(
{ {
email: STRING(50), email: STRING,
pwd: STRING(50) pwd: STRING
}, },
{ sequelize, modelName: 'AppUser' } { sequelize, modelName: 'AppUsers' },
) )
AppUser.belongsTo(Language, {
foreignKey: 'languageId'
})
import { Model, STRING, UUID } from 'sequelize' import { Model, STRING, UUID } from 'sequelize'
import sequelize from './_index' import sequelize from './_index'
import { AppUser } from './appuser' import { AppUsers } from './appuser'
export class Language extends Model { export class Language extends Model {
} }
......
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