preparing to update item base data
Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
This commit is contained in:
@@ -4,11 +4,14 @@ package de.srsoftware.umbrella.core.model;
|
|||||||
import static de.srsoftware.umbrella.core.Constants.*;
|
import static de.srsoftware.umbrella.core.Constants.*;
|
||||||
|
|
||||||
import de.srsoftware.tools.Mappable;
|
import de.srsoftware.tools.Mappable;
|
||||||
|
import org.json.JSONObject;
|
||||||
|
|
||||||
import java.sql.ResultSet;
|
import java.sql.ResultSet;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
public class Item implements Mappable {
|
public class Item implements Mappable {
|
||||||
private long id;
|
private long id;
|
||||||
@@ -16,6 +19,7 @@ public class Item implements Mappable {
|
|||||||
private String code, name;
|
private String code, name;
|
||||||
private Location location;
|
private Location location;
|
||||||
private Collection<Property> properties;
|
private Collection<Property> properties;
|
||||||
|
private Set<String> dirtyFields = new HashSet<>();
|
||||||
|
|
||||||
private Item(Mappable owner, long id, Location location, String code, String name) {
|
private Item(Mappable owner, long id, Location location, String code, String name) {
|
||||||
this.owner = owner;
|
this.owner = owner;
|
||||||
@@ -26,6 +30,10 @@ public class Item implements Mappable {
|
|||||||
this.properties = new HashSet<>();
|
this.properties = new HashSet<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isDirty(){
|
||||||
|
return !dirtyFields.isEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
public long id(){
|
public long id(){
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
@@ -62,4 +70,19 @@ public class Item implements Mappable {
|
|||||||
public String toString() {
|
public String toString() {
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Item patch(JSONObject json) {
|
||||||
|
for (var field : json.keySet()){
|
||||||
|
var known = true;
|
||||||
|
switch (field) {
|
||||||
|
case NAME:
|
||||||
|
name = json.getString(field);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
known = false;
|
||||||
|
}
|
||||||
|
if (known) dirtyFields.add(field);
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
<script>
|
<script>
|
||||||
|
import LineEditor from '../../Components/LineEditor.svelte';
|
||||||
import { onMount } from 'svelte';
|
import { onMount } from 'svelte';
|
||||||
import { api } from '../../urls.svelte';
|
import { api } from '../../urls.svelte';
|
||||||
import { error, yikes } from '../../warn.svelte';
|
import { error, yikes } from '../../warn.svelte';
|
||||||
@@ -40,10 +41,25 @@
|
|||||||
yikes();
|
yikes();
|
||||||
} else error(res);
|
} else error(res);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function updateTitle(newVal){
|
||||||
|
const url = api('stock');
|
||||||
|
const data = {
|
||||||
|
id : item.id,
|
||||||
|
owner : item.owner,
|
||||||
|
title : newVal
|
||||||
|
}
|
||||||
|
const res = await fetch(url,{
|
||||||
|
credentials:'include',
|
||||||
|
method:'PATCH',
|
||||||
|
body:JSON.stringify(data)
|
||||||
|
});
|
||||||
|
return false;
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
{#if item}
|
{#if item}
|
||||||
<h3>{item.name}</h3>
|
<LineEditor type="h3" editable={true} value={item.name} onSet={updateTitle}/>
|
||||||
|
|
||||||
<table>
|
<table>
|
||||||
<tbody>
|
<tbody>
|
||||||
|
|||||||
@@ -222,6 +222,21 @@ public class SqliteDb extends BaseDb implements StockDb {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Item loadItem(Mappable owner, long itemId) {
|
||||||
|
var ownerId = owner instanceof Company comp ? -comp.id() : (owner instanceof UmbrellaUser u ? u.id() : 0);
|
||||||
|
if (ownerId == 0) throw databaseException("Failed to load item: unknown owner type ({0})",owner.getClass().getSimpleName());
|
||||||
|
try {
|
||||||
|
var rs = select(ALL).from(TABLE_ITEMS).where(OWNER,equal(ownerId)).where(ID,equal(itemId)).exec(db);
|
||||||
|
Item result = null;
|
||||||
|
if (rs.next()) result = Item.of(rs,owner,null);
|
||||||
|
rs.close();
|
||||||
|
if (result != null) return result;
|
||||||
|
} catch (SQLException ignored) {
|
||||||
|
}
|
||||||
|
throw databaseException("Failed to load item");
|
||||||
|
}
|
||||||
|
|
||||||
private Location loadLocation(long locationId) {
|
private Location loadLocation(long locationId) {
|
||||||
try {
|
try {
|
||||||
var rs = select(ALL).from(TABLE_LOCATIONS).where(ID,equal(locationId)).exec(db);
|
var rs = select(ALL).from(TABLE_LOCATIONS).where(ID,equal(locationId)).exec(db);
|
||||||
@@ -276,6 +291,12 @@ public class SqliteDb extends BaseDb implements StockDb {
|
|||||||
db.prepareStatement(format("ALTER TABLE {0} RENAME TO {1}","locations_temp",TABLE_LOCATIONS)).execute();
|
db.prepareStatement(format("ALTER TABLE {0} RENAME TO {1}","locations_temp",TABLE_LOCATIONS)).execute();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Item save(Item item) {
|
||||||
|
LOG.log(ERROR,"StockDb.save(…) not implemented!");
|
||||||
|
return item;
|
||||||
|
}
|
||||||
|
|
||||||
private void transformItems(Map<String, Long> oldLocationIdsToNew) throws SQLException {
|
private void transformItems(Map<String, Long> oldLocationIdsToNew) throws SQLException {
|
||||||
var rs = select(ALL).from(TABLE_ITEMS).exec(db);
|
var rs = select(ALL).from(TABLE_ITEMS).exec(db);
|
||||||
var insert = insertInto("items_temp",OWNER, ID, CODE, NAME, LOCATION_ID);
|
var insert = insertInto("items_temp",OWNER, ID, CODE, NAME, LOCATION_ID);
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
/* © SRSoftware 2025 */
|
/* © SRSoftware 2025 */
|
||||||
package de.srsoftware.umbrella.stock;
|
package de.srsoftware.umbrella.stock;
|
||||||
|
|
||||||
|
import de.srsoftware.tools.Mappable;
|
||||||
import de.srsoftware.umbrella.core.model.*;
|
import de.srsoftware.umbrella.core.model.*;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
|
||||||
@@ -12,4 +13,7 @@ public interface StockDb {
|
|||||||
Collection<Item> listItemsAt(long locationId);
|
Collection<Item> listItemsAt(long locationId);
|
||||||
Collection<Property> listProperties();
|
Collection<Property> listProperties();
|
||||||
Collection<Location> listUserLocations(UmbrellaUser userId);
|
Collection<Location> listUserLocations(UmbrellaUser userId);
|
||||||
|
Item loadItem(Mappable owner, long itemId);
|
||||||
|
|
||||||
|
Item save(Item item);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ import static java.util.Comparator.comparing;
|
|||||||
|
|
||||||
import com.sun.net.httpserver.HttpExchange;
|
import com.sun.net.httpserver.HttpExchange;
|
||||||
import de.srsoftware.configuration.Configuration;
|
import de.srsoftware.configuration.Configuration;
|
||||||
|
import de.srsoftware.tools.Mappable;
|
||||||
import de.srsoftware.tools.Path;
|
import de.srsoftware.tools.Path;
|
||||||
import de.srsoftware.tools.SessionToken;
|
import de.srsoftware.tools.SessionToken;
|
||||||
import de.srsoftware.umbrella.core.BaseHandler;
|
import de.srsoftware.umbrella.core.BaseHandler;
|
||||||
@@ -62,6 +63,19 @@ public class StockModule extends BaseHandler implements StockService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean doPatch(Path path, HttpExchange ex) throws IOException {
|
||||||
|
addCors(ex);
|
||||||
|
try {
|
||||||
|
Optional<Token> token = SessionToken.from(ex).map(Token::of);
|
||||||
|
var user = userService().loadUser(token);
|
||||||
|
if (user.isEmpty()) return unauthorized(ex);
|
||||||
|
return patchItem(user.get(),ex);
|
||||||
|
} catch (UmbrellaException e){
|
||||||
|
return send(ex,e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean doPost(Path path, HttpExchange ex) throws IOException {
|
public boolean doPost(Path path, HttpExchange ex) throws IOException {
|
||||||
addCors(ex);
|
addCors(ex);
|
||||||
@@ -130,6 +144,31 @@ public class StockModule extends BaseHandler implements StockService {
|
|||||||
return sendContent(ex, result);
|
return sendContent(ex, result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean patchItem(UmbrellaUser user, HttpExchange ex) throws IOException {
|
||||||
|
var json = json(ex);
|
||||||
|
if (!(json.get(ID) instanceof Number id)) throw missingFieldException(ID);
|
||||||
|
if (!(json.get(OWNER) instanceof JSONObject ownerRef)) throw missingFieldException(OWNER);
|
||||||
|
json.remove(ID);
|
||||||
|
json.remove(OWNER);
|
||||||
|
var owner = toOwner(ownerRef);
|
||||||
|
|
||||||
|
var item = stockDb.loadItem(owner,id.longValue());
|
||||||
|
item.patch(json);
|
||||||
|
return sendContent(ex,stockDb.save(item));
|
||||||
|
}
|
||||||
|
|
||||||
|
private Mappable toOwner(JSONObject owner) {
|
||||||
|
var keys = owner.keySet();
|
||||||
|
if (keys.size() != 1) throw unprocessable("{0} expected to have only one child!",OWNER);
|
||||||
|
String key = new ArrayList<>(keys).getFirst();
|
||||||
|
return switch (key) {
|
||||||
|
case COMPANY -> companyService().get(owner.getLong(key));
|
||||||
|
case USER -> userService().loadUser(owner.getLong(key));
|
||||||
|
default -> throw invalidFieldException(format("Single child of {0}", OWNER), format("either {0} or {1}", COMPANY, USER));
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
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);
|
||||||
@@ -139,14 +178,7 @@ public class StockModule extends BaseHandler implements StockService {
|
|||||||
if (!propData.has(VALUE)) throw missingFieldException(VALUE);
|
if (!propData.has(VALUE)) throw missingFieldException(VALUE);
|
||||||
var value = propData.get(VALUE);
|
var value = propData.get(VALUE);
|
||||||
if (value == null) throw missingFieldException(VALUE);
|
if (value == null) throw missingFieldException(VALUE);
|
||||||
var keys = owner.keySet();
|
var ownerId = toOwnerId(owner);
|
||||||
if (keys.size() != 1) throw unprocessable("{0} expected to have only one child!",OWNER);
|
|
||||||
String key = new ArrayList<>(keys).getFirst();
|
|
||||||
long ownerId = switch (key) {
|
|
||||||
case COMPANY -> -owner.getLong(key);
|
|
||||||
case USER -> owner.getLong(key);
|
|
||||||
default -> throw invalidFieldException(format("Single child of {0}", OWNER), format("either {0} or {1}", COMPANY, USER));
|
|
||||||
};
|
|
||||||
|
|
||||||
Property property = null;
|
Property property = null;
|
||||||
if (propData.get("existing_prop_id") instanceof Number existingPropId && existingPropId.longValue() != 0L){
|
if (propData.get("existing_prop_id") instanceof Number existingPropId && existingPropId.longValue() != 0L){
|
||||||
@@ -160,6 +192,18 @@ public class StockModule extends BaseHandler implements StockService {
|
|||||||
return sendContent(ex,property);
|
return sendContent(ex,property);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private long toOwnerId(JSONObject owner) {
|
||||||
|
var keys = owner.keySet();
|
||||||
|
if (keys.size() != 1) throw unprocessable("{0} expected to have only one child!",OWNER);
|
||||||
|
String key = new ArrayList<>(keys).getFirst();
|
||||||
|
return switch (key) {
|
||||||
|
case COMPANY -> -owner.getLong(key);
|
||||||
|
case USER -> owner.getLong(key);
|
||||||
|
default -> throw invalidFieldException(format("Single child of {0}", OWNER), format("either {0} or {1}", COMPANY, USER));
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Collection<Object> redefineMe(long company_id) {
|
public Collection<Object> redefineMe(long company_id) {
|
||||||
return List.of();
|
return List.of();
|
||||||
|
|||||||
Reference in New Issue
Block a user