Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Band model and controller #2

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
14 changes: 14 additions & 0 deletions api/controllers/AlbumController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';

const album = require('../models/AlbumModel').instance;

class AlbumController {
getAlbum(req, res){
album.getAlbum(req.params.bandId)
.then(documents => res.json(documents))
.catch(error => res.json({error: error.message}));
}
}

exports.AlbumController = AlbumController;
exports.instance = new AlbumController;
14 changes: 14 additions & 0 deletions api/controllers/ArtistController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';

const artists = require('../models/ArtistModel').instance;

class ArtistController {
getArtists(req, res){
artists.getArtists(req.params.bandId)
.then(documents => res.json(documents))
.catch(error => res.json({error: error.message}));
}
}

exports.ArtistController = ArtistController;
exports.instance = new ArtistController;
19 changes: 19 additions & 0 deletions api/controllers/BandController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict';

const band = require('../models/BandModel').instance;

class BandController {
getBands (req, res){
band.getBands()
.then(documents => res.json(documents))
.catch(error => res.json({error: error.message}));
}
getBand(req, res){
band.getBand(req.params.bandId)
.then(documents => res.json(documents))
.catch(error => res.json({error: error.message}));
}
}

exports.BandController = BandController;
exports.instance = new BandController;
14 changes: 14 additions & 0 deletions api/controllers/DocumentController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';

const documents = require('../models/DocumentModel').instance;

class DocumentController {
getDocuments(req, res){
documents.getDocuments(req.params.trackId)
.then(documents => res.json(documents))
.catch(error => res.json({error: error.message}));
}
}

exports.DocumentController = DocumentController;
exports.instance = new DocumentController;
8 changes: 7 additions & 1 deletion api/controllers/TrackController.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ class TrackController {
.then(documents => res.json(documents))
.catch(error => res.json({error: error.message}));
}
getById (req, res) {
track.getById(req.params.id)
.then(tracks => res.json(tracks))
.catch(error => res.json({error: error.message}));

}
}
exports.TrackController = TrackController;
exports.instance = new TrackController();
exports.instance = new TrackController();
12 changes: 12 additions & 0 deletions api/models/AlbumModel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict';

const dataBase = require('../../database/Database').instance;
const docTypes = require('../../database/docTypes');

class AlbumModel {
getAlbum (id) {
return dataBase.findOne({docType: docTypes.ALBUM, _id:id})
}
}
module.exports.AlbumModel = AlbumModel;
module.exports.instance = new AlbumModel();
15 changes: 15 additions & 0 deletions api/models/ArtistModel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict';

const dataBase = require('../../database/Database').instance;
const docTypes = require('../../database/docTypes');

class ArtistModel {
getArtists() {
return dataBase.find({docType: docTypes.ARTIST})
}
getArtist(id) {
return dataBase.findOne({docType: docTypes.ARTIST, _id:id})
}
}
module.exports.ArtistModel = ArtistModel;
module.exports.instance = new ArtistModel;
15 changes: 15 additions & 0 deletions api/models/BandModel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict';

const dataBase = require('../../database/Database').instance;
const docTypes = require('../../database/docTypes')

class BandModel {
getBands() {
return dataBase.find({docType: docTypes.BAND})
}
getBand(id) {
return dataBase.findOne({docType: docTypes.BAND, _id:id})
}
}
module.exports.BandModel = BandModel;
module.exports.instance = new BandModel;
12 changes: 12 additions & 0 deletions api/models/DocumentModel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict';

const dataBase = require('../../database/Database').instance;
const docTypes = require('../../database/docTypes');

class DocumentModel {
getDocuments(id) {
return dataBase.findOne({docType: docTypes.COMMENT, _id:id})
}
}
module.exports.DocumentModel = DocumentModel;
module.exports.instance = new DocumentModel;
7 changes: 5 additions & 2 deletions api/models/TrackModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ const docTypes = require('../../database/docTypes');
class TrackModel {

getList () {
return dataBase.find({docType: docTypes.TRACK})
return dataBase.find({docType: docTypes.TRACK});
}
getById (id) {
return dataBase.findOne({docType: docTypes.TRACK, _id: id});
}
}
module.exports.TrackModel = TrackModel;
module.exports.instance = new TrackModel();
module.exports.instance = new TrackModel();
13 changes: 11 additions & 2 deletions api/router.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
const router = require('express').Router();
const colors = require('colors/safe');
const trackController = require('./controllers/TrackController').instance; //Sample controller

const bandsController = require('./controllers/BandController').instance;
const albumController = require('./controllers/AlbumController').instance;
const artistsController = require('./controllers/ArtistController').instance;
const documentsController = require('./controllers/DocumentController').instance;
// Specific router middleware that shows the request timestamp
router.use((req, res, next) => {
console.log(`${colors.green('Requesting: ')} ${req.method} ${req.path} -> Time: `, Date.now());
Expand All @@ -10,6 +13,12 @@ router.use((req, res, next) => {

// API Routes
router.get('/tracks', trackController.getList); //Sample route
router.get('/tracks/:id', trackController.getById);

router.get('/bands', bandsController.getBands); //Bands route
router.get('/bands/:bandId', bandsController.getBand); //Bands route
router.get('/bands/:bandId/albums', albumController.getAlbum); //Albun route
router.get('/bands/:bandId/artists', artistsController.getArtists); //Artists route
router.get('/tracks/:trackId/comments', documentsController.getDocuments); //Documents route

module.exports = router;
module.exports = router;
Loading