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
94041cfe
Commit
94041cfe
authored
Apr 01, 2023
by
Zhanara
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
репозитории поменяли на классовый компонент
parent
fefb3368
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
28 additions
and
27 deletions
+28
-27
IDataBase.ts
backend/src/interfaces/IDataBase.ts
+4
-0
mongoose.ts
backend/src/repository/mongoose.ts
+21
-24
posts.ts
backend/src/services/posts.ts
+3
-3
No files found.
backend/src/interfaces/IDataBase.ts
0 → 100644
View file @
94041cfe
export
default
interface
IDataBase
{
init
:
()
=>
Promise
<
void
>
close
:
()
=>
Promise
<
void
>
|
void
}
\ No newline at end of file
backend/src/repository/mongoose.ts
View file @
94041cfe
import
{
connect
,
connection
}
from
'mongoose'
;
import
{
Post
}
from
'../models/Post'
;
import
{
Post
}
from
'../models/Post'
;
import
{
EStatuses
}
from
'../enums/Estatuses'
;
import
{
EStatuses
}
from
'../enums/Estatuses'
;
import
IPost
from
'../interfaces/IPost'
;
import
IPost
from
'../interfaces/IPost'
;
import
IResponse
from
'../interfaces/IResponse'
;
import
IResponse
from
'../interfaces/IResponse'
;
import
IPostDto
from
'../interfaces/IPostDto'
;
import
IPostDto
from
'../interfaces/IPostDto'
;
import
mongoose
,
{
Mongoose
}
from
'mongoose'
import
IDataBase
from
'../interfaces/IDataBase'
;
export
const
mongoose
=
{
export
class
Mongo
implements
IDataBase
{
run
:
async
()
=>
{
private
client
:
Mongoose
|
null
=
null
try
{
return
await
connect
(
`
${
process
.
env
.
MONGO_URL
}
/hw92`
);
public
close
=
async
():
Promise
<
void
>
=>
{
if
(
!
this
.
client
)
return
}
catch
(
error
)
{
await
this
.
client
.
disconnect
()
console
.
log
(
error
);
}
}
},
public
async
init
():
Promise
<
void
>
{
stop
:
async
()
=>
{
this
.
client
=
await
mongoose
.
connect
(
process
.
env
.
MONGO_CLIENT_URL
||
'mongodb://localhost/myStore'
)
try
{
console
.
log
(
'Mongo mongoose is connected'
)
return
await
connection
.
destroy
();
}
catch
(
error
)
{
console
.
log
(
error
);
}
}
},
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
[]
>
=
{
...
@@ -41,8 +38,8 @@ export const mongoose = {
...
@@ -41,8 +38,8 @@ export const mongoose = {
return
response
return
response
}
}
}
,
}
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
>
=
{
...
@@ -60,8 +57,8 @@ export const mongoose = {
...
@@ -60,8 +57,8 @@ export const mongoose = {
}
}
return
response
return
response
}
}
}
,
}
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
()
...
@@ -80,8 +77,8 @@ export const mongoose = {
...
@@ -80,8 +77,8 @@ export const mongoose = {
}
}
return
response
return
response
}
}
}
,
}
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
>
=
{
...
@@ -102,5 +99,5 @@ export const mongoose = {
...
@@ -102,5 +99,5 @@ export const mongoose = {
}
}
};
};
export
const
mongo
=
new
Mongo
()
backend/src/services/posts.ts
View file @
94041cfe
import
IPost
from
"../interfaces/IPost"
import
IPost
from
"../interfaces/IPost"
import
IPostDto
from
"../interfaces/IPostDto"
import
IPostDto
from
"../interfaces/IPostDto"
import
IResponse
from
"../interfaces/IResponse"
import
IResponse
from
"../interfaces/IResponse"
import
{
mongoose
}
from
"../repository/mongoose"
import
{
Mongo
,
mongo
}
from
"../repository/mongoose"
export
class
PostService
{
export
class
PostService
{
private
repository
private
repository
:
Mongo
constructor
()
{
constructor
()
{
this
.
repository
=
mongo
ose
this
.
repository
=
mongo
}
}
public
getPosts
=
async
():
Promise
<
IResponse
<
IPost
[]
|
undefined
>>
=>
{
public
getPosts
=
async
():
Promise
<
IResponse
<
IPost
[]
|
undefined
>>
=>
{
return
await
this
.
repository
.
getPosts
()
return
await
this
.
repository
.
getPosts
()
...
...
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