Item update works

main
Ashelyn Dawn 5 years ago
parent b3d76cbdc7
commit b92a645836

@ -24,7 +24,7 @@ router.get('/', async (req, res) => {
res.json(items) res.json(items)
}) })
const newItemValidators = [ const itemValidators = [
validate.validUrlSlug('urlslug'), validate.validUrlSlug('urlslug'),
validate.publishedBool, validate.publishedBool,
validate.positiveInteger('price_cents'), validate.positiveInteger('price_cents'),
@ -33,7 +33,7 @@ const newItemValidators = [
validate.handleApiError validate.handleApiError
] ]
router.post('/', parseJSON, newItemValidators, async (req, res) => { router.post('/', parseJSON, itemValidators, async (req, res) => {
const item = await db.item.create( const item = await db.item.create(
req.body.name, req.body.name,
req.body.urlslug, req.body.urlslug,
@ -50,6 +50,19 @@ router.get('/by-slug/:slug', async (req, res) => {
res.json(item) res.json(item)
}) })
router.post('/:uuid', parseJSON, itemValidators, async (req, res) => {
const item = await db.item.update(
req.params.uuid,
req.body.name,
req.body.urlslug,
req.body.description,
req.body.price_cents,
req.body.published
)
res.json(item)
})
router.post('/:uuid/images', upload.single('image'), async (req, res) => { router.post('/:uuid/images', upload.single('image'), async (req, res) => {
// Handle either image upload body or JSON body // Handle either image upload body or JSON body
try { try {

@ -69,6 +69,25 @@ item.create = async (name, urlslug, description, price_cents, published) => {
return joinjs.map(rows, mappings, 'itemMap', 'item_')[0] return joinjs.map(rows, mappings, 'itemMap', 'item_')[0]
} }
item.update = async (uuid, name, urlslug, description, price_cents, published) => {
const query = {
text: 'select * from sos.update_item($1::uuid, $2::text, $3::citext, $4::text, $5::integer, $6::boolean)',
values: [
uuid,
name,
urlslug,
description,
price_cents,
published
]
}
debug(query)
const {rows} = await pg.query(query)
return joinjs.map(rows, mappings, 'itemMap', 'item_')[0]
}
item.addImage = async (item_uuid, image_buffer, uploader_uuid) => { item.addImage = async (item_uuid, image_buffer, uploader_uuid) => {
// Default param chain: output as png // Default param chain: output as png

@ -105,6 +105,28 @@ begin
return query select * from sos.v_item where item_uuid = _item_uuid; return query select * from sos.v_item where item_uuid = _item_uuid;
end; $function$; end; $function$;
create or replace function sos.update_item(_uuid uuid, _name text, _urlslug citext, _description text, _price_cents integer, _published boolean)
returns setof sos.v_item
language plpgsql
as $function$
begin
update sos."item" set (
item_name,
item_urlslug,
item_description,
item_price_cents,
item_published
) = (
_name,
_urlslug,
_description,
_price_cents,
_published
) where item_uuid = _uuid;
return query select * from sos.v_item where item_uuid = _uuid;
end; $function$;
create or replace function sos.add_image_to_item(_item_uuid uuid, _large_file bytea, _thumb_file bytea, _mime_type varchar, _uploader_uuid uuid) create or replace function sos.add_image_to_item(_item_uuid uuid, _large_file bytea, _thumb_file bytea, _mime_type varchar, _uploader_uuid uuid)
returns setof sos.v_item returns setof sos.v_item
language plpgsql language plpgsql

@ -1,4 +1,5 @@
import React from 'react' import React from 'react'
import router from 'next/router'
import {FormController, Input, DecimalInput, Button, Checkbox} from '~/components/form' import {FormController, Input, DecimalInput, Button, Checkbox} from '~/components/form'
EditItem.getInitialProps = async ({ctx: {axios, query: {slug}}}) => { EditItem.getInitialProps = async ({ctx: {axios, query: {slug}}}) => {
@ -15,10 +16,14 @@ export default function EditItem({item}) {
return true; return true;
} }
const afterUpdate = (item) => {
router.replace(`/admin/items/${item.urlslug}`)
}
return ( return (
<> <>
<h2>Editing {item.name}</h2> <h2>Editing {item.name}</h2>
<FormController afterSubmit={console.log}> <FormController url={`/api/items/${item.uuid}`} afterSubmit={afterUpdate}>
<Input name="name" initialValue={item.name} validate={stringLengthAtLeastOne} /> <Input name="name" initialValue={item.name} validate={stringLengthAtLeastOne} />
<Input name="description" initialValue={item.description} validate={stringLengthAtLeastOne} /> <Input name="description" initialValue={item.description} validate={stringLengthAtLeastOne} />
<Input label="URL Slug" name="urlslug" initialValue={item.urlslug} validate={slugRestrictions} /> <Input label="URL Slug" name="urlslug" initialValue={item.urlslug} validate={slugRestrictions} />

Loading…
Cancel
Save