Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
A
ajs12_shop
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
Egor Kremnev
ajs12_shop
Commits
2c21f1d4
Commit
2c21f1d4
authored
Apr 20, 2023
by
Egor Kremnev
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add mongoose
parent
6667ac63
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
353 additions
and
50 deletions
+353
-50
config.js
api_client/config.js
+5
-1
fileDb.js
api_client/db/fileDb.js
+0
-27
index.js
api_client/index.js
+19
-8
Product.js
api_client/models/Product.js
+19
-0
package-lock.json
api_client/package-lock.json
+274
-0
package.json
api_client/package.json
+2
-0
products.js
api_client/routes/products.js
+32
-12
ProductItem.jsx
...client/src/components/Product/ProductItem/ProductItem.jsx
+1
-1
Products.jsx
front_client/src/containers/Products/Products.jsx
+1
-1
No files found.
api_client/config.js
View file @
2c21f1d4
...
...
@@ -4,5 +4,9 @@ const rootPath = __dirname;
module
.
exports
=
{
rootPath
,
port
:
8001
,
uploadPath
:
path
.
join
(
rootPath
,
'public'
,
'uploads'
)
uploadPath
:
path
.
join
(
rootPath
,
'public'
,
'uploads'
),
db
:
{
host
:
'mongodb://localhost'
,
database
:
'shop'
,
}
};
api_client/db/fileDb.js
deleted
100644 → 0
View file @
6667ac63
const
fs
=
require
(
'fs'
);
const
fileName
=
'./db/db.json'
;
let
data
=
[];
module
.
exports
=
{
init
()
{
try
{
const
content
=
fs
.
readFileSync
(
fileName
);
data
=
JSON
.
parse
(
content
.
toString
());
}
catch
(
e
)
{
}
},
getItems
()
{
return
data
;
},
getItem
(
id
)
{
return
data
.
find
(
item
=>
item
.
id
===
id
);
},
addItem
(
item
)
{
data
.
push
(
item
);
this
.
save
();
},
save
()
{
fs
.
writeFileSync
(
fileName
,
JSON
.
stringify
(
data
));
}
};
api_client/index.js
View file @
2c21f1d4
const
cors
=
require
(
'cors'
);
const
express
=
require
(
'express'
);
const
app
=
express
();
const
{
port
}
=
require
(
'./config'
);
const
{
port
,
db
:
dbConfig
}
=
require
(
'./config'
);
const
createProductsRoutes
=
require
(
'./routes/products'
);
const
fileDb
=
require
(
'./db/fileDb'
);
fileDb
.
init
();
const
mongoose
=
require
(
'mongoose'
);
app
.
use
(
cors
());
app
.
use
(
express
.
json
());
app
.
use
(
express
.
static
(
'public'
));
app
.
use
(
'/api/v1/products'
,
createProductsRoutes
(
fileDb
));
app
.
use
(
'/api/v1/products'
,
createProductsRoutes
());
const
run
=
async
()
=>
{
await
mongoose
.
connect
(
dbConfig
.
host
+
'/'
+
dbConfig
.
database
,
{
useNewUrlParser
:
true
}
);
app
.
listen
(
port
,
()
=>
{
console
.
log
(
"Server running at http://localhost:"
+
port
);
});
process
.
on
(
'exit'
,
()
=>
{
mongoose
.
disconnect
();
});
};
app
.
listen
(
port
,
()
=>
{
console
.
log
(
"Server running at http://localhost:"
+
port
);
});
run
().
catch
(
e
=>
console
.
error
(
e
));
api_client/models/Product.js
0 → 100644
View file @
2c21f1d4
const
mongoose
=
require
(
'mongoose'
);
const
Schema
=
mongoose
.
Schema
;
const
ProductSchema
=
new
Schema
({
title
:
{
required
:
true
,
type
:
String
},
price
:
{
required
:
true
,
type
:
Number
},
description
:
String
,
image
:
String
});
const
Product
=
mongoose
.
model
(
'Product'
,
ProductSchema
);
module
.
exports
=
Product
;
api_client/package-lock.json
View file @
2c21f1d4
This diff is collapsed.
Click to expand it.
api_client/package.json
View file @
2c21f1d4
...
...
@@ -15,6 +15,8 @@
"cors"
:
"^2.8.5"
,
"express"
:
"^4.18.2"
,
"fix-esm"
:
"^1.0.1"
,
"mongodb"
:
"^5.3.0"
,
"mongoose"
:
"^7.0.4"
,
"multer"
:
"^1.4.5-lts.1"
,
"nanoid"
:
"^4.0.2"
},
...
...
api_client/routes/products.js
View file @
2c21f1d4
...
...
@@ -4,6 +4,7 @@ const {nanoid} = require('fix-esm').require('nanoid');
const
multer
=
require
(
'multer'
);
const
path
=
require
(
'path'
);
const
{
uploadPath
}
=
require
(
'./../config'
);
const
Product
=
require
(
'../models/Product'
);
const
storage
=
multer
.
diskStorage
({
destination
:
(
req
,
file
,
cb
)
=>
{
...
...
@@ -16,25 +17,44 @@ const storage = multer.diskStorage({
const
upload
=
multer
({
storage
});
const
createRoutes
=
(
db
)
=>
{
router
.
post
(
'/'
,
upload
.
single
(
'image'
),
(
req
,
res
)
=>
{
const
item
=
{...
req
.
body
,
id
:
nanoid
()
};
const
createRoutes
=
()
=>
{
router
.
post
(
'/'
,
upload
.
single
(
'image'
),
async
(
req
,
res
)
=>
{
const
productData
=
{...
req
.
body
};
if
(
req
.
file
)
item
.
image
=
req
.
file
.
filename
;
if
(
req
.
file
)
productData
.
image
=
req
.
file
.
filename
;
else
productData
.
image
=
null
;
db
.
addItem
(
item
);
res
.
send
(
item
);
try
{
const
product
=
new
Product
(
productData
);
res
.
status
(
201
)
.
send
(
await
product
.
save
());
}
catch
(
e
)
{
res
.
status
(
400
)
.
send
(
e
);
}
});
router
.
get
(
'/'
,
(
req
,
res
)
=>
{
res
.
send
(
db
.
getItems
());
router
.
get
(
'/'
,
async
(
req
,
res
)
=>
{
try
{
res
.
send
(
await
Product
.
find
());
}
catch
(
e
)
{
res
.
sendStatus
(
500
);
}
});
router
.
get
(
'/:id'
,
(
req
,
res
)
=>
{
const
item
=
db
.
getItem
(
req
.
params
.
id
);
router
.
get
(
'/:id'
,
async
(
req
,
res
)
=>
{
try
{
const
result
=
await
Product
.
findById
(
req
.
params
.
id
);
if
(
result
)
return
res
.
send
(
result
);
if
(
item
)
res
.
send
(
item
);
res
.
sendStatus
(
404
);
res
.
sendStatus
(
404
);
}
catch
(
e
)
{
res
.
sendStatus
(
500
);
}
});
return
router
;
...
...
front_client/src/components/Product/ProductItem/ProductItem.jsx
View file @
2c21f1d4
...
...
@@ -25,7 +25,7 @@ const ProductItem = ({product}) => {
</
strong
>
</
CardContent
>
<
CardActions
>
<
IconButton
component=
{
Link
}
to=
{
PRODUCT_VIEW
.
replace
(
':id'
,
product
.
id
)
}
>
<
IconButton
component=
{
Link
}
to=
{
PRODUCT_VIEW
.
replace
(
':id'
,
product
.
_
id
)
}
>
<
ArrowForwardIosIcon
/>
</
IconButton
>
</
CardActions
>
...
...
front_client/src/containers/Products/Products.jsx
View file @
2c21f1d4
...
...
@@ -44,7 +44,7 @@ const Products = () => {
{
products
.
map
(
product
=>
(
<
ProductItem
key=
{
product
.
id
}
key=
{
product
.
_
id
}
product=
{
product
}
/>
))
...
...
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