UNPKG

nodulator

Version:

Complete NodeJS Framework for Restfull APIs

82 lines (61 loc) 2.39 kB
const N = require('..') class WeaponRoute extends N.Route { Config () { this.Get(() => this.resource.List()); } } const Weapon = N('weapons', WeaponRoute, { schema: 'strict' }); Weapon.Field('power', 'int').Default(10); class Unit extends N('unit', { abstract: true, schema: 'strict' }) { LevelUp () { return this.Set({ level: this.level + 1 }); } Attack (targetId) { if (!this.Weapon) throw('No weapon'); const Target = this._type === 'player' ? Monster : Player; return Target .Fetch(targetId) .Set((target) => target.life -= this.Weapon.power) ; } } Unit.Field('level', 'int') .Default(1); Unit.Field('life', 'int') .Default(100); Unit.MayBelongsTo(Weapon); class UnitRoute extends N.Route.Collection { Config () { super.Config(); this.Put('/:id/levelup', (req) => req.instance.LevelUp()); this.Put('/:id/attack/:targetId', (req) => req.instance.Attack(parseInt(req.params.targetId))); } } const Player = Unit.Extend('players', UnitRoute); const Monster = Unit.Extend('monsters', UnitRoute); // Exemple seed: Player .Create() .Add(Weapon.Create({ power: 25 })) ; Monster .Create() .Add(Weapon.Create()) ; // Created routes : // - GET /api/1/players => Get all players // - GET /api/1/players/:id => Get player with given id // - POST /api/1/players => Create a player // - PUT /api/1/players/:id => Modify the player with given id // - DELETE /api/1/players/:id => Delete the given player // - PUT /api/1/players/:id/levelup => LevelUp the given player // - PUT /api/1/players/:id/attack/:targetId => Attack the given monster // // - GET /api/1/monsters => Get all monsters // - GET /api/1/monsters/:id => Get monster with given id // - POST /api/1/monsters => Create a monster // - PUT /api/1/monsters/:id => Modify the monster with given id // - DELETE /api/1/monsters/:id => Delete the given monster // - PUT /api/1/monsters/:id/levelup => LevelUp the given monster // - PUT /api/1/monsters/:id/attack/:targetId => Attack the given player // // - GET /api/1/weapons => Get all weapons