Browse Source

preparing to delete locations

Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
module/document
Stephan Richter 2 weeks ago
parent
commit
59e6a7001d
  1. 10
      core/src/main/java/de/srsoftware/umbrella/core/model/DbLocation.java
  2. 21
      core/src/main/java/de/srsoftware/umbrella/core/model/Location.java
  3. 2
      frontend/src/routes/stock/Index.svelte
  4. 5
      frontend/src/routes/stock/Locations.svelte
  5. 20
      stock/src/main/java/de/srsoftware/umbrella/stock/SqliteDb.java
  6. 13
      stock/src/main/java/de/srsoftware/umbrella/stock/StockDb.java

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

@ -50,11 +50,11 @@ public class DbLocation extends Location {
@Override @Override
public Map<String, Object> toMap() { public Map<String, Object> toMap() {
return Map.of( var map = super.toMap();
OWNER,owner.toMap(), map.put(OWNER,owner.toMap());
ID,id(), map.put(NAME,name);
NAME,name, map.put(DESCRIPTION,description);
DESCRIPTION,description); return map;
} }
@Override @Override

21
core/src/main/java/de/srsoftware/umbrella/core/model/Location.java

@ -9,16 +9,27 @@ import static java.text.MessageFormat.format;
import de.srsoftware.tools.Mappable; import de.srsoftware.tools.Mappable;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
public class Location implements Mappable { public class Location implements Mappable {
private final long id; private long id;
public Location(long id){ public Location(long id){
this.id = id; this.id = id;
} }
public long id(){
return id;
}
public <T extends Location> T id(long newValue){
id = newValue;
//noinspection unchecked
return (T) this;
}
public static Location of(ResultSet rs) throws SQLException { public static Location of(ResultSet rs) throws SQLException {
return new Location(rs.getLong(LOCATION_ID)); return new Location(rs.getLong(LOCATION_ID));
} }
@ -31,13 +42,11 @@ public class Location implements Mappable {
return stockService().loadLocation(id()); return stockService().loadLocation(id());
} }
public long id(){
return id;
}
@Override @Override
public Map<String, Object> toMap() { public Map<String, Object> toMap() {
return Map.of(ID,id); var map = new HashMap<String,Object>();
map.put(ID,id);
return map;
} }
@Override @Override

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

@ -102,7 +102,7 @@
{:then data} {:then data}
<div class="items"> <div class="items">
{#if location} {#if location}
<h3>{location.name}</h3> <h3>{location.name} <button class="symbol" title={t('delete_object',{object:t('location')})} onclick={e => deleteLocation(location)}></button></h3>
{/if} {/if}
<ItemList items={data?.items.sort((a,b) => a.code.localeCompare(b.code))} bind:selected={item} drag_start={item => draggedItem = item} /> <ItemList items={data?.items.sort((a,b) => a.code.localeCompare(b.code))} bind:selected={item} drag_start={item => draggedItem = item} />
</div> </div>

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

@ -52,9 +52,11 @@
}); });
if (res.ok){ if (res.ok){
yikes; yikes;
const saved = await res.json();
locations.push(saved);
return true; return true;
} else { } else {
error(ok); error(res);
return false; return false;
} }
} }
@ -67,7 +69,6 @@
name: new_location_name, name: new_location_name,
parent: parent parent: parent
} }
console.log(JSON.parse(JSON.stringify(data)));
} }
function show_loc_form(ev){ function show_loc_form(ev){

20
stock/src/main/java/de/srsoftware/umbrella/stock/SqliteDb.java

@ -276,6 +276,26 @@ 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 Location save(DbLocation location) {
if (location.id() == 0) { // new location
try {
var rs = insertInto(TABLE_LOCATIONS,OWNER,PARENT_LOCATION_ID,NAME,DESCRIPTION)
.values(location.owner().dbCode(),location.parent(),location.name(),null)
.execute(db).getGeneratedKeys();
long id = 0;
if (rs.next()) id = rs.getLong(1);
rs.close();
if (id == 0) throw databaseException("Failed to save new location ({0})",location.name());
return location.id(id);
} catch (SQLException e){
throw databaseException("Failed to save new location ({0})",location.name());
}
} else {
throw databaseException("Updating locations not implemented");
}
}
@Override @Override
public Item save(Item item) { public Item save(Item item) {
if (item.id() == 0){ // TODO if (item.id() == 0){ // TODO

13
stock/src/main/java/de/srsoftware/umbrella/stock/StockDb.java

@ -6,14 +6,15 @@ import de.srsoftware.umbrella.core.model.Location;
import java.util.Collection; import java.util.Collection;
public interface StockDb { public interface StockDb {
Property addNewProperty(long itemId, String name, Object value, String unit); Property addNewProperty(long itemId, String name, Object value, String unit);
Collection<DbLocation> listChildLocations(long parentId); Collection<DbLocation> listChildLocations(long parentId);
Collection<DbLocation> listCompanyLocations(Company company); Collection<DbLocation> listCompanyLocations(Company company);
Collection<Item> listItemsAt(Location location); Collection<Item> listItemsAt(Location location);
Collection<Property> listProperties(); Collection<Property> listProperties();
Collection<DbLocation> listUserLocations(UmbrellaUser userId); Collection<DbLocation> listUserLocations(UmbrellaUser userId);
Item loadItem(long id); Item loadItem(long id);
DbLocation loadLocation(long locationId); DbLocation loadLocation(long locationId);
Item save(Item item); Location save(DbLocation location);
Property setProperty(long itemId, long existingPropId, Object value); Item save(Item item);
Property setProperty(long itemId, long existingPropId, Object value);
} }

Loading…
Cancel
Save