implemented deletion of locations
This commit is contained in:
@@ -14,6 +14,7 @@ import static java.lang.System.Logger.Level.ERROR;
|
||||
import static java.lang.System.Logger.Level.WARNING;
|
||||
import static java.text.MessageFormat.format;
|
||||
|
||||
import de.srsoftware.tools.jdbc.Query;
|
||||
import de.srsoftware.umbrella.core.BaseDb;
|
||||
import de.srsoftware.umbrella.core.model.*;
|
||||
import de.srsoftware.umbrella.core.model.Location;
|
||||
@@ -56,6 +57,16 @@ public class SqliteDb extends BaseDb implements StockDb {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Location delete(DbLocation location) {
|
||||
try {
|
||||
Query.delete().from(TABLE_LOCATIONS).where(ID,equal(location.id())).execute(db);
|
||||
return location;
|
||||
} catch (SQLException e){
|
||||
throw databaseException("Failed to delete \"{0}\"",location.name());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* id, owner, owner_id, code, name, location_id
|
||||
* @throws SQLException
|
||||
@@ -333,7 +344,7 @@ public class SqliteDb extends BaseDb implements StockDb {
|
||||
rs.close();
|
||||
if (prop == null) throw databaseException("Failed to add new property to item {0}",itemId);
|
||||
if ("".equals(value)){
|
||||
delete().from(TABLE_ITEM_PROPERTIES).where(ITEM_ID,equal(itemId)).where(PROPERTY_ID,equal(existingPropId)).execute(db);
|
||||
Query.delete().from(TABLE_ITEM_PROPERTIES).where(ITEM_ID,equal(itemId)).where(PROPERTY_ID,equal(existingPropId)).execute(db);
|
||||
} else {
|
||||
replaceInto(TABLE_ITEM_PROPERTIES,ITEM_ID,PROPERTY_ID,VALUE).values(itemId,existingPropId,value).execute(db);
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import java.util.Collection;
|
||||
|
||||
public interface StockDb {
|
||||
Property addNewProperty(long itemId, String name, Object value, String unit);
|
||||
Location delete(DbLocation location);
|
||||
Collection<DbLocation> listChildLocations(long parentId);
|
||||
Collection<DbLocation> listCompanyLocations(Company company);
|
||||
Collection<Item> listItemsAt(Location location);
|
||||
|
||||
@@ -44,7 +44,39 @@ public class StockModule extends BaseHandler implements StockService {
|
||||
if (owner instanceof UmbrellaUser u && user.id() == u.id()) return true;
|
||||
if (owner instanceof Company comp) return companyService().membership(comp.id(),user.id());
|
||||
return false;
|
||||
}
|
||||
|
||||
private boolean deleteLocation(UmbrellaUser user, Location locationRef, HttpExchange ex) throws IOException {
|
||||
var location = locationRef.resolve();
|
||||
var owner = location.owner().resolve();
|
||||
if (!assigned(owner,user)) throw forbidden("You are not allowed to modify \"{0}\"",location);
|
||||
if (!stockDb.listItemsAt(location).isEmpty()) throw forbidden("\"{0}\" cannot be deleted, as it contains items!",location);
|
||||
if (!stockDb.listChildLocations(location.id()).isEmpty()) throw forbidden("\"{0}\" cannot be deleted, as it contains other locations!",location);
|
||||
return sendContent(ex,stockDb.delete(location));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean doDelete(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);
|
||||
var head = path.pop();
|
||||
return switch (head) {
|
||||
case LOCATION -> {
|
||||
try {
|
||||
var location = Location.of(Long.parseLong(path.pop()));
|
||||
yield deleteLocation(user.get(), location, ex);
|
||||
} catch (NumberFormatException e){
|
||||
yield super.doGet(path,ex);
|
||||
}
|
||||
}
|
||||
case null, default -> super.doDelete(path,ex);
|
||||
};
|
||||
} catch (UmbrellaException e){
|
||||
return send(ex,e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user