Browse Source

preparing to create new items

Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
module/document
Stephan Richter 2 weeks ago
parent
commit
4b4a575356
  1. 2
      frontend/src/routes/stock/Index.svelte
  2. 29
      frontend/src/routes/stock/ItemList.svelte
  3. 8
      frontend/src/urls.svelte.js
  4. 8
      stock/src/main/java/de/srsoftware/umbrella/stock/StockModule.java

2
frontend/src/routes/stock/Index.svelte

@ -189,7 +189,7 @@
</h3> </h3>
<MarkdownEditor editable={true} value={location.description} type="div" onSet={newDesc => patchLocation(location,'description',newDesc)} /> <MarkdownEditor editable={true} value={location.description} type="div" onSet={newDesc => patchLocation(location,'description',newDesc)} />
{/if} {/if}
<ItemList items={data?.items.sort((a,b) => a.code.localeCompare(b.code))} bind:selected={item} drag_start={drag_item} /> <ItemList {location} items={data?.items.sort((a,b) => a.code.localeCompare(b.code))} bind:selected={item} drag_start={drag_item} />
</div> </div>
<div class="properties"> <div class="properties">
<ItemProps {item} {properties} /> <ItemProps {item} {properties} />

29
frontend/src/routes/stock/ItemList.svelte

@ -1,7 +1,20 @@
<script> <script>
import { t } from '../../translations.svelte'; import { api, post } from '../../urls.svelte';
import { error, yikes } from '../../warn.svelte';
import { t } from '../../translations.svelte';
let { items, selected = $bindable(null), drag_start = item => console.log({dragging:item}) } = $props(); let { items, selected = $bindable(null), location = null, drag_start = item => console.log({dragging:item}) } = $props();
let newItem = $state({name:null, code: null});
async function saveNewItem(){
newItem.location = location;
console.log(JSON.parse(JSON.stringify(newItem)));
const url = api('stock/item');
const res = post(url,newItem);
if (res.ok) {
yikes();
} else error(res);
}
</script> </script>
<table> <table>
<thead> <thead>
@ -19,5 +32,17 @@
<td>{item.name}</td> <td>{item.name}</td>
</tr> </tr>
{/each} {/each}
{#if location}
<tr>
<td>{t('create_new_object',{object:t('item')})}</td>
<td>
<input type="text" bind:value={newItem.code} />
</td>
<td>
<input type="text" bind:value={newItem.name} />
<button onclick={saveNewItem}>{t('save')}</button>
</td>
</tr>
{/if}
</tbody> </tbody>
</table> </table>

8
frontend/src/urls.svelte.js

@ -4,6 +4,14 @@ export function api(rel_path){
return `${location.protocol}//${location.host.replace('5173','8080')}/api/${rel_path}`; return `${location.protocol}//${location.host.replace('5173','8080')}/api/${rel_path}`;
} }
export async function post(url,data){
return fetch(url,{
credentials : 'include',
method : 'POST',
body : JSON.stringify(data)
});
}
export function target(code){ export function target(code){
if (!code) return null; if (!code) return null;
let altered = code; let altered = code;

8
stock/src/main/java/de/srsoftware/umbrella/stock/StockModule.java

@ -140,6 +140,7 @@ public class StockModule extends BaseHandler implements StockService {
if (user.isEmpty()) return unauthorized(ex); if (user.isEmpty()) return unauthorized(ex);
var head = path.pop(); var head = path.pop();
return switch (head) { return switch (head) {
case ITEM -> postItem(user.get(), ex);
case LOCATION -> postLocation(user.get(),ex); case LOCATION -> postLocation(user.get(),ex);
case PROPERTY -> postProperty(user.get(),ex); case PROPERTY -> postProperty(user.get(),ex);
case null, default -> super.doPost(path,ex); case null, default -> super.doPost(path,ex);
@ -262,6 +263,13 @@ public class StockModule extends BaseHandler implements StockService {
return sendContent(ex,location); return sendContent(ex,location);
} }
private boolean postItem(UmbrellaUser user, HttpExchange ex) throws IOException {
var json = json(ex);
if (!json.has(NAME) || !(json.get(NAME) instanceof String name)) throw missingFieldException(NAME);
if (!json.has(CODE) || !(json.get(CODE) instanceof String code)) throw missingFieldException(CODE);
}
private boolean postLocation(UmbrellaUser user, HttpExchange ex) throws IOException { private boolean postLocation(UmbrellaUser user, HttpExchange ex) throws IOException {
var json = json(ex); var json = json(ex);
if (!(json.get(NAME) instanceof String name)) throw missingFieldException(NAME); if (!(json.get(NAME) instanceof String name)) throw missingFieldException(NAME);

Loading…
Cancel
Save