Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
S
shop-api-js5
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
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
Vadim
shop-api-js5
Commits
b7814c0d
Commit
b7814c0d
authored
Jan 04, 2021
by
Алексей Анпилогов
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
конец 83 занятия
parent
2fdd1578
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
482 additions
and
25 deletions
+482
-25
Products.js
app/models/Products.js
+5
-0
User.js
app/models/User.js
+51
-0
products.js
app/products.js
+9
-2
users.js
app/users.js
+40
-0
package-lock.json
package-lock.json
+374
-23
package.json
package.json
+1
-0
server.js
server.js
+2
-0
No files found.
app/models/Products.js
View file @
b7814c0d
...
...
@@ -18,6 +18,11 @@ const ProductSchema = new Schema({
type
:
Schema
.
Types
.
ObjectId
,
ref
:
'Category'
,
required
:
true
},
user
:
{
type
:
Schema
.
Types
.
ObjectId
,
ref
:
'User'
,
required
:
true
}
},
{
versionKey
:
false
...
...
app/models/User.js
0 → 100644
View file @
b7814c0d
const
mongoose
=
require
(
"mongoose"
);
const
{
nanoid
}
=
require
(
"nanoid"
);
const
bcrypt
=
require
(
"bcrypt"
);
const
SALT_WORK_FACTOR
=
10
;
const
Schema
=
mongoose
.
Schema
;
const
UserSchema
=
new
Schema
({
username
:
{
type
:
String
,
unique
:
true
,
required
:
true
},
password
:
{
type
:
String
,
required
:
true
},
token
:
{
type
:
String
,
required
:
true
}
});
UserSchema
.
pre
(
"save"
,
async
function
(
next
)
{
if
(
!
this
.
isModified
(
"password"
))
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
,
ret
)
=>
{
delete
ret
.
password
;
return
ret
;
}
});
UserSchema
.
methods
.
checkPassword
=
function
(
password
)
{
return
bcrypt
.
compare
(
password
,
this
.
password
);
};
UserSchema
.
methods
.
generateToken
=
function
()
{
this
.
token
=
nanoid
();
};
const
User
=
mongoose
.
model
(
"User"
,
UserSchema
);
module
.
exports
=
User
;
\ No newline at end of file
app/products.js
View file @
b7814c0d
...
...
@@ -5,6 +5,7 @@ const router = express.Router();
const
{
nanoid
}
=
require
(
'nanoid'
);
const
config
=
require
(
'./config'
);
const
Product
=
require
(
'./models/Products'
);
const
User
=
require
(
"./models/User"
);
const
storage
=
multer
.
diskStorage
({
destination
:
(
req
,
file
,
cb
)
=>
{
...
...
@@ -28,8 +29,6 @@ const createRouter = () => {
}
catch
(
e
)
{
res
.
status
(
500
).
send
(
e
);
}
});
router
.
get
(
'/:id'
,
async
(
req
,
res
)
=>
{
...
...
@@ -42,10 +41,18 @@ const createRouter = () => {
});
router
.
post
(
'/'
,
upload
.
single
(
'image'
),
async
(
req
,
res
)
=>
{
const
token
=
req
.
get
(
"Authorization"
);
const
user
=
await
User
.
findOne
({
token
});
if
(
!
user
)
{
return
res
.
status
(
401
).
send
({
error
:
"Wrong token"
});
}
const
product
=
new
Product
(
req
.
body
);
if
(
req
.
file
)
{
product
.
image
=
req
.
file
.
filename
;
}
product
.
user
=
user
.
_id
;
try
{
await
product
.
save
();
res
.
send
(
product
);
...
...
app/users.js
0 → 100644
View file @
b7814c0d
const
express
=
require
(
"express"
);
const
User
=
require
(
"./models/User"
);
const
router
=
express
.
Router
();
const
createRouter
=
()
=>
{
router
.
get
(
"/"
,
async
(
req
,
res
)
=>
{
res
.
send
(
await
User
.
find
());
});
router
.
post
(
"/"
,
async
(
req
,
res
)
=>
{
try
{
const
user
=
new
User
(
req
.
body
);
user
.
generateToken
();
await
user
.
save
();
res
.
send
(
user
);
}
catch
(
e
)
{
res
.
status
(
400
).
send
(
e
);
}
});
router
.
post
(
"/sessions"
,
async
(
req
,
res
)
=>
{
const
error
=
"Username or password are wrong"
;
const
user
=
await
User
.
findOne
({
username
:
req
.
body
.
username
});
if
(
!
user
)
{
return
res
.
status
(
400
).
send
({
error
});
}
const
isMatch
=
await
user
.
checkPassword
(
req
.
body
.
password
);
if
(
!
isMatch
)
{
return
res
.
status
(
400
).
send
({
error
});
}
user
.
generateToken
();
await
user
.
save
();
res
.
send
(
user
);
});
return
router
;
};
module
.
exports
=
createRouter
;
\ No newline at end of file
package-lock.json
View file @
b7814c0d
This diff is collapsed.
Click to expand it.
package.json
View file @
b7814c0d
...
...
@@ -10,6 +10,7 @@
"author"
:
""
,
"license"
:
"
ISC
"
,
"dependencies"
:
{
"
bcrypt
"
:
"^5.0.0"
,
"
cors
"
:
"^2.8.5"
,
"
express
"
:
"^4.17.1"
,
"
mongodb
"
:
"^3.6.3"
,
...
...
server.js
View file @
b7814c0d
...
...
@@ -4,6 +4,7 @@ const config = require('./app/config');
const
mongoose
=
require
(
'mongoose'
);
const
products
=
require
(
'./app/products'
);
const
categories
=
require
(
'./app/categories'
);
const
users
=
require
(
'./app/users'
);
const
app
=
express
();
const
port
=
8000
;
...
...
@@ -23,6 +24,7 @@ const run = async () => {
app
.
use
(
'/products'
,
products
());
app
.
use
(
'/categories'
,
categories
());
app
.
use
(
'/users'
,
users
());
app
.
listen
(
port
,
()
=>
{
console
.
log
(
`Server started on port
${
port
}
`
)
});
...
...
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