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
03dd82c3
Commit
03dd82c3
authored
Apr 03, 2023
by
Zhanara
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
реализавана валидация на запросы
parent
0701dfb0
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
43 additions
and
1 deletion
+43
-1
posts.ts
backend/src/controllers/posts.ts
+1
-1
mongoose.ts
backend/src/repository/mongoose.ts
+18
-0
post.schema.ts
backend/src/schema/post.schema.ts
+24
-0
No files found.
backend/src/controllers/posts.ts
View file @
03dd82c3
...
...
@@ -44,7 +44,7 @@ export class PostsController {
private
addPost
=
async
(
req
:
Request
,
res
:
Response
):
Promise
<
void
>
=>
{
const
post
=
req
.
body
post
.
image
=
req
.
file
?
req
.
file
.
filename
:
''
post
.
image
=
req
.
file
?
req
.
file
.
filename
:
req
.
body
.
image
const
response
=
await
this
.
service
.
addPost
(
post
)
res
.
send
(
response
)
}
...
...
backend/src/repository/mongoose.ts
View file @
03dd82c3
...
...
@@ -45,7 +45,17 @@ export class Mongo implements IDataBase {
}
public
getPostById
=
async
(
id
:
string
):
Promise
<
IResponse
<
IPost
|
undefined
>>
=>
{
try
{
if
(
!
id
||
!
id
.
trim
()
||
!
mongoose
.
Types
.
ObjectId
.
isValid
(
id
))
{
const
response
:
IResponse
<
undefined
>
=
{
status
:
EStatuses
.
NOT_OK
,
result
:
undefined
,
message
:
`Invalid post id:
${
id
}
`
};
return
response
;
}
const
data
=
await
Post
.
findById
(
id
)
const
response
:
IResponse
<
IPost
>
=
{
status
:
EStatuses
.
OK
,
result
:
data
as
any
,
...
...
@@ -84,6 +94,14 @@ export class Mongo implements IDataBase {
}
public
deletePostById
=
async
(
id
:
string
):
Promise
<
IResponse
<
IPost
|
undefined
>>
=>
{
try
{
if
(
!
id
||
!
id
.
trim
()
||
!
mongoose
.
Types
.
ObjectId
.
isValid
(
id
))
{
const
response
:
IResponse
<
undefined
>
=
{
status
:
EStatuses
.
NOT_OK
,
result
:
undefined
,
message
:
`Invalid item id:
${
id
}
`
};
return
response
;
}
const
data
=
await
Post
.
findOneAndDelete
({
_id
:
id
})
const
response
:
IResponse
<
IPost
>
=
{
status
:
EStatuses
.
OK
,
...
...
backend/src/schema/post.schema.ts
0 → 100644
View file @
03dd82c3
import
{
object
,
string
,
TypeOf
,
}
from
'zod'
;
export
const
PostSchema
=
object
({
body
:
object
({
title
:
string
({
required_error
:
'Title is required'
,
}),
description
:
string
().
optional
(),
image
:
string
().
optional
(),
}).
refine
((
data
)
=>
{
if
(
!
data
.
description
||
!
data
.
image
)
{
throw
new
Error
(
'At least one of description or image is required'
);
}
if
(
data
.
description
&&
data
.
image
)
{
return
{
description
:
data
.
description
,
image
:
data
.
image
,
};
}
return
true
;
})
})
export
type
PostInput
=
TypeOf
<
typeof
PostSchema
>
[
'body'
];
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