Browse Source

refactored location patching

Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
module/document
Stephan Richter 2 weeks ago
parent
commit
8bf0790fca
  1. 23
      core/src/main/java/de/srsoftware/umbrella/core/model/DbLocation.java
  2. 8
      frontend/src/routes/stock/Index.svelte
  3. 31
      stock/src/main/java/de/srsoftware/umbrella/stock/StockModule.java

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

@ -4,6 +4,8 @@ package de.srsoftware.umbrella.core.model; @@ -4,6 +4,8 @@ package de.srsoftware.umbrella.core.model;
import static de.srsoftware.umbrella.core.Constants.*;
import de.srsoftware.umbrella.core.api.Owner;
import org.json.JSONObject;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashSet;
@ -59,9 +61,24 @@ public class DbLocation extends Location { @@ -59,9 +61,24 @@ public class DbLocation extends Location {
return parentLocationId;
}
public DbLocation parent(DbLocation newParent) {
parentLocationId = newParent.id();
dirtyFields.add(PARENT_LOCATION_ID);
public DbLocation patch(JSONObject json) {
for (var field : json.keySet()){
boolean known = true;
switch (field) {
case PARENT_LOCATION_ID:
parentLocationId = json.getLong(field);
break;
case NAME:
name = json.getString(NAME);
break;
case DESCRIPTION:
description = json.getString(DESCRIPTION);
break;
default:
known = false;
}
if (known) dirtyFields.add(field);
}
return this;
}

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

@ -60,8 +60,8 @@ @@ -60,8 +60,8 @@
}
async function move_dragged_to(new_loc){
const data = draggedItem ? { item : draggedItem.id, target: new_loc.id } : { location : draggedLocation.id, target: new_loc.id }
const url = api(draggedItem ? 'stock/move_item' : 'stock/move_location');
const data = draggedItem ? { item : draggedItem.id, target: new_loc.id } : { parent_location_id: new_loc.id }
const url = api(draggedItem ? 'stock/move_item' : `stock/location/${draggedLocation.id}`);
const res = await fetch(url,{
credentials : 'include',
method : 'PATCH',
@ -125,10 +125,6 @@ @@ -125,10 +125,6 @@
onMount(load);
</script>
<style>
</style>
<h2>{t('Stock')}</h2>
<div class="grid3">
<div class="locations">

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

@ -114,7 +114,15 @@ public class StockModule extends BaseHandler implements StockService { @@ -114,7 +114,15 @@ public class StockModule extends BaseHandler implements StockService {
if (user.isEmpty()) return unauthorized(ex);
return switch (path.pop()){
case MOVE_ITEM -> patchMoveItem(user.get(), path,ex);
case MOVE_LOCATION -> patchMoveLocation(user.get(), path, ex);
case LOCATION -> {
try {
var id = Long.parseLong(path.pop());
yield patchLocation(id, user.get(), ex);
} catch (NumberFormatException nfe){
yield super.doPatch(path,ex);
}
}
case null -> patchItem(user.get(),ex);
default -> super.doPatch(path,ex);
};
@ -233,21 +241,20 @@ public class StockModule extends BaseHandler implements StockService { @@ -233,21 +241,20 @@ public class StockModule extends BaseHandler implements StockService {
return sendContent(ex,item);
}
private boolean patchMoveLocation(UmbrellaUser user, Path path, HttpExchange ex) throws IOException {
private boolean patchLocation(long locationId, UmbrellaUser user, HttpExchange ex) throws IOException {
var json = json(ex);
if (!(json.get(LOCATION) instanceof Number locationId)) throw missingFieldException(LOCATION);
if (!(json.get(TARGET) instanceof Number destLocationId)) throw missingFieldException(TARGET);
var location = stockDb.loadLocation(locationId.longValue());
var location = stockDb.loadLocation(locationId);
var owner = location.owner().resolve();
if (!assigned(owner,user)) throw forbidden("You are not allowed to alter the location of \"{0}\"!",location.name());
if (!assigned(owner,user)) throw forbidden("You are not allowed to edit \"{0}\"!",location.name());
var target = stockDb.loadLocation(destLocationId.longValue());
var locOwner = target.resolve().owner().resolve();
if (!assigned(locOwner,user)) throw forbidden("You are not allowed to modify \"{0}\"!",target.name());
if (json.has(PARENT_LOCATION_ID) && json.get(PARENT_LOCATION_ID) instanceof Number parentId){
var target = stockDb.loadLocation(parentId.longValue());
var targetOwner = target.owner().resolve();
if (!assigned(targetOwner,user)) throw forbidden("You are not allowed to edit \"{0}\"!",target.name());
if (!targetOwner.equals(owner)) throw unprocessable("You may not move locations from one owner ({0}) to another ({1})",owner,targetOwner);
}
if (!locOwner.equals(owner)) throw unprocessable("You may not move locations from one owner ({0}) to another ({1})",owner,locOwner);
location = stockDb.save(location.parent(target));
location = stockDb.save(location.patch(json));
return sendContent(ex,location);
}

Loading…
Cancel
Save