added controllers and services

parent 9c5d2760
......@@ -11,15 +11,18 @@
"dependencies": {
"@types/node": "^18.15.3",
"body-parser": "^1.20.2",
"class-validator": "^0.14.0",
"cors": "^2.8.5",
"dotenv": "^16.0.3",
"express": "^4.18.2",
"express-validator": "^6.15.0",
"http-status-codes": "^2.2.0",
"joi": "^17.9.1",
"mongoose": "^7.0.1",
"multer": "^1.4.5-lts.1",
"ts-node-dev": "^2.0.0"
},
"devDependencies": {
"@types/body-parser": "^1.19.2",
"@types/cors": "^2.8.13",
"@types/express": "^4.17.17",
"@types/mongoose": "^5.11.97",
......@@ -37,6 +40,19 @@
"node": ">=12"
}
},
"node_modules/@hapi/hoek": {
"version": "9.3.0",
"resolved": "https://registry.npmjs.org/@hapi/hoek/-/hoek-9.3.0.tgz",
"integrity": "sha512-/c6rf4UJlmHlC9b5BaNvzAcFv7HZ2QHaV0D4/HNlBdvFnvQq8RI4kYdhyPCl7Xj+oWvTWQ8ujhqS53LIgAe6KQ=="
},
"node_modules/@hapi/topo": {
"version": "5.1.0",
"resolved": "https://registry.npmjs.org/@hapi/topo/-/topo-5.1.0.tgz",
"integrity": "sha512-foQZKJig7Ob0BMAYBfcJk8d77QtOe7Wo4ox7ff1lQYoNNAb6jwcY1ncdoy2e9wQZzvNy7ODZCYJkK8kzmcAnAg==",
"dependencies": {
"@hapi/hoek": "^9.0.0"
}
},
"node_modules/@jridgewell/resolve-uri": {
"version": "3.1.0",
"resolved": "https://registry.npmjs.org/@jridgewell/resolve-uri/-/resolve-uri-3.1.0.tgz",
......@@ -59,6 +75,24 @@
"@jridgewell/sourcemap-codec": "^1.4.10"
}
},
"node_modules/@sideway/address": {
"version": "4.1.4",
"resolved": "https://registry.npmjs.org/@sideway/address/-/address-4.1.4.tgz",
"integrity": "sha512-7vwq+rOHVWjyXxVlR76Agnvhy8I9rpzjosTESvmhNeXOXdZZB15Fl+TI9x1SiHZH5Jv2wTGduSxFDIaq0m3DUw==",
"dependencies": {
"@hapi/hoek": "^9.0.0"
}
},
"node_modules/@sideway/formula": {
"version": "3.0.1",
"resolved": "https://registry.npmjs.org/@sideway/formula/-/formula-3.0.1.tgz",
"integrity": "sha512-/poHZJJVjx3L+zVD6g9KgHfYnb443oi7wLu/XKojDviHy6HOEOA6z1Trk5aR1dGcmPenJEgb2sK2I80LeS3MIg=="
},
"node_modules/@sideway/pinpoint": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/@sideway/pinpoint/-/pinpoint-2.0.0.tgz",
"integrity": "sha512-RNiOoTPkptFtSVzQevY/yWtZwf/RxyVnPy/OcA9HBM3MlGDnBEYL5B41H0MTn0Uec8Hi+2qUtTfG2WWZBmMejQ=="
},
"node_modules/@tsconfig/node10": {
"version": "1.0.9",
"resolved": "https://registry.npmjs.org/@tsconfig/node10/-/node10-1.0.9.tgz",
......@@ -192,6 +226,11 @@
"resolved": "https://registry.npmjs.org/@types/strip-json-comments/-/strip-json-comments-0.0.30.tgz",
"integrity": "sha512-7NQmHra/JILCd1QqpSzl8+mJRc8ZHz3uDm8YV1Ks9IhK0epEiTw8aIErbvH9PI+6XbqhyIQy3462nEsn7UVzjQ=="
},
"node_modules/@types/validator": {
"version": "13.7.14",
"resolved": "https://registry.npmjs.org/@types/validator/-/validator-13.7.14.tgz",
"integrity": "sha512-J6OAed6rhN6zyqL9Of6ZMamhlsOEU/poBVvbHr/dKOYKTeuYYMlDkMv+b6UUV0o2i0tw73cgyv/97WTWaUl0/g=="
},
"node_modules/@types/webidl-conversions": {
"version": "7.0.0",
"resolved": "https://registry.npmjs.org/@types/webidl-conversions/-/webidl-conversions-7.0.0.tgz",
......@@ -390,6 +429,16 @@
"fsevents": "~2.3.2"
}
},
"node_modules/class-validator": {
"version": "0.14.0",
"resolved": "https://registry.npmjs.org/class-validator/-/class-validator-0.14.0.tgz",
"integrity": "sha512-ct3ltplN8I9fOwUd8GrP8UQixwff129BkEtuWDKL5W45cQuLd19xqmTLu5ge78YDm/fdje6FMt0hGOhl0lii3A==",
"dependencies": {
"@types/validator": "^13.7.10",
"libphonenumber-js": "^1.10.14",
"validator": "^13.7.0"
}
},
"node_modules/concat-map": {
"version": "0.0.1",
"resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz",
......@@ -579,6 +628,18 @@
"node": ">= 0.10.0"
}
},
"node_modules/express-validator": {
"version": "6.15.0",
"resolved": "https://registry.npmjs.org/express-validator/-/express-validator-6.15.0.tgz",
"integrity": "sha512-r05VYoBL3i2pswuehoFSy+uM8NBuVaY7avp5qrYjQBDzagx2Z5A77FZqPT8/gNLF3HopWkIzaTFaC4JysWXLqg==",
"dependencies": {
"lodash": "^4.17.21",
"validator": "^13.9.0"
},
"engines": {
"node": ">= 8.0.0"
}
},
"node_modules/express/node_modules/body-parser": {
"version": "1.20.1",
"resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.20.1.tgz",
......@@ -763,6 +824,11 @@
"node": ">= 0.8"
}
},
"node_modules/http-status-codes": {
"version": "2.2.0",
"resolved": "https://registry.npmjs.org/http-status-codes/-/http-status-codes-2.2.0.tgz",
"integrity": "sha512-feERVo9iWxvnejp3SEfm/+oNG517npqL2/PIA8ORjyOZjGC7TwCRQsZylciLS64i6pJ0wRYz3rkXLRwbtFa8Ng=="
},
"node_modules/iconv-lite": {
"version": "0.4.24",
"resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.24.tgz",
......@@ -855,6 +921,18 @@
"resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz",
"integrity": "sha512-VLghIWNM6ELQzo7zwmcg0NmTVyWKYjvIeM83yjp0wRDTmUnrM678fQbcKBo6n2CJEF0szoG//ytg+TKla89ALQ=="
},
"node_modules/joi": {
"version": "17.9.1",
"resolved": "https://registry.npmjs.org/joi/-/joi-17.9.1.tgz",
"integrity": "sha512-FariIi9j6QODKATGBrEX7HZcja8Bsh3rfdGYy/Sb65sGlZWK/QWesU1ghk7aJWDj95knjXlQfSmzFSPPkLVsfw==",
"dependencies": {
"@hapi/hoek": "^9.0.0",
"@hapi/topo": "^5.0.0",
"@sideway/address": "^4.1.3",
"@sideway/formula": "^3.0.1",
"@sideway/pinpoint": "^2.0.0"
}
},
"node_modules/kareem": {
"version": "2.5.1",
"resolved": "https://registry.npmjs.org/kareem/-/kareem-2.5.1.tgz",
......@@ -863,6 +941,16 @@
"node": ">=12.0.0"
}
},
"node_modules/libphonenumber-js": {
"version": "1.10.24",
"resolved": "https://registry.npmjs.org/libphonenumber-js/-/libphonenumber-js-1.10.24.tgz",
"integrity": "sha512-3Dk8f5AmrcWqg+oHhmm9hwSTqpWHBdSqsHmjCJGroULFubi0+x7JEIGmRZCuL3TI8Tx39xaKqfnhsDQ4ALa/Nw=="
},
"node_modules/lodash": {
"version": "4.17.21",
"resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz",
"integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg=="
},
"node_modules/make-error": {
"version": "1.3.6",
"resolved": "https://registry.npmjs.org/make-error/-/make-error-1.3.6.tgz",
......@@ -1686,6 +1774,14 @@
"resolved": "https://registry.npmjs.org/v8-compile-cache-lib/-/v8-compile-cache-lib-3.0.1.tgz",
"integrity": "sha512-wa7YjyUGfNZngI/vtK0UHAN+lgDCxBPCylVXGp0zu59Fz5aiGtNXaq3DhIov063MorB+VfufLh3JlF2KdTK3xg=="
},
"node_modules/validator": {
"version": "13.9.0",
"resolved": "https://registry.npmjs.org/validator/-/validator-13.9.0.tgz",
"integrity": "sha512-B+dGG8U3fdtM0/aNK4/X8CXq/EcxU2WPrPEkJGslb47qyHsxmbggTWK0yEA4qnYVNF+nxNlN88o14hIcPmSIEA==",
"engines": {
"node": ">= 0.10"
}
},
"node_modules/vary": {
"version": "1.1.2",
"resolved": "https://registry.npmjs.org/vary/-/vary-1.1.2.tgz",
......@@ -1745,6 +1841,19 @@
"@jridgewell/trace-mapping": "0.3.9"
}
},
"@hapi/hoek": {
"version": "9.3.0",
"resolved": "https://registry.npmjs.org/@hapi/hoek/-/hoek-9.3.0.tgz",
"integrity": "sha512-/c6rf4UJlmHlC9b5BaNvzAcFv7HZ2QHaV0D4/HNlBdvFnvQq8RI4kYdhyPCl7Xj+oWvTWQ8ujhqS53LIgAe6KQ=="
},
"@hapi/topo": {
"version": "5.1.0",
"resolved": "https://registry.npmjs.org/@hapi/topo/-/topo-5.1.0.tgz",
"integrity": "sha512-foQZKJig7Ob0BMAYBfcJk8d77QtOe7Wo4ox7ff1lQYoNNAb6jwcY1ncdoy2e9wQZzvNy7ODZCYJkK8kzmcAnAg==",
"requires": {
"@hapi/hoek": "^9.0.0"
}
},
"@jridgewell/resolve-uri": {
"version": "3.1.0",
"resolved": "https://registry.npmjs.org/@jridgewell/resolve-uri/-/resolve-uri-3.1.0.tgz",
......@@ -1764,6 +1873,24 @@
"@jridgewell/sourcemap-codec": "^1.4.10"
}
},
"@sideway/address": {
"version": "4.1.4",
"resolved": "https://registry.npmjs.org/@sideway/address/-/address-4.1.4.tgz",
"integrity": "sha512-7vwq+rOHVWjyXxVlR76Agnvhy8I9rpzjosTESvmhNeXOXdZZB15Fl+TI9x1SiHZH5Jv2wTGduSxFDIaq0m3DUw==",
"requires": {
"@hapi/hoek": "^9.0.0"
}
},
"@sideway/formula": {
"version": "3.0.1",
"resolved": "https://registry.npmjs.org/@sideway/formula/-/formula-3.0.1.tgz",
"integrity": "sha512-/poHZJJVjx3L+zVD6g9KgHfYnb443oi7wLu/XKojDviHy6HOEOA6z1Trk5aR1dGcmPenJEgb2sK2I80LeS3MIg=="
},
"@sideway/pinpoint": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/@sideway/pinpoint/-/pinpoint-2.0.0.tgz",
"integrity": "sha512-RNiOoTPkptFtSVzQevY/yWtZwf/RxyVnPy/OcA9HBM3MlGDnBEYL5B41H0MTn0Uec8Hi+2qUtTfG2WWZBmMejQ=="
},
"@tsconfig/node10": {
"version": "1.0.9",
"resolved": "https://registry.npmjs.org/@tsconfig/node10/-/node10-1.0.9.tgz",
......@@ -1896,6 +2023,11 @@
"resolved": "https://registry.npmjs.org/@types/strip-json-comments/-/strip-json-comments-0.0.30.tgz",
"integrity": "sha512-7NQmHra/JILCd1QqpSzl8+mJRc8ZHz3uDm8YV1Ks9IhK0epEiTw8aIErbvH9PI+6XbqhyIQy3462nEsn7UVzjQ=="
},
"@types/validator": {
"version": "13.7.14",
"resolved": "https://registry.npmjs.org/@types/validator/-/validator-13.7.14.tgz",
"integrity": "sha512-J6OAed6rhN6zyqL9Of6ZMamhlsOEU/poBVvbHr/dKOYKTeuYYMlDkMv+b6UUV0o2i0tw73cgyv/97WTWaUl0/g=="
},
"@types/webidl-conversions": {
"version": "7.0.0",
"resolved": "https://registry.npmjs.org/@types/webidl-conversions/-/webidl-conversions-7.0.0.tgz",
......@@ -2046,6 +2178,16 @@
"readdirp": "~3.6.0"
}
},
"class-validator": {
"version": "0.14.0",
"resolved": "https://registry.npmjs.org/class-validator/-/class-validator-0.14.0.tgz",
"integrity": "sha512-ct3ltplN8I9fOwUd8GrP8UQixwff129BkEtuWDKL5W45cQuLd19xqmTLu5ge78YDm/fdje6FMt0hGOhl0lii3A==",
"requires": {
"@types/validator": "^13.7.10",
"libphonenumber-js": "^1.10.14",
"validator": "^13.7.0"
}
},
"concat-map": {
"version": "0.0.1",
"resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz",
......@@ -2230,6 +2372,15 @@
}
}
},
"express-validator": {
"version": "6.15.0",
"resolved": "https://registry.npmjs.org/express-validator/-/express-validator-6.15.0.tgz",
"integrity": "sha512-r05VYoBL3i2pswuehoFSy+uM8NBuVaY7avp5qrYjQBDzagx2Z5A77FZqPT8/gNLF3HopWkIzaTFaC4JysWXLqg==",
"requires": {
"lodash": "^4.17.21",
"validator": "^13.9.0"
}
},
"fill-range": {
"version": "7.0.1",
"resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.0.1.tgz",
......@@ -2334,6 +2485,11 @@
"toidentifier": "1.0.1"
}
},
"http-status-codes": {
"version": "2.2.0",
"resolved": "https://registry.npmjs.org/http-status-codes/-/http-status-codes-2.2.0.tgz",
"integrity": "sha512-feERVo9iWxvnejp3SEfm/+oNG517npqL2/PIA8ORjyOZjGC7TwCRQsZylciLS64i6pJ0wRYz3rkXLRwbtFa8Ng=="
},
"iconv-lite": {
"version": "0.4.24",
"resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.24.tgz",
......@@ -2405,11 +2561,33 @@
"resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz",
"integrity": "sha512-VLghIWNM6ELQzo7zwmcg0NmTVyWKYjvIeM83yjp0wRDTmUnrM678fQbcKBo6n2CJEF0szoG//ytg+TKla89ALQ=="
},
"joi": {
"version": "17.9.1",
"resolved": "https://registry.npmjs.org/joi/-/joi-17.9.1.tgz",
"integrity": "sha512-FariIi9j6QODKATGBrEX7HZcja8Bsh3rfdGYy/Sb65sGlZWK/QWesU1ghk7aJWDj95knjXlQfSmzFSPPkLVsfw==",
"requires": {
"@hapi/hoek": "^9.0.0",
"@hapi/topo": "^5.0.0",
"@sideway/address": "^4.1.3",
"@sideway/formula": "^3.0.1",
"@sideway/pinpoint": "^2.0.0"
}
},
"kareem": {
"version": "2.5.1",
"resolved": "https://registry.npmjs.org/kareem/-/kareem-2.5.1.tgz",
"integrity": "sha512-7jFxRVm+jD+rkq3kY0iZDJfsO2/t4BBPeEb2qKn2lR/9KhuksYk5hxzfRYWMPV8P/x2d0kHD306YyWLzjjH+uA=="
},
"libphonenumber-js": {
"version": "1.10.24",
"resolved": "https://registry.npmjs.org/libphonenumber-js/-/libphonenumber-js-1.10.24.tgz",
"integrity": "sha512-3Dk8f5AmrcWqg+oHhmm9hwSTqpWHBdSqsHmjCJGroULFubi0+x7JEIGmRZCuL3TI8Tx39xaKqfnhsDQ4ALa/Nw=="
},
"lodash": {
"version": "4.17.21",
"resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz",
"integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg=="
},
"make-error": {
"version": "1.3.6",
"resolved": "https://registry.npmjs.org/make-error/-/make-error-1.3.6.tgz",
......@@ -2989,6 +3167,11 @@
"resolved": "https://registry.npmjs.org/v8-compile-cache-lib/-/v8-compile-cache-lib-3.0.1.tgz",
"integrity": "sha512-wa7YjyUGfNZngI/vtK0UHAN+lgDCxBPCylVXGp0zu59Fz5aiGtNXaq3DhIov063MorB+VfufLh3JlF2KdTK3xg=="
},
"validator": {
"version": "13.9.0",
"resolved": "https://registry.npmjs.org/validator/-/validator-13.9.0.tgz",
"integrity": "sha512-B+dGG8U3fdtM0/aNK4/X8CXq/EcxU2WPrPEkJGslb47qyHsxmbggTWK0yEA4qnYVNF+nxNlN88o14hIcPmSIEA=="
},
"vary": {
"version": "1.1.2",
"resolved": "https://registry.npmjs.org/vary/-/vary-1.1.2.tgz",
......
......@@ -12,15 +12,18 @@
"dependencies": {
"@types/node": "^18.15.3",
"body-parser": "^1.20.2",
"class-validator": "^0.14.0",
"cors": "^2.8.5",
"dotenv": "^16.0.3",
"express": "^4.18.2",
"express-validator": "^6.15.0",
"http-status-codes": "^2.2.0",
"joi": "^17.9.1",
"mongoose": "^7.0.1",
"multer": "^1.4.5-lts.1",
"ts-node-dev": "^2.0.0"
},
"devDependencies": {
"@types/body-parser": "^1.19.2",
"@types/cors": "^2.8.13",
"@types/express": "^4.17.17",
"@types/mongoose": "^5.11.97",
......
import express from 'express';
import {ArtistService} from '../services/artist';
import {unlink} from 'fs';
export const ArtistController = {
createArtist: async (req: express.Request, res: express.Response) => {
try {
const {name, info} = req.body;
await ArtistService.generate({
name,
info,
photo: req.file?.filename || '',
})
.then((result) => {
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) {
res.json({err});
}
},
getArtists: async (req: express.Request, res: express.Response) => {
try {
ArtistService.get()
.then((result) => {
res.send(result);
})
.catch((err) => {
res.json({err});
});
} catch (err: unknown) {
res.json({err});
}
},
};
import express, {Express, json} from 'express';
import express, {Express, json, urlencoded} from 'express';
import 'dotenv/config';
import {artistRouter} from './routes/artist';
import cors from 'cors';
import {albumRouter} from './routes/albums';
import {connect} from 'mongoose';
import {trackRouter} from './routes/track';
import {mongoose} from './repository/mongoose';
import {ArtistRouter} from './routes/artist';
mongoose.run();
const app: Express = express();
app.use(json());
app.use(cors());
app.use(urlencoded({extended: true}));
app.use('/artists', artistRouter);
app.use('/albums', albumRouter);
app.use('/tracks', trackRouter);
const run = async () => {
await connect(`${process.env.MONGO_URL}/musicApp`);
};
run().catch((err) => console.log(err));
app.use(express.static('images'));
app.use('/artists', ArtistRouter);
app.listen(process.env.PORT, () => {
console.log('Server started on port ' + process.env.PORT);
console.log(`App started on port ${process.env.PORT}`);
});
export default interface IArtist {
name: string;
photo: string;
info: string;
}
import {model, Schema} from 'mongoose';
import IArtist from '../interfaces/IArtist';
const ArtistSchema = new Schema<IArtist>(
{
name: {
type: String,
required: [true, 'Name is required'],
trim: true,
},
photo: {
type: String,
required: [true, 'Photo is required'],
},
info: {
type: String,
required: [true, 'Info is required'],
trim: true,
},
},
{versionKey: false}
);
const ArtistModel = model<IArtist>('artist', ArtistSchema);
export default ArtistModel;
import {connect, connection} from 'mongoose';
export const mongoose = {
run: async () => {
try {
return await connect(`${process.env.MONGO_URL}/MyPlayer`);
} catch (error) {
console.log(error);
}
},
stop: async () => {
try {
return await connection.destroy();
} catch (error) {
console.log(error);
}
},
};
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();
export 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) => {
if (!req.query.artist) {
try {
const albums = await Album.find().populate<{artist: IArtist}>('artist');
res.send(albums);
return;
} catch (err: unknown) {
res.send('Could not get albums');
}
}
try {
const artistsAlbums = await Album.find({artist: req.query.artist});
res.send(artistsAlbums);
} catch (err: unknown) {
res.send(`Could not find albums of ${req.query.artist} `);
}
});
router.get('/:id', async (req: Request, res: Response) => {
try {
const album = await Album.findById(req.params.id).populate<{
artist: IArtist;
}>('artist');
if (album === null) {
res.send('Nothing such album found');
}
res.send(album);
} catch (err: unknown) {
res.send('No such artist exists');
}
});
router.post(
'/',
upload.single('albumImage'),
async (req: Request, res: Response) => {
if (!req.body.name || !req.file || !req.body.year || !req.body.artist) {
res.send('Name, year, artist or albumimage is required');
return;
}
try {
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');
} catch (err: unknown) {
res.send('Could not post album');
}
}
);
export {router as albumRouter};
import express, {Request, Response} from 'express';
import {model, Schema, HydratedDocument} from 'mongoose';
import expres, {Router} from 'express';
import multer from 'multer';
import fs from 'fs';
import path from 'path';
import {ArtistController} from '../controllers/artist';
const upload = multer({dest: 'uploads/artists'});
const router = express.Router();
const upload = multer({dest: 'uploads'});
const router: Router = expres.Router();
export interface IArtist {
name: string;
image: File;
description: string;
}
router.get('/', ArtistController.getArtists);
router.post('/', upload.single('photo'), ArtistController.createArtist);
const artistSchema = new Schema<IArtist>({
name: {
type: String,
required: true,
},
image: {
data: Buffer,
contentType: String,
},
description: {
type: String,
required: true,
},
});
const Artist = model<IArtist>('Artist', artistSchema);
router.get('/', async (req: Request, res: Response) => {
try {
const artists = await Artist.find();
res.send(artists);
} catch (err: unknown) {
res.send('could not get artists');
}
});
router.post(
'/',
upload.single('image'),
async (req: Request, res: Response) => {
if (!req.body.name || !req.body.description || !req.file) {
res.send('Name, description or image cannot be emtpty');
return;
}
try {
const artist: HydratedDocument<IArtist> = new Artist({
name: req.body.name,
description: req.body.description,
image: {
data: fs.readFileSync(
path.join('uploads/artists/' + req.file?.filename)
),
contentType: 'image',
},
});
await artist.save();
res.send('Sucessfully saved');
} catch (err: unknown) {
res.send('Could not post');
}
}
);
export {router as artistRouter};
export {router as ArtistRouter};
import express, {Request, Response} from 'express';
import {model, Schema, HydratedDocument, Types} from 'mongoose';
import {IAlbum} from './albums';
const router = express.Router();
interface ITrack {
name: string;
album: Types.ObjectId;
duration: string;
}
const Track = model<ITrack>(
'Track',
new Schema({
name: String,
duration: String,
album: {
type: Schema.Types.ObjectId,
ref: 'Album',
},
})
);
router.get('/', async (req: Request, res: Response) => {
if (!req.query.album) {
try {
const tracks = await Track.find().populate<{album: IAlbum}>('album');
res.send(tracks);
return;
} catch (err: unknown) {
res.send('Could not get tracks');
}
}
try {
const albumTracks = await Track.find({album: req.query.album});
res.send(albumTracks);
} catch (err: unknown) {
res.send(`Could not find album tracks of ${req.query.album} `);
}
});
router.post('/', async (req: Request, res: Response) => {
if (!req.body.name || !req.body.album || !req.body.duration) {
res.send('Name, album or duration cannot be emtpty');
return;
}
try {
const track: HydratedDocument<ITrack> = new Track({
name: req.body.name,
album: req.body.album,
duration: req.body.duration,
});
await track.save();
res.send(`Saved track: ${track}`);
} catch (err: unknown) {
res.send('Could not create track');
}
});
export {router as trackRouter};
import IArtist from '../interfaces/IArtist';
import ArtistModel from '../models/artist';
export const ArtistService = {
generate: async ({name, info, photo}: IArtist) => {
return await new ArtistModel({
name,
info,
photo,
}).save();
},
get: async () => {
return await ArtistModel.find();
},
};
......@@ -11,7 +11,7 @@
// "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */
/* Language and Environment */
"target": "ES2022", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
"target": "ES2022" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */,
// "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
// "jsx": "preserve", /* Specify what JSX code is generated. */
// "experimentalDecorators": true, /* Enable experimental support for TC39 stage 2 draft decorators. */
......@@ -25,9 +25,9 @@
// "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */
/* Modules */
"module": "NodeNext", /* Specify what module code is generated. */
"module": "NodeNext" /* Specify what module code is generated. */,
// "rootDir": "./", /* Specify the root folder within your source files. */
"moduleResolution": "nodenext", /* Specify how TypeScript looks up a file from a given module specifier. */
"moduleResolution": "nodenext" /* Specify how TypeScript looks up a file from a given module specifier. */,
// "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
// "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
// "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
......@@ -49,7 +49,7 @@
// "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */
// "sourceMap": true, /* Create source map files for emitted JavaScript files. */
// "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If 'declaration' is true, also designates a file that bundles all .d.ts output. */
"outDir": "dist", /* Specify an output folder for all emitted files. */
"outDir": "dist" /* Specify an output folder for all emitted files. */,
// "removeComments": true, /* Disable emitting comments. */
// "noEmit": true, /* Disable emitting files from a compilation. */
// "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */
......@@ -71,13 +71,13 @@
/* Interop Constraints */
// "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */
// "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */
"esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */
"esModuleInterop": true /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */,
// "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */
"forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */
"forceConsistentCasingInFileNames": true /* Ensure that casing is correct in imports. */,
/* Type Checking */
"strict": true, /* Enable all strict type-checking options. */
// "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied 'any' type. */
"strict": true /* Enable all strict type-checking options. */,
"noImplicitAny": true /* Enable error reporting for expressions and declarations with an implied 'any' type. */,
// "strictNullChecks": true, /* When type checking, take into account 'null' and 'undefined'. */
// "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */
// "strictBindCallApply": true, /* Check that the arguments for 'bind', 'call', and 'apply' methods match the original function. */
......
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