removed unlink from fs

parent e50c9c12
import express from 'express';
import {AlbumService} from '../services/album';
export const AlbumController = {
createAlbum: async (req: express.Request, res: express.Response) => {
try {
const {name, year, artist} = req.body;
await AlbumService.generate({
name,
year,
artist,
image: req.file?.filename || '',
})
.then((result) => {
return res.send(result);
})
.catch((err) => {
return res.send({error: err.message});
});
} catch (err: unknown) {
res.json({err});
}
},
getAlbums: async (req: express.Request, res: express.Response) => {
try {
await AlbumService.get(req.query.artist?.toString())
.then((result) => {
return res.send(result);
})
.catch((err) => {
return res.json({err});
});
} catch (err: unknown) {
res.json({err});
}
},
getAlbum: async (req: express.Request, res: express.Response) => {
try {
await AlbumService.getById(req.params.id)
.then((result) => {
return res.send(result);
})
.catch((err) => {
return res.json({err});
});
} catch (err: unknown) {
res.json({err});
}
},
};
import express from 'express';
import {ArtistService} from '../services/artist';
import {unlink} from 'fs';
export const ArtistController = {
createArtist: async (req: express.Request, res: express.Response) => {
......@@ -16,9 +15,6 @@ export const ArtistController = {
return res.send(result);
})
.catch((err) => {
unlink(`uploads/${req.file?.filename}`, (err) => {
if (err) throw err;
});
return res.send({error: err.message});
});
} catch (err: unknown) {
......@@ -27,7 +23,7 @@ export const ArtistController = {
},
getArtists: async (req: express.Request, res: express.Response) => {
try {
ArtistService.get()
await ArtistService.get()
.then((result) => {
res.send(result);
})
......
import express from 'express';
import {TrackService} from '../services/tracks';
export const TrackController = {
createTrack: async (req: express.Request, res: express.Response) => {
try {
const {name, duration, album} = req.body;
await TrackService.generate({
name,
duration,
album,
})
.then((result) => {
return res.send(result);
})
.catch((err) => {
return res.send({error: err.message});
});
} catch (err: unknown) {
res.json({err});
}
},
getTracks: async (req: express.Request, res: express.Response) => {
try {
await TrackService.get(req.query.album?.toString())
.then((result) => {
return res.send(result);
})
.catch((err) => {
return res.json({err});
});
} catch (err: unknown) {
res.json({err});
}
},
};
......@@ -3,6 +3,8 @@ import 'dotenv/config';
import cors from 'cors';
import {mongoose} from './repository/mongoose';
import {ArtistRouter} from './routes/artist';
import {AlbumRouter} from './routes/albums';
import {TrackRouter} from './routes/track';
mongoose.run();
......@@ -13,6 +15,8 @@ app.use(urlencoded({extended: true}));
app.use(express.static('images'));
app.use('/artists', ArtistRouter);
app.use('/albums', AlbumRouter);
app.use('/tracks', TrackRouter);
app.listen(process.env.PORT, () => {
console.log(`App started on port ${process.env.PORT}`);
......
import {ObjectId} from 'mongoose';
export default interface IAlbum {
name: string;
artist: ObjectId;
year: number;
image: string;
}
import {ObjectId} from 'mongoose';
export default interface ITrack {
name: string;
album: ObjectId;
duration: string;
}
import {model, Schema} from 'mongoose';
import IAlbum from '../interfaces/IAlbum';
const AlbumSchema = new Schema<IAlbum>(
{
name: {
type: String,
required: [true, 'Name is required'],
trim: true,
},
image: {
type: String,
required: [true, 'Image is required'],
},
artist: {
type: Schema.Types.ObjectId,
required: [true, 'Artist is required'],
trim: true,
ref: 'artist',
},
year: {
type: Number,
required: [true, 'Year is required'],
trim: true,
},
},
{versionKey: false}
);
const AlbumModel = model<IAlbum>('album', AlbumSchema);
export default AlbumModel;
import {model, Schema} from 'mongoose';
import ITrack from '../interfaces/ITrack';
const TrackSchema = new Schema<ITrack>(
{
name: {
type: String,
required: [true, 'Name is required'],
trim: true,
},
album: {
type: Schema.Types.ObjectId,
required: [true, 'Album is required'],
trim: true,
ref: 'album',
},
duration: {
type: String,
required: [true, 'Duration is required'],
trim: true,
},
},
{versionKey: false}
);
const TrackModel = model<ITrack>('track', TrackSchema);
export default TrackModel;
import expres, {Router} from 'express';
import multer from 'multer';
import {AlbumController} from '../controllers/album';
const upload = multer({dest: 'uploads'});
const router: Router = expres.Router();
router.get('/', AlbumController.getAlbums);
router.get('/:id', AlbumController.getAlbum);
router.post('/', upload.single('image'), AlbumController.createAlbum);
export {router as AlbumRouter};
import expres, {Router} from 'express';
import {TrackController} from '../controllers/track';
const router: Router = expres.Router();
router.get('/', TrackController.getTracks);
router.post('/', TrackController.createTrack);
export {router as TrackRouter};
import IAlbum from '../interfaces/IAlbum';
import AlbumModel from '../models/album';
export const AlbumService = {
generate: async ({name, year, image, artist}: IAlbum) => {
return await new AlbumModel({
name,
year,
image,
artist,
}).save();
},
get: async (id?: string) => {
return await AlbumModel.find(id ? {artist: id} : {}).populate('artist');
},
getById: async (id: string) => {
return await AlbumModel.findById(id).populate('artist');
},
};
import ITrack from '../interfaces/ITrack';
import TrackModel from '../models/track';
export const TrackService = {
generate: async ({name, duration, album}: ITrack) => {
return await new TrackModel({
name,
duration,
album,
}).save();
},
get: async (id?: string) => {
return await TrackModel.find(id ? {album: id} : {}).populate('album');
},
};
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