angularjs - In project created with "generator-angular-fullstack", how to filter list items (in server side)? -
i using generator-angular-fullstack:
npm install -g generator-angular-fullstack@3.2.0 created project settings: out of box create angularjs app express server.
client
? write markup with? html ? write stylesheets with? css ? angular router use? uirouter ? include bootstrap? yes ? include ui bootstrap? yes
server
? use data modeling? mongoose (mongodb) ? scaffold out authentication boilerplate? yes ? include additional oauth strategies? facebook, twitter ? use socket.io? yes
project
? write tests with? (use arrow keys)? jasmine
i created new endpoint "reservations":
yo angular-fullstack:endpoint reservation //server side yo angular-fullstack:route reservatios //client route updated new page populated "reservatios" list. gets "reservation" items.
i want reservations when user isadmin. in other cases (non users, or simple users) want active reservations.
so there simple reservation model:
var reservationschema = new schema({ date: { type: date, required: true } //,user: { // type : schema.objectid, // ref : 'user' //} }); reservations active, when "new date()" < reservatio.date.
so there these server side controllers: server\api\reservation\ reservation.controller.js
where these methods like:
// gets list of reservations export function index(req, res) { reservation.findasync() .then(responsewithresult(res)) .catch(handleerror(res)); } so created 1 more function activereservations:
// gets list of active reservations export function active(req, res) { reservation.findasync() .then(getonlyactive(res)) .then(responsewithresult(res)) .catch(handleerror(res)); } so added custom functio "getonlyactive(res)".
my question: should in function, how filter "res", active reservations? think filtering should done here. correct , point correct approatch how filtered items mongodb (using mongoose).
finally find out how filtering :)...
in example model (server\api\reservation\reservation.model.js):
var mongoose = require('bluebird').promisifyall(require('mongoose')); var schema = mongoose.schema; var reservationschema = new schema({ date: { type: date, required: true } }); export default mongoose.model('reservation', reservationschema); i added new static function it:
reservationschema.statics.findactive = function (next) { return this.find({"date": {"$gte": new date()}}); }; then added new function reservation controller (server\api\reservation\reservation.controller.js)
// gets list of active reservations export function active(req, res) { reservation.findactive() .execasync() .then(responsewithresult(res)) .catch(handleerror(res)); } finally added route active reservations (server\api\reservation\index.js):
router.get('/active', controller.active); now works :). check calling in browser:
http://localhost:9000/api/reservations // gets reservations http://localhost:9000/api/reservations/active // gets active reservations - "reservation.date > new date()".
Comments
Post a Comment