Some changes
This commit is contained in:
parent
e738d80579
commit
60cd0f69cc
8 changed files with 58 additions and 8 deletions
20
app.js
20
app.js
|
@ -29,8 +29,28 @@ app.use(express.urlencoded({ extended: false }))
|
||||||
app.use(cookieParser())
|
app.use(cookieParser())
|
||||||
app.use(express.static(path.join(__dirname, 'public')))
|
app.use(express.static(path.join(__dirname, 'public')))
|
||||||
|
|
||||||
|
app.use('/',
|
||||||
|
/**
|
||||||
|
* Set defaults env for all routes
|
||||||
|
* @param {Object} req
|
||||||
|
* @param {Object} res
|
||||||
|
* @param {Object} next
|
||||||
|
*/
|
||||||
|
function (req, res, next) {
|
||||||
|
// Website you wish to allow to connect
|
||||||
|
res.setHeader('Access-Control-Allow-Origin', '*')
|
||||||
|
// Request methods you wish to allow
|
||||||
|
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE')
|
||||||
|
// Request headers you wish to allow
|
||||||
|
res.setHeader('Access-Control-Allow-Headers',
|
||||||
|
'Origin, X-Requested-With, Content-Type, Accept, Authorization, cache-control, pragma, If-Modified-Since')
|
||||||
|
// Pass to next layer of middleware
|
||||||
|
next()
|
||||||
|
})
|
||||||
|
|
||||||
app.use('/', require('./routes/vegetableTypes')(passport))
|
app.use('/', require('./routes/vegetableTypes')(passport))
|
||||||
app.use('/', require('./routes/vegetables')(passport))
|
app.use('/', require('./routes/vegetables')(passport))
|
||||||
|
app.use('/', require('./routes/user')(passport))
|
||||||
|
|
||||||
app.use(function (req, res, next) {
|
app.use(function (req, res, next) {
|
||||||
next(createError(404))
|
next(createError(404))
|
||||||
|
|
|
@ -5,7 +5,8 @@ class VegetableTypes {
|
||||||
vegetableTypes.findAndCountAll({
|
vegetableTypes.findAndCountAll({
|
||||||
order: [
|
order: [
|
||||||
['name', 'ASC']
|
['name', 'ASC']
|
||||||
]
|
],
|
||||||
|
include: ['Vegetables']
|
||||||
})
|
})
|
||||||
.then(items => {
|
.then(items => {
|
||||||
if (!items) {
|
if (!items) {
|
||||||
|
@ -30,7 +31,11 @@ class VegetableTypes {
|
||||||
}
|
}
|
||||||
|
|
||||||
static getOne (req, callback) {
|
static getOne (req, callback) {
|
||||||
vegetableTypes.findById(Number(req.params.vegetableTypesId))
|
vegetableTypes.findById(
|
||||||
|
req.params.vegetableTypesId,
|
||||||
|
{
|
||||||
|
include: ['Vegetables']
|
||||||
|
})
|
||||||
.then(item => {
|
.then(item => {
|
||||||
if (!item) {
|
if (!item) {
|
||||||
callback(new Error('Item vegetable type not found'), 404)
|
callback(new Error('Item vegetable type not found'), 404)
|
||||||
|
|
|
@ -83,6 +83,7 @@ class Vegetables {
|
||||||
where: {
|
where: {
|
||||||
vegetableTypeId: req.params.vegetableTypesId
|
vegetableTypeId: req.params.vegetableTypesId
|
||||||
},
|
},
|
||||||
|
include: ['Type'],
|
||||||
order: [
|
order: [
|
||||||
['name', 'ASC']
|
['name', 'ASC']
|
||||||
]
|
]
|
||||||
|
@ -135,7 +136,7 @@ class Vegetables {
|
||||||
vegetableTypeId: req.params.vegetableTypesId,
|
vegetableTypeId: req.params.vegetableTypesId,
|
||||||
id: req.params.vegetablesId
|
id: req.params.vegetablesId
|
||||||
},
|
},
|
||||||
include: ['Type']
|
include: ['Type', 'Pictures']
|
||||||
})
|
})
|
||||||
.then(item => {
|
.then(item => {
|
||||||
if (!item) {
|
if (!item) {
|
||||||
|
|
|
@ -12,12 +12,13 @@ module.exports = (sequelize, DataTypes) => {
|
||||||
}
|
}
|
||||||
}, {})
|
}, {})
|
||||||
vegetables.associate = function (models) {
|
vegetables.associate = function (models) {
|
||||||
vegetables.hasOne(models.vegetableTypes, {
|
vegetables.belongsTo(models.vegetableTypes, {
|
||||||
as: 'Type',
|
as: 'Type',
|
||||||
foreignKey: 'id'
|
foreignKey: 'vegetableTypeId'
|
||||||
})
|
})
|
||||||
vegetables.hasMany(models.vegetablePictures, {
|
vegetables.hasMany(models.vegetablePictures, {
|
||||||
as: 'Pictures',
|
as: 'Pictures',
|
||||||
|
foreignKey: 'vegetablesId',
|
||||||
onDelete: 'cascade'
|
onDelete: 'cascade'
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,8 @@ module.exports = (sequelize, DataTypes) => {
|
||||||
vegetableTypes.associate = function (models) {
|
vegetableTypes.associate = function (models) {
|
||||||
vegetableTypes.hasMany(models.vegetables, {
|
vegetableTypes.hasMany(models.vegetables, {
|
||||||
as: 'Vegetables',
|
as: 'Vegetables',
|
||||||
onDelete: 'cascade'
|
onDelete: 'cascade',
|
||||||
|
foreignKey: 'vegetableTypeId'
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
return vegetableTypes
|
return vegetableTypes
|
||||||
|
|
22
routes/user.js
Normal file
22
routes/user.js
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
const express = require('express')
|
||||||
|
const router = express.Router()
|
||||||
|
|
||||||
|
module.exports = function (passport) {
|
||||||
|
router.route('/api/login/')
|
||||||
|
.post(
|
||||||
|
passport.authenticate(['basic-auth']),
|
||||||
|
function (req, res) {
|
||||||
|
res.status(200).send(req.user)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
router.route('/api/me/')
|
||||||
|
.get(
|
||||||
|
passport.authenticate(['basic-auth']),
|
||||||
|
function (req, res) {
|
||||||
|
res.status(200).send(req.user)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
return router
|
||||||
|
}
|
|
@ -26,7 +26,7 @@ module.exports = function (passport) {
|
||||||
if (err) {
|
if (err) {
|
||||||
res.status(item || 500).send(err.message)
|
res.status(item || 500).send(err.message)
|
||||||
} else {
|
} else {
|
||||||
res.status(200).json(item)
|
res.status(201).json(item)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,7 +26,7 @@ module.exports = function (passport) {
|
||||||
if (err) {
|
if (err) {
|
||||||
res.status(item || 500).send(err.message)
|
res.status(item || 500).send(err.message)
|
||||||
} else {
|
} else {
|
||||||
res.status(200).json(item)
|
res.status(201).json(item)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue