Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
P
products_lesson
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
Pavel Mishakov
products_lesson
Commits
eb23ea9e
Commit
eb23ea9e
authored
Apr 04, 2023
by
Pavel Mishakov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
93 done BACK
parent
c1f6e9ad
Hide whitespace changes
Inline
Side-by-side
Showing
11 changed files
with
96 additions
and
5 deletions
+96
-5
package.json
package.json
+1
-0
.gitignore
public/uploads/.gitignore
+3
-1
1.jpg
public/uploads/1.jpg
+0
-0
2.jpg
public/uploads/2.jpg
+0
-0
products.ts
src/controllers/products.ts
+2
-1
fixtures.ts
src/fixtures.ts
+45
-0
IUser.ts
src/interfaces/IUser.ts
+1
-0
IUserGetDto.ts
src/interfaces/IUserGetDto.ts
+1
-0
permission.ts
src/middlewares/permission.ts
+34
-0
User.ts
src/models/mongo/User.ts
+5
-0
mongo.ts
src/repository/mongo.ts
+4
-3
No files found.
package.json
View file @
eb23ea9e
...
...
@@ -5,6 +5,7 @@
"main"
:
"index.js"
,
"scripts"
:
{
"dev"
:
"ts-node-dev --respawn --trace-warnings --transpile-only src/index.ts"
,
"seed"
:
"ts-node-dev --trace-warnings --transpile-only src/fixtures.ts"
,
"test"
:
"echo
\"
Error: no test specified
\"
&& exit 1"
},
"author"
:
""
,
...
...
public/uploads/.gitignore
View file @
eb23ea9e
*
!.gitignore
\ No newline at end of file
!.gitignore
!1.jpg
!2.jpg
\ No newline at end of file
public/uploads/1.jpg
0 → 100644
View file @
eb23ea9e
72.8 KB
public/uploads/2.jpg
0 → 100644
View file @
eb23ea9e
61.9 KB
src/controllers/products.ts
View file @
eb23ea9e
...
...
@@ -3,6 +3,7 @@ import multer from 'multer'
import
{
config
}
from
'../index.config'
import
{
auth
}
from
'../middlewares/auth'
import
{
productService
,
ProductService
}
from
'../services/products'
import
{
permission
}
from
'../middlewares/permission'
const
storage
=
multer
.
diskStorage
({
destination
(
req
,
file
,
callback
)
{
...
...
@@ -23,7 +24,7 @@ export class ProductsController {
constructor
()
{
this
.
router
=
express
.
Router
()
this
.
router
.
get
(
'/'
,
auth
,
this
.
getProducts
)
this
.
router
.
get
(
'/'
,
permission
(
'USER'
)
,
this
.
getProducts
)
this
.
router
.
get
(
'/:id'
,
auth
,
this
.
getProductById
)
this
.
router
.
post
(
'/'
,
[
auth
,
upload
.
single
(
'image'
)],
this
.
addProduct
)
this
.
router
.
delete
(
'/:id'
,
auth
,
this
.
deleteProductById
)
...
...
src/fixtures.ts
0 → 100644
View file @
eb23ea9e
import
mongoose
from
'mongoose'
import
{
User
}
from
'./models/mongo/User'
import
{
Product
}
from
'./models/mongo/Product'
import
dotenv
from
'dotenv'
dotenv
.
config
()
mongoose
.
connect
(
process
.
env
.
MONGO_CLIENT_URL
||
''
)
const
db
=
mongoose
.
connection
db
.
once
(
'open'
,
async
()
=>
{
try
{
await
db
.
dropCollection
(
'users'
)
await
db
.
dropCollection
(
'products'
)
}
catch
(
err
)
{
console
.
log
(
err
)
}
await
User
.
create
({
username
:
'pashamishakov@gmail.com'
,
password
:
'123'
},
{
username
:
'test@gmail.com'
,
password
:
'123'
})
await
Product
.
create
({
product
:
'Apple'
,
description
:
'This is an apple'
,
price
:
100
,
image
:
'1.jpg'
},
{
product
:
'Orange'
,
description
:
'This is an orange'
,
price
:
200
,
image
:
'2.jpg'
},
{
product
:
'Dragon fruit'
,
description
:
'This is a dragon fruit'
,
price
:
300
,
image
:
''
},)
db
.
close
()
})
\ No newline at end of file
src/interfaces/IUser.ts
View file @
eb23ea9e
...
...
@@ -5,5 +5,6 @@ export default interface IUser extends Document {
username
:
string
password
:
string
active
:
boolean
role
?:
string
checkPassword
:
(
pass
:
string
)
=>
boolean
}
\ No newline at end of file
src/interfaces/IUserGetDto.ts
View file @
eb23ea9e
...
...
@@ -4,4 +4,5 @@ export default interface IUserGetDto {
_id
:
string
username
:
IUSer
[
'username'
]
token
:
string
role
:
string
}
\ No newline at end of file
src/middlewares/permission.ts
0 → 100644
View file @
eb23ea9e
import
{
NextFunction
,
Request
,
Response
}
from
"express"
;
import
{
EStatuses
}
from
"../enums/EStatuses"
;
import
IResponse
from
"../interfaces/IResponse"
;
import
jwt
from
'jsonwebtoken'
import
IRequestWithTokenData
from
"../interfaces/IRequestWithTokenData"
;
export
const
permission
=
(
role
:
string
)
=>
{
return
(
expressReq
:
Request
,
res
:
Response
,
next
:
NextFunction
)
=>
{
const
req
=
expressReq
as
IRequestWithTokenData
if
(
req
.
method
===
'OPTIONS'
)
{
next
()
}
try
{
const
data
=
jwt
.
verify
(
req
.
get
(
'Authorization'
)
||
''
,
process
.
env
.
SECRET_KEY
||
''
)
if
(
data
&&
typeof
data
==
'object'
&&
data
.
role
===
role
)
{
req
.
dataFromToken
=
data
next
()
}
else
{
const
response
:
IResponse
<
undefined
>
=
{
status
:
EStatuses
.
NOT_OK
,
result
:
undefined
,
message
:
'Not authorized'
}
res
.
status
(
200
).
send
(
response
)
}
}
catch
{
const
response
:
IResponse
<
undefined
>
=
{
status
:
EStatuses
.
NOT_OK
,
result
:
undefined
,
message
:
'Not authorized'
}
res
.
status
(
200
).
send
(
response
)
}
}
}
\ No newline at end of file
src/models/mongo/User.ts
View file @
eb23ea9e
...
...
@@ -11,6 +11,11 @@ const UserSchema: Schema = new Schema<IUser>({
password
:
{
type
:
String
,
required
:
true
},
role
:
{
type
:
String
,
required
:
true
,
default
:
'USER'
}
})
...
...
src/repository/mongo.ts
View file @
eb23ea9e
...
...
@@ -261,7 +261,8 @@ export class Mongo implements IDataBase {
const
data
=
{
_id
:
user
.
_id
,
username
:
user
.
username
,
token
:
generateJWT
({
_id
:
user
.
_id
,
username
:
user
.
username
},
'2h'
)
token
:
generateJWT
({
_id
:
user
.
_id
,
username
:
user
.
username
,
role
:
user
.
role
||
''
},
'2h'
),
role
:
user
.
role
||
''
}
return
{
status
:
EStatuses
.
OK
,
...
...
@@ -288,11 +289,11 @@ export class Mongo implements IDataBase {
if
(
!
isMatch
)
{
throw
new
Error
(
'Wrong password'
)
}
console
.
log
(
'IS MATHC!!!!!!!! '
,
isMatch
)
const
data
=
{
_id
:
user
.
_id
,
username
:
user
.
username
,
token
:
generateJWT
({
_id
:
user
.
_id
,
username
:
user
.
username
},
'2h'
)
token
:
generateJWT
({
_id
:
user
.
_id
,
username
:
user
.
username
,
role
:
user
.
role
||
''
},
'2h'
),
role
:
user
.
role
||
''
}
await
user
.
save
()
return
{
...
...
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