Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
H
Homework_89_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_89_Tsoy_Danil
Commits
afed28db
Commit
afed28db
authored
Mar 22, 2023
by
Цой Данил
💬
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
#4
#5
added rouers and controllers for user. Homework 89
parent
d5c731ca
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
101 additions
and
11 deletions
+101
-11
usersController.ts
src/controllers/usersController.ts
+30
-0
mongooseDB.ts
src/repository/mongooseDB.ts
+25
-1
albumsService.ts
src/services/albumsService.ts
+8
-3
artistsService.ts
src/services/artistsService.ts
+8
-3
tracksService.ts
src/services/tracksService.ts
+8
-4
usersService.ts
src/services/usersService.ts
+22
-0
No files found.
src/controllers/usersController.ts
0 → 100644
View file @
afed28db
import
express
,
{
Request
,
Response
,
Router
}
from
"express"
import
IResponse
from
"../interfaces/IResponse"
;
import
IUser
from
"../interfaces/IUser"
;
import
{
usersService
,
UsersService
}
from
"../services/usersService"
;
export
class
UsersController
{
private
service
:
UsersService
private
router
:
Router
constructor
(){
this
.
service
=
usersService
this
.
router
=
express
.
Router
()
this
.
router
.
post
(
'/'
,
this
.
addUser
)
this
.
router
.
post
(
'/sessions'
,
this
.
loginUser
)
}
public
getRouter
=
()
=>
{
return
this
.
router
}
private
addUser
=
async
(
req
:
Request
,
res
:
Response
):
Promise
<
void
>
=>
{
const
response
:
IResponse
<
IUser
|
null
>
=
await
this
.
service
.
addUser
(
req
.
body
)
res
.
status
(
200
).
send
(
response
)
}
public
loginUser
=
async
(
req
:
Request
,
res
:
Response
):
Promise
<
void
>
=>
{
const
response
:
IResponse
<
IUser
|
null
>
=
await
this
.
service
.
loginUser
(
req
.
body
)
res
.
status
(
200
).
send
(
response
)
}
}
\ No newline at end of file
src/repository/mongooseDB.ts
View file @
afed28db
...
@@ -197,7 +197,31 @@ export class MongooseDB {
...
@@ -197,7 +197,31 @@ export class MongooseDB {
}
}
}
}
public
loginUser
=
async
(
userDto
:
IUserDto
):
Promise
<
IResponse
<
IUser
|
null
>>
=>
{
try
{
const
user
=
await
User
.
findOne
({
username
:
userDto
.
username
})
if
(
!
user
)
throw
new
Error
(
'User not found'
)
// @ts-ignore
const
isMatch
:
boolean
=
await
user
.
checkPassword
(
userDto
.
password
)
if
(
!
isMatch
)
throw
new
Error
(
'Wrong password'
)
// @ts-ignore
user
.
generateToken
()
const
response
:
IResponse
<
IUser
>
=
{
status
:
EStatuses
.
SUCCESS
,
result
:
user
,
message
:
'Access granted'
}
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
()
export
const
mongooseDB
=
new
MongooseDB
()
\ No newline at end of file
src/services/albumsService.ts
View file @
afed28db
...
@@ -2,16 +2,21 @@ import IAlbum from "../interfaces/IAlbum";
...
@@ -2,16 +2,21 @@ import IAlbum from "../interfaces/IAlbum";
import
IResponse
from
"../interfaces/IResponse"
;
import
IResponse
from
"../interfaces/IResponse"
;
import
IAlbumDto
from
"../interfaces/IAlbumDto"
;
import
IAlbumDto
from
"../interfaces/IAlbumDto"
;
import
{
Request
}
from
"express"
;
import
{
Request
}
from
"express"
;
import
{
mongooseDB
}
from
"../repository/mongooseDB"
;
import
{
MongooseDB
,
mongooseDB
}
from
"../repository/mongooseDB"
;
export
class
AlbumsService
{
export
class
AlbumsService
{
private
repository
:
MongooseDB
constructor
(){
this
.
repository
=
mongooseDB
}
public
getAlbums
=
async
(
req
:
Request
):
Promise
<
IResponse
<
IAlbum
[]
|
IAlbum
|
null
>>
=>
{
public
getAlbums
=
async
(
req
:
Request
):
Promise
<
IResponse
<
IAlbum
[]
|
IAlbum
|
null
>>
=>
{
return
await
mongooseDB
.
getAlbums
(
req
)
return
await
this
.
repository
.
getAlbums
(
req
)
}
}
public
addAlbum
=
async
(
albumDto
:
IAlbumDto
):
Promise
<
IResponse
<
IAlbum
|
null
>>
=>
{
public
addAlbum
=
async
(
albumDto
:
IAlbumDto
):
Promise
<
IResponse
<
IAlbum
|
null
>>
=>
{
return
await
mongooseDB
.
addAlbum
(
albumDto
)
return
await
this
.
repository
.
addAlbum
(
albumDto
)
}
}
}
}
...
...
src/services/artistsService.ts
View file @
afed28db
import
IArtist
from
"../interfaces/IArtist"
;
import
IArtist
from
"../interfaces/IArtist"
;
import
IArtistDto
from
"../interfaces/IArtistDto"
;
import
IArtistDto
from
"../interfaces/IArtistDto"
;
import
IResponse
from
"../interfaces/IResponse"
;
import
IResponse
from
"../interfaces/IResponse"
;
import
{
mongooseDB
}
from
"../repository/mongooseDB"
;
import
{
MongooseDB
,
mongooseDB
}
from
"../repository/mongooseDB"
;
export
class
ArtistsService
{
export
class
ArtistsService
{
private
repository
:
MongooseDB
constructor
(){
this
.
repository
=
mongooseDB
}
public
getArtists
=
async
():
Promise
<
IResponse
<
IArtist
[]
|
null
>>
=>
{
public
getArtists
=
async
():
Promise
<
IResponse
<
IArtist
[]
|
null
>>
=>
{
return
await
mongooseDB
.
getArtists
()
return
await
this
.
repository
.
getArtists
()
}
}
public
addArtist
=
async
(
artistDto
:
IArtistDto
):
Promise
<
IResponse
<
IArtist
|
null
>>
=>
{
public
addArtist
=
async
(
artistDto
:
IArtistDto
):
Promise
<
IResponse
<
IArtist
|
null
>>
=>
{
return
await
mongooseDB
.
addArtist
(
artistDto
)
return
await
this
.
repository
.
addArtist
(
artistDto
)
}
}
}
}
...
...
src/services/tracksService.ts
View file @
afed28db
...
@@ -2,17 +2,21 @@ import { Request } from "express";
...
@@ -2,17 +2,21 @@ import { Request } from "express";
import
IResponse
from
"../interfaces/IResponse"
;
import
IResponse
from
"../interfaces/IResponse"
;
import
ITrack
from
"../interfaces/ITrack"
;
import
ITrack
from
"../interfaces/ITrack"
;
import
ITrackDto
from
"../interfaces/ITrackDto"
;
import
ITrackDto
from
"../interfaces/ITrackDto"
;
import
{
mongooseDB
}
from
"../repository/mongooseDB"
;
import
{
MongooseDB
,
mongooseDB
}
from
"../repository/mongooseDB"
;
export
class
TracksService
{
export
class
TracksService
{
private
repository
:
MongooseDB
constructor
(){
this
.
repository
=
mongooseDB
}
public
getTracks
=
async
(
req
:
Request
):
Promise
<
IResponse
<
ITrack
[]
|
null
>>
=>
{
public
getTracks
=
async
(
req
:
Request
):
Promise
<
IResponse
<
ITrack
[]
|
null
>>
=>
{
return
await
mongooseDB
.
getTracks
(
req
)
return
await
this
.
repository
.
getTracks
(
req
)
}
}
public
addTrack
=
async
(
trackDto
:
ITrackDto
):
Promise
<
IResponse
<
ITrack
|
null
>>
=>
{
public
addTrack
=
async
(
trackDto
:
ITrackDto
):
Promise
<
IResponse
<
ITrack
|
null
>>
=>
{
return
await
mongooseDB
.
addTrack
(
trackDto
)
return
await
this
.
repository
.
addTrack
(
trackDto
)
}
}
}
}
...
...
src/services/usersService.ts
0 → 100644
View file @
afed28db
import
IResponse
from
"../interfaces/IResponse"
;
import
IUser
from
"../interfaces/IUser"
;
import
IUserDto
from
"../interfaces/IUserDto"
;
import
{
mongooseDB
,
MongooseDB
}
from
"../repository/mongooseDB"
;
export
class
UsersService
{
private
repository
:
MongooseDB
constructor
(){
this
.
repository
=
mongooseDB
}
public
addUser
=
async
(
userDto
:
IUserDto
):
Promise
<
IResponse
<
IUser
|
null
>>
=>
{
return
await
this
.
repository
.
addUser
(
userDto
)
}
public
loginUser
=
async
(
userDto
:
IUserDto
):
Promise
<
IResponse
<
IUser
|
null
>>
=>
{
return
await
this
.
repository
.
loginUser
(
userDto
)
}
}
export
const
usersService
=
new
UsersService
()
\ 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