preparing to manage weights in poll

Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
This commit is contained in:
2026-02-25 09:17:25 +01:00
parent cbc6ce188d
commit 4a2c49c42c
6 changed files with 86 additions and 14 deletions

View File

@@ -5,8 +5,7 @@ import static de.srsoftware.umbrella.core.ConnectionProvider.connect;
import static de.srsoftware.umbrella.core.ModuleRegistry.userService;
import static de.srsoftware.umbrella.core.constants.Field.FIELD;
import static de.srsoftware.umbrella.core.constants.Field.ID;
import static de.srsoftware.umbrella.core.constants.Path.LIST;
import static de.srsoftware.umbrella.core.constants.Path.OPTION;
import static de.srsoftware.umbrella.core.constants.Path.*;
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.*;
import static de.srsoftware.umbrella.poll.Constants.CONFIG_DATABASE;
import static java.lang.System.Logger.Level.WARNING;
@@ -117,6 +116,7 @@ public class PollModule extends BaseHandler implements PollService {
return switch (head){
case null -> patchPoll(ex, poll);
case OPTION -> patchPollOptions(ex, path, poll);
case WEIGHT -> patchWeights(ex, poll);
default -> notFound(ex);
};
}
@@ -136,6 +136,11 @@ public class PollModule extends BaseHandler implements PollService {
return sendContent(ex,pollDb.save(poll));
}
private boolean patchWeights(HttpExchange ex, Poll poll) throws IOException {
// TODO
return notFound(ex);
}
private boolean patchPollOptions(HttpExchange ex, Path path, Poll poll) throws IOException {
try {
if (path.empty()) throw missingField(ID);
@@ -180,7 +185,7 @@ public class PollModule extends BaseHandler implements PollService {
var head = path.pop();
return switch (head){
case OPTION -> postOption(ex, poll);
default -> notFound(ex);
case null, default -> notFound(ex);
};
}

View File

@@ -10,13 +10,11 @@ import static de.srsoftware.umbrella.core.constants.Field.*;
import static de.srsoftware.umbrella.core.constants.Field.DESCRIPTION;
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.*;
import static de.srsoftware.umbrella.poll.Constants.*;
import static java.lang.System.Logger.Level.WARNING;
import static java.text.MessageFormat.format;
import de.srsoftware.umbrella.core.BaseDb;
import de.srsoftware.umbrella.core.constants.Field;
import de.srsoftware.umbrella.core.constants.Text;
import de.srsoftware.umbrella.core.exceptions.UmbrellaException;
import de.srsoftware.umbrella.core.model.Poll;
import de.srsoftware.umbrella.core.model.UmbrellaUser;
@@ -112,6 +110,15 @@ public class SqliteDb extends BaseDb implements PollDb {
}
}
private void dropWeight(Poll poll, int weight){
try {
delete().from(TABLE_WEIGHTS).where(POLL_ID, equal(poll.id())).where(ID, equal(weight)).execute(db);
poll.weights().remove(weight);
} catch (SQLException e){
throw failedToDropObject(Text.WEIGHT);
}
}
@Override
public Collection<Poll> listPolls(UmbrellaUser user) {
try {
@@ -145,7 +152,7 @@ public class SqliteDb extends BaseDb implements PollDb {
while (rs.next()) poll.options().add(Poll.Option.of(rs));
rs.close();
rs = select(ALL).from(TABLE_WEIGHTS).where(POLL_ID,equal(id)).exec(db);
rs = select(ALL).from(TABLE_WEIGHTS).where(POLL_ID,equal(id)).sort(WEIGHT+" ASC").exec(db);
while (rs.next()) {
var weight = rs.getInt(WEIGHT);
var descr = rs.getString(DESCRIPTION);
@@ -200,13 +207,25 @@ public class SqliteDb extends BaseDb implements PollDb {
}
private Poll update(Poll poll) {
if (poll.isDirty()) try {
LOG.log(WARNING,"Updating poll not fully implemented");
if (poll.isDirty(NAME, DESCRIPTION)) try {
replaceInto(TABLE_POLLS,ID,Field.USER_ID,NAME,DESCRIPTION).values(poll.id(),poll.owner().id(),poll.name(),poll.description()).execute(db);
} catch (SQLException e){
throw failedToStoreObject(poll);
}
if (poll.isDirty(WEIGHTS)) try {
Map.copyOf(poll.weights())
.entrySet().stream()
.filter(entry -> entry.getValue().isBlank())
.map(Map.Entry::getKey)
.forEach(w -> dropWeight(poll,w));
var query = replaceInto(TABLE_WEIGHTS, POLL_ID, WEIGHT, DESCRIPTION);
for (var entry : poll.weights().entrySet()){
query.values(poll.id(),entry.getKey(),entry.getValue());
}
query.execute(db).close();
} catch (SQLException e) {
throw failedToStoreObject(poll);
}
for (var option : poll.options()) saveOption(poll.id(),option);
return poll;
}