From d35db8944ed49f2a86896b269fbbbb6e7a3691f1 Mon Sep 17 00:00:00 2001 From: capitanwesler Date: Sun, 14 Mar 2021 14:33:55 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=92=A5=20Implementing=20the=20models=20fo?= =?UTF-8?q?r=20each=20collection?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/UtilsController.ts | 26 ++++++++++++++++++++ src/database/models/genre.model.ts | 19 +++++++++++++++ src/database/models/radiostation.model.ts | 19 +++++++++++++++ src/database/models/theme.model.ts | 29 +++++++++++++++++++++++ src/routes.ts | 1 + 5 files changed, 94 insertions(+) create mode 100644 src/database/models/genre.model.ts create mode 100644 src/database/models/radiostation.model.ts create mode 100644 src/database/models/theme.model.ts diff --git a/src/controllers/UtilsController.ts b/src/controllers/UtilsController.ts index 8c14d2d..9622044 100644 --- a/src/controllers/UtilsController.ts +++ b/src/controllers/UtilsController.ts @@ -3,6 +3,9 @@ import Parser from 'rss-parser'; import urls from '../utils/urls'; import { obtainPreviewNews } from '../utils/obtainPreviews'; import { requestGot } from '../utils/requestCall'; +import RadioStationModel, { + RadioStation, +} from '../database/models/radiostation.model'; /* UtilsController - controller to parse the @@ -277,4 +280,27 @@ export default class UtilsController { res.status(500).json({ message: 'Aruppi lost in the shell' }); } } + + async getRadioStations(req: Request, res: Response, next: NextFunction) { + let data: RadioStation[]; + + try { + data = await RadioStationModel.find(); + } catch (err) { + return next(err); + } + + const results: any[] = data.map((item: RadioStation) => { + return { + name: item.name, + url: item.url, + }; + }); + + if (results.length > 0) { + res.status(200).json({ stations: results }); + } else { + res.status(500).json({ message: 'Aruppi lost in the shell' }); + } + } } diff --git a/src/database/models/genre.model.ts b/src/database/models/genre.model.ts new file mode 100644 index 0000000..984d36b --- /dev/null +++ b/src/database/models/genre.model.ts @@ -0,0 +1,19 @@ +import { Document, model, Types, Schema } from 'mongoose'; + +/* + This is the model for each genre + of the directory, the genre model. +*/ + +export interface Genre extends Document { + name: string; + value: string; +} + +// Schema for the theme +const GenreSchema: Schema = new Schema({ + name: { type: String }, + value: { type: String }, +}); + +export default model('Genre', GenreSchema); diff --git a/src/database/models/radiostation.model.ts b/src/database/models/radiostation.model.ts new file mode 100644 index 0000000..2e5681f --- /dev/null +++ b/src/database/models/radiostation.model.ts @@ -0,0 +1,19 @@ +import { Document, model, Schema } from 'mongoose'; + +/* + This is the model for each radiostation + of the directory, the radiostation model. +*/ + +export interface RadioStation extends Document { + name: string; + url: string; +} + +// Schema for the theme +const RadioStationSchema: Schema = new Schema({ + name: { type: String }, + url: { type: String }, +}); + +export default model('RadioStation', RadioStationSchema); diff --git a/src/database/models/theme.model.ts b/src/database/models/theme.model.ts new file mode 100644 index 0000000..25bf68b --- /dev/null +++ b/src/database/models/theme.model.ts @@ -0,0 +1,29 @@ +import { Document, model, Types, Schema } from 'mongoose'; + +/* + This is the model for each theme + of the directory, the theme model. +*/ + +interface TInterface { + title: string; + video: string; + type: string; +} + +export interface Theme extends Document { + id: string; + title: string; + year: string; + themes: Types.Array; +} + +// Schema for the theme +const ThemeSchema: Schema = new Schema({ + id: { type: String }, + title: { type: String }, + year: { type: String }, + themes: [{ type: Object }], +}); + +export default model('Theme', ThemeSchema); diff --git a/src/routes.ts b/src/routes.ts index 9ee3ea7..de650af 100644 --- a/src/routes.ts +++ b/src/routes.ts @@ -97,5 +97,6 @@ routes.get('/api/v4/news', utilsController.getNews); routes.get('/api/v4/images/:title', utilsController.getImages); routes.get('/api/v4/videos/:channelId', utilsController.getVideos); routes.get('/api/v4/sectionedVideos/:type', utilsController.getSectionVideos); +routes.get('/api/v4/radio', utilsController.getRadioStations); export default routes;