added track back

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