Skip to content

Commit

Permalink
Adds API v2 which includes a bulkier activities API and facts, riddle…
Browse files Browse the repository at this point in the history
…s, and websites APIs
  • Loading branch information
drewthoennes committed May 30, 2020
1 parent 5919645 commit d1d0577
Show file tree
Hide file tree
Showing 54 changed files with 2,429 additions and 585 deletions.
197 changes: 0 additions & 197 deletions activities.json

This file was deleted.

196 changes: 196 additions & 0 deletions db/activities.json

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions db/facts.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{"fact":"The first computer was invented in the 1940s.","source":"https://bestlifeonline.com/random-fun-facts","key":"8929851"}
{"fact":"The unicorn is the national animal of Scotland.","source":"https://bestlifeonline.com/random-fun-facts/","key":"4920184"}
{"fact":"Playing the accordion was once required for teachers in North Korea.","source":"https://bestlifeonline.com/random-fun-facts/","key":"1848104"}
{"fact":"Water makes different pouring sounds depending on its temperature.","source":"https://bestlifeonline.com/random-fun-facts/","key":"2562345"}
4 changes: 4 additions & 0 deletions db/riddles.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{"question":"You live in a one story house made entirely of redwood. What color would the stairs be?","answer":"What stairs? You live in a one-story house.","difficulty":"easy","source":"https://www.riddles.com/","key":"3684252"}
{"question":"What has six faces, but does not wear makeup, has twenty-one eyes, but cannot see? What is it?","answer":"A die.","difficulty":"easy","source":"https://www.riddles.com/","key":"7643252"}
{"question":"What can you catch but never throw?","answer":"A cold.","difficulty":"normal","source":"https://www.riddles.com/","key":"7974324"}
{"question":"Who is that with a neck and no head, two arms and no hands? What is it?","answer":"A shirt.","difficulty":"hard","source":"https://www.riddles.com/","key":"2347324"}
3 changes: 3 additions & 0 deletions db/websites.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{"url":"https://weirdorconfusing.com","description":"Random random being sold.","key":"4298747"}
{"url":"http://eelslap.com","description":"Eel slap.","key":"8738873"}
{"url":"https://heeeeeeeey.com","description":"Hey there.","key":"9723037"}
43 changes: 43 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"start": "export NODE_ENV=production; npm run build && node server.js",
"clean": "rm -r dist/*",
"test-i": "mocha test/backend/integration --timeout 60000",
"test-db": "mocha test/backend/db --timeout 60000",
"test-db": "mocha test/db --timeout 60000",
"test": "npm run test-i && npm run test-db"
},
"license": "MIT",
Expand All @@ -21,6 +21,7 @@
"@s": "./scripts"
},
"dependencies": {
"@hapi/joi": "^17.1.1",
"body-parser": "^1.19.0",
"chalk": "^4.0.0",
"dotenv": "^8.2.0",
Expand Down
10 changes: 10 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ app.get('/favicon.ico', (req, res) => {
// Backend API routes
app.use(require('./src/backend/routes')());

// Catch any errors
app.use((err, req, res, next) => {
if (err) {
res.error(`There was an error parsing the request: ${err}`);
return;
}

next();
});

// Frontend endpoints
app.use(express.static(__dirname + '/dist'));
app.use('/', express.static(__dirname + '/dist'));
Expand Down
23 changes: 23 additions & 0 deletions src/backend/controllers/activities.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const Activity = require('@b/models/Activity');

exports.findActivity = params => {
return Activity.findOne(params).then(activity => {
if (!activity) {
throw new Error('No activities found with the specified parameters');
}

return activity;
});
};

exports.findRandomActivity = params => {
return Activity.countDocuments(params).then(count => {
if (!count || count === 0) throw new Error('No activity found with the specified parameters');

return Activity.findOne(params).skip(Math.floor(Math.random() * count));
}).then(activity => {
if (!activity) throw new Error('No activity found with the specified parameters');

return activity;
});
};
23 changes: 23 additions & 0 deletions src/backend/controllers/facts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const Fact = require('@b/models/Fact');

exports.findFactByKey = key => {
return Fact.findOne({'key': key}).then(fact => {
if (!fact) {
throw new Error('No facts found with the specified parameters');
}

return fact;
});
};

exports.findRandomFact = () => {
return Fact.countDocuments().then(count => {
if (!count || count === 0) throw new Error('No facts found');

return Fact.findOne().skip(Math.floor(Math.random() * count));
}).then(fact => {
if (!fact) throw new Error('No facts found');

return fact;
});
};
23 changes: 23 additions & 0 deletions src/backend/controllers/riddles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const Riddle = require('@b/models/Riddle');

exports.findRiddle = params => {
return Riddle.findOne(params).then(riddle => {
if (!riddle) {
throw new Error('No riddles found with the specified parameters');
}

return riddle;
});
};

exports.findRandomRiddle = params => {
return Riddle.countDocuments(params).then(count => {
if (!count || count === 0) throw new Error('No riddles found with the specified query');

return Riddle.findOne(params).skip(Math.floor(Math.random() * count));
}).then(riddle => {
if (!riddle) throw new Error('No riddles found with the specified query');

return riddle;
});
};
Empty file.
File renamed without changes.
23 changes: 23 additions & 0 deletions src/backend/controllers/websites.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const Website = require('@b/models/Website');

exports.findWebsiteByKey = key => {
return Website.findOne({'key': key}).then(website => {
if (!website) {
throw new Error('No websites found with the specified parameters');
}

return website;
});
};

exports.findRandomWebsite = () => {
return Website.countDocuments().then(count => {
if (!count || count === 0) throw new Error('No websites found');

return Website.findOne().skip(Math.floor(Math.random() * count));
}).then(website => {
if (!website) throw new Error('No websites found');

return website;
});
};
17 changes: 5 additions & 12 deletions src/backend/keen/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,13 @@ const client = new Keen({
writeKey: process.env.KEEN_WRITE_KEY
});

exports.logActivity = (req, params) => {
exports.logQuery = (type, query) => {
if (process.env.NODE_ENV === 'dev') return;

return client.recordEvent('endpoints', {
'protocol': req.protocol || '',
'key': params.key || '',
'accessibility': params.accessibility || '',
'type': params.type || '',
'participants': params.participants || '',
'price': params.price || ''
return client.recordEvent('query', {
type,
query
}, err => {
if (err) {
console.log(err);
return;
}
if (err) console.log(err);
});
};
10 changes: 10 additions & 0 deletions src/backend/middleware/express.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module.exports = function(router) {
// Add res.error method for error reporting
router.use((req, res, next) => {
res.error = function(err) {
res.json({'error': err});
};

next();
});
}
24 changes: 24 additions & 0 deletions src/backend/middleware/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const expressMiddleware = require('./express');

exports.validate = (schema) => {
return (req, res, next) => {
let err = schema.validate(req.body).error;
if (err != null) {
res.error(`Invalid fields: ${err}`);
return;
}

next();
};
}

exports.express = function(router) {
// Add res.error method for error reporting
router.use((req, res, next) => {
res.error = function(err) {
res.json({'error': err});
};

next();
});
}
43 changes: 30 additions & 13 deletions src/backend/models/Activity.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,52 @@
const mongoose = require('mongoose');

var ActivitySchema = new mongoose.Schema({
const activitySchema = new mongoose.Schema({
activity: {
type: String,
trim: true,
required: true
},
accessibility: { // 0.0 - 1.0
type: Number
},
type: { // Sport, education, entertainment, social, ...
type: String
type: {
type: String,
enum: ['charity', 'cooking', 'music', 'diy', 'education', 'social', 'busywork', 'recreational', 'relaxation'],
required: true
},
participants: { // 1 - n
type: Number
type: Number,
required: true
},
price: { // 0.0 - 1.0
type: Number
type: Number,
required: true
},
link: { // URL to resource
type: String
availability: { // 0.0 - 1.0
type: Number,
required: true
},
key: {
accessibility: {
type: String,
enum: ['Few to no challenges', 'Minor challenges', 'Major challenges']
},
duration: {
type: String,
enum: ['minutes', 'hours', 'days', 'weeks'],
default: 'minutes',
required: true
},
enabled: {
kidFriendly: {
type: Boolean,
default: false,
required: true
},
link: { // URL to resource
type: String
},
key: {
type: String,
required: true
}
}, {
collection: 'activities'
});

module.exports = mongoose.model('Activity', ActivitySchema);
module.exports = mongoose.model('Activity', activitySchema);
19 changes: 19 additions & 0 deletions src/backend/models/Fact.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const mongoose = require('mongoose');

const factSchema = new mongoose.Schema({
fact: {
type: String,
required: true
},
source: {
type: String
},
key: {
type: String,
required: true
}
}, {
collection: 'facts'
});

module.exports = mongoose.model('Fact', factSchema);
29 changes: 29 additions & 0 deletions src/backend/models/Riddle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const mongoose = require('mongoose');

const riddleSchema = new mongoose.Schema({
question: {
type: String,
required: true
},
answer: {
type: String,
required: true
},
difficulty: {
type: String,
enum: ['easy', 'normal', 'hard'],
default: 'normal',
required: true
},
source: {
type: String
},
key: {
type: String,
required: true
}
}, {
collection: 'riddles'
});

module.exports = mongoose.model('Riddle', riddleSchema);
Loading

0 comments on commit d1d0577

Please sign in to comment.