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
ccf3c81d
Commit
ccf3c81d
authored
Mar 22, 2023
by
Цой Данил
💬
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
#4
#5
homewrok 89. Added all needed checking for tokens and allowanse of user history
parent
83dabed5
Show whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
82 additions
and
5 deletions
+82
-5
albumsController.ts
src/controllers/albumsController.ts
+1
-1
artistsController.ts
src/controllers/artistsController.ts
+1
-1
trackHistoriesController.ts
src/controllers/trackHistoriesController.ts
+30
-0
tracksController.ts
src/controllers/tracksController.ts
+1
-1
index.ts
src/index.ts
+2
-0
TrackHistory.ts
src/models/TrackHistory.ts
+2
-2
mongooseDB.ts
src/repository/mongooseDB.ts
+28
-0
trackHistoriesService.ts
src/services/trackHistoriesService.ts
+17
-0
No files found.
src/controllers/albumsController.ts
View file @
ccf3c81d
...
...
@@ -28,7 +28,7 @@ export class AlbumsController {
this
.
router
.
post
(
'/'
,
upload
.
single
(
'coverImage'
),
this
.
addAlbum
)
}
getRouter
()
{
public
getRouter
()
{
return
this
.
router
;
}
...
...
src/controllers/artistsController.ts
View file @
ccf3c81d
...
...
@@ -27,7 +27,7 @@ export class ArtistsController {
this
.
router
.
post
(
'/'
,
upload
.
single
(
'photo'
),
this
.
addArtist
)
}
getRouter
()
{
public
getRouter
()
{
return
this
.
router
;
}
...
...
src/controllers/trackHistoriesController.ts
0 → 100644
View file @
ccf3c81d
import
{
trackHistoriesService
,
TrackHistoriesService
}
from
"../services/trackHistoriesService"
import
express
,
{
Request
,
Response
,
Router
}
from
"express"
;
import
ITrackHistoryDto
from
"../interfaces/ITrackHistoryDto"
;
import
{
EStatuses
}
from
"../enum/EStatuses"
;
export
class
TrackHistoriesController
{
private
router
:
Router
private
service
:
TrackHistoriesService
constructor
()
{
this
.
router
=
express
.
Router
()
this
.
service
=
trackHistoriesService
this
.
router
.
post
(
'/'
,
this
.
addTrackHistory
)
}
public
getRouter
()
{
return
this
.
router
;
}
private
addTrackHistory
=
async
(
req
:
Request
,
res
:
Response
):
Promise
<
void
>
=>
{
const
trackHistoryDto
:
ITrackHistoryDto
=
req
.
body
const
response
=
await
this
.
service
.
addTrackHistory
(
trackHistoryDto
)
if
(
response
.
status
===
EStatuses
.
FAILURE
){
res
.
status
(
200
).
send
(
response
)
}
else
{
res
.
status
(
401
).
send
(
response
)
}
}
}
export
const
trackHistoriesController
=
new
TrackHistoriesController
()
\ No newline at end of file
src/controllers/tracksController.ts
View file @
ccf3c81d
...
...
@@ -14,7 +14,7 @@ export class TracksController {
this
.
router
.
post
(
'/'
,
this
.
addTrack
)
}
getRouter
()
{
public
getRouter
()
{
return
this
.
router
;
}
...
...
src/index.ts
View file @
ccf3c81d
...
...
@@ -7,6 +7,7 @@ import { artistsController, ArtistsController } from "./controllers/artistsContr
import
{
albumsController
,
AlbumsController
}
from
"./controllers/albumsController"
;
import
{
tracksController
,
TracksController
}
from
"./controllers/tracksController"
;
import
{
usersController
}
from
"./controllers/usersController"
;
import
{
trackHistoriesController
}
from
"./controllers/trackHistoriesController"
;
dotenv
.
config
()
...
...
@@ -26,6 +27,7 @@ class App {
this
.
app
.
use
(
'/albums'
,
albumsController
.
getRouter
())
this
.
app
.
use
(
'/tracks'
,
tracksController
.
getRouter
())
this
.
app
.
use
(
'/users'
,
usersController
.
getRouter
())
this
.
app
.
use
(
'/track_history'
,
trackHistoriesController
.
getRouter
())
this
.
app
.
listen
(
process
.
env
.
APP_PORT
,
()
=>
{
console
.
log
(
`Server is running on http://localhost:
${
process
.
env
.
APP_PORT
}
`
)
})
...
...
src/models/TrackHistory.ts
View file @
ccf3c81d
...
...
@@ -15,9 +15,9 @@ const TrackHistorySchema: Schema = new Schema<ITrackHistory>({
},
datetime
:{
type
:
Date
,
timestamps
:
true
default
:
Date
.
now
}
})
}
,{
versionKey
:
false
}
)
export
const
TrackHistory
=
mongoose
.
model
<
ITrackHistory
>
(
'TrackHistory'
,
TrackHistorySchema
)
src/repository/mongooseDB.ts
View file @
ccf3c81d
...
...
@@ -9,11 +9,14 @@ import IArtistDto from '../interfaces/IArtistDto';
import
IResponse
from
'../interfaces/IResponse'
;
import
ITrack
from
'../interfaces/ITrack'
;
import
ITrackDto
from
'../interfaces/ITrackDto'
;
import
ITrackHistory
from
'../interfaces/ITrackHistory'
;
import
ITrackHistoryDto
from
'../interfaces/ITrackHistoryDto'
;
import
IUser
from
'../interfaces/IUser'
;
import
IUserDto
from
'../interfaces/IUserDto'
;
import
{
Album
}
from
'../models/Album'
;
import
{
Artist
}
from
'../models/Artist'
;
import
{
Track
}
from
'../models/Track'
;
import
{
TrackHistory
}
from
'../models/TrackHistory'
;
import
{
User
}
from
'../models/User'
;
dotenv
.
config
();
...
...
@@ -223,6 +226,31 @@ export class MongooseDB {
return
response
}
}
public
addTrackHistory
=
async
(
trackHistoryDto
:
ITrackHistoryDto
):
Promise
<
IResponse
<
ITrackHistory
|
null
>>=>
{
try
{
const
user
=
await
User
.
findOne
({
token
:
trackHistoryDto
.
token
})
if
(
!
user
)
throw
new
Error
(
'User unauthorized!'
)
const
trackHistory
=
new
TrackHistory
({
user
:
user
.
_id
,
track
:
trackHistoryDto
.
track
})
const
trackExist
=
await
Track
.
exists
({
_id
:
trackHistoryDto
.
track
})
if
(
!
trackExist
)
throw
new
Error
(
'Track not found'
)
const
data
=
await
trackHistory
.
save
()
const
response
:
IResponse
<
ITrackHistory
>
=
{
status
:
EStatuses
.
SUCCESS
,
result
:
data
,
message
:
'Track history 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
src/services/trackHistoriesService.ts
0 → 100644
View file @
ccf3c81d
import
IResponse
from
"../interfaces/IResponse"
import
ITrackHistory
from
"../interfaces/ITrackHistory"
import
ITrackHistoryDto
from
"../interfaces/ITrackHistoryDto"
import
{
MongooseDB
,
mongooseDB
}
from
"../repository/mongooseDB"
export
class
TrackHistoriesService
{
private
repository
:
MongooseDB
constructor
(){
this
.
repository
=
mongooseDB
}
public
addTrackHistory
=
async
(
trackHistoryDto
:
ITrackHistoryDto
):
Promise
<
IResponse
<
ITrackHistory
|
null
>>
=>
{
return
await
this
.
repository
.
addTrackHistory
(
trackHistoryDto
)
}
}
export
const
trackHistoriesService
=
new
TrackHistoriesService
()
\ 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