Skip to content

Commit

Permalink
Add challenge map service
Browse files Browse the repository at this point in the history
  • Loading branch information
Berkeley Martinez authored and Berkeley Martinez committed Jul 29, 2016
1 parent f29545e commit 4514d39
Show file tree
Hide file tree
Showing 8 changed files with 227 additions and 42 deletions.
12 changes: 12 additions & 0 deletions common/models/block.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Observable } from 'rx';

export default function(Block) {
Block.on('dataSourceAttached', () => {
Block.findOne$ =
Observable.fromNodeCallback(Block.findOne, Block);
Block.findById$ =
Observable.fromNodeCallback(Block.findById, Block);
Block.find$ =
Observable.fromNodeCallback(Block.find, Block);
});
}
49 changes: 49 additions & 0 deletions common/models/block.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"name": "block",
"base": "PersistedModel",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
"superBlock": {
"type": "string",
"required": true,
"description": "The super block that this block belongs too"
},
"order": {
"type": "number",
"required": true,
"description": "the order in which this block appears"
},
"name": {
"type": "string",
"required": true,
"description": "The name of this block derived from the title, suitable for regex search"
},
"superOrder": {
"type": "number",
"required": true
},
"dashedName": {
"type": "string",
"required": true,
"description": "Generated from the title to be URL friendly"
},
"title": {
"type": "string",
"required": true,
"description": "The title of this block, suitable for display"
}
},
"validations": [],
"relations": {
"challenges": {
"type": "hasMany",
"model": "challenge",
"foreignKey": "blockId"
}
},
"acls": [],
"methods": {}
}
103 changes: 66 additions & 37 deletions seed/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,68 +5,97 @@ var adler32 = require('adler32');

var Rx = require('rx'),
_ = require('lodash'),
utils = require('../server/utils'),
getChallenges = require('./getChallenges'),
app = require('../server/server');


var dasherize = utils.dasherize;
var nameify = utils.nameify;
var Observable = Rx.Observable;
var Challenge = app.models.Challenge;
var destroy = Rx.Observable.fromNodeCallback(Challenge.destroyAll, Challenge);
var create = Rx.Observable.fromNodeCallback(Challenge.create, Challenge);

destroy()
.flatMap(function() { return Rx.Observable.from(getChallenges()); })
var destroyChallenges =
Observable.fromNodeCallback(Challenge.destroyAll, Challenge);
var createChallenges =
Observable.fromNodeCallback(Challenge.create, Challenge);

var Block = app.models.Block;
var destroyBlocks = Observable.fromNodeCallback(Block.destroyAll, Block);
var createBlocks = Observable.fromNodeCallback(Block.create, Block);

Observable.combineLatest(
destroyChallenges(),
destroyBlocks()
)
.last()
.flatMap(function() { return Observable.from(getChallenges()); })
.flatMap(function(challengeSpec) {
var order = challengeSpec.order;
var block = challengeSpec.name;
var blockName = challengeSpec.name;
var superBlock = challengeSpec.superBlock;
var superOrder = challengeSpec.superOrder;
var isBeta = !!challengeSpec.isBeta;
var isComingSoon = !!challengeSpec.isComingSoon;
var fileName = challengeSpec.fileName;
var helpRoom = challengeSpec.helpRoom || 'Help';

console.log('parsed %s successfully', block);
console.log('parsed %s successfully', blockName);

// challenge file has no challenges...
if (challengeSpec.challenges.length === 0) {
return Rx.Observable.just([{ block: 'empty ' + block }]);
return Rx.Observable.just([{ block: 'empty ' + blockName }]);
}

var challenges = challengeSpec.challenges
.map(function(challenge, index) {
challenge.name = challenge.title.replace(/[^a-zA-Z0-9\s]/g, '');
var block = {
title: blockName,
name: nameify(blockName),
dashedName: dasherize(blockName),
superOrder: superOrder,
superBlock: superBlock,
order: order
};

return createBlocks(block)
.map(block => {
console.log('successfully created %s block', block.name);

challenge.dashedName = challenge.name
.toLowerCase()
.replace(/\:/g, '')
.replace(/\s/g, '-');
return challengeSpec.challenges
.map(function(challenge, index) {
challenge.name = nameify(challenge.title);

challenge.checksum = adler32.sum(
Buffer(challenge.title +
JSON.stringify(challenge.description) +
JSON.stringify(challenge.challengeSeed) +
JSON.stringify(challenge.tests)));
challenge.dashedName = dasherize(challenge.name);

challenge.fileName = fileName;
challenge.helpRoom = helpRoom;
challenge.order = order;
challenge.suborder = index + 1;
challenge.block = block;
challenge.isBeta = challenge.isBeta || isBeta;
challenge.isComingSoon = challenge.isComingSoon || isComingSoon;
challenge.time = challengeSpec.time;
challenge.superOrder = superOrder;
challenge.superBlock = superBlock
.split('-')
.map(function(word) {
return _.capitalize(word);
})
.join(' ');
challenge.checksum = adler32.sum(
Buffer(
challenge.title +
JSON.stringify(challenge.description) +
JSON.stringify(challenge.challengeSeed) +
JSON.stringify(challenge.tests)
)
);

return challenge;
});
challenge.fileName = fileName;
challenge.helpRoom = helpRoom;
challenge.order = order;
challenge.suborder = index + 1;
challenge.block = blockName;
challenge.blockId = block.id;
challenge.isBeta = challenge.isBeta || isBeta;
challenge.isComingSoon = challenge.isComingSoon || isComingSoon;
challenge.time = challengeSpec.time;
challenge.superOrder = superOrder;
challenge.superBlock = superBlock
.split('-')
.map(function(word) {
return _.capitalize(word);
})
.join(' ');

return create(challenges);
return challenge;
});
})
.flatMap(challenges => createChallenges(challenges));
})
.subscribe(
function(challenges) {
Expand Down
3 changes: 3 additions & 0 deletions server/boot/a-services.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@ import Fetchr from 'fetchr';
import getHikesService from '../services/hikes';
import getJobServices from '../services/job';
import getUserServices from '../services/user';
import getMapServices from '../services/map';

export default function bootServices(app) {
const hikesService = getHikesService(app);
const jobServices = getJobServices(app);
const userServices = getUserServices(app);
const mapServices = getMapServices(app);

Fetchr.registerFetcher(hikesService);
Fetchr.registerFetcher(jobServices);
Fetchr.registerFetcher(userServices);
Fetchr.registerFetcher(mapServices);
app.use('/services', Fetchr.middleware());
}
4 changes: 4 additions & 0 deletions server/model-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,9 @@
"flyer": {
"dataSource": "db",
"public": true
},
"block": {
"dataSource": "db",
"public": true
}
}
5 changes: 1 addition & 4 deletions server/services/hikes.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import debugFactory from 'debug';
import assign from 'object.assign';

const debug = debugFactory('fcc:services:hikes');

Expand All @@ -19,9 +18,7 @@ export default function hikesService(app) {

debug('dashedName', dashedName);
if (dashedName) {
assign(query.where, {
dashedName: { like: dashedName, options: 'i' }
});
query.where.dashedName = { like: dashedName, options: 'i' };
}
debug('query', query);
Challenge.find(query, (err, hikes) => {
Expand Down
84 changes: 84 additions & 0 deletions server/services/map.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { Observable } from 'rx';
import { Schema, valuesOf, arrayOf, normalize } from 'normalizr';
import { nameify, dasherize } from '../utils';

const challenge = new Schema('challenge', { idAttribute: 'dashedName' });
const block = new Schema('block', { idAttribute: 'dashedName' });
const superBlock = new Schema('superBlock', { idAttribute: 'dashedName' });

block.define({
challenges: arrayOf(challenge)
});

superBlock.define({
blocks: arrayOf(block)
});

const mapSchema = valuesOf(superBlock);

/*
* interface ChallengeMap {
* result: [superBlockDashedName: String]
* entities: {
* superBlock: {
* [superBlockDashedName: String]: {
* blocks: [blockDashedName: String]
* }
* },
* block: {
* [blockDashedName: String]: {
* challenges: [challengeDashedName: String]
* }
* },
* challenge: {
* [challengeDashedName: String]: Challenge
* }
* }
* }
*/
function cachedMap(Block) {
const query = {
include: 'challenges',
order: ['superOrder ASC', 'order ASC']
};
return Block.find$(query)
.flatMap(blocks => Observable.from(blocks.map(block => block.toJSON())))
.reduce((map, block) => {
if (map[block.superBlock]) {
map[block.superBlock].blocks.push(block);
} else {
map[block.superBlock] = {
title: block.superBlock,
order: block.superOrder,
name: nameify(block.superBlock),
dashedName: dasherize(block.superBlock),
blocks: [block]
};
}
return map;
}, {})
.map(map => normalize(map, mapSchema))
.map(map => {
const result = Object.keys(map.result).reduce((result, supName) => {
const index = map.entities.superBlock[supName].order;
result[index] = supName;
return result;
}, []);
return {
...map,
result
};
})
.shareReplay();
}

export default function mapService(app) {
const Block = app.models.Block;
const challengeMap$ = cachedMap(Block);
return {
name: 'map',
read: (req, resource, params, config, cb) => {
return challengeMap$.subscribe(map => cb(null, map), cb);
}
};
}
9 changes: 8 additions & 1 deletion server/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,14 @@ module.exports = {
return ('' + name)
.toLowerCase()
.replace(/\s/g, '-')
.replace(/[^a-z0-9\-\.]/gi, '');
.replace(/[^a-z0-9\-\.]/gi, '')
.replace(/\:/g, '');
},

nameify: function nameify(str) {
return ('' + str)
.replace(/[^a-zA-Z0-9\s]/g, '')
.replace(/\:/g, '');
},

unDasherize: function unDasherize(name) {
Expand Down

0 comments on commit 4514d39

Please sign in to comment.