add targetcontroller and model

pull/30/head
unknown 2018-11-14 22:51:29 -05:00
parent 56db388a06
commit 935058df28
4 changed files with 64 additions and 1 deletions

View File

@ -1,8 +1,49 @@
'use strict'
const HttpError = require('../errors/HttpError')
module.exports = {
show: function (req, res) {
res.view('pages/targets', {
email: req.user.email
})
},
create: async function (req, res) {
try {
const url = await TargetUrl.create({
user: req.user
}).fetch()
return res.json(url)
} catch (e) {
return (new HttpError(500, e.message)).send(res)
}
},
edit: async function (req, res) {
try {
const id = req.param('id')
const value = req.param('url')
const url = await TargetUrl.update({ id, user: req.user }, { url: value }).fetch()
return res.json(url)
} catch (e) {
return (new HttpError(500, e.message)).send(res)
}
},
delete: async function (req, res) {
try {
await TargetUrl.destroy({ id: req.param('id') })
return res.status(204)
} catch (e) {
return (new HttpError(500, e.message)).send(res)
}
},
list: async function (req, res) {
try {
const urls = await TargetUrl.where({
user: req.user
})
return res.json(urls)
} catch (e) {
return (new HttpError(500, e.message)).send(res)
}
}
}

16
api/models/TargetUrl.js Normal file
View File

@ -0,0 +1,16 @@
module.exports = {
attributes: {
id: {
type: 'number',
unique: true,
autoIncrement: true
},
user: {
model: 'User',
required: true
},
url: {
type: 'string'
}
}
}

View File

@ -66,7 +66,12 @@ module.exports.routes = {
'POST /api/publish': 'BooksController.publish',
'GET /api/books': 'BooksController.list',
'GET /api/me': 'UserController.me'
'GET /api/me': 'UserController.me',
'POST /api/targets': 'TargetController.create',
'PATCH /api/targets/:id': 'TargetController.edit',
'DELETE /api/targets/:id': 'TargetController.delete',
'GET /api/targets': 'TargetController.list'
// ╦ ╦╔═╗╔╗ ╦ ╦╔═╗╔═╗╦╔═╔═╗
// ║║║║╣ ╠╩╗╠═╣║ ║║ ║╠╩╗╚═╗

View File

@ -82,6 +82,7 @@
"User",
"Book",
"Passport",
"TargetUrl",
"_"
],
"parser": "babel-eslint"