Skip to content

Commit

Permalink
add best practice, error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Yash Walia authored and Yash Walia committed Apr 27, 2021
1 parent bd5b303 commit 7727ef8
Show file tree
Hide file tree
Showing 4 changed files with 491 additions and 95 deletions.
166 changes: 84 additions & 82 deletions loaders/getArticle.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
'use strict';

var quotes = require('../models/quotes');
var books = require('../models/books');
var movies = require('../models/movies');
var series = require('../models/series');
var shortStories = require('../models/shortStories');
var comics = require('../models/comics');
var quotes = require('../models/quotes'),
books = require('../models/books'),
movies = require('../models/movies'),
series = require('../models/series'),
shortStories = require('../models/shortStories'),
comics = require('../models/comics');

module.exports = {
getRandom: function getRandom(number, type) {
if (type == 'quotes'){
function getRandom(type, number) {
switch (type) {
case 'quotes':
var limit = number > quotes.length ? quotes.length : number;
var out = new Array(limit);
var quote;
Expand All @@ -21,8 +21,7 @@ module.exports = {
out[i] = quote;
}
return out;
}
else if (type == 'books') {
case 'books':
var limit = number > books.length ? books.length : number;
var out = new Array(limit);
var book;
Expand All @@ -33,8 +32,7 @@ module.exports = {
out[i] = book;
}
return out;
}
else if (type == 'movies') {
case 'movies':
var limit = number > movies.length ? movies.length : number;
var out = new Array(limit);
var movie;
Expand All @@ -45,8 +43,7 @@ module.exports = {
out[i] = movie;
}
return out;
}
else if (type == 'series') {
case 'series':
var limit = number > series.length ? series.length : number;
var out = new Array(limit);
var serie;
Expand All @@ -57,8 +54,7 @@ module.exports = {
out[i] = serie;
}
return out;
}
else if (type == 'stories') {
case 'stories':
var limit = number > shortStories.length ? shortStories.length : number;
var out = new Array(limit);
var story;
Expand All @@ -69,8 +65,7 @@ module.exports = {
out[i] = story;
}
return out;
}
else if (type == 'comics') {
case 'comics':
var limit = number > comics.length ? comics.length : number;
var out = new Array(limit);
var comic;
Expand All @@ -81,74 +76,81 @@ module.exports = {
out[i] = comic;
}
return out;
default:
return { "Error": "Bad Request" }
}
}

function getArticle(type, id) {
if (type == 'quotes') {
if (id < 0) {
return quotes[1]
}
},
getArticleFromId: function getArticle(id, type) {
if (type == 'quotes') {
if (id < 0) {
return quotes[1]
}
else if (id > quotes.length) {
return quotes[quotes.length - 1]
}
else {
return quotes[id - 1]
}
else if (id > quotes.length) {
return quotes[quotes.length - 1]
}
else if (type == 'books') {
if (id < 0) {
return books[1]
}
else if (id > books.length) {
return books[books.length - 1]
}
else {
return books[id - 1]
}
else {
return quotes[id - 1]
}
else if (type == 'movies') {
if (id < 0) {
return movies[1]
}
else if (id > movies.length) {
return movies[movies.length - 1]
}
else {
return movies[id - 1]
}
}
else if (type == 'books') {
if (id < 0) {
return books[1]
}
else if (type == 'series') {
if (id < 0) {
return series[1]
}
else if (id > series.length) {
return series[series.length - 1]
}
else {
return series[id - 1]
}
else if (id > books.length) {
return books[books.length - 1]
}
else if (type == 'stories') {
if (id < 0) {
return shortStories[1]
}
else if (id > stories.length) {
return shortStories[shortStories.length - 1]
}
else {
return shortStories[id - 1]
}
else {
return books[id - 1]
}
else if (type == 'comics') {
if (id < 0) {
return comics[1]
}
else if (id > comics.length) {
return comics[comics.length - 1]
}
else {
return comics[id - 1]
}
}
else if (type == 'movies') {
if (id < 0) {
return movies[1]
}
else if (id > movies.length) {
return movies[movies.length - 1]
}
else {
return movies[id - 1]
}
}
else if (type == 'series') {
if (id < 0) {
return series[1]
}
else if (id > series.length) {
return series[series.length - 1]
}
else {
return series[id - 1]
}
}
};
else if (type == 'stories') {
if (id < 0) {
return shortStories[1]
}
else if (id > stories.length) {
return shortStories[shortStories.length - 1]
}
else {
return shortStories[id - 1]
}
}
else if (type == 'comics') {
if (id < 0) {
return comics[1]
}
else if (id > comics.length) {
return comics[comics.length - 1]
}
else {
return comics[id - 1]
}
}
else {
return { "Error": "Bad Request"}
}
}

module.exports = { getRandom, getArticle };
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"start": "node server.js"
},
"dependencies": {
"cors": "^2.8.5",
"express": "^4.17.1"
}
}
38 changes: 25 additions & 13 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,37 @@
'use strict';
const express = require('express');
const cors = require('cors');
const app = express();
const PORT = process.env.PORT || 5000;
// const CONNECTION_URL = 'mongodb+srv://yashwalia:[email protected]/myFirstDatabase?retryWrites=true&w=majority'
const getArticle = require('./loaders/getArticle');

var express = require('express');
var getArticle = require('./loaders/getArticle')
var app = express();
var port = process.env.PORT || 3001;
// app.use(bodyParser.json({ limit: "30mb", extended: true }));
// app.use(bodyParser.urlencoded({ limit: "30mb", extended: true }));
app.use(cors());

app.all('*', function (req, res, next) {
res.set('Access-Control-Allow-Origin', '*');
next();
});

app.get('/:type/:num?', function (req, res) {
console.log(req.params);
res.send(getArticle.getRandom(req.params.num || 1, req.params.type));
res.status(200).send(getArticle.getRandom(req.params.type, req.params.num || 1));
console.log(req.params)
});

app.get('/:type/id/:id?', function (req, res) {
console.log(req.params);
res.send(getArticle.getArticleFromId(req.params.id || 1, req.params.type));
res.status(200).send(getArticle.getArticle(req.params.type ,req.params.id || 1));
});

app.get('/', (req, res) => {
res.status(200).status(200).send("Welcome to the DUNE API")
});

app.listen(port, function () {
console.log('Server running on port', port);
})
app.listen(PORT, function () {
console.log('Server running on port', PORT);
});

// mongoose.connect(CONNECTION_URL, { useNewUrlParser: true, useUnifiedTopology: true })
// .then(() => app.listen(PORT, () => console.log(`Server running on port: ${PORT}`)))
// .catch((error) => console.log(error.message))

// mongoose.set('useFindAndModify', false);
Loading

0 comments on commit 7727ef8

Please sign in to comment.