forked from freeCodeCamp/freeCodeCamp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Berkeley Martinez
authored and
Berkeley Martinez
committed
Jul 29, 2016
1 parent
f29545e
commit 4514d39
Showing
8 changed files
with
227 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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": {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -70,5 +70,9 @@ | |
"flyer": { | ||
"dataSource": "db", | ||
"public": true | ||
}, | ||
"block": { | ||
"dataSource": "db", | ||
"public": true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters