Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
H
hw92
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
5
Issues
5
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
Болатов Ален
hw92
Commits
fefb3368
Commit
fefb3368
authored
Apr 01, 2023
by
Zhanara
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Для поста созданы сервис, контроллер
parent
c6afdce8
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
152 additions
and
1 deletion
+152
-1
posts.ts
backend/src/controllers/posts.ts
+29
-1
IResponse.ts
backend/src/interfaces/IResponse.ts
+7
-0
mongoose.ts
backend/src/repository/mongoose.ts
+87
-0
posts.ts
backend/src/services/posts.ts
+29
-0
No files found.
backend/src/controllers/posts.ts
View file @
fefb3368
import
express
,
{
Router
,
Request
,
Response
}
from
'express'
import
multer
from
'multer'
import
{
config
}
from
'../index.config'
import
{
PostService
,
postService
}
from
'../services/posts'
const
storage
=
multer
.
diskStorage
({
destination
(
req
,
file
,
callback
)
{
...
...
@@ -17,13 +18,40 @@ const upload = multer({storage})
export
class
PostsController
{
private
router
:
Router
private
service
:
PostService
constructor
()
{
this
.
router
=
express
.
Router
()
this
.
router
.
get
(
'/'
,
this
.
getPosts
)
this
.
router
.
get
(
'/:id'
,
this
.
getPostById
)
this
.
router
.
post
(
'/'
,
upload
.
single
(
'image'
),
this
.
addPost
)
this
.
router
.
delete
(
'/:id'
,
this
.
deletePostById
)
this
.
service
=
postService
}
public
getRouter
=
():
Router
=>
{
return
this
.
router
}
private
getPosts
=
async
(
req
:
Request
,
res
:
Response
):
Promise
<
void
>
=>
{
const
response
=
await
this
.
service
.
getPosts
()
res
.
send
(
response
)
}
private
getPostById
=
async
(
req
:
Request
,
res
:
Response
):
Promise
<
void
>
=>
{
const
response
=
await
this
.
service
.
getPostById
(
req
.
params
.
id
)
res
.
send
(
response
)
}
private
addPost
=
async
(
req
:
Request
,
res
:
Response
):
Promise
<
void
>
=>
{
const
post
=
req
.
body
post
.
image
=
req
.
file
?
req
.
file
.
filename
:
''
const
response
=
await
this
.
service
.
addPost
(
post
)
res
.
send
(
response
)
}
private
deletePostById
=
async
(
req
:
Request
,
res
:
Response
):
Promise
<
void
>
=>
{
const
response
=
await
this
.
service
.
deletePostById
(
req
.
params
.
id
)
res
.
send
(
response
)
}
}
\ No newline at end of file
backend/src/interfaces/IResponse.ts
0 → 100644
View file @
fefb3368
import
{
EStatuses
}
from
"../enums/Estatuses"
export
default
interface
IResponse
<
T
>
{
result
:
T
message
:
string
status
:
EStatuses
}
\ No newline at end of file
backend/src/repository/mongoose.ts
View file @
fefb3368
import
{
connect
,
connection
}
from
'mongoose'
;
import
{
Post
}
from
'../models/Post'
;
import
{
EStatuses
}
from
'../enums/Estatuses'
;
import
IPost
from
'../interfaces/IPost'
;
import
IResponse
from
'../interfaces/IResponse'
;
import
IPostDto
from
'../interfaces/IPostDto'
;
export
const
mongoose
=
{
run
:
async
()
=>
{
try
{
return
await
connect
(
`
${
process
.
env
.
MONGO_URL
}
/hw92`
);
}
catch
(
error
)
{
console
.
log
(
error
);
}
...
...
@@ -16,4 +22,85 @@ export const mongoose = {
console
.
log
(
error
);
}
},
getPosts
:
async
():
Promise
<
IResponse
<
IPost
[]
|
undefined
>>=>
{
try
{
const
data
=
await
Post
.
find
()
const
response
:
IResponse
<
IPost
[]
>
=
{
status
:
EStatuses
.
OK
,
result
:
data
as
any
,
message
:
'Posts found'
}
return
response
}
catch
(
err
:
unknown
)
{
const
error
=
err
as
Error
const
response
:
IResponse
<
undefined
>
=
{
status
:
EStatuses
.
NOT_OK
,
result
:
undefined
,
message
:
error
.
message
}
return
response
}
},
getPostById
:
async
(
id
:
string
):
Promise
<
IResponse
<
IPost
|
undefined
>>=>
{
try
{
const
data
=
await
Post
.
findById
(
id
)
const
response
:
IResponse
<
IPost
>
=
{
status
:
EStatuses
.
OK
,
result
:
data
as
any
,
message
:
'Post found'
}
return
response
}
catch
(
err
:
unknown
)
{
const
error
=
err
as
Error
const
response
:
IResponse
<
undefined
>
=
{
status
:
EStatuses
.
NOT_OK
,
result
:
undefined
,
message
:
error
.
message
}
return
response
}
},
addPost
:
async
(
postDto
:
IPostDto
):
Promise
<
IResponse
<
IPost
|
undefined
>>=>
{
try
{
const
post
=
new
Post
(
postDto
)
const
data
=
await
post
.
save
()
const
response
:
IResponse
<
IPost
>
=
{
status
:
EStatuses
.
OK
,
result
:
data
,
message
:
'Post added'
}
return
response
}
catch
(
err
:
unknown
)
{
const
error
=
err
as
Error
const
response
:
IResponse
<
undefined
>
=
{
status
:
EStatuses
.
NOT_OK
,
result
:
undefined
,
message
:
error
.
message
}
return
response
}
},
deletePostById
:
async
(
id
:
string
):
Promise
<
IResponse
<
IPost
|
undefined
>>=>
{
try
{
const
data
=
await
Post
.
findOneAndDelete
({
_id
:
id
})
const
response
:
IResponse
<
IPost
>
=
{
status
:
EStatuses
.
OK
,
result
:
data
as
any
,
message
:
'Post deleted'
}
return
response
}
catch
(
err
:
unknown
)
{
const
error
=
err
as
Error
const
response
:
IResponse
<
undefined
>
=
{
status
:
EStatuses
.
NOT_OK
,
result
:
undefined
,
message
:
error
.
message
}
return
response
}
}
};
backend/src/services/posts.ts
View file @
fefb3368
import
IPost
from
"../interfaces/IPost"
import
IPostDto
from
"../interfaces/IPostDto"
import
IResponse
from
"../interfaces/IResponse"
import
{
mongoose
}
from
"../repository/mongoose"
export
class
PostService
{
private
repository
constructor
()
{
this
.
repository
=
mongoose
}
public
getPosts
=
async
():
Promise
<
IResponse
<
IPost
[]
|
undefined
>>
=>
{
return
await
this
.
repository
.
getPosts
()
}
public
getPostById
=
async
(
id
:
string
):
Promise
<
IResponse
<
IPost
|
undefined
>>
=>
{
return
await
this
.
repository
.
getPostById
(
id
)
}
public
addPost
=
async
(
postDto
:
IPostDto
):
Promise
<
IResponse
<
IPost
|
undefined
>>
=>
{
return
await
this
.
repository
.
addPost
(
postDto
)
}
public
deletePostById
=
async
(
id
:
string
):
Promise
<
IResponse
<
IPost
|
undefined
>>
=>
{
return
await
this
.
repository
.
deletePostById
(
id
)
}
}
export
const
postService
=
new
PostService
()
\ No newline at end of file
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