Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
H
Homework_95_Tsoy_Danil
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
Цой Данил
Homework_95_Tsoy_Danil
Commits
246bec2b
Commit
246bec2b
authored
Apr 12, 2023
by
Цой Данил
💬
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added methods to get last 30 messages + add fixtures
parent
fb87f679
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
187 additions
and
3 deletions
+187
-3
fixtures.ts
backend/src/fixtures.ts
+183
-0
Message.ts
backend/src/models/Message.ts
+2
-1
mongooseDB.ts
backend/src/repository/mongooseDB.ts
+2
-2
No files found.
backend/src/fixtures.ts
View file @
246bec2b
import
dotenv
from
'dotenv'
import
mongoose
from
'mongoose'
import
{
User
}
from
'./models/User'
import
{
Message
}
from
'./models/Message'
dotenv
.
config
()
mongoose
.
connect
(
process
.
env
.
MONGO_CLIENT_URL
||
'mongodb://localhost/TsoyDanilChatDB'
)
const
db
=
mongoose
.
connection
export
default
db
.
once
(
'open'
,
async
()
=>
{
try
{
await
db
.
dropCollection
(
'messages'
)
await
db
.
dropCollection
(
'users'
)
}
catch
(
err
:
unknown
){
console
.
log
(
`Skipped db drop because of:
${
err
}
`
);
}
const
[
userOne
,
userTwo
,
userThree
,
userFour
,
userFive
]
=
await
User
.
create
(
{
username
:
'user1'
,
password
:
'pass'
},
{
username
:
'user2'
,
password
:
'pass'
},
{
username
:
'user3'
,
password
:
'pass'
},
{
username
:
'user4'
,
password
:
'pass'
},
{
username
:
'user5'
,
password
:
'pass'
}
)
await
Message
.
create
(
{
user
:
userOne
.
_id
,
message
:
'mess1'
},
{
user
:
userOne
.
_id
,
message
:
'mess2'
},
{
user
:
userOne
.
_id
,
message
:
'mess3'
},
{
user
:
userOne
.
_id
,
message
:
'mess4'
},
{
user
:
userOne
.
_id
,
message
:
'mess5'
},
{
user
:
userOne
.
_id
,
message
:
'mess6'
},
{
user
:
userOne
.
_id
,
message
:
'mess7'
},
{
user
:
userOne
.
_id
,
message
:
'mess8'
},
{
user
:
userOne
.
_id
,
message
:
'mess9'
},
{
user
:
userOne
.
_id
,
message
:
'mess10'
},
{
user
:
userOne
.
_id
,
message
:
'mess11'
},
{
user
:
userOne
.
_id
,
message
:
'mess12'
},
{
user
:
userOne
.
_id
,
message
:
'mess13'
},
{
user
:
userOne
.
_id
,
message
:
'mess14'
},
{
user
:
userOne
.
_id
,
message
:
'mess15'
},
{
user
:
userOne
.
_id
,
message
:
'mess16'
},
{
user
:
userOne
.
_id
,
message
:
'mess17'
},
{
user
:
userOne
.
_id
,
message
:
'mess19'
},
{
user
:
userOne
.
_id
,
message
:
'mess20'
},
{
user
:
userOne
.
_id
,
message
:
'mess21'
},
{
user
:
userOne
.
_id
,
message
:
'mess22'
},
{
user
:
userOne
.
_id
,
message
:
'mess23'
},
{
user
:
userOne
.
_id
,
message
:
'mess24'
},
{
user
:
userOne
.
_id
,
message
:
'mess25'
},
{
user
:
userOne
.
_id
,
message
:
'mess26'
},
{
user
:
userOne
.
_id
,
message
:
'mess27'
},
{
user
:
userOne
.
_id
,
message
:
'mess28'
},
{
user
:
userOne
.
_id
,
message
:
'mess29'
},
{
user
:
userOne
.
_id
,
message
:
'mess30'
},
{
user
:
userOne
.
_id
,
message
:
'mess31'
},
{
user
:
userOne
.
_id
,
message
:
'mess32'
},
{
user
:
userOne
.
_id
,
message
:
'mess33'
},
{
user
:
userOne
.
_id
,
message
:
'mess34'
},
{
user
:
userOne
.
_id
,
message
:
'mess35'
}
)
db
.
close
()
})
\ No newline at end of file
backend/src/models/Message.ts
View file @
246bec2b
...
...
@@ -6,6 +6,7 @@ const MessageSchema: Schema = new Schema<IMessage>({
type
:
Schema
.
Types
.
ObjectId
,
ref
:
'User'
,
minlength
:
1
,
trim
:
true
,
required
:
[
true
,
'User id has to be provided'
]
},
message
:
{
...
...
@@ -14,6 +15,6 @@ const MessageSchema: Schema = new Schema<IMessage>({
minlength
:
1
,
required
:
[
true
,
'Username should exist'
]
}
})
}
,
{
versionKey
:
false
}
)
export
const
Message
=
mongoose
.
model
<
IMessage
>
(
'Message'
,
MessageSchema
)
\ No newline at end of file
backend/src/repository/mongooseDB.ts
View file @
246bec2b
...
...
@@ -87,11 +87,11 @@ export class MongooseDB {
public
getMessages
=
async
():
Promise
<
IResponse
<
IMessage
[]
|
null
>>
=>
{
try
{
const
data
=
await
Message
.
find
().
populate
(
'user'
).
limit
(
30
)
const
data
=
(
await
Message
.
find
().
populate
(
'user'
).
sort
({
$natural
:
-
1
}).
limit
(
30
)).
reverse
(
)
const
response
:
IResponse
<
IMessage
[]
|
null
>
=
{
status
:
EStatuses
.
SUCCESS
,
result
:
data
,
message
:
'
Track
s found'
message
:
'
Message
s found'
}
return
response
}
catch
(
err
:
unknown
){
...
...
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