Browse Source

preparing to store new locations

Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
module/document
Stephan Richter 2 weeks ago
parent
commit
4e9c8c0f69
  1. 3
      core/src/main/java/de/srsoftware/umbrella/core/model/DbLocation.java
  2. 2
      frontend/src/routes/stock/Index.svelte
  3. 29
      frontend/src/routes/stock/Locations.svelte
  4. 2
      stock/src/main/java/de/srsoftware/umbrella/stock/Constants.java
  5. 32
      stock/src/main/java/de/srsoftware/umbrella/stock/StockModule.java

3
core/src/main/java/de/srsoftware/umbrella/core/model/DbLocation.java

@ -15,7 +15,7 @@ public class DbLocation extends Location {
private String description; private String description;
private String relation; // when added to an item, this field describes the type of the relation private String relation; // when added to an item, this field describes the type of the relation
private DbLocation(long id, Owner owner, Long parentLocationId, String name, String description){ public DbLocation(long id, Owner owner, Long parentLocationId, String name, String description){
super(id); super(id);
this.owner = owner; this.owner = owner;
this.parentLocationId = parentLocationId; this.parentLocationId = parentLocationId;
@ -32,7 +32,6 @@ public class DbLocation extends Location {
} }
public static DbLocation of(ResultSet rs) throws SQLException { public static DbLocation of(ResultSet rs) throws SQLException {
return new DbLocation(rs.getLong(ID), OwnerRef.of(rs), rs.getLong(PARENT_LOCATION_ID), rs.getString(NAME),rs.getString(DESCRIPTION)); return new DbLocation(rs.getLong(ID), OwnerRef.of(rs), rs.getLong(PARENT_LOCATION_ID), rs.getString(NAME),rs.getString(DESCRIPTION));
} }

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

@ -92,7 +92,7 @@
{#each top_level as realm,idx} {#each top_level as realm,idx}
<h3>{realm.name}</h3> <h3>{realm.name}</h3>
{#if realm.locations} {#if realm.locations}
<Locations locations={realm.locations} bind:selected={location} {move_dragged_to} /> <Locations locations={realm.locations} parent={realm.parent} bind:selected={location} {move_dragged_to} />
{/if} {/if}
{/each} {/each}
{/if} {/if}

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

@ -5,7 +5,7 @@
import LineEditor from '../../Components/LineEditor.svelte'; import LineEditor from '../../Components/LineEditor.svelte';
let { locations, move_dragged_to = new_loc => {}, selected = $bindable(null) } = $props(); let { locations, move_dragged_to = new_loc => {}, parent = null, selected = $bindable(null) } = $props();
let show_location_form = $state(false); let show_location_form = $state(false);
let new_location_name = $state(null); let new_location_name = $state(null);
@ -39,14 +39,35 @@
return false; return false;
} }
async function onSet(new_location_name){
const data = {
name: new_location_name,
parent: parent
}
const url = api('stock/location');
const res = await fetch(url,{
credentials: 'include',
method: 'POST',
body: JSON.stringify(data)
});
if (res.ok){
yikes;
return true;
} else {
error(ok);
return false;
}
}
function onsubmit(ev){ function onsubmit(ev){
ev.preventDefault(); ev.preventDefault();
ev.stopPropagation(); ev.stopPropagation();
const data = { const data = {
name: new_location_name, name: new_location_name,
parent: parent.user ? {user:parent.user} : {company:parent.company} parent: parent
} }
console.log(JSON.parse(JSON.stringify(data)));
} }
function show_loc_form(ev){ function show_loc_form(ev){
@ -67,7 +88,7 @@
<li> <li>
{#if show_location_form} {#if show_location_form}
<form {onsubmit}> <form {onsubmit}>
<input type="text" placeholder={t('new_location_name')} bind:value={new_location_name} /> <LineEditor simple={true} bind:value={new_location_name} {onSet} />
</form> </form>
{:else} {:else}
<a onclick={show_loc_form}> <a onclick={show_loc_form}>
@ -84,7 +105,7 @@
ondragleave={e => delete location.highlight}> ondragleave={e => delete location.highlight}>
<span class="name">{location.name}</span> <span class="name">{location.name}</span>
{#if location.locations} {#if location.locations}
<svelte:self locations={location.locations} {move_dragged_to} bind:selected /> <svelte:self locations={location.locations} {move_dragged_to} parent={{location:location.id}} bind:selected />
{/if} {/if}
</li> </li>
{/each} {/each}

2
stock/src/main/java/de/srsoftware/umbrella/stock/Constants.java

@ -5,6 +5,7 @@ public class Constants {
private Constants(){} private Constants(){}
public static final String BELOW = "below"; public static final String BELOW = "below";
public static final String CONFIG_DATABASE = "umbrella.modules.stock.database"; public static final String CONFIG_DATABASE = "umbrella.modules.stock.database";
public static final String ITEM = "item"; public static final String ITEM = "item";
@ -13,6 +14,7 @@ public class Constants {
public static final String LOCATIONS = "locations"; public static final String LOCATIONS = "locations";
public static final String MOVE_ITEM = "move_item"; public static final String MOVE_ITEM = "move_item";
public static final String OF_USER = "of_user"; public static final String OF_USER = "of_user";
public static final String PARENT = "parent";
public static final String PROPERTY_ID = "prop_id"; public static final String PROPERTY_ID = "prop_id";
public static final String TABLE_ITEMS = "items"; public static final String TABLE_ITEMS = "items";
public static final String TABLE_ITEM_PROPERTIES = "item_props"; public static final String TABLE_ITEM_PROPERTIES = "item_props";

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

@ -99,6 +99,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 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);
}; };
@ -149,7 +150,7 @@ public class StockModule extends BaseHandler implements StockService {
var result = new ArrayList<Object>(); var result = new ArrayList<Object>();
var userLocations = stockDb.listUserLocations(user); var userLocations = stockDb.listUserLocations(user);
result.add(Map.of( result.add(Map.of(
OWNER, Map.of(USER, user.id()), PARENT, Map.of(USER, user.id()),
NAME,user.name(), NAME,user.name(),
LOCATIONS,userLocations.stream().map(DbLocation::toMap).toList())); LOCATIONS,userLocations.stream().map(DbLocation::toMap).toList()));
@ -157,7 +158,7 @@ public class StockModule extends BaseHandler implements StockService {
companies.values().stream().sorted(comparing(a -> a.name().toLowerCase())).forEach(company -> { companies.values().stream().sorted(comparing(a -> a.name().toLowerCase())).forEach(company -> {
var locations = stockDb.listCompanyLocations(company); var locations = stockDb.listCompanyLocations(company);
result.add(Map.of( result.add(Map.of(
COMPANY, company.id(), PARENT, Map.of(COMPANY, company.id()),
NAME,company.name(), NAME,company.name(),
LOCATIONS,locations.stream().sorted(comparing(a -> a.name().toLowerCase())).map(DbLocation::toMap).toList())); LOCATIONS,locations.stream().sorted(comparing(a -> a.name().toLowerCase())).map(DbLocation::toMap).toList()));
@ -199,6 +200,33 @@ public class StockModule extends BaseHandler implements StockService {
return sendContent(ex,item); return sendContent(ex,item);
} }
private boolean postLocation(UmbrellaUser user, HttpExchange ex) throws IOException {
var json = json(ex);
if (!(json.get(NAME) instanceof String name)) throw missingFieldException(NAME);
if (!(json.get(PARENT) instanceof JSONObject parentData)) throw missingFieldException(PARENT);
var key = parentData.keySet().stream().findFirst().orElseThrow(() -> missingFieldException(PARENT));
if (!(parentData.get(key) instanceof Number id)) throw missingFieldException(key);
Location parent;
Owner owner;
switch (key){
case COMPANY:
owner = companyService().get(id.longValue());
parent = null;
break;
case USER:
owner = userService().loadUser(id.longValue());
parent = null;
break;
case LOCATION:
parent = stockDb.loadLocation(id.longValue());
owner = parent.resolve().owner().resolve();
break;
default: throw unprocessable("Unknown parent object: {0} → {1}",key,id);
};
var loc = new DbLocation(0,owner,parent == null?null:parent.id(),name,null);
return sendContent(ex,stockDb.save(loc));
}
private boolean postProperty(UmbrellaUser user, HttpExchange ex) throws IOException { private boolean postProperty(UmbrellaUser user, HttpExchange ex) throws IOException {
var json = json(ex); var json = json(ex);
if (!(json.get(FIELD_ITEM) instanceof JSONObject itemData)) throw missingFieldException(FIELD_ITEM); if (!(json.get(FIELD_ITEM) instanceof JSONObject itemData)) throw missingFieldException(FIELD_ITEM);

Loading…
Cancel
Save