preparing to store new locations
Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
This commit is contained in:
@@ -15,7 +15,7 @@ public class DbLocation extends Location {
|
||||
private String description;
|
||||
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);
|
||||
this.owner = owner;
|
||||
this.parentLocationId = parentLocationId;
|
||||
@@ -32,7 +32,6 @@ public class DbLocation extends Location {
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
|
||||
@@ -92,7 +92,7 @@
|
||||
{#each top_level as realm,idx}
|
||||
<h3>{realm.name}</h3>
|
||||
{#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}
|
||||
{/each}
|
||||
{/if}
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
|
||||
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 new_location_name = $state(null);
|
||||
@@ -39,14 +39,35 @@
|
||||
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){
|
||||
ev.preventDefault();
|
||||
ev.stopPropagation();
|
||||
|
||||
const data = {
|
||||
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){
|
||||
@@ -67,7 +88,7 @@
|
||||
<li>
|
||||
{#if show_location_form}
|
||||
<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>
|
||||
{:else}
|
||||
<a onclick={show_loc_form}>
|
||||
@@ -84,7 +105,7 @@
|
||||
ondragleave={e => delete location.highlight}>
|
||||
<span class="name">{location.name}</span>
|
||||
{#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}
|
||||
</li>
|
||||
{/each}
|
||||
|
||||
@@ -5,6 +5,7 @@ public class Constants {
|
||||
|
||||
private Constants(){}
|
||||
|
||||
|
||||
public static final String BELOW = "below";
|
||||
public static final String CONFIG_DATABASE = "umbrella.modules.stock.database";
|
||||
public static final String ITEM = "item";
|
||||
@@ -13,6 +14,7 @@ public class Constants {
|
||||
public static final String LOCATIONS = "locations";
|
||||
public static final String MOVE_ITEM = "move_item";
|
||||
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 TABLE_ITEMS = "items";
|
||||
public static final String TABLE_ITEM_PROPERTIES = "item_props";
|
||||
|
||||
@@ -99,6 +99,7 @@ public class StockModule extends BaseHandler implements StockService {
|
||||
if (user.isEmpty()) return unauthorized(ex);
|
||||
var head = path.pop();
|
||||
return switch (head) {
|
||||
case LOCATION -> postLocation(user.get(),ex);
|
||||
case PROPERTY -> postProperty(user.get(),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 userLocations = stockDb.listUserLocations(user);
|
||||
result.add(Map.of(
|
||||
OWNER, Map.of(USER, user.id()),
|
||||
PARENT, Map.of(USER, user.id()),
|
||||
NAME,user.name(),
|
||||
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 -> {
|
||||
var locations = stockDb.listCompanyLocations(company);
|
||||
result.add(Map.of(
|
||||
COMPANY, company.id(),
|
||||
PARENT, Map.of(COMPANY, company.id()),
|
||||
NAME,company.name(),
|
||||
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);
|
||||
}
|
||||
|
||||
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 {
|
||||
var json = json(ex);
|
||||
if (!(json.get(FIELD_ITEM) instanceof JSONObject itemData)) throw missingFieldException(FIELD_ITEM);
|
||||
|
||||
Reference in New Issue
Block a user