Commit ccf3c81d authored by Цой Данил's avatar Цой Данил 💬

#4 #5 homewrok 89. Added all needed checking for tokens and allowanse of user history

parent 83dabed5
......@@ -28,7 +28,7 @@ export class AlbumsController {
this.router.post('/', upload.single('coverImage'), this.addAlbum)
}
getRouter () {
public getRouter () {
return this.router;
}
......
......@@ -27,7 +27,7 @@ export class ArtistsController {
this.router.post('/', upload.single('photo'), this.addArtist)
}
getRouter () {
public getRouter () {
return this.router;
}
......
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
......@@ -14,7 +14,7 @@ export class TracksController {
this.router.post('/', this.addTrack)
}
getRouter () {
public getRouter () {
return this.router;
}
......
......@@ -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}`)
})
......
......@@ -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)
......@@ -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
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
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment