working on poll evaluation

Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
This commit is contained in:
2026-03-03 15:33:25 +01:00
parent 1589bbe471
commit 6125bc62a2
9 changed files with 123 additions and 15 deletions

View File

@@ -17,4 +17,6 @@ public interface PollDb {
void saveSelection(Poll poll, Map<Integer,Integer> optionsToWeights, String guestName);
void saveSelection(Poll poll, Map<Integer,Integer> optionsToWeights, UmbrellaUser user);
Poll.Evaluation loadEvaluation(String id);
}

View File

@@ -8,6 +8,7 @@ import static de.srsoftware.umbrella.core.constants.Field.ID;
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;
import static java.text.MessageFormat.format;
import com.sun.net.httpserver.HttpExchange;
@@ -100,10 +101,13 @@ public class PollModule extends BaseHandler implements PollService {
private boolean getPollEvaluation(HttpExchange ex, UmbrellaUser user, Path path) throws IOException {
if (path.empty()) throw missingField(ID);
var poll = loadPoll(user,path.pop());
// TODO: load data required for evaluation
return sendContent(ex,poll);
var poll = loadPoll(user,path.pop());
LOG.log(WARNING,"Mising permission check for poll evaluation");
// TODO: check permissions
var result = new HashMap<>(poll.toMap());
var evaluation = pollDb.loadEvaluation(poll.id());
result.put(Field.EVALUATION,evaluation.toMap());
return sendContent(ex,result);
}
private boolean getPollList(HttpExchange ex, UmbrellaUser user) throws IOException {

View File

@@ -134,12 +134,26 @@ public class SqliteDb extends BaseDb implements PollDb {
poll.shares().put(user,rs.getLong(PERMISSION));
list.add(poll);
}
rs.close();
return list;
} catch (SQLException sqle){
throw failedToLoadObject(TABLE_POLLS);
}
}
@Override
public Poll.Evaluation loadEvaluation(String pollId) {
try {
var result = new Poll.Evaluation();
var rs = select(ALL).from(TABLE_SELECTIONS).where(POLL_ID,equal(pollId)).exec(db);
while (rs.next()) result.count(rs);
rs.close();
return result;
} catch (SQLException e) {
throw failedToLoadObject(Text.EVALUATION);
}
}
@Override
public Poll loadPoll(String id) {
try {
@@ -193,7 +207,7 @@ public class SqliteDb extends BaseDb implements PollDb {
try {
insertInto(TABLE_POLLS,Field.ID, USER_ID, NAME, DESCRIPTION, PRIVATE)
.values(uuid,poll.owner().id(),poll.name(),poll.description(),poll.isPrivate())
.execute(db);
.execute(db).close();
} catch (SQLException e) {
throw failedToStoreObject(poll);
}
@@ -203,7 +217,7 @@ public class SqliteDb extends BaseDb implements PollDb {
private Poll.Option saveNew(String pollId, Poll.Option option) throws SQLException {
insertInto(TABLE_OPTIONS, ID, POLL_ID, NAME, DESCRIPTION, STATUS)
.values(option.id(), pollId, option.name(), option.description(), option.status())
.execute(db);
.execute(db).close();
return option;
}
@@ -218,7 +232,8 @@ public class SqliteDb extends BaseDb implements PollDb {
}
private void saveSelection(String pollId, Map<Integer, Integer> optionsToWeights, Object editor) {
var query = insertInto(TABLE_SELECTIONS,POLL_ID,OPTION_ID,USER_ID,WEIGHT);
var query = replaceInto(TABLE_SELECTIONS,POLL_ID,OPTION_ID,USER_ID,WEIGHT);
for (var entry : optionsToWeights.entrySet()){
var optionId = entry.getKey();
var weight = entry.getValue();
@@ -233,7 +248,7 @@ public class SqliteDb extends BaseDb implements PollDb {
private Poll update(Poll poll) {
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);
replaceInto(TABLE_POLLS,ID,Field.USER_ID,NAME,DESCRIPTION).values(poll.id(),poll.owner().id(),poll.name(),poll.description()).execute(db).close();
} catch (SQLException e){
throw failedToStoreObject(poll);
}
@@ -261,7 +276,7 @@ public class SqliteDb extends BaseDb implements PollDb {
return null;
}
replaceInto(TABLE_OPTIONS,POLL_ID, ID, NAME, DESCRIPTION, STATUS)
.values(pollId, option.id(), option.name(), option.description(), option.status()).execute(db);
.values(pollId, option.id(), option.name(), option.description(), option.status()).execute(db).close();
return option;
}