From 3db816eb29715a24ca4e172bb3e6e4311a07175a Mon Sep 17 00:00:00 2001 From: Ashelyn Dawn Date: Tue, 7 Jul 2020 22:11:24 -0600 Subject: [PATCH] Can update category details --- api/categories.js | 13 ++++++++++++- db/models/category.js | 13 +++++++++++++ db/sql/3-functions.sql | 19 +++++++++++++++++++ pages/admin/categories/[slug]/index.js | 3 --- 4 files changed, 44 insertions(+), 4 deletions(-) diff --git a/api/categories.js b/api/categories.js index 52e6b74..c54e38e 100644 --- a/api/categories.js +++ b/api/categories.js @@ -12,7 +12,7 @@ router.get('/', async (req, res) => { router.get('/by-slug/:slug', async (req, res) => { const category = await db.category.findBySlug(req.params.slug) - + res.json(category) }) @@ -32,6 +32,17 @@ router.post('/', parseJSON, newCategoryValidators, async (req, res) => { res.json(category) }) +router.post('/:uuid', parseJSON, newCategoryValidators, async (req, res) => { + const category = await db.category.update( + req.params.uuid, + req.body.name, + req.body.urlslug, + req.body.description + ) + + res.json(category) +}) + router.put('/:category_uuid/items/:item_uuid', async (req, res) => { const category = await db.category.addItem(req.params.category_uuid, req.params.item_uuid); res.json(category) diff --git a/db/models/category.js b/db/models/category.js index 3a9e400..fa103d8 100644 --- a/db/models/category.js +++ b/db/models/category.js @@ -2,6 +2,7 @@ const pg = require('../pg') const joinjs = require('join-js').default; const debug = require('debug')('sos:db:category') const mappings = require('../mappings') +const dbUtil = require('../util') const category = module.exports = {} @@ -87,3 +88,15 @@ category.addCategory = async (parent_uuid, child_uuid) => { return joinjs.map(rows, mappings, 'categoryMap', 'category_')[0]; } +category.update = async (uuid, name, urlslug, description) => + dbUtil.executeFunction({ + name: 'update_category', + params: [ + uuid, + name, + urlslug, + description + ], + returnType: 'category', + single: true + }) diff --git a/db/sql/3-functions.sql b/db/sql/3-functions.sql index 15058d5..42cdb1f 100644 --- a/db/sql/3-functions.sql +++ b/db/sql/3-functions.sql @@ -235,6 +235,25 @@ begin return query select * from sos.v_category where category_uuid = _category_uuid; end; $function$; +create or replace function sos.update_category(_category_uuid uuid, _category_name text, _category_urlslug citext, _category_description text) + returns setof sos.v_category + language plpgsql +as $function$ +begin + + update sos."category" set ( + category_name, + category_urlslug, + category_description + ) = ( + _category_name, + _category_urlslug, + _category_description + ) where category_uuid = _category_uuid; + + return query select * from sos.v_category where category_uuid = _category_uuid; +end; $function$; + create or replace function sos.add_item_to_category(_category_uuid uuid, _item_uuid uuid) returns setof sos.v_category language plpgsql diff --git a/pages/admin/categories/[slug]/index.js b/pages/admin/categories/[slug]/index.js index 1b4c320..f2265f5 100644 --- a/pages/admin/categories/[slug]/index.js +++ b/pages/admin/categories/[slug]/index.js @@ -30,9 +30,6 @@ export default function Editcategory({category}) { router.push(`/admin/categories/${category.urlslug}/items`)}>Edit Items ({category.items.length}) router.push(`/admin/categories/${category.urlslug}/children`)}>Edit Child Categories ({category.children.length}) -

- This is only half finished . . . you can make new stuff but editing doesn't work yet. -