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
fb87f679
Commit
fb87f679
authored
Apr 11, 2023
by
Цой Данил
💬
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added all methods to backend part. Planning to start work with websockets
parent
d52ebea0
Show whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
187 additions
and
3 deletions
+187
-3
messagesController.ts
backend/src/controllers/messagesController.ts
+55
-0
usersController.ts
backend/src/controllers/usersController.ts
+0
-1
index.config.ts
backend/src/index.config.ts
+6
-0
index.ts
backend/src/index.ts
+39
-0
IMessage.ts
backend/src/interfaces/IMessage.ts
+1
-1
IMessageDto.ts
backend/src/interfaces/IMessageDto.ts
+1
-1
Message.ts
backend/src/models/Message.ts
+19
-0
mongooseDB.ts
backend/src/repository/mongooseDB.ts
+44
-0
messagesService.ts
backend/src/services/messagesService.ts
+22
-0
No files found.
backend/src/controllers/messagesController.ts
0 → 100644
View file @
fb87f679
import
express
,
{
Request
,
Response
,
Router
}
from
"express"
import
{
MessagesService
,
messagesService
}
from
"../services/messagesService"
import
{
auth
}
from
"../middlewares/auth"
import
IResponse
from
"../interfaces/IResponse"
import
IMessage
from
"../interfaces/IMessage"
import
{
EStatuses
}
from
"../enum/EStatuses"
import
IModifiedRequest
from
"../interfaces/IModifiedRequest"
import
IMessageDto
from
"../interfaces/IMessageDto"
export
class
MessagesController
{
private
service
:
MessagesService
private
router
:
Router
constructor
(){
this
.
service
=
messagesService
this
.
router
=
express
.
Router
()
this
.
router
.
get
(
'/'
,
auth
,
this
.
getMessages
)
this
.
router
.
post
(
'/'
,
auth
,
this
.
addMessage
)
}
public
getRouter
=
()
=>
{
return
this
.
router
}
private
getMessages
=
async
(
req
:
Request
,
res
:
Response
):
Promise
<
void
>
=>
{
const
response
:
IResponse
<
IMessage
[]
|
null
>
=
await
this
.
service
.
getMessages
()
if
(
response
.
status
===
EStatuses
.
SUCCESS
){
res
.
status
(
200
).
send
(
response
)
}
else
{
res
.
status
(
418
).
send
(
response
)
}
}
private
addMessage
=
async
(
req
:
Request
,
res
:
Response
):
Promise
<
void
>
=>
{
const
modifiedReq
=
req
as
IModifiedRequest
if
(
typeof
modifiedReq
.
verifiedData
===
'object'
){
const
{
message
}
=
req
.
body
const
{
_id
}
=
modifiedReq
.
verifiedData
const
messageDto
:
IMessageDto
=
{
user
:
_id
,
message
:
message
}
const
response
:
IResponse
<
IMessage
|
null
>
=
await
this
.
service
.
addMessage
(
messageDto
)
if
(
response
.
status
===
EStatuses
.
SUCCESS
){
res
.
status
(
200
).
send
(
response
)
}
else
{
res
.
status
(
418
).
send
(
response
)
}
}
else
{
res
.
status
(
418
).
send
(
'error in request. Some invalid field appeared'
)
}
}
}
export
const
messagesController
=
new
MessagesController
()
\ No newline at end of file
backend/src/controllers/usersController.ts
View file @
fb87f679
import
express
,
{
Request
,
Response
,
Router
}
from
"express"
import
{
EStatuses
}
from
"../enum/EStatuses"
;
import
IResponse
from
"../interfaces/IResponse"
;
import
IUser
from
"../interfaces/IUser"
;
import
IUserGetDto
from
"../interfaces/IUserGetDto"
;
import
{
auth
}
from
"../middlewares/auth"
;
import
{
usersService
,
UsersService
}
from
"../services/usersService"
;
...
...
backend/src/index.config.ts
View file @
fb87f679
import
path
from
'path'
export
const
config
=
{
port
:
8000
,
filePath
:
path
.
join
(
__dirname
,
'../public/uploads'
)
}
backend/src/index.ts
View file @
fb87f679
import
express
,
{
Express
}
from
"express"
;
import
dotenv
from
'dotenv'
;
import
cors
from
'cors'
;
import
{
healthCheckController
}
from
"./controllers/healthCheck"
;
import
{
mongooseDB
}
from
"./repository/mongooseDB"
;
import
{
usersController
}
from
"./controllers/usersController"
;
import
{
messagesController
}
from
"./controllers/messagesController"
;
dotenv
.
config
()
class
App
{
private
app
:
Express
constructor
()
{
this
.
app
=
express
()
this
.
app
.
use
(
express
.
json
())
this
.
app
.
use
(
cors
())
}
public
init
=
async
():
Promise
<
void
>
=>
{
try
{
this
.
app
.
use
(
'/health-check'
,
healthCheckController
.
getRouter
())
this
.
app
.
use
(
'/users'
,
usersController
.
getRouter
())
this
.
app
.
use
(
'/messages'
,
messagesController
.
getRouter
())
this
.
app
.
listen
(
process
.
env
.
APP_PORT
,
()
=>
{
console
.
log
(
`Server is running on http://localhost:
${
process
.
env
.
APP_PORT
}
`
)
})
await
mongooseDB
.
init
()
process
.
on
(
'exit'
,
()
=>
{
mongooseDB
.
close
()
})
}
catch
(
err
:
unknown
){
console
.
log
(
err
);
}
}
}
const
app
=
new
App
();
app
.
init
();
\ No newline at end of file
backend/src/interfaces/IMessage.ts
View file @
fb87f679
...
...
@@ -3,6 +3,6 @@ import IUser from "./IUser";
export
default
interface
IMessage
{
_id
:
Types
.
ObjectId
author
:
IUser
[
'username'
]
user
:
IUser
message
:
string
}
\ No newline at end of file
backend/src/interfaces/IMessageDto.ts
View file @
fb87f679
import
IMessage
from
"./IMessage"
;
export
default
interface
IMessageDto
{
author
:
IMessage
[
'author
'
]
user
:
IMessage
[
'_id
'
]
message
:
IMessage
[
'message'
]
}
\ No newline at end of file
backend/src/models/Message.ts
0 → 100644
View file @
fb87f679
import
mongoose
,
{
Schema
}
from
"mongoose"
;
import
IMessage
from
"../interfaces/IMessage"
;
const
MessageSchema
:
Schema
=
new
Schema
<
IMessage
>
({
user
:
{
type
:
Schema
.
Types
.
ObjectId
,
ref
:
'User'
,
minlength
:
1
,
required
:
[
true
,
'User id has to be provided'
]
},
message
:
{
type
:
String
,
trim
:
true
,
minlength
:
1
,
required
:
[
true
,
'Username should exist'
]
}
})
export
const
Message
=
mongoose
.
model
<
IMessage
>
(
'Message'
,
MessageSchema
)
\ No newline at end of file
backend/src/repository/mongooseDB.ts
View file @
fb87f679
...
...
@@ -6,6 +6,9 @@ import IUserGetDto from '../interfaces/IUserGetDto'
import
{
generateJWT
}
from
'../helpers/generateJWT'
import
IUserCreateDto
from
'../interfaces/IUserCreateDto'
import
{
User
}
from
'../models/User'
import
IMessage
from
'../interfaces/IMessage'
import
{
Message
}
from
'../models/Message'
import
IMessageDto
from
'../interfaces/IMessageDto'
dotenv
.
config
()
...
...
@@ -81,6 +84,47 @@ export class MongooseDB {
return
response
}
}
public
getMessages
=
async
():
Promise
<
IResponse
<
IMessage
[]
|
null
>>
=>
{
try
{
const
data
=
await
Message
.
find
().
populate
(
'user'
).
limit
(
30
)
const
response
:
IResponse
<
IMessage
[]
|
null
>
=
{
status
:
EStatuses
.
SUCCESS
,
result
:
data
,
message
:
'Tracks found'
}
return
response
}
catch
(
err
:
unknown
){
const
error
=
err
as
Error
const
response
:
IResponse
<
null
>
=
{
status
:
EStatuses
.
FAILURE
,
result
:
null
,
message
:
error
.
message
}
return
response
}
}
public
addMessage
=
async
(
messageDto
:
IMessageDto
):
Promise
<
IResponse
<
IMessage
|
null
>>
=>
{
try
{
const
message
=
new
Message
(
messageDto
)
const
responseData
=
await
message
.
save
()
const
response
:
IResponse
<
IMessage
>
=
{
status
:
EStatuses
.
SUCCESS
,
result
:
responseData
,
message
:
'Artist added'
}
return
response
}
catch
(
err
:
unknown
){
const
error
=
err
as
Error
const
response
:
IResponse
<
null
>
=
{
status
:
EStatuses
.
FAILURE
,
result
:
null
,
message
:
error
.
message
}
return
response
}
}
}
export
const
mongooseDB
=
new
MongooseDB
()
\ No newline at end of file
backend/src/services/messagesService.ts
0 → 100644
View file @
fb87f679
import
IMessage
from
"../interfaces/IMessage"
;
import
IMessageDto
from
"../interfaces/IMessageDto"
;
import
IResponse
from
"../interfaces/IResponse"
;
import
{
MongooseDB
,
mongooseDB
}
from
"../repository/mongooseDB"
;
export
class
MessagesService
{
private
repository
:
MongooseDB
constructor
(){
this
.
repository
=
mongooseDB
}
public
getMessages
=
async
():
Promise
<
IResponse
<
IMessage
[]
|
null
>>
=>
{
return
await
this
.
repository
.
getMessages
()
}
public
addMessage
=
async
(
messageDto
:
IMessageDto
):
Promise
<
IResponse
<
IMessage
|
null
>>
=>
{
return
await
this
.
repository
.
addMessage
(
messageDto
)
}
}
export
const
messagesService
=
new
MessagesService
()
\ 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