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}) {
- This is only half finished . . . you can make new stuff but editing doesn't work yet. -