Created
December 8, 2014 14:33
-
-
Save moonglum/bf9dd6d1b4ba02fe24b7 to your computer and use it in GitHub Desktop.
Foxx Example App from the Blog Series on the ArangoDB Blog.
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
/*jslint indent: 2, nomen: true, maxlen: 100 */ | |
/*global require, applicationContext, Joi */ | |
(function () { | |
'use strict'; | |
var FoxxGenerator = require('foxx_generator').Generator, | |
Joi = require('joi'), | |
generator; | |
generator = new FoxxGenerator('books', { | |
mediaType: 'application/vnd.siren+json', | |
applicationContext: applicationContext, | |
}); | |
/** Get the list of all books | |
* | |
* Get a list of all books that are saved in the system. | |
*/ | |
generator.defineTransition('books'); | |
/** Get a list of all users | |
* | |
* Get a list of all users that are saved in the system. | |
*/ | |
generator.defineTransition('users'); | |
/** Create a new book | |
* | |
* Add a book to the list of existing books and get a representation | |
* of this newly created book | |
*/ | |
generator.defineTransition('createBook', { | |
type: 'connect', | |
parameters: { | |
title: Joi.string() | |
} | |
}); | |
/** Get to the detail view on the item | |
* | |
* Show the detail view of this specific item. | |
*/ | |
generator.defineTransition('item'); | |
/** Show people that liked this book | |
* | |
*/ | |
generator.defineTransition('likedBy', { | |
to: 'many' | |
}); | |
/** Add people that like this book | |
* | |
*/ | |
generator.defineTransition('addLikes', { | |
type: 'connect', | |
as: 'likedBy', | |
to: 'many' | |
}); | |
/** Show a list of all likes of this book | |
* | |
*/ | |
generator.defineTransition('likes'); | |
generator.addStartState({ | |
transitions: [ | |
{ to: 'books', via: 'books' }, | |
{ to: 'users', via: 'users' } | |
] | |
}); | |
generator.addState('books', { | |
type: 'repository', | |
contains: 'book', | |
transitions: [ | |
{ to: 'book', via: 'item' }, | |
{ to: 'book', via: 'createBook' } | |
] | |
}); | |
generator.addState('users', { | |
type: 'repository', | |
contains: 'user', | |
transitions: [ | |
{ to: 'user', via: 'item' } | |
] | |
}); | |
generator.addState('book', { | |
type: 'entity', | |
parameterized: true, | |
containedIn: 'books', | |
attributes: { | |
title: Joi.string().required() | |
}, | |
transitions: [ | |
{ to: 'likes', via: 'likes' }, | |
{ to: 'user', via: 'likedBy' }, | |
{ to: 'user', via: 'addLikes' } | |
] | |
}); | |
generator.addState('user', { | |
type: 'entity', | |
parameterized: true, | |
containedIn: 'users', | |
attributes: { | |
name: Joi.string().required() | |
}, | |
transitions: [ | |
] | |
}); | |
var usersForBook = function (book) { | |
var db = require('org/arangodb').db, | |
query; | |
query = "FOR user " + | |
"IN GRAPH_NEIGHBORS('books', { '_id': '" + book.get('_id') + "' }) " + | |
"RETURN user.vertex"; | |
return db._query(query).toArray(); | |
}; | |
generator.addState('likes', { | |
type: 'service', | |
superstate: 'book', | |
verb: 'get', | |
action: function (req, res, opts) { | |
var book = opts.superstate.entity; | |
res.json({ | |
'properties': { | |
'bookKey': book.get('_key'), | |
'likes': usersForBook(book) | |
}, | |
'links': [] | |
}); | |
} | |
}); | |
generator.generate(); | |
}()); |
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
{ | |
"name": "books", | |
"version": "0.0.1", | |
"description": "Example on the blog", | |
"author": "Lucas Dohmen", | |
"license": "Apache 2 License", | |
"contributors": [ | |
], | |
"repository": { | |
}, | |
"keywords": [ | |
], | |
"controllers": { | |
"/api": "books.js" | |
}, | |
"rootElement": true, | |
"assets": {}, | |
"files": {}, | |
"lib": "." | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment