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
e5f6ca58
Commit
e5f6ca58
authored
Apr 04, 2023
by
Рахметова Альбина
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch '
#32
' into 'dev'
displayed comments by post See merge request
!35
parents
1dd2db46
43d26f89
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
99 additions
and
16 deletions
+99
-16
comments.ts
backend/src/controllers/comments.ts
+8
-2
mongoose.ts
backend/src/repository/mongoose.ts
+20
-0
comments.ts
backend/src/services/comments.ts
+4
-0
Post.tsx
frontend/src/components/Post/Post.tsx
+7
-1
DetailsPost.css
frontend/src/containers/DetailsPost/DetailsPost.css
+12
-0
DetailsPost.tsx
frontend/src/containers/DetailsPost/DetailsPost.tsx
+46
-5
ICommentDto.ts
frontend/src/interfaces/ICommentDto.ts
+2
-0
ICommentState.ts
frontend/src/store/comments/ICommentState.ts
+0
-7
comments.slice.ts
frontend/src/store/comments/comments.slice.ts
+0
-1
No files found.
backend/src/controllers/comments.ts
View file @
e5f6ca58
...
...
@@ -20,8 +20,14 @@ export class CommentController {
return
this
.
router
}
private
getComments
=
async
(
req
:
Request
,
res
:
Response
):
Promise
<
void
>
=>
{
const
response
=
await
this
.
service
.
getComments
()
res
.
send
(
response
)
if
(
req
.
query
.
post_id
)
{
//@ts-ignore
const
response
=
await
this
.
service
.
getCommentByPost
(
req
.
query
.
post_id
)
res
.
send
(
response
)
}
else
{
const
response
=
await
this
.
service
.
getComments
()
res
.
send
(
response
)
}
}
private
getCommentById
=
async
(
req
:
Request
,
res
:
Response
):
Promise
<
void
>
=>
{
...
...
backend/src/repository/mongoose.ts
View file @
e5f6ca58
...
...
@@ -159,6 +159,26 @@ export class Mongo implements IDataBase {
return
response
}
}
public
getCommentByPost
=
async
(
post_id
:
any
):
Promise
<
IResponse
<
IComment
[]
|
undefined
>>
=>
{
try
{
const
data
=
await
Comment
.
find
({
post
:
post_id
})
const
response
:
IResponse
<
IComment
[]
>
=
{
status
:
EStatuses
.
OK
,
message
:
'Posts comment(s) found'
,
result
:
data
as
any
}
return
response
}
catch
(
err
:
unknown
)
{
const
error
=
err
as
Error
const
response
:
IResponse
<
undefined
>
=
{
status
:
EStatuses
.
NOT_OK
,
message
:
error
.
message
,
result
:
undefined
}
return
response
}
}
public
addComment
=
async
(
commentDto
:
ICommentDto
):
Promise
<
IResponse
<
IComment
|
undefined
>>
=>
{
try
{
const
comment
=
new
Comment
(
commentDto
)
...
...
backend/src/services/comments.ts
View file @
e5f6ca58
...
...
@@ -16,6 +16,10 @@ export class CommentService {
public
getCommentById
=
async
(
id
:
string
):
Promise
<
IResponse
<
IComment
|
undefined
>>
=>
{
return
await
this
.
repository
.
getCommentById
(
id
)
}
public
getCommentByPost
=
async
(
id
:
string
):
Promise
<
IResponse
<
IComment
[]
|
undefined
>>
=>
{
return
await
this
.
repository
.
getCommentByPost
(
id
)
}
public
addComment
=
async
(
commentDto
:
ICommentDto
):
Promise
<
IResponse
<
IComment
|
undefined
>>
=>
{
return
await
this
.
repository
.
addComment
(
commentDto
)
...
...
frontend/src/components/Post/Post.tsx
View file @
e5f6ca58
...
...
@@ -7,8 +7,14 @@ interface IPostProps {
}
const
Post
:
FunctionComponent
<
IPostProps
>
=
({
post
}):
ReactElement
=>
{
const
navigate
=
useNavigate
()
const
toIdPost
=
(
id
:
string
)
=>
{
navigate
({
pathname
:
`/posts/
${
id
}
`
})
}
return
(
<
div
className=
" rounded-lg shadow-md flex flex-col justify-between mb-5"
>
<
div
onClick=
{
()
=>
toIdPost
(
post
.
_id
)
}
className=
" rounded-lg shadow-md flex flex-col justify-between mb-5"
>
<
img
className=
"object-cover w-full h-80 rounded-t-lg"
src=
{
`${import.meta.env.VITE_BASE_URL}uploads/${post.image}`
}
...
...
frontend/src/containers/DetailsPost/DetailsPost.css
View file @
e5f6ca58
.addcomment_block
{
border
:
1px
solid
black
;
padding
:
30px
20px
;
}
.addComment
{
margin-bottom
:
20px
;
}
.addComment_title
{
margin-right
:
50px
;
}
\ No newline at end of file
frontend/src/containers/DetailsPost/DetailsPost.tsx
View file @
e5f6ca58
...
...
@@ -4,36 +4,77 @@ import { useParams } from "react-router-dom";
import
{
getPostById
}
from
"../../store/posts/posts.slice"
;
import
{
useSelector
}
from
"react-redux"
;
import
{
RootState
}
from
"../../store/store"
;
import
{
getCommentsByPost
}
from
"../../store/comments/comments.slice"
;
import
{
createComment
,
getCommentsByPost
}
from
"../../store/comments/comments.slice"
;
import
Comment
from
"../../components/Comment/Comment"
;
import
Post
from
"../../components/Post/Post"
;
import
ICommentDto
from
"../../interfaces/ICommentDto"
;
import
'./DetailsPost.css'
const
Details
:
FunctionComponent
=
():
ReactElement
=>
{
const
DetailsPost
:
FunctionComponent
=
():
ReactElement
=>
{
const
params
:
any
=
useParams
();
const
dispatch
=
useDispatch
();
const
{
comments
}
=
useSelector
((
state
:
RootState
)
=>
state
.
comments
)
const
{
post
}
=
useSelector
((
state
:
RootState
)
=>
state
.
posts
)
const
{
user
}
=
useSelector
((
state
:
RootState
)
=>
state
.
user
)
const
[
comment
,
setComment
]
=
useState
<
ICommentDto
>
({
user
:
user
.
_id
,
post
:
params
.
id
,
comment
:
''
})
useEffect
(()
=>
{
//@ts-ignore
dispatch
(
getPostById
(
params
.
id
))
},
[
params
])
},
[
params
.
id
])
useEffect
(()
=>
{
//@ts-ignore
dispatch
(
getCommentsByPost
(
params
.
id
))
},
[
params
.
id
])
const
inputHandler
=
(
e
:
ChangeEvent
<
HTMLInputElement
>
):
void
=>
{
setComment
(
prevState
=>
{
return
{
...
prevState
,
[
e
.
target
.
name
]:
e
.
target
.
value
}
})
}
const
sendComment
=
()
=>
{
try
{
//@ts-ignore
dispatch
(
createComment
(
comment
))
}
finally
{
//@ts-ignore
dispatch
(
getCommentByNews
(
params
.
id
))
}
}
return
(
<
div
className=
"details"
>
<
div
className=
"container"
>
<
Post
post=
{
post
}
/>
{
comments
?
<
h1
>
Comments
</
h1
>
:
''
}
{
comments
.
map
(
c
=>
{
return
<
Comment
key=
{
c
.
_id
}
params_id=
{
params
.
id
}
comments=
{
c
}
/>
})
}
<
h2
>
Add comment
</
h2
>
<
div
className=
"addcomment_block"
>
<
div
className=
"addComment"
>
<
span
className=
"addComment_title"
>
Comment
</
span
>
<
input
value=
{
comment
.
comment
}
onChange=
{
inputHandler
}
name=
"comment"
type=
"text"
/>
</
div
>
<
button
className=
"addcomment_btn"
onClick=
{
sendComment
}
>
Send
</
button
>
</
div
>
</
div
>
</
div
>
)
}
export
default
Details
\ No newline at end of file
export
default
DetailsPost
\ No newline at end of file
frontend/src/interfaces/ICommentDto.ts
View file @
e5f6ca58
...
...
@@ -2,4 +2,6 @@ import IComment from "./IComment";
export
default
interface
ICommentDto
{
comment
:
IComment
[
'comment'
]
post
:
string
user
:
string
}
\ No newline at end of file
frontend/src/store/comments/ICommentState.ts
deleted
100644 → 0
View file @
1dd2db46
import
IComment
from
"../../interfaces/IComment"
export
default
interface
ICommentState
{
comments
:
IComment
[]
loadingComments
:
boolean
messageComments
:
string
}
\ No newline at end of file
frontend/src/store/comments/comments.slice.ts
View file @
e5f6ca58
import
{
createAsyncThunk
,
createSlice
}
from
'@reduxjs/toolkit'
import
IResponse
from
'../../interfaces/IResponse'
import
IComment
from
'../../interfaces/IComment'
import
ICommentState
from
'./ICommentState'
import
{
commentsApi
}
from
'../../api/commentApi'
import
ICommentDto
from
'../../interfaces/ICommentDto'
...
...
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