add book controller and route

pull/1/head
unknown 2018-10-23 23:13:56 -04:00
parent 138acc3f1a
commit d738b9061f
3 changed files with 75 additions and 3 deletions

View File

@ -0,0 +1,35 @@
/**
* BooksController
*
* @description :: Server-side actions for handling incoming requests.
* @help :: See https://sailsjs.com/docs/concepts/actions
*/
module.exports = {
publish: async function (req, res) {
try {
const body = req.body
const host = req.hostname
let result
if (!host) throw new Error('Missing hostname')
if (!body) throw new Error('Missing body')
const bookExists = await Book.findOne(body)
if (bookExists) {
throw new Error('Version already exists')
} else {
result = await Book.create(body)
}
return res.json({
...result
})
} catch (e) {
return res.status(400).json({
error: e.message
})
}
}
}

31
api/models/Book.js Normal file
View File

@ -0,0 +1,31 @@
/**
* Book.js
*
* @description :: A model definition. Represents a database table/collection/etc.
* @docs :: https://sailsjs.com/docs/concepts/models-and-orm/models
*/
module.exports = {
attributes: {
// ╔═╗╦═╗╦╔╦╗╦╔╦╗╦╦ ╦╔═╗╔═╗
// ╠═╝╠╦╝║║║║║ ║ ║╚╗╔╝║╣ ╚═╗
// ╩ ╩╚═╩╩ ╩╩ ╩ ╩ ╚╝ ╚═╝╚═╝
title: {type: 'string', required: true},
author: {type: 'string'},
isbn: {type: 'string'},
version: {type: 'string'},
// ╔═╗╔╦╗╔╗ ╔═╗╔╦╗╔═╗
// ║╣ ║║║╠╩╗║╣ ║║╚═╗
// ╚═╝╩ ╩╚═╝╚═╝═╩╝╚═╝
// ╔═╗╔═╗╔═╗╔═╗╔═╗╦╔═╗╔╦╗╦╔═╗╔╗╔╔═╗
// ╠═╣╚═╗╚═╗║ ║║ ║╠═╣ ║ ║║ ║║║║╚═╗
// ╩ ╩╚═╝╚═╝╚═╝╚═╝╩╩ ╩ ╩ ╩╚═╝╝╚╝╚═╝
},
}

View File

@ -9,8 +9,6 @@
*/
module.exports.routes = {
// ╦ ╦╔═╗╔╗ ╔═╗╔═╗╔═╗╔═╗╔═╗
// ║║║║╣ ╠╩╗╠═╝╠═╣║ ╦║╣ ╚═╗
// ╚╩╝╚═╝╚═╝╩ ╩ ╩╚═╝╚═╝╚═╝
@ -28,6 +26,10 @@ module.exports.routes = {
view: 'pages/homepage'
},
'GET /test': {
view: 'pages/test'
},
/***************************************************************************
* *
* More custom routes here... *
@ -44,6 +46,10 @@ module.exports.routes = {
// ╠═╣╠═╝║ ║╣ ║║║ ║║╠═╝║ ║║║║║ ║ ╚═╗
// ╩ ╩╩ ╩ ╚═╝╝╚╝═╩╝╩ ╚═╝╩╝╚╝ ╩ ╚═╝
'POST /api/publish': {
controller: 'books',
action: 'publish'
},
// ╦ ╦╔═╗╔╗ ╦ ╦╔═╗╔═╗╦╔═╔═╗
@ -56,4 +62,4 @@ module.exports.routes = {
// ╩ ╩╩╚═╝╚═╝
};
}