Merge branch 'module/poll' into dev
All checks were successful
Build Docker Image / Docker-Build (push) Successful in 2m23s
Build Docker Image / Clean-Registry (push) Successful in -6s

This commit is contained in:
2026-03-10 15:34:23 +01:00
4 changed files with 23 additions and 4 deletions

View File

@@ -16,8 +16,8 @@
let res = await get(url); let res = await get(url);
if (res.ok){ if (res.ok){
poll = await res.json(); poll = await res.json();
if (poll.selection) selection = poll.selection;
yikes(); yikes();
console.log(Object.entries(poll.weights).sort((a,b) => a[0] - b[0]));
} else error(res); } else error(res);
} }

View File

@@ -14,6 +14,8 @@ public interface PollDb {
Poll loadPoll(String id); Poll loadPoll(String id);
Map<Integer,Integer> loadSelections(Poll poll, UmbrellaUser user);
Poll save(Poll poll); Poll save(Poll poll);
void saveSelection(Poll poll, Map<Integer,Integer> optionsToWeights, String guestName); void saveSelection(Poll poll, Map<Integer,Integer> optionsToWeights, String guestName);

View File

@@ -96,7 +96,10 @@ public class PollModule extends BaseHandler implements PollService {
var poll = pollDb.loadPoll(pollId); var poll = pollDb.loadPoll(pollId);
var permitted = !poll.isPrivate() || poll.owner().equals(user); var permitted = !poll.isPrivate() || poll.owner().equals(user);
if (!permitted && poll.permissions().get(user) == null) throw forbidden(Text.NOT_ALLOWED_TO_EDIT, Field.OBJECT,Text.POLL); if (!permitted && poll.permissions().get(user) == null) throw forbidden(Text.NOT_ALLOWED_TO_EDIT, Field.OBJECT,Text.POLL);
return sendContent(ex,poll); var result = new HashMap<>(poll.toMap());
if (user != null) result.put(SELECTION,pollDb.loadSelections(poll, user));
return sendContent(ex,result);
} }
private boolean getPollEvaluation(HttpExchange ex, UmbrellaUser user, Path path) throws IOException { private boolean getPollEvaluation(HttpExchange ex, UmbrellaUser user, Path path) throws IOException {
@@ -207,7 +210,7 @@ public class PollModule extends BaseHandler implements PollService {
var poll = pollDb.loadPoll(id); var poll = pollDb.loadPoll(id);
if (SELECT.equals(head)) { if (SELECT.equals(head)) {
if (user == null && poll.isPrivate()) return unauthorized(ex); if (user == null && poll.isPrivate()) return unauthorized(ex);
return postSelection(ex, poll, null); return postSelection(ex, poll, user);
} }
var permitted = poll.owner().equals(user); var permitted = poll.owner().equals(user);
if (!permitted && !Set.of(Permission.OWNER, Permission.EDIT).contains(poll.permissions().get(user))) throw forbidden(Text.NOT_ALLOWED_TO_EDIT, Field.OBJECT,Text.POLL); if (!permitted && !Set.of(Permission.OWNER, Permission.EDIT).contains(poll.permissions().get(user))) throw forbidden(Text.NOT_ALLOWED_TO_EDIT, Field.OBJECT,Text.POLL);

View File

@@ -196,6 +196,20 @@ public class SqliteDb extends BaseDb implements PollDb {
} }
} }
@Override
public Map<Integer, Integer> loadSelections(Poll poll, UmbrellaUser user) {
try {
var map = new HashMap<Integer,Integer>();
var rs = select(ALL).from(TABLE_SELECTIONS).where(POLL_ID, equal(poll.id())).where(USER, equal(user.id())).exec(db);
while (rs.next()) map.put(rs.getInt(OPTION_ID),rs.getInt(WEIGHT));
rs.close();
return map;
} catch (SQLException e) {
throw failedToLoadObject(Text.SELECTIONS);
}
}
@Override @Override
public Poll save(Poll poll) { public Poll save(Poll poll) {
return is0(poll.id()) ? saveNew(poll) : update(poll); return is0(poll.id()) ? saveNew(poll) : update(poll);
@@ -242,7 +256,7 @@ public class SqliteDb extends BaseDb implements PollDb {
private void saveSelection(String pollId, Map<Integer, Integer> optionsToWeights, Object editor) { private void saveSelection(String pollId, Map<Integer, Integer> optionsToWeights, Object editor) {
var query = replaceInto(TABLE_SELECTIONS,POLL_ID,OPTION_ID,USER_ID,WEIGHT); var query = replaceInto(TABLE_SELECTIONS,POLL_ID,OPTION_ID,USER,WEIGHT);
for (var entry : optionsToWeights.entrySet()){ for (var entry : optionsToWeights.entrySet()){
var optionId = entry.getKey(); var optionId = entry.getKey();
var weight = entry.getValue(); var weight = entry.getValue();