Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
E
exam_12_Tsoy_Danil
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
Цой Данил
exam_12_Tsoy_Danil
Commits
ffb02e6b
Commit
ffb02e6b
authored
Apr 15, 2023
by
Цой Данил
💬
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added users contollers + photos controllers + connected them to index and wrote all routes
parent
8ac0a8fb
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
134 additions
and
0 deletions
+134
-0
photosController.ts
backend/src/controllers/photosController.ts
+85
-0
usersController.ts
backend/src/controllers/usersController.ts
+45
-0
index.ts
backend/src/index.ts
+4
-0
No files found.
backend/src/controllers/photosController.ts
0 → 100644
View file @
ffb02e6b
import
multer
from
"multer"
;
import
shortid
from
"shortid"
;
import
path
from
'path'
import
{
config
}
from
"../index.config"
;
import
express
,
{
Request
,
Response
,
Router
}
from
"express"
;
import
{
PhotosService
,
photosService
}
from
"../services/photosService"
;
import
IModifiedRequest
from
"../interfaces/IModifiedRequest"
;
import
IPhotoDto
from
"../interfaces/IPhotoDto"
;
import
{
EStatuses
}
from
"../enum/EStatuses"
;
import
{
auth
}
from
"../middlewares/auth"
;
const
storage
=
multer
.
diskStorage
({
destination
(
req
,
file
,
callback
){
callback
(
null
,
config
.
filePath
)
},
filename
(
req
,
file
,
callback
){
callback
(
null
,
shortid
.
generate
()
+
path
.
extname
(
file
.
originalname
))
},
})
const
upload
=
multer
({
storage
})
export
class
PhotosController
{
private
router
:
Router
private
service
:
PhotosService
constructor
(){
this
.
router
=
express
.
Router
()
this
.
service
=
photosService
this
.
router
.
get
(
'/'
,
this
.
getAllPhotos
)
this
.
router
.
get
(
'/:id'
,
this
.
getPhotosByUserId
)
this
.
router
.
post
(
'/'
,
[
auth
,
upload
.
single
(
'photo'
)],
this
.
addPhoto
)
this
.
router
.
delete
(
'/:id'
,
auth
,
this
.
deletePhotoById
)
}
public
getRouter
()
{
return
this
.
router
;
}
private
getAllPhotos
=
async
(
req
:
Request
,
res
:
Response
):
Promise
<
void
>
=>
{
const
response
=
await
this
.
service
.
getAllPhotos
()
if
(
response
.
status
===
EStatuses
.
FAILURE
){
res
.
status
(
418
).
send
(
response
)
}
else
{
res
.
status
(
200
).
send
(
response
)
}
}
private
addPhoto
=
async
(
req
:
Request
,
res
:
Response
):
Promise
<
void
>
=>
{
const
modifiedReq
=
req
as
IModifiedRequest
if
(
typeof
modifiedReq
.
verifiedData
===
'object'
){
const
photoDto
:
IPhotoDto
=
{
user
:
modifiedReq
.
verifiedData
.
_id
,
title
:
req
.
body
.
title
,
photo
:
req
.
file
?
req
.
file
.
filename
:
''
,
}
const
response
=
await
this
.
service
.
addPhoto
(
photoDto
)
if
(
response
.
status
===
EStatuses
.
FAILURE
){
res
.
status
(
418
).
send
(
response
)
}
else
{
res
.
status
(
200
).
send
(
response
)
}
}
else
{
res
.
status
(
418
).
send
(
'error in request. Some invalid field appeared'
)
}
}
private
getPhotosByUserId
=
async
(
req
:
Request
,
res
:
Response
):
Promise
<
void
>
=>
{
const
response
=
await
this
.
service
.
getPhotosByUserId
(
req
.
params
.
id
)
response
.
status
===
EStatuses
.
FAILURE
?
res
.
status
(
418
).
send
(
response
)
:
res
.
status
(
200
).
send
(
response
)
}
private
deletePhotoById
=
async
(
req
:
Request
,
res
:
Response
):
Promise
<
void
>
=>
{
const
modifiedReq
=
req
as
IModifiedRequest
if
(
typeof
modifiedReq
.
verifiedData
===
'object'
){
const
response
=
await
this
.
service
.
deletePhotoById
(
req
.
params
.
id
,
modifiedReq
.
verifiedData
.
_id
)
if
(
response
.
status
===
EStatuses
.
FAILURE
){
res
.
status
(
418
).
send
(
response
)
}
else
{
res
.
status
(
200
).
send
(
response
)
}
}
else
{
res
.
status
(
418
).
send
(
'error in request. Some invalid field appeared'
)
}
}
}
export
const
photosController
=
new
PhotosController
()
\ No newline at end of file
backend/src/controllers/usersController.ts
0 → 100644
View file @
ffb02e6b
import
express
,
{
Request
,
Response
,
Router
}
from
"express"
import
IResponse
from
"../interfaces/IResponse"
import
IUserGetDto
from
"../interfaces/IUserGetDto"
import
{
EStatuses
}
from
"../enum/EStatuses"
import
{
auth
}
from
"../middlewares/auth"
import
IModifiedRequest
from
"../interfaces/IModifiedRequest"
import
{
UsersService
,
usersService
}
from
"../services/usersService"
export
class
UsersController
{
private
service
:
UsersService
private
router
:
Router
constructor
(){
this
.
service
=
usersService
this
.
router
=
express
.
Router
()
this
.
router
.
post
(
'/'
,
this
.
createUser
)
this
.
router
.
post
(
'/login'
,
this
.
loginUser
)
this
.
router
.
get
(
'/token'
,
auth
,
this
.
checkToken
)
}
public
getRouter
=
()
=>
{
return
this
.
router
}
private
createUser
=
async
(
req
:
Request
,
res
:
Response
):
Promise
<
void
>
=>
{
const
response
:
IResponse
<
IUserGetDto
|
null
>
=
await
this
.
service
.
createUser
(
req
.
body
)
res
.
status
(
200
).
send
(
response
)
}
public
loginUser
=
async
(
req
:
Request
,
res
:
Response
):
Promise
<
void
>
=>
{
const
response
:
IResponse
<
IUserGetDto
|
null
>
=
await
this
.
service
.
loginUser
(
req
.
body
)
res
.
status
(
200
).
send
(
response
)
}
public
checkToken
=
async
(
expressReq
:
Request
,
res
:
Response
):
Promise
<
void
>
=>
{
const
req
=
expressReq
as
IModifiedRequest
const
response
:
IResponse
<
IUserGetDto
|
null
>
=
{
status
:
EStatuses
.
SUCCESS
,
result
:
req
.
verifiedData
as
IUserGetDto
,
message
:
'Token is ok'
}
res
.
status
(
200
).
send
(
response
)
}
}
export
const
usersController
=
new
UsersController
()
\ No newline at end of file
backend/src/index.ts
View file @
ffb02e6b
...
@@ -3,6 +3,8 @@ import dotenv from 'dotenv';
...
@@ -3,6 +3,8 @@ import dotenv from 'dotenv';
import
cors
from
'cors'
;
import
cors
from
'cors'
;
import
{
healthCheckController
}
from
"./controllers/healthCheckController"
;
import
{
healthCheckController
}
from
"./controllers/healthCheckController"
;
import
{
mongooseDB
}
from
"./repository/mongooseDB"
;
import
{
mongooseDB
}
from
"./repository/mongooseDB"
;
import
{
usersController
}
from
"./controllers/usersController"
;
import
{
photosController
}
from
"./controllers/photosController"
;
dotenv
.
config
()
dotenv
.
config
()
...
@@ -18,6 +20,8 @@ class App {
...
@@ -18,6 +20,8 @@ class App {
public
init
=
async
():
Promise
<
void
>
=>
{
public
init
=
async
():
Promise
<
void
>
=>
{
try
{
try
{
this
.
app
.
use
(
'/health-check'
,
healthCheckController
.
getRouter
())
this
.
app
.
use
(
'/health-check'
,
healthCheckController
.
getRouter
())
this
.
app
.
use
(
'/users'
,
usersController
.
getRouter
())
this
.
app
.
use
(
'/photos'
,
photosController
.
getRouter
())
this
.
app
.
listen
(
process
.
env
.
APP_PORT
,
()
=>
{
this
.
app
.
listen
(
process
.
env
.
APP_PORT
,
()
=>
{
console
.
log
(
`Server is running on http://localhost:
${
process
.
env
.
APP_PORT
}
`
);
console
.
log
(
`Server is running on http://localhost:
${
process
.
env
.
APP_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