added getAll func in track history service

parent 88ce6589
......@@ -37,3 +37,15 @@ export const authorize = async (req: Request, res: Response) => {
return res.status(500).send(getErrorMessage(error));
}
};
export const getTracks = async (req: Request, res: Response) => {
try {
const bearedHeader = req.headers['authorization'];
const bearer = bearedHeader!.split(' ');
const token = bearer[1];
const response = await TrackHistoryService.getAll(token);
return res.send(response);
} catch (err) {
return res.status(500).send(getErrorMessage(err));
}
};
......@@ -2,7 +2,7 @@ import expres, {Router} from 'express';
import multer from 'multer';
import {ArtistController} from '../controllers/artist';
const upload = multer({dest: 'uploads'});
const upload = multer({dest: 'public/uploads'});
const router: Router = expres.Router();
router.get('/', ArtistController.getArtists);
......
......@@ -5,5 +5,6 @@ import {auth} from '../middleware/auth';
const router: Router = Router();
router.post('/', auth, TrackHistoryController.authorize);
router.get('/', TrackHistoryController.getTracks);
export {router as TrackHistoryRouter};
import ITrackHistory from '../interfaces/ITrackHistory';
import TrackHistoryModel from '../models/track.history';
import UserModel from '../models/user';
export async function create(trackHistory: ITrackHistory) {
try {
......@@ -12,3 +13,20 @@ export async function create(trackHistory: ITrackHistory) {
throw error;
}
}
export async function getAll(token: string) {
try {
const userId = await UserModel.findOne({token});
const trackHistories = await TrackHistoryModel.find({
user: userId,
})
.populate({
path: 'track',
populate: {path: 'album', populate: {path: 'artist'}},
})
.sort({datetime: -1});
return trackHistories;
} catch (error) {
throw error;
}
}
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