#3 added album model

parent a0e4c63e
......@@ -2,12 +2,21 @@ import express, {Express, json} from 'express';
import 'dotenv/config';
import {artistRouter} from './routes/artist';
import cors from 'cors';
import {albumRouter} from './routes/albums';
import {connect} from 'mongoose';
const app: Express = express();
app.use(json());
app.use(cors());
app.use('/artists', artistRouter);
app.use('/albums', albumRouter);
const run = async () => {
await connect(`${process.env.MONGO_URL}/musicApp`);
};
run().catch((err) => console.log(err));
app.listen(process.env.PORT, () => {
console.log('Server started on port ' + process.env.PORT);
......
import express, {Request, Response} from 'express';
import {model, Schema, HydratedDocument, Types} from 'mongoose';
import multer from 'multer';
import fs from 'fs';
import path from 'path';
import {IArtist} from './artist';
const upload = multer({dest: 'uploads/albums'});
const router = express.Router();
interface IAlbum {
name: string;
albumImage: File;
year: number;
artist: Types.ObjectId;
}
const Album = model<IAlbum>(
'Album',
new Schema({
artist: {
type: Schema.Types.ObjectId,
ref: 'Artist',
},
name: String,
year: Number,
albumImage: {
data: Buffer,
contentType: String,
},
})
);
router.get('/', async (req: Request, res: Response) => {
const albums = await Album.find().populate<{artist: IArtist}>('artist');
res.send(albums);
});
router.post(
'/',
upload.single('albumImage'),
async (req: Request, res: Response) => {
const album: HydratedDocument<IAlbum> = new Album({
name: req.body.name,
albumImage: {
data: fs.readFileSync(
path.join('uploads/albums/' + req.file?.filename)
),
contentType: 'image',
},
year: req.body.year,
artist: req.body.artist,
});
await album.save();
res.send('Sucessfully saved');
}
);
export {router as albumRouter};
import express, {Request, Response} from 'express';
import {model, Schema, connect, HydratedDocument} from 'mongoose';
import {model, Schema, HydratedDocument} from 'mongoose';
import multer from 'multer';
import fs from 'fs';
import path from 'path';
const upload = multer({dest: 'uploads/'});
const upload = multer({dest: 'uploads/artists'});
const router = express.Router();
interface IArtist {
export interface IArtist {
name: string;
image: File;
description: string;
......@@ -30,12 +30,6 @@ const artistSchema = new Schema<IArtist>({
const Artist = model<IArtist>('Artist', artistSchema);
const run = async () => {
await connect(`${process.env.MONGO_URL}/artist`);
};
run().catch((err) => console.log(err));
router.get('/', async (req: Request, res: Response) => {
const artists = await Artist.find();
res.send(artists);
......@@ -49,7 +43,9 @@ router.post(
name: req.body.name,
description: req.body.description,
image: {
data: fs.readFileSync(path.join('uploads/' + req.file?.filename)),
data: fs.readFileSync(
path.join('uploads/artists/' + req.file?.filename)
),
contentType: 'image',
},
});
......
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