First add of geolets service.

This commit is contained in:
Linus Miller 2016-07-14 18:54:01 +02:00
parent fc59ed3835
commit 27811515b4
4 changed files with 151 additions and 1 deletions

View File

@ -8,4 +8,6 @@ module.exports = [
next();
} ]
];
].concat(
require('./services/geolets/routes')
);

View File

@ -0,0 +1,118 @@
'use strict';
const mw = {
formatQuery: require('warepot/format-query'),
paginate: require('warepot/paginate')
};
const mongoose = require('mongoose');
const Geolet = require('./model');
function create(req, res, next) {
Geolet.create(req.body, function (err, geolet) {
if (err) return next(err);
res.status(201).json(geolet);
});
}
function find(req, res, next) {
const page = Math.max(0, req.query.page) || 0;
const perPage = Math.max(0, req.query.limit) || res.locals.perPage;
const query = Geolet.find(_.omit(req.query, 'limit', 'sort', 'page'),
null,
{ sort: req.query.sort || '-dateCreated', lean: true });
if (perPage)
query.limit(perPage).skip(perPage * page);
query.exec(function (err, geolets) {
res.locals.geolets = geolets;
next(err);
});
}
function getAll(req, res, next) {
Geolet.find({}, function (err, geolets) {
if (err) return next(err);
res.locals.geolets = geolets;
next();
});
}
function findById(req, res, next) {
if (req.params.id === 'new') return next();
const query = {};
query[mongoose.Types.ObjectId.isValid(req.params.id) ? '_id' : '_hid'] = req.params.id;
Geolet.findOne(query, function (err, geolet) {
if (err) return next(err);
res.locals.geolet = geolet;
next();
});
}
function patch(req, res, next) {
const query = {};
query[mongoose.Types.ObjectId.isValid(req.params.id) ? '_id' : '_hid'] = req.params.id;
Geolet.findOne(query, function (err, geolet) {
delete req.body._id;
delete req.body.__v;
_.extend(geolet, req.body);
return geolet.save(function (err) {
if (err) return next(err);
return res.status(200).json(geolet);
});
});
}
function put(req, res, next) {
const query = {};
query[mongoose.Types.ObjectId.isValid(req.params.id) ? '_id' : '_hid'] = req.params.id;
Geolet.findOne(query, function (err, geolet) {
_.difference(_.keys(geolet.toObject()), _.keys(req.body)).forEach(function (key) {
geolet[key] = undefined;
});
_.extend(geolet, _.omit(req.body, '_id', '__v'));
return geolet.save(function (err) {
if (err) return next(err);
return res.status(200).json(geolet);
});
});
}
function remove(req, res, next) {
return Geolet.findById(req.params.id, function (err, geolet) {
if (err) return next(err);
return geolet.remove(function (err) {
if (err) return next(err);
return res.sendStatus(200);
});
});
}
module.exports = {
create,
find,
getAll,
findById,
formatQuery: mw.formatQuery([ 'page', 'limit', 'sort' ]),
paginate: mw.paginate(Geolet, 10),
patch,
put,
remove
};

View File

@ -0,0 +1,18 @@
'use strict';
const mongoose = require('mongoose');
const schema = {
latitude: Number,
longitude: Number,
title: String,
description: String,
images: [],
tags: []
};
const GeoletSchema = new mongoose.Schema(schema);
GeoletSchema.plugin(require('mongopot/plugins/base'));
module.exports = mongoose.model('Geolet', GeoletSchema);

View File

@ -0,0 +1,12 @@
'use strict';
const mw = require('./middleware');
module.exports = [
[ '/api/geolets/', 'get', [ mw.formatQuery, mw.paginate, mw.find ]],
[ '/api/geolets/', 'post', [ mw.create ]],
[ '/api/geolets/:id', 'get', [ mw.findById ]],
[ '/api/geolets/:id', 'put', [ mw.put ]],
[ '/api/geolets/:id', 'patch', [ mw.patch ]],
[ '/api/geolets/:id', 'delete', [ mw.remove ]]
];