Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
P
planner-team-one
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
21
Issues
21
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
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
Евгений Положенцев
planner-team-one
Commits
8de9704a
Commit
8de9704a
authored
Oct 31, 2022
by
“Yevgeniy
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
#6
added packages
parent
4071d664
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
6705 additions
and
1538 deletions
+6705
-1538
package-lock.json
planner-api/package-lock.json
+6575
-1537
package.json
planner-api/package.json
+10
-0
User.ts
planner-api/src/models/User.ts
+59
-0
users.ts
planner-api/src/routers/users.ts
+33
-0
server.ts
planner-api/src/server.ts
+28
-1
No files found.
planner-api/package-lock.json
View file @
8de9704a
This diff is collapsed.
Click to expand it.
planner-api/package.json
View file @
8de9704a
...
@@ -20,5 +20,15 @@
...
@@ -20,5 +20,15 @@
"nodemon"
:
"^2.0.20"
,
"nodemon"
:
"^2.0.20"
,
"ts-node"
:
"^10.9.1"
,
"ts-node"
:
"^10.9.1"
,
"typescript"
:
"^4.8.4"
"typescript"
:
"^4.8.4"
},
"dependencies"
:
{
"@types/bcrypt"
:
"^5.0.0"
,
"@types/cors"
:
"^2.8.12"
,
"@types/express"
:
"^4.17.14"
,
"bcrypt"
:
"^5.1.0"
,
"cors"
:
"^2.8.5"
,
"express"
:
"^4.18.2"
,
"mongoose"
:
"^6.7.0"
,
"nanoid"
:
"^3.3.4"
}
}
}
}
planner-api/src/models/User.ts
0 → 100644
View file @
8de9704a
import
{
Schema
,
model
}
from
'mongoose'
;
import
bcrypt
from
'bcrypt'
;
import
{
nanoid
}
from
'nanoid'
;
const
SALT_WORK_FACTOR
=
10
;
// 1. Create an interface representing a document in MongoDB.
interface
IUser
{
name
:
string
;
surname
:
string
;
email
:
string
;
displayName
:
string
;
password
:
string
;
role
:
string
;
}
// 2. Create a Schema corresponding to the document interface.
const
UserSchema
=
new
Schema
<
IUser
>
({
name
:
{
type
:
String
,
required
:
true
},
surname
:
{
type
:
String
,
required
:
true
},
email
:
{
type
:
String
,
required
:
true
},
displayName
:
{
type
:
String
,
required
:
true
},
password
:
{
type
:
String
,
required
:
true
},
role
:{
type
:
String
,
default
:
'user'
,
enum
:[
'user'
,
'admin'
]}
});
// How does next's type defined?
UserSchema
.
pre
(
'save'
,
async
function
(
next
:
any
):
Promise
<
void
>
{
if
(
!
this
.
isModified
(
'password'
))
return
next
();
console
.
log
(
'next'
,
next
)
const
salt
=
await
bcrypt
.
genSalt
(
SALT_WORK_FACTOR
);
const
hash
=
await
bcrypt
.
hash
(
this
.
password
,
salt
);
this
.
password
=
hash
;
next
();
})
UserSchema
.
set
(
'toJSON'
,{
transform
:(
doc
:
any
,
ret
:
any
,
options
:
any
)
=>
{
delete
ret
.
password
;
return
ret
;
}
})
UserSchema
.
methods
.
checkPassword
=
function
(
password
:
string
):
Promise
<
boolean
>
{
return
bcrypt
.
compare
(
password
,
this
.
password
);
}
UserSchema
.
methods
.
generateToken
=
function
(){
this
.
token
=
nanoid
();
}
// 3. Create a Model.
const
User
=
model
<
IUser
>
(
'User'
,
UserSchema
);
export
default
User
;
planner-api/src/routers/users.ts
0 → 100644
View file @
8de9704a
import
express
,{
Router
,
Request
,
Response
}
from
'express'
;
import
User
from
'../models/User'
;
const
router
:
Router
=
express
.
Router
();
router
.
post
(
'/'
,
async
(
req
:
Request
,
res
:
Response
)
=>
{
try
{
const
{
name
,
surname
,
password
,
email
}
=
req
.
body
;
const
displayName
=
surname
+
' '
+
name
[
0
]
+
'.'
const
user
=
new
User
({
name
:
name
,
surname
:
surname
,
email
:
email
,
password
:
password
,
displayName
:
displayName
})
await
user
.
save
();
return
res
.
send
({
user
})
}
catch
(
e
){
if
(
e
instanceof
Error
){
return
res
.
status
(
404
).
send
({
'message'
:
e
.
message
});
}
return
res
.
status
(
500
).
send
({
'message'
:
'Broke server'
});
}
})
router
.
get
(
'/'
,
async
(
req
:
Request
,
res
:
Response
):
Promise
<
Response
<
any
,
Record
<
string
,
any
>>>
=>
{
const
users
=
await
User
.
find
()
return
res
.
send
({
users
})
})
export
default
router
;
planner-api/src/server.ts
View file @
8de9704a
console
.
log
(
'Hello Michail'
)
import
express
,
{
Express
}
from
'express'
;
\ No newline at end of file
import
cors
from
'cors'
;
import
mongoose
from
'mongoose'
;
import
users
from
'./routers/users'
;
console
.
log
(
'Hello world!'
);
const
app
:
Express
=
express
();
app
.
use
(
express
.
static
(
'public'
));
app
.
use
(
cors
())
app
.
use
(
express
.
json
());
const
PORT
=
8000
;
app
.
use
(
'/users'
,
users
)
const
run
=
async
()
=>
{
mongoose
.
connect
(
`mongodb://localhost:27017/task-planner`
);
app
.
listen
(
PORT
,
()
=>
{
console
.
log
(
`Server started at http://localhost:
${
PORT
}
/`
);
})
process
.
on
(
"exit"
,
()
=>
{
mongoose
.
disconnect
();
})
}
run
().
catch
(
console
.
error
);
\ No newline at end of file
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