|
|
|
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 = {}
|
|
|
|
|
|
|
|
category.findAll = async () => {
|
|
|
|
const query = 'select * from sos.v_category'
|
|
|
|
|
|
|
|
debug(query);
|
|
|
|
|
|
|
|
const {rows} = await pg.query(query)
|
|
|
|
return joinjs.map(rows, mappings, 'categoryMap', 'category_');
|
|
|
|
}
|
|
|
|
|
|
|
|
category.findBySlug = async slug => {
|
|
|
|
const query = {
|
|
|
|
text: 'select * from sos.v_category where category_urlslug = $1',
|
|
|
|
values: [slug]
|
|
|
|
}
|
|
|
|
|
|
|
|
debug(query);
|
|
|
|
|
|
|
|
const {rows} = await pg.query(query)
|
|
|
|
return joinjs.map(rows, mappings, 'categoryMap', 'category_')[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
category.create = async (name, urlslug, description) => {
|
|
|
|
const query = {
|
|
|
|
text: 'select * from sos.create_category($1::text, $2::citext, $3::text)',
|
|
|
|
values: [
|
|
|
|
name,
|
|
|
|
urlslug,
|
|
|
|
description
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
debug(query)
|
|
|
|
|
|
|
|
const {rows} = await pg.query(query);
|
|
|
|
return joinjs.map(rows, mappings, 'categoryMap', 'category_')[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
category.addItem = async (category_uuid, item_uuid) => {
|
|
|
|
const query = {
|
|
|
|
text: 'select * from sos.add_item_to_category($1::uuid, $2::uuid)',
|
|
|
|
values: [
|
|
|
|
category_uuid,
|
|
|
|
item_uuid
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
debug(query)
|
|
|
|
|
|
|
|
const {rows} = await pg.query(query);
|
|
|
|
return joinjs.map(rows, mappings, 'categoryMap', 'category_')[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
category.removeItem = async (category_uuid, item_uuid) => {
|
|
|
|
const query = {
|
|
|
|
text: 'select * from sos.remove_item_from_category($1::uuid, $2::uuid)',
|
|
|
|
values: [
|
|
|
|
category_uuid,
|
|
|
|
item_uuid
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
debug(query)
|
|
|
|
|
|
|
|
const {rows} = await pg.query(query);
|
|
|
|
return joinjs.map(rows, mappings, 'categoryMap', 'category_')[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
category.addCategory = async (parent_uuid, child_uuid) => {
|
|
|
|
const query = {
|
|
|
|
text: 'select * from sos.add_category_to_category($1, $2)',
|
|
|
|
values: [
|
|
|
|
parent_uuid,
|
|
|
|
child_uuid
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
debug(query)
|
|
|
|
|
|
|
|
const {rows} = await pg.query(query);
|
|
|
|
return joinjs.map(rows, mappings, 'categoryMap', 'category_')[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
category.removeCategory = async (parent_uuid, child_uuid) => {
|
|
|
|
const query = {
|
|
|
|
text: 'select * from sos.remove_category_from_category($1, $2)',
|
|
|
|
values: [
|
|
|
|
parent_uuid,
|
|
|
|
child_uuid
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
debug(query)
|
|
|
|
|
|
|
|
const {rows} = await pg.query(query);
|
|
|
|
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
|
|
|
|
})
|