#6 89 реализовал истории прослушивания треков

parent a412de03
import {Request, Response} from 'express';
import UserModel from '../models/user';
import {getErrorMessage} from '../utils/errors.util';
import * as TrackHistoryService from '../services/track.history';
import TrackModel from '../models/track';
export const authorize = async (req: Request, res: Response) => {
try {
const bearedHeader = req.headers['authorization'];
const bearer = bearedHeader!.split(' ');
const token = bearer[1];
UserModel.findOne({token})
.then(async (user) => {
if (!user) return res.status(401).json({error: 'Unauthorized'});
const trackExists = await TrackModel.exists({_id: req.body.track});
if (!trackExists)
return res.json({
error: 'This track does not exist. Please choode another one',
});
TrackHistoryService.create({
track: req.body.track,
user: user._id,
})
.then((result) => {
return res.send('Successfully created track history');
})
.catch((err) => {
return res.json({error: err.message});
});
})
.catch((err) => {
return res.json({err});
});
} catch (error) {
return res.status(500).send(getErrorMessage(error));
}
};
......@@ -6,6 +6,7 @@ import {ArtistRouter} from './routes/artist';
import {AlbumRouter} from './routes/albums';
import {TrackRouter} from './routes/track';
import {UsersRouter} from './routes/user';
import {TrackHistoryRouter} from './routes/track.history';
mongoose.run();
......@@ -19,6 +20,7 @@ app.use('/artists', ArtistRouter);
app.use('/albums', AlbumRouter);
app.use('/tracks', TrackRouter);
app.use('/users', UsersRouter);
app.use('/track_history', TrackHistoryRouter);
app.listen(process.env.PORT, () => {
console.log(`App started on port ${process.env.PORT}`);
......
import {Date, ObjectId} from 'mongoose';
export default interface ITrackHistory {
user: ObjectId;
track: ObjectId;
datetime?: Date;
}
......@@ -11,7 +11,6 @@ export interface CustomRequest extends Request {
export const auth = async (req: Request, res: Response, next: NextFunction) => {
try {
const token = req.header('Authorization')?.replace('Bearer ', '');
if (!token) {
throw new Error();
}
......
import mongoose, {model, Schema} from 'mongoose';
import ITrackHistory from '../interfaces/ITrackHistory';
const TrackHistorySchema: mongoose.Schema<ITrackHistory> =
new mongoose.Schema<ITrackHistory>(
{
user: {
type: Schema.Types.ObjectId,
required: [true, 'User is required'],
ref: 'user',
},
track: {
type: Schema.Types.ObjectId,
required: [true, 'Track is required'],
ref: 'track',
},
datetime: {
type: Date,
default: Date.now,
},
},
{versionKey: false}
);
const TrackHistoryModel = model<ITrackHistory>(
'trackHistory',
TrackHistorySchema
);
export default TrackHistoryModel;
import {Router} from 'express';
import * as TrackHistoryController from '../controllers/track.history';
import {auth} from '../middleware/auth';
const router: Router = Router();
router.post('/', auth, TrackHistoryController.authorize);
export {router as TrackHistoryRouter};
import ITrackHistory from '../interfaces/ITrackHistory';
import TrackHistoryModel from '../models/track.history';
export async function create(trackHistory: ITrackHistory) {
try {
const newTrackHistory = await new TrackHistoryModel({
user: trackHistory.user,
track: trackHistory.track,
}).save();
return newTrackHistory.populate('track user');
} catch (error) {
throw error;
}
}
import {JwtPayload} from 'jsonwebtoken';
declare global {
namespace NodeJS {
interface ProcessEnv {
......
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