Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
chinguyen_nodejs_managestudents
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
1
Merge Requests
1
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
chinguyen
chinguyen_nodejs_managestudents
Commits
4a35042b
Commit
4a35042b
authored
Feb 26, 2021
by
Trai Nguyen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update express-validator
parent
f45a28fa
Changes
7
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
63 additions
and
66 deletions
+63
-66
package.json
package.json
+1
-0
appusers.ts
src/dao/appusers.ts
+11
-17
appusers.post.ts
src/endpoints/appusers/appusers.post.ts
+37
-30
appusers.ts
src/routes/appusers.ts
+2
-4
config.json
src/sqlz/config/config.json
+4
-4
appuser.ts
src/sqlz/models/appuser.ts
+7
-10
language.ts
src/sqlz/models/language.ts
+1
-1
No files found.
package.json
View file @
4a35042b
...
...
@@ -37,6 +37,7 @@
"
cross-env
"
:
"
7.0.0
"
,
"
express
"
:
"
4.17.1
"
,
"
express-boom
"
:
"
3.0.0
"
,
"
express-validator
"
:
"
^6.9.2
"
,
"
morgan
"
:
"
1.9.1
"
,
"
pg
"
:
"
^8.5.1
"
,
"
pg-hstore
"
:
"
^2.3.3
"
,
...
...
src/dao/appusers.ts
View file @
4a35042b
import
*
as
uuid
from
'uuid'
import
{
AppUser
}
from
'./../sqlz/models/appuser'
import
{
AppUser
s
}
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
export
function
createUserDao
(
appUsers
:
any
):
Promise
<
any
>
{
return
AppUsers
.
create
({
id
:
uuid
.
v1
(),
email
:
appUser
.
email
,
pwd
:
appUser
.
pwd
,
languageId
:
language
.
get
(
'id'
)
})
email
:
appUsers
.
email
,
pwd
:
appUsers
.
pwd
})
}
export
function
findAll
():
Promise
<
any
>
{
return
AppUser
return
AppUser
s
.
findAll
({
include
:
[{
all
:
true
}]
})
}
export
function
login
(
appUser
:
any
):
Promise
<
any
>
{
return
AppUser
return
AppUser
s
.
findOne
({
where
:
{
email
:
appUser
.
email
,
pwd
:
appUser
.
pwd
},
include
:
[
Language
]
//
include: [Language]
})
}
src/endpoints/appusers/appusers.post.ts
View file @
4a35042b
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
()
const
createUserValidationRules
=
[
body
(
'pwd'
,
'Password is required'
).
notEmpty
(),
body
(
'email'
,
'Email is required'
).
notEmpty
(),
body
(
'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
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
login
(
req
:
Request
,
res
:
Response
)
{
export
function
createUser
(
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
()
return
AppUserDao
.
createUserDao
(
req
.
body
).
then
(
user
=>
res
.
json
(
user
))
}
req
.
getValidationResult
()
.
then
(
function
(
result
)
{
if
(
result
.
isEmpty
())
{
return
AppUserDao
.
login
(
req
.
body
)
}
else
{
res
.
boom
.
badRequest
(
'Validation errors'
,
result
.
mapped
())
export
function
login
(
req
:
Request
,
res
:
Response
)
{
let
errors
=
validationResult
(
req
);
if
(
!
errors
.
isEmpty
())
{
return
res
.
status
(
400
).
json
({
errors
:
errors
.
array
()
});
}
})
.
then
(
appuser
=>
res
.
status
(
200
).
send
(
appuser
))
.
catch
(
error
=>
res
.
boom
.
badRequest
(
error
))
return
AppUserDao
.
login
(
req
.
body
).
then
(
user
=>
res
.
json
(
user
))
}
src/routes/appusers.ts
View file @
4a35042b
...
...
@@ -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
)
}
src/sqlz/config/config.json
View file @
4a35042b
{
"development"
:
{
"username"
:
"postgres"
,
"password"
:
"
chinguyen@123
"
,
"database"
:
"
node_sequeliz
e"
,
"host"
:
"
127.0.0.1
"
,
"port"
:
543
2
,
"password"
:
"
meu@sds12@!#gh
"
,
"database"
:
"
chinguyen_nodejs-practic
e"
,
"host"
:
"
27.74.255.96
"
,
"port"
:
543
0
,
"dialect"
:
"postgres"
}
}
\ No newline at end of file
src/sqlz/models/appuser.ts
View file @
4a35042b
...
...
@@ -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
AppUser
s
extends
Model
{
}
export
class
AppUserModel
{
export
class
AppUser
s
Model
{
id
:
string
name
:
string
email
:
string
pwd
:
string
createdAt
:
Date
updatedAt
:
Date
}
AppUser
.
init
(
AppUser
s
.
init
(
{
email
:
STRING
(
50
)
,
pwd
:
STRING
(
50
)
email
:
STRING
,
pwd
:
STRING
},
{
sequelize
,
modelName
:
'AppUser
'
}
{
sequelize
,
modelName
:
'AppUser
s'
},
)
AppUser
.
belongsTo
(
Language
,
{
foreignKey
:
'languageId'
})
src/sqlz/models/language.ts
View file @
4a35042b
import
{
Model
,
STRING
,
UUID
}
from
'sequelize'
import
sequelize
from
'./_index'
import
{
AppUser
}
from
'./appuser'
import
{
AppUser
s
}
from
'./appuser'
export
class
Language
extends
Model
{
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment