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
da51e85b
Commit
da51e85b
authored
Apr 01, 2023
by
Zhanara
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Создан сервис и контроллер для comment
parent
e59e054a
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
236 additions
and
24 deletions
+236
-24
comments.ts
backend/src/controllers/comments.ts
+44
-0
IComment.ts
backend/src/interfaces/IComment.ts
+11
-0
ICommentDto.ts
backend/src/interfaces/ICommentDto.ts
+5
-0
IUser.ts
backend/src/interfaces/IUser.ts
+3
-0
Comments.ts
backend/src/models/Comments.ts
+29
-0
mongoose.ts
backend/src/repository/mongoose.ts
+114
-24
comments.ts
backend/src/services/comments.ts
+30
-0
No files found.
backend/src/controllers/comments.ts
0 → 100644
View file @
da51e85b
import
express
,
{
Router
,
Request
,
Response
}
from
'express'
import
{
CommentService
,
commentService
}
from
'../services/comments'
import
IUser
from
'../interfaces/IUser'
export
class
CommentController
{
private
router
:
Router
private
service
:
CommentService
constructor
()
{
this
.
router
=
express
.
Router
()
this
.
service
=
commentService
this
.
router
.
get
(
'/'
,
this
.
getComments
)
this
.
router
.
get
(
'/:id'
,
this
.
getCommentById
)
this
.
router
.
post
(
'/'
,
this
.
addComment
)
this
.
router
.
delete
(
'/:id'
,
this
.
deleteCommentById
)
}
public
getRouter
=
():
Router
=>
{
return
this
.
router
}
private
getComments
=
async
(
req
:
Request
,
res
:
Response
):
Promise
<
void
>
=>
{
const
response
=
await
this
.
service
.
getComments
()
res
.
send
(
response
)
}
private
getCommentById
=
async
(
req
:
Request
,
res
:
Response
):
Promise
<
void
>
=>
{
const
response
=
await
this
.
service
.
getCommentById
(
req
.
params
.
id
)
res
.
send
(
response
)
}
private
addComment
=
async
(
req
:
Request
,
res
:
Response
):
Promise
<
void
>
=>
{
const
response
=
await
this
.
service
.
addComment
(
req
.
body
)
res
.
send
(
response
)
}
private
deleteCommentById
=
async
(
req
:
Request
,
res
:
Response
):
Promise
<
void
>
=>
{
const
user
=
req
.
user
as
IUser
;
const
id
=
req
.
params
.
id
;
const
response
=
await
this
.
service
.
deleteCommentById
(
user
,
id
)
res
.
send
(
response
)
}
}
\ No newline at end of file
backend/src/interfaces/IComment.ts
0 → 100644
View file @
da51e85b
import
{
Document
,
ObjectId
}
from
"mongoose"
;
import
IUser
from
"./IUser"
;
import
IPost
from
"./IPost"
;
export
default
interface
IComment
extends
Document
{
_id
:
ObjectId
user
:
IUser
post
:
IPost
comment
:
string
datetime
:
Date
}
\ No newline at end of file
backend/src/interfaces/ICommentDto.ts
0 → 100644
View file @
da51e85b
import
IComment
from
"./IComment"
export
default
interface
ICommentDto
{
comment
:
IComment
[
'comment'
]
}
\ No newline at end of file
backend/src/interfaces/IUser.ts
View file @
da51e85b
import
{
ObjectId
}
from
"mongoose"
;
export
default
interface
IUser
{
export
default
interface
IUser
{
_id
:
ObjectId
;
username
:
string
;
username
:
string
;
password
:
string
;
password
:
string
;
token
:
string
;
token
:
string
;
...
...
backend/src/models/Comments.ts
0 → 100644
View file @
da51e85b
import
mongoose
,
{
Schema
}
from
"mongoose"
;
import
IComment
from
"../interfaces/IComment"
;
const
CommentSchema
:
Schema
=
new
Schema
<
IComment
>
({
comment
:
{
type
:
String
,
required
:
true
},
datetime
:
{
type
:
Date
},
user
:
[
{
type
:
Schema
.
Types
.
ObjectId
,
ref
:
'User'
}
],
post
:
[
{
type
:
Schema
.
Types
.
ObjectId
,
ref
:
'Post'
}
],
},
{
versionKey
:
false
})
export
const
Comment
=
mongoose
.
model
<
IComment
>
(
'Comment'
,
CommentSchema
)
\ No newline at end of file
backend/src/repository/mongoose.ts
View file @
da51e85b
...
@@ -5,11 +5,15 @@ import IResponse from '../interfaces/IResponse';
...
@@ -5,11 +5,15 @@ import IResponse from '../interfaces/IResponse';
import
IPostDto
from
'../interfaces/IPostDto'
;
import
IPostDto
from
'../interfaces/IPostDto'
;
import
mongoose
,
{
Mongoose
}
from
'mongoose'
import
mongoose
,
{
Mongoose
}
from
'mongoose'
import
IDataBase
from
'../interfaces/IDataBase'
;
import
IDataBase
from
'../interfaces/IDataBase'
;
import
IComment
from
'../interfaces/IComment'
;
import
ICommentDto
from
'../interfaces/ICommentDto'
;
import
{
Comment
}
from
'../models/Comments'
;
import
IUser
from
'../interfaces/IUser'
;
export
class
Mongo
implements
IDataBase
{
export
class
Mongo
implements
IDataBase
{
private
client
:
Mongoose
|
null
=
null
private
client
:
Mongoose
|
null
=
null
public
close
=
async
():
Promise
<
void
>
=>
{
public
close
=
async
():
Promise
<
void
>
=>
{
if
(
!
this
.
client
)
return
if
(
!
this
.
client
)
return
await
this
.
client
.
disconnect
()
await
this
.
client
.
disconnect
()
}
}
...
@@ -19,8 +23,8 @@ import IDataBase from '../interfaces/IDataBase';
...
@@ -19,8 +23,8 @@ import IDataBase from '../interfaces/IDataBase';
console
.
log
(
'Mongo mongoose is connected'
)
console
.
log
(
'Mongo mongoose is connected'
)
}
}
public
getPosts
=
async
():
Promise
<
IResponse
<
IPost
[]
|
undefined
>>
=>
{
public
getPosts
=
async
():
Promise
<
IResponse
<
IPost
[]
|
undefined
>>
=>
{
try
{
try
{
const
data
=
await
Post
.
find
()
const
data
=
await
Post
.
find
()
const
response
:
IResponse
<
IPost
[]
>
=
{
const
response
:
IResponse
<
IPost
[]
>
=
{
status
:
EStatuses
.
OK
,
status
:
EStatuses
.
OK
,
...
@@ -38,8 +42,8 @@ import IDataBase from '../interfaces/IDataBase';
...
@@ -38,8 +42,8 @@ import IDataBase from '../interfaces/IDataBase';
return
response
return
response
}
}
}
}
public
getPostById
=
async
(
id
:
string
):
Promise
<
IResponse
<
IPost
|
undefined
>>
=>
{
public
getPostById
=
async
(
id
:
string
):
Promise
<
IResponse
<
IPost
|
undefined
>>
=>
{
try
{
try
{
const
data
=
await
Post
.
findById
(
id
)
const
data
=
await
Post
.
findById
(
id
)
const
response
:
IResponse
<
IPost
>
=
{
const
response
:
IResponse
<
IPost
>
=
{
status
:
EStatuses
.
OK
,
status
:
EStatuses
.
OK
,
...
@@ -57,13 +61,13 @@ import IDataBase from '../interfaces/IDataBase';
...
@@ -57,13 +61,13 @@ import IDataBase from '../interfaces/IDataBase';
return
response
return
response
}
}
}
}
public
addPost
=
async
(
postDto
:
IPostDto
):
Promise
<
IResponse
<
IPost
|
undefined
>>
=>
{
public
addPost
=
async
(
postDto
:
IPostDto
):
Promise
<
IResponse
<
IPost
|
undefined
>>
=>
{
try
{
try
{
const
post
=
new
Post
(
postDto
)
const
post
=
new
Post
(
postDto
)
const
data
=
await
post
.
save
()
const
data
=
await
post
.
save
()
const
response
:
IResponse
<
IPost
>
=
{
const
response
:
IResponse
<
IPost
>
=
{
status
:
EStatuses
.
OK
,
status
:
EStatuses
.
OK
,
result
:
data
,
result
:
data
,
message
:
'Post added'
message
:
'Post added'
}
}
return
response
return
response
...
@@ -77,9 +81,9 @@ import IDataBase from '../interfaces/IDataBase';
...
@@ -77,9 +81,9 @@ import IDataBase from '../interfaces/IDataBase';
return
response
return
response
}
}
}
}
public
deletePostById
=
async
(
id
:
string
):
Promise
<
IResponse
<
IPost
|
undefined
>>
=>
{
public
deletePostById
=
async
(
id
:
string
):
Promise
<
IResponse
<
IPost
|
undefined
>>
=>
{
try
{
try
{
const
data
=
await
Post
.
findOneAndDelete
({
_id
:
id
})
const
data
=
await
Post
.
findOneAndDelete
({
_id
:
id
})
const
response
:
IResponse
<
IPost
>
=
{
const
response
:
IResponse
<
IPost
>
=
{
status
:
EStatuses
.
OK
,
status
:
EStatuses
.
OK
,
result
:
data
as
any
,
result
:
data
as
any
,
...
@@ -97,6 +101,92 @@ import IDataBase from '../interfaces/IDataBase';
...
@@ -97,6 +101,92 @@ import IDataBase from '../interfaces/IDataBase';
}
}
}
}
public
getComments
=
async
():
Promise
<
IResponse
<
IComment
[]
|
undefined
>>
=>
{
try
{
const
data
=
await
Comment
.
find
().
populate
(
'user'
).
populate
(
'post'
)
const
response
:
IResponse
<
IComment
[]
>
=
{
status
:
EStatuses
.
OK
,
result
:
data
as
any
,
message
:
'Comments 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
}
}
public
getCommentById
=
async
(
id
:
string
):
Promise
<
IResponse
<
IComment
|
undefined
>>
=>
{
try
{
const
data
=
await
Comment
.
findById
(
id
).
populate
(
'user'
).
populate
(
'post'
);
const
response
:
IResponse
<
IComment
>
=
{
status
:
EStatuses
.
OK
,
result
:
data
as
any
,
message
:
'Comment 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
}
}
public
addComment
=
async
(
commentDto
:
ICommentDto
):
Promise
<
IResponse
<
IComment
|
undefined
>>
=>
{
try
{
const
comment
=
new
Comment
(
commentDto
)
const
data
=
await
comment
.
save
()
const
response
:
IResponse
<
IComment
>
=
{
status
:
EStatuses
.
OK
,
result
:
data
,
message
:
'Comment 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
}
}
public
deleteCommentById
=
async
(
user
:
IUser
,
id
:
string
):
Promise
<
IResponse
<
IComment
|
undefined
>>
=>
{
try
{
const
comment
=
await
Comment
.
findById
(
id
);
if
(
!
comment
)
{
throw
new
Error
(
'Comment not found'
);
}
if
(
comment
.
user
.
toString
()
!==
user
.
_id
.
toString
())
{
throw
new
Error
(
'You are not authorized to delete this comment!'
);
}
const
data
=
await
Comment
.
findOneAndDelete
({
_id
:
id
})
const
response
:
IResponse
<
IComment
>
=
{
status
:
EStatuses
.
OK
,
result
:
data
as
any
,
message
:
'Comment 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
}
}
};
};
export
const
mongo
=
new
Mongo
()
export
const
mongo
=
new
Mongo
()
backend/src/services/comments.ts
0 → 100644
View file @
da51e85b
import
IComment
from
"../interfaces/IComment"
import
ICommentDto
from
"../interfaces/ICommentDto"
import
IResponse
from
"../interfaces/IResponse"
import
IUser
from
"../interfaces/IUser"
import
{
Mongo
,
mongo
}
from
"../repository/mongoose"
export
class
CommentService
{
private
repository
:
Mongo
constructor
()
{
this
.
repository
=
mongo
}
public
getComments
=
async
():
Promise
<
IResponse
<
IComment
[]
|
undefined
>>
=>
{
return
await
this
.
repository
.
getComments
()
}
public
getCommentById
=
async
(
id
:
string
):
Promise
<
IResponse
<
IComment
|
undefined
>>
=>
{
return
await
this
.
repository
.
getCommentById
(
id
)
}
public
addComment
=
async
(
commentDto
:
ICommentDto
):
Promise
<
IResponse
<
IComment
|
undefined
>>
=>
{
return
await
this
.
repository
.
addComment
(
commentDto
)
}
public
deleteCommentById
=
async
(
user
:
IUser
,
id
:
string
):
Promise
<
IResponse
<
IComment
|
undefined
>>
=>
{
return
await
this
.
repository
.
deleteCommentById
(
user
,
id
)
}
}
export
const
commentService
=
new
CommentService
()
\ 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