diff --git a/api/images.js b/api/images.js index 4136e57..00eb79b 100644 --- a/api/images.js +++ b/api/images.js @@ -8,4 +8,9 @@ router.get('/:uuid/:size', async (req, res) => { res.end(image.file) }) -module.exports = router \ No newline at end of file +router.delete('/:uuid', async (req, res) => { + const item = await db.item.removeImage(req.params.uuid) + res.json(item) +}) + +module.exports = router diff --git a/api/items.js b/api/items.js index f6656f0..4a3428d 100644 --- a/api/items.js +++ b/api/items.js @@ -9,7 +9,7 @@ const upload = require('multer')({ storage: require('multer').memoryStorage(), limits: { files: 1, - fileSize: 500000 + fileSize: 3000000 } }) @@ -44,10 +44,15 @@ router.post('/', parseJSON, newItemValidators, async (req, res) => { res.json(item) }) +router.delete('/:uuid', async (req, res) => { + const result = await db.item.removeItem(req.params.uuid) + res.json({deleted: true}) +}) + router.post('/:uuid/images', upload.single('image'), async (req, res) => { // TODO: Use the real user when we have authentication req.user = { - uuid: '5e17388d-612b-4ea8-b76d-620a7bb24824' + uuid: '56881ad0-8d80-496a-b036-aed03d0895ce' } // Handle either image upload body or JSON body @@ -62,4 +67,4 @@ router.post('/:uuid/images', upload.single('image'), async (req, res) => { } res.json(await db.item.findById(req.params.uuid)) -}) \ No newline at end of file +}) diff --git a/db/models/item.js b/db/models/item.js index db76f9d..7277198 100644 --- a/db/models/item.js +++ b/db/models/item.js @@ -105,4 +105,28 @@ item.getImage = async (image_uuid, size) => { const {rows} = await pg.query(query) return joinjs.map(rows, mappings, 'bareImageMap', 'image_')[0]; -} \ No newline at end of file +} + +item.removeImage = async (image_uuid) => { + const query = { + text: 'select * from public.delete_image($1)', + values: [ + image_uuid + ] + } + + const {rows} = await pg.query(query) + return joinjs.map(rows, mappings, 'itemMap', 'item_')[0] +} + +item.removeItem = async (item_uuid) => { + const query = { + text: 'select * from public.delete_item($1)', + values: [ + item_uuid + ] + } + + const {rows} = await pg.query(query) + return joinjs.map(rows, mappings, 'itemMap', 'item_')[0] +} diff --git a/db/sql/2-views.sql b/db/sql/2-views.sql index 50a53fd..713af81 100644 --- a/db/sql/2-views.sql +++ b/db/sql/2-views.sql @@ -24,19 +24,6 @@ create or replace view public.v_cart as left join "cart_item" on "cart".cart_uuid = "cart_item".cart_item_cart_uuid left join "item" on "cart_item".cart_item_item_uuid = "item".item_uuid; -create or replace view public.v_category as - select - "category".*, - "child_category".category_uuid as child_category_uuid, - "child_category".category_name as child_category_name, - "child_category".category_urlslug as child_category_urlslug, - "item".* - from "category" - left join "category_category" on "category".category_uuid = "category_category".category_category_parent_uuid - left join "category" "child_category" on "category_category".category_category_child_uuid = "child_category".category_uuid - left join "category_item" on "category".category_uuid = "category_item".category_item_category_uuid - left join "item" on "category_item".category_item_item_uuid = "item".item_uuid; - create or replace view public.v_item as select "item".*, @@ -50,9 +37,14 @@ create or replace view public.v_item as left join "user" on "image".image_uploader_uuid = "user".user_uuid; create or replace view public.v_category as - select + select "category".*, + "child_category".category_uuid as child_category_uuid, + "child_category".category_name as child_category_name, + "child_category".category_urlslug as child_category_urlslug, v_item.* from "category" - left join "category_item" on "category_item".category_item_category_uuid = "category".category_uuid - left join v_item on "category_item".category_item_item_uuid = item_uuid; \ No newline at end of file + left join "category_category" on "category".category_uuid = "category_category".category_category_parent_uuid + left join "category" "child_category" on "category_category".category_category_child_uuid = "child_category".category_uuid + left join "category_item" on "category".category_uuid = "category_item".category_item_category_uuid + left join v_item on "category_item".category_item_item_uuid = item_uuid;