From 935058df2800137dd4b3a511e81c9225b557503e Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 14 Nov 2018 22:51:29 -0500 Subject: [PATCH] add targetcontroller and model --- api/controllers/TargetController.js | 41 +++++++++++++++++++++++++++++ api/models/TargetUrl.js | 16 +++++++++++ config/routes.js | 7 ++++- package.json | 1 + 4 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 api/models/TargetUrl.js diff --git a/api/controllers/TargetController.js b/api/controllers/TargetController.js index 345dfde..72110cc 100644 --- a/api/controllers/TargetController.js +++ b/api/controllers/TargetController.js @@ -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) + } } } diff --git a/api/models/TargetUrl.js b/api/models/TargetUrl.js new file mode 100644 index 0000000..fa169bb --- /dev/null +++ b/api/models/TargetUrl.js @@ -0,0 +1,16 @@ +module.exports = { + attributes: { + id: { + type: 'number', + unique: true, + autoIncrement: true + }, + user: { + model: 'User', + required: true + }, + url: { + type: 'string' + } + } +} diff --git a/config/routes.js b/config/routes.js index fa0672b..dfe64c9 100644 --- a/config/routes.js +++ b/config/routes.js @@ -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' // ╦ ╦╔═╗╔╗ ╦ ╦╔═╗╔═╗╦╔═╔═╗ // ║║║║╣ ╠╩╗╠═╣║ ║║ ║╠╩╗╚═╗ diff --git a/package.json b/package.json index 7b14f57..ecd0444 100644 --- a/package.json +++ b/package.json @@ -82,6 +82,7 @@ "User", "Book", "Passport", + "TargetUrl", "_" ], "parser": "babel-eslint"