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

update express-validator

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