added track back

parent a9b2cf17
import express from 'express'; import express, {Request, Response} from 'express';
import {AlbumService} from '../services/album'; import {AlbumService} from '../services/album';
import AlbumModel from '../models/album';
import IUser from '../interfaces/IUser';
import UserModel from '../models/user';
export const AlbumController = { export const AlbumController = {
createAlbum: async (req: express.Request, res: express.Response) => { createAlbum: async (req: express.Request, res: express.Response) => {
...@@ -48,4 +51,25 @@ export const AlbumController = { ...@@ -48,4 +51,25 @@ export const AlbumController = {
res.json({err}); res.json({err});
} }
}, },
publishAlbum: async (req: Request, res: Response) => {
try {
const id = req.params.id;
await AlbumModel.findOneAndUpdate({_id: id}, {published: true});
res.send('Published');
} catch (err) {
res.status(500).send(err);
}
},
deleteAlbum: async (req: Request, res: Response) => {
try {
const id = req.params.id;
const user: IUser | null = await UserModel.findById(req.token._id);
if (user && user.role === 'Admin') {
await AlbumModel.deleteOne({_id: id});
}
res.send('Deleted');
} catch (err) {
res.status(500).send(err);
}
},
}; };
import express from 'express'; import express, {Request, Response} from 'express';
import {ArtistService} from '../services/artist'; import {ArtistService} from '../services/artist';
import ArtistModel from '../models/artist';
import UserModel from '../models/user';
import IUser from '../interfaces/IUser';
export const ArtistController = { export const ArtistController = {
createArtist: async (req: express.Request, res: express.Response) => { createArtist: async (req: express.Request, res: express.Response) => {
...@@ -34,4 +37,25 @@ export const ArtistController = { ...@@ -34,4 +37,25 @@ export const ArtistController = {
res.json({err}); res.json({err});
} }
}, },
deleteArtist: async (req: Request, res: Response) => {
try {
const id = req.params.id;
const user: IUser | null = await UserModel.findById(req.token._id);
if (user && user.role === 'Admin') {
await ArtistModel.deleteOne({_id: id});
}
res.send('Deleted');
} catch (err) {
res.status(500).send(err);
}
},
publishArtist: async (req: Request, res: Response) => {
try {
const id = req.params.id;
await ArtistModel.findOneAndUpdate({_id: id}, {published: true});
res.send('Published');
} catch (err) {
res.status(500).send(err);
}
},
}; };
import express from 'express'; import express, {Request, Response} from 'express';
import {TrackService} from '../services/tracks'; import {TrackService} from '../services/tracks';
import IUser from '../interfaces/IUser';
import UserModel from '../models/user';
import TrackModel from '../models/track';
export const TrackController = { export const TrackController = {
createTrack: async (req: express.Request, res: express.Response) => { createTrack: async (req: express.Request, res: express.Response) => {
...@@ -34,4 +37,26 @@ export const TrackController = { ...@@ -34,4 +37,26 @@ export const TrackController = {
res.json({err}); res.json({err});
} }
}, },
deleteTrack: async (req: Request, res: Response) => {
try {
const id = req.params.id;
const user: IUser | null = await UserModel.findById(req.token._id);
if (user && user.role === 'Admin') {
await TrackModel.deleteOne({_id: id});
}
res.send('Deleted');
} catch (err) {
res.status(500).send(err);
}
},
publishTrack: async (req: Request, res: Response) => {
try {
const id = req.params.id;
console.log('sdfd');
await TrackModel.findOneAndUpdate({_id: id}, {published: true});
res.send('Published');
} catch (err) {
res.status(500).send(err);
}
},
}; };
...@@ -44,24 +44,28 @@ db.once('open', async () => { ...@@ -44,24 +44,28 @@ db.once('open', async () => {
info: 'Aubrey Drake Graham is a Canadian rapper and singer. An influential global figure in contemporary popular music, Drake has been credited for popularizing singing and R&B sensibilities in hip hop.', info: 'Aubrey Drake Graham is a Canadian rapper and singer. An influential global figure in contemporary popular music, Drake has been credited for popularizing singing and R&B sensibilities in hip hop.',
name: 'Drake', name: 'Drake',
photo: 'drake.jpg', photo: 'drake.jpg',
published: true,
}, },
{ {
_id: eminemId, _id: eminemId,
info: 'Marshall Bruce Mathers III, known professionally as Eminem, is an American rapper, songwriter, and record producer. He is credited with popularizing hip hop in middle America and is critically acclaimed as one of the greatest rappers of all time.', info: 'Marshall Bruce Mathers III, known professionally as Eminem, is an American rapper, songwriter, and record producer. He is credited with popularizing hip hop in middle America and is critically acclaimed as one of the greatest rappers of all time.',
name: 'Eminem', name: 'Eminem',
photo: 'eminem.png', photo: 'eminem.png',
published: true,
}, },
{ {
_id: kizaruId, _id: kizaruId,
info: 'Рэп-исполнитель новой волны, проживающий в Барселоне', info: 'Рэп-исполнитель новой волны, проживающий в Барселоне',
name: 'Kizaru', name: 'Kizaru',
photo: 'kizaru.jpg', photo: 'kizaru.jpg',
published: true,
}, },
{ {
_id: snoopId, _id: snoopId,
info: 'Calvin Cordozar Broadus Jr. (born October 20, 1971), known professionally as Snoop Dogg is an American rapper and actor.', info: 'Calvin Cordozar Broadus Jr. (born October 20, 1971), known professionally as Snoop Dogg is an American rapper and actor.',
name: 'Snoop Dogg', name: 'Snoop Dogg',
photo: 'snoop.jpg', photo: 'snoop.jpg',
published: false,
} }
); );
...@@ -78,6 +82,7 @@ db.once('open', async () => { ...@@ -78,6 +82,7 @@ db.once('open', async () => {
image: 'album1.jpg', image: 'album1.jpg',
year: 2001, year: 2001,
name: 'Room for Improvement', name: 'Room for Improvement',
published: true,
}, },
{ {
_id: secondAlbumId, _id: secondAlbumId,
...@@ -85,6 +90,7 @@ db.once('open', async () => { ...@@ -85,6 +90,7 @@ db.once('open', async () => {
image: 'album2.jpg', image: 'album2.jpg',
year: 2005, year: 2005,
name: 'Comeback Season', name: 'Comeback Season',
published: false,
}, },
{ {
_id: thirdAlbumId, _id: thirdAlbumId,
...@@ -92,6 +98,7 @@ db.once('open', async () => { ...@@ -92,6 +98,7 @@ db.once('open', async () => {
image: 'album3.jpg', image: 'album3.jpg',
year: 2011, year: 2011,
name: 'One fortune', name: 'One fortune',
published: true,
}, },
{ {
_id: fourthAlbumId, _id: fourthAlbumId,
...@@ -99,6 +106,7 @@ db.once('open', async () => { ...@@ -99,6 +106,7 @@ db.once('open', async () => {
image: 'kizaruAlbum.jpg', image: 'kizaruAlbum.jpg',
year: 2005, year: 2005,
name: 'Bandana', name: 'Bandana',
published: true,
}, },
{ {
_id: fifthAlbumId, _id: fifthAlbumId,
...@@ -106,6 +114,7 @@ db.once('open', async () => { ...@@ -106,6 +114,7 @@ db.once('open', async () => {
image: 'snoopAlbum.jpg', image: 'snoopAlbum.jpg',
year: 2005, year: 2005,
name: 'High', name: 'High',
published: true,
} }
); );
...@@ -114,126 +123,151 @@ db.once('open', async () => { ...@@ -114,126 +123,151 @@ db.once('open', async () => {
album: firstAlbumId, album: firstAlbumId,
duration: '2:56', duration: '2:56',
name: 'sfdg12', name: 'sfdg12',
published: true,
}, },
{ {
album: firstAlbumId, album: firstAlbumId,
duration: '2:56', duration: '2:56',
name: 'sdfsadfsad', name: 'sdfsadfsad',
published: true,
}, },
{ {
album: firstAlbumId, album: firstAlbumId,
duration: '2:36', duration: '2:36',
name: '12312', name: '12312',
published: true,
}, },
{ {
album: firstAlbumId, album: firstAlbumId,
duration: '2:56', duration: '2:56',
name: 'sadf', name: 'sadf',
published: true,
}, },
{ {
album: firstAlbumId, album: firstAlbumId,
duration: '2:56', duration: '2:56',
name: 'gfjgfh', name: 'gfjgfh',
published: true,
}, },
{ {
album: secondAlbumId, album: secondAlbumId,
duration: '3:10', duration: '3:10',
name: 'hgjfgh', name: 'hgjfgh',
published: true,
}, },
{ {
album: secondAlbumId, album: secondAlbumId,
duration: '3:10', duration: '3:10',
name: 'sadfasd', name: 'sadfasd',
published: true,
}, },
{ {
album: secondAlbumId, album: secondAlbumId,
duration: '3:10', duration: '3:10',
name: '21312312', name: '21312312',
published: true,
}, },
{ {
album: secondAlbumId, album: secondAlbumId,
duration: '3:10', duration: '3:10',
name: 'sadfasfasd', name: 'sadfasfasd',
published: true,
}, },
{ {
album: secondAlbumId, album: secondAlbumId,
duration: '3:10', duration: '3:10',
name: 'asdvdsa', name: 'asdvdsa',
published: true,
}, },
{ {
album: thirdAlbumId, album: thirdAlbumId,
duration: '3:10', duration: '3:10',
name: 'hgjfgh', name: 'hgjfgh',
published: true,
}, },
{ {
album: thirdAlbumId, album: thirdAlbumId,
duration: '3:10', duration: '3:10',
name: 'sadfasd', name: 'sadfasd',
published: true,
}, },
{ {
album: thirdAlbumId, album: thirdAlbumId,
duration: '3:10', duration: '3:10',
name: '21312312', name: '21312312',
published: true,
}, },
{ {
album: thirdAlbumId, album: thirdAlbumId,
duration: '3:10', duration: '3:10',
name: 'sadfasfasd', name: 'sadfasfasd',
published: true,
}, },
{ {
album: thirdAlbumId, album: thirdAlbumId,
duration: '3:10', duration: '3:10',
name: 'asdvdsa', name: 'asdvdsa',
published: true,
}, },
{ {
album: fourthAlbumId, album: fourthAlbumId,
duration: '3:10', duration: '3:10',
name: 'hgjfgh', name: 'hgjfgh',
published: true,
}, },
{ {
album: fourthAlbumId, album: fourthAlbumId,
duration: '3:10', duration: '3:10',
name: 'sadfasd', name: 'sadfasd',
published: true,
}, },
{ {
album: fourthAlbumId, album: fourthAlbumId,
duration: '3:10', duration: '3:10',
name: '21312312', name: '21312312',
published: true,
}, },
{ {
album: fourthAlbumId, album: fourthAlbumId,
duration: '3:10', duration: '3:10',
name: 'sadfasfasd', name: 'sadfasfasd',
published: true,
}, },
{ {
album: fourthAlbumId, album: fourthAlbumId,
duration: '3:10', duration: '3:10',
name: 'asdvdsa', name: 'asdvdsa',
published: true,
}, },
{ {
album: fifthAlbumId, album: fifthAlbumId,
duration: '3:10', duration: '3:10',
name: 'hgjfgh', name: 'hgjfgh',
published: true,
}, },
{ {
album: fifthAlbumId, album: fifthAlbumId,
duration: '3:10', duration: '3:10',
name: 'sadfasd', name: 'sadfasd',
published: true,
}, },
{ {
album: fifthAlbumId, album: fifthAlbumId,
duration: '3:10', duration: '3:10',
name: '21312312', name: '21312312',
published: false,
}, },
{ {
album: fifthAlbumId, album: fifthAlbumId,
duration: '3:10', duration: '3:10',
name: 'sadfasfasd', name: 'sadfasfasd',
published: false,
}, },
{ {
album: fifthAlbumId, album: fifthAlbumId,
duration: '3:10', duration: '3:10',
name: 'asdvdsa', name: 'asdvdsa',
published: false,
} }
); );
......
...@@ -5,4 +5,5 @@ export default interface IAlbum { ...@@ -5,4 +5,5 @@ export default interface IAlbum {
artist: ObjectId; artist: ObjectId;
year: number; year: number;
image: string; image: string;
published?: boolean;
} }
...@@ -2,4 +2,5 @@ export default interface IArtist { ...@@ -2,4 +2,5 @@ export default interface IArtist {
name: string; name: string;
photo: string; photo: string;
info: string; info: string;
published?: boolean;
} }
...@@ -5,4 +5,5 @@ export default interface ITrack { ...@@ -5,4 +5,5 @@ export default interface ITrack {
album: ObjectId; album: ObjectId;
duration: string; duration: string;
seq?: Number; seq?: Number;
published?: boolean;
} }
...@@ -4,19 +4,15 @@ import {Request, Response, NextFunction} from 'express'; ...@@ -4,19 +4,15 @@ import {Request, Response, NextFunction} from 'express';
export const SECRET_KEY: Secret = export const SECRET_KEY: Secret =
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkFsZW4gQm9sYXRvdiIsImlhdCI6MTUxNjIzOTAyMn0.d2_x9z4HZivq8qQUvKEhgROH_zLKwV82bC0a0hXaIvY'; 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkFsZW4gQm9sYXRvdiIsImlhdCI6MTUxNjIzOTAyMn0.d2_x9z4HZivq8qQUvKEhgROH_zLKwV82bC0a0hXaIvY';
export interface CustomRequest extends Request {
token: string | JwtPayload;
}
export const auth = async (req: Request, res: Response, next: NextFunction) => { export const auth = async (req: Request, res: Response, next: NextFunction) => {
try { try {
const token = req.header('Authorization')?.replace('Bearer ', ''); const token = req.header('Authorization')?.replace('Bearer ', '');
if (!token) { if (!token) {
throw new Error(); return res.status(401).send('Access denied');
} }
const decoded = jwt.verify(token, SECRET_KEY); const decoded = jwt.verify(token, SECRET_KEY);
(req as CustomRequest).token = decoded; req.token = decoded;
next(); next();
} catch (err) { } catch (err) {
......
...@@ -23,6 +23,11 @@ const AlbumSchema = new Schema<IAlbum>( ...@@ -23,6 +23,11 @@ const AlbumSchema = new Schema<IAlbum>(
required: [true, 'Year is required'], required: [true, 'Year is required'],
trim: true, trim: true,
}, },
published: {
type: Boolean,
required: true,
default: false,
},
}, },
{versionKey: false} {versionKey: false}
); );
......
...@@ -17,6 +17,11 @@ const ArtistSchema = new Schema<IArtist>( ...@@ -17,6 +17,11 @@ const ArtistSchema = new Schema<IArtist>(
required: [true, 'Info is required'], required: [true, 'Info is required'],
trim: true, trim: true,
}, },
published: {
type: Boolean,
required: true,
default: false,
},
}, },
{versionKey: false} {versionKey: false}
); );
......
...@@ -22,6 +22,11 @@ const TrackSchema = new Schema<ITrack>( ...@@ -22,6 +22,11 @@ const TrackSchema = new Schema<ITrack>(
seq: { seq: {
type: Number, type: Number,
}, },
published: {
type: Boolean,
required: true,
default: false,
},
}, },
{versionKey: false} {versionKey: false}
); );
......
import expres, {Router} from 'express'; import expres, {Router} from 'express';
import multer from 'multer'; import multer from 'multer';
import {AlbumController} from '../controllers/album'; import {AlbumController} from '../controllers/album';
import {auth} from '../middleware/auth';
const upload = multer({dest: 'public/uploads'}); const upload = multer({dest: 'public/uploads'});
const router: Router = expres.Router(); const router: Router = expres.Router();
...@@ -8,5 +9,7 @@ const router: Router = expres.Router(); ...@@ -8,5 +9,7 @@ const router: Router = expres.Router();
router.get('/', AlbumController.getAlbums); router.get('/', AlbumController.getAlbums);
router.get('/:id', AlbumController.getAlbum); router.get('/:id', AlbumController.getAlbum);
router.post('/', upload.single('image'), AlbumController.createAlbum); router.post('/', upload.single('image'), AlbumController.createAlbum);
router.post('/:id/publish', AlbumController.publishAlbum);
router.delete('/:id', auth, AlbumController.deleteAlbum);
export {router as AlbumRouter}; export {router as AlbumRouter};
import expres, {Router} from 'express'; import expres, {Router} from 'express';
import multer from 'multer'; import multer from 'multer';
import {ArtistController} from '../controllers/artist'; import {ArtistController} from '../controllers/artist';
import {auth} from '../middleware/auth';
const upload = multer({dest: 'public/uploads'}); const upload = multer({dest: 'public/uploads'});
const router: Router = expres.Router(); const router: Router = expres.Router();
...@@ -8,4 +9,7 @@ const router: Router = expres.Router(); ...@@ -8,4 +9,7 @@ const router: Router = expres.Router();
router.get('/', ArtistController.getArtists); router.get('/', ArtistController.getArtists);
router.post('/', upload.single('photo'), ArtistController.createArtist); router.post('/', upload.single('photo'), ArtistController.createArtist);
router.delete('/:id', auth, ArtistController.deleteArtist);
router.post('/:id/publish', ArtistController.publishArtist);
export {router as ArtistRouter}; export {router as ArtistRouter};
import expres, {Router} from 'express'; import expres, {Router} from 'express';
import {TrackController} from '../controllers/track'; import {TrackController} from '../controllers/track';
import {auth} from '../middleware/auth';
const router: Router = expres.Router(); const router: Router = expres.Router();
router.get('/', TrackController.getTracks); router.get('/', TrackController.getTracks);
router.post('/', TrackController.createTrack); router.post('/', TrackController.createTrack);
router.delete('/:id', auth, TrackController.deleteTrack);
router.post('/:id/publish', TrackController.publishTrack);
export {router as TrackRouter}; export {router as TrackRouter};
...@@ -15,7 +15,7 @@ export async function register(user: IUser): Promise<void> { ...@@ -15,7 +15,7 @@ export async function register(user: IUser): Promise<void> {
export async function login(user: IUser) { export async function login(user: IUser) {
try { try {
const foundUser = await UserModel.findOne({username: user.username}); const foundUser = await UserModel.findOne({username: user.username});
console.log(foundUser);
if (!foundUser) { if (!foundUser) {
throw new Error('Name of user is not correct'); throw new Error('Name of user is not correct');
} }
...@@ -23,17 +23,22 @@ export async function login(user: IUser) { ...@@ -23,17 +23,22 @@ export async function login(user: IUser) {
if (isMatch) { if (isMatch) {
const token = jwt.sign( const token = jwt.sign(
{_id: foundUser._id?.toString(), username: foundUser.username}, {
_id: foundUser._id?.toString(),
username: foundUser.username,
},
SECRET_KEY, SECRET_KEY,
{ {
expiresIn: '2 days', expiresIn: '2 days',
} }
); );
await UserModel.findOneAndUpdate({username: user.username}, {token}); await UserModel.findOneAndUpdate({username: user.username}, {token});
return { return {
token: token, token: token,
username: user.username, username: user.username,
id: foundUser._id.toString(), id: foundUser._id.toString(),
role: foundUser.role,
}; };
} else { } else {
throw new Error('Password is not correct'); throw new Error('Password is not correct');
......
...@@ -7,6 +7,11 @@ declare global { ...@@ -7,6 +7,11 @@ declare global {
MONGO_URL: string; MONGO_URL: string;
} }
} }
namespace Express {
interface Request {
token: string | JwtPayload<{_id: string; username: string}>; //or can be anythin
}
}
} }
export {}; export {};
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