76 lines
1.9 KiB
JavaScript
76 lines
1.9 KiB
JavaScript
|
const express = require('express')
|
||
|
const router = express.Router()
|
||
|
const Properties = require('../middleware/Properties')
|
||
|
|
||
|
module.exports = function (passport) {
|
||
|
const basePath = '/api/properties/'
|
||
|
const itemPath = basePath + ':propertyId'
|
||
|
|
||
|
router.route(basePath)
|
||
|
.get(
|
||
|
function (req, res) {
|
||
|
Properties.getAll(req, function (err, items) {
|
||
|
if (err) {
|
||
|
res.status(items || 500).send(err.message)
|
||
|
} else {
|
||
|
res.status(200).json(items)
|
||
|
}
|
||
|
})
|
||
|
}
|
||
|
)
|
||
|
.post(
|
||
|
passport.authenticate(['basic-auth']),
|
||
|
function (req, res) {
|
||
|
const property = new Properties()
|
||
|
property.createOne(req, function (err, item) {
|
||
|
if (err) {
|
||
|
res.status(item || 500).send(err.message)
|
||
|
} else {
|
||
|
res.status(201).json(item)
|
||
|
}
|
||
|
})
|
||
|
}
|
||
|
)
|
||
|
|
||
|
router.route(itemPath)
|
||
|
.get(
|
||
|
function (req, res) {
|
||
|
Properties.getOne(req, function (err, item) {
|
||
|
if (err) {
|
||
|
res.status(item || 500).send(err.message)
|
||
|
} else {
|
||
|
res.status(200).json(item)
|
||
|
}
|
||
|
})
|
||
|
}
|
||
|
)
|
||
|
.patch(
|
||
|
passport.authenticate(['basic-auth']),
|
||
|
function (req, res) {
|
||
|
const property = new Properties()
|
||
|
property.patchOne(req, function (err, item) {
|
||
|
if (err) {
|
||
|
res.status(item || 500).send(err.message)
|
||
|
} else {
|
||
|
res.status(200).json(item)
|
||
|
}
|
||
|
})
|
||
|
}
|
||
|
)
|
||
|
.delete(
|
||
|
passport.authenticate(['basic-auth']),
|
||
|
function (req, res) {
|
||
|
const property = new Properties()
|
||
|
property.deleteOne(req, function (err, item) {
|
||
|
if (err) {
|
||
|
res.status(item || 500).send(err.message)
|
||
|
} else {
|
||
|
res.status(200).json(item)
|
||
|
}
|
||
|
})
|
||
|
}
|
||
|
)
|
||
|
|
||
|
return router
|
||
|
}
|