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
2fdd1578
Commit
2fdd1578
authored
Dec 24, 2020
by
Vadim
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
конец занятия №82
parent
9996b974
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
80 additions
and
4 deletions
+80
-4
categories.js
app/categories.js
+26
-0
Category.js
app/models/Category.js
+16
-0
Products.js
app/models/Products.js
+10
-1
products.js
app/products.js
+5
-2
package-lock.json
package-lock.json
+19
-0
package.json
package.json
+1
-0
server.js
server.js
+3
-1
No files found.
app/categories.js
0 → 100644
View file @
2fdd1578
const
express
=
require
(
'express'
);
const
Category
=
require
(
'./models/Category'
);
const
router
=
express
.
Router
();
const
createRouter
=
()
=>
{
router
.
get
(
'/'
,
async
(
req
,
res
)
=>
{
try
{
const
categories
=
await
Category
.
find
();
res
.
send
(
categories
);
}
catch
(
e
)
{
res
.
sendStatus
(
500
);
}
});
router
.
post
(
'/'
,
async
(
req
,
res
)
=>
{
const
category
=
new
Category
(
req
.
body
);
try
{
await
category
.
save
();
res
.
send
(
category
)
}
catch
(
e
)
{
res
.
status
(
400
).
send
(
e
);
}
});
return
router
};
module
.
exports
=
createRouter
;
\ No newline at end of file
app/models/Category.js
0 → 100644
View file @
2fdd1578
const
mongoose
=
require
(
'mongoose'
);
const
Schema
=
mongoose
.
Schema
;
const
CategorySchema
=
new
Schema
({
title
:
{
type
:
String
,
required
:
true
,
unique
:
true
},
description
:
String
});
const
Category
=
mongoose
.
model
(
'Category'
,
CategorySchema
);
module
.
exports
=
Category
;
\ No newline at end of file
app/models/Products.js
View file @
2fdd1578
const
mongoose
=
require
(
'mongoose'
);
const
idvalidator
=
require
(
'mongoose-id-validator'
);
const
Schema
=
mongoose
.
Schema
;
...
...
@@ -12,11 +13,19 @@ const ProductSchema = new Schema({
required
:
true
},
description
:
String
,
image
:
String
image
:
String
,
category
:
{
type
:
Schema
.
Types
.
ObjectId
,
ref
:
'Category'
,
required
:
true
}
},
{
versionKey
:
false
});
ProductSchema
.
plugin
(
idvalidator
,
{
message
:
'Bad ID value for {PATH}'
});
const
Product
=
mongoose
.
model
(
'Product'
,
ProductSchema
);
module
.
exports
=
Product
;
app/products.js
View file @
2fdd1578
...
...
@@ -18,8 +18,12 @@ const Product = require('./models/Products');
const
createRouter
=
()
=>
{
router
.
get
(
'/'
,
async
(
req
,
res
)
=>
{
let
filter
=
{};
if
(
req
.
query
.
category
){
filter
.
category
=
req
.
query
.
category
;
}
try
{
const
products
=
await
Product
.
find
();
const
products
=
await
Product
.
find
(
filter
).
populate
(
'category'
,
'_id title'
);
res
.
send
(
products
);
}
catch
(
e
)
{
res
.
status
(
500
).
send
(
e
);
...
...
@@ -42,7 +46,6 @@ const createRouter = () => {
if
(
req
.
file
)
{
product
.
image
=
req
.
file
.
filename
;
}
try
{
await
product
.
save
();
res
.
send
(
product
);
...
...
package-lock.json
View file @
2fdd1578
...
...
@@ -357,6 +357,11 @@
"integrity"
:
"sha512-y4coMcylgSCdVinjiDBuR8PCC2bLjyGTwEmPb9NHR/QaNU6EUOXcTY/s6VjGMD6ENSEaeQYHCY0GNGS5jfMwPw=="
,
"dev"
:
true
},
"clone"
:
{
"version"
:
"1.0.4"
,
"resolved"
:
"https://registry.npmjs.org/clone/-/clone-1.0.4.tgz"
,
"integrity"
:
"sha1-2jCcwmPfFZlMaIypAheco8fNfH4="
},
"clone-response"
:
{
"version"
:
"1.0.2"
,
"resolved"
:
"https://registry.npmjs.org/clone-response/-/clone-response-1.0.2.tgz"
,
...
...
@@ -1073,6 +1078,15 @@
}
}
},
"mongoose-id-validator"
:
{
"version"
:
"0.6.0"
,
"resolved"
:
"https://registry.npmjs.org/mongoose-id-validator/-/mongoose-id-validator-0.6.0.tgz"
,
"integrity"
:
"sha512-y3b3/PkmaiMKSbKB8tsEEGUjgCgKQGpD2Ood7jaVEob3V2HWgnmNKCgiSQUpEtQuDU0lUnLJQ5JE9PH1Bytziw=="
,
"requires"
:
{
"clone"
:
"^1.0.2"
,
"traverse"
:
"^0.6.6"
}
},
"mongoose-legacy-pluralize"
:
{
"version"
:
"1.0.2"
,
"resolved"
:
"https://registry.npmjs.org/mongoose-legacy-pluralize/-/mongoose-legacy-pluralize-1.0.2.tgz"
,
...
...
@@ -1670,6 +1684,11 @@
"nopt"
:
"~1.0.10"
}
},
"traverse"
:
{
"version"
:
"0.6.6"
,
"resolved"
:
"https://registry.npmjs.org/traverse/-/traverse-0.6.6.tgz"
,
"integrity"
:
"sha1-y99WD9e5r2MlAv7UD5GMFX6pcTc="
},
"type-fest"
:
{
"version"
:
"0.8.1"
,
"resolved"
:
"https://registry.npmjs.org/type-fest/-/type-fest-0.8.1.tgz"
,
...
...
package.json
View file @
2fdd1578
...
...
@@ -14,6 +14,7 @@
"
express
"
:
"^4.17.1"
,
"
mongodb
"
:
"^3.6.3"
,
"
mongoose
"
:
"^5.11.8"
,
"
mongoose-id-validator
"
:
"^0.6.0"
,
"
multer
"
:
"^1.4.2"
,
"
mysql
"
:
"^2.18.1"
,
"
nanoid
"
:
"^3.1.18"
...
...
server.js
View file @
2fdd1578
const
express
=
require
(
'express'
);
const
products
=
require
(
'./app/products'
);
const
cors
=
require
(
'cors'
);
const
config
=
require
(
'./app/config'
);
const
mongoose
=
require
(
'mongoose'
);
const
products
=
require
(
'./app/products'
);
const
categories
=
require
(
'./app/categories'
);
const
app
=
express
();
const
port
=
8000
;
...
...
@@ -21,6 +22,7 @@ const run = async () => {
await
mongoose
.
connect
(
"mongodb://localhost/shop"
,
{
useNewUrlParser
:
true
});
app
.
use
(
'/products'
,
products
());
app
.
use
(
'/categories'
,
categories
());
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