Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
N
node_template
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
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
Владислав Цой
node_template
Commits
c6a837d1
Commit
c6a837d1
authored
Sep 12, 2023
by
Разиев Абдрашид
💬
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
#2 Добавлена логика на запросы
parent
21ddc528
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
51 additions
and
0 deletions
+51
-0
messages.service.ts
src/services/messages.service.ts
+51
-0
No files found.
src/services/messages.service.ts
0 → 100644
View file @
c6a837d1
import
{
IMessage
,
IMessages
}
from
"@/interfaces/IMessages.interface"
;
import
{
randomUUID
}
from
"crypto"
;
import
{
readFileSync
,
writeFileSync
}
from
"node:fs"
;
import
path
from
'path'
export
class
MessagesService
{
private
filePath
:
string
;
private
messages
:
IMessages
[]
=
[];
constructor
()
{
this
.
filePath
=
path
.
resolve
(
__dirname
,
`../../data/messages.json`
);
this
.
restore
()
}
public
getAllMessages
=
():
IMessages
[]
=>
{
return
this
.
messages
.
length
>
30
?
this
.
messages
.
slice
(
-
30
)
:
this
.
messages
};
public
createMessages
=
(
data
:
IMessage
):
IMessages
|
undefined
=>
{
const
message
=
{
id
:
randomUUID
(),
message
:
data
.
message
,
author
:
data
.
author
,
datetime
:
new
Date
()
};
this
.
messages
.
push
(
message
);
this
.
save
()
return
message
};
public
getMessage
=
(
date
:
string
):
IMessage
[]
=>
{
console
.
log
(
new
Date
(
date
),
new
Date
(
this
.
messages
[
this
.
messages
.
length
-
1
].
datetime
))
if
(
new
Date
(
date
).
getTime
()
!==
new
Date
(
this
.
messages
[
this
.
messages
.
length
-
1
].
datetime
).
getTime
()){
return
this
.
messages
.
length
>
30
?
this
.
messages
.
slice
(
-
30
)
:
this
.
messages
}
else
{
return
[]
}
}
private
restore
=
()
=>
{
try
{
const
saveData
=
readFileSync
(
this
.
filePath
,
{
encoding
:
"utf-8"
});
this
.
messages
=
JSON
.
parse
(
saveData
)
}
catch
(
e
:
any
){
console
.
log
(
e
)
}
}
private
save
=
()
=>
{
writeFileSync
(
this
.
filePath
,
JSON
.
stringify
(
this
.
messages
),
{
encoding
:
"utf-8"
})
}
}
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