Merge branch 'module/poll' into dev
This commit is contained in:
@@ -16,8 +16,8 @@
|
||||
let res = await get(url);
|
||||
if (res.ok){
|
||||
poll = await res.json();
|
||||
if (poll.selection) selection = poll.selection;
|
||||
yikes();
|
||||
console.log(Object.entries(poll.weights).sort((a,b) => a[0] - b[0]));
|
||||
} else error(res);
|
||||
}
|
||||
|
||||
|
||||
@@ -14,6 +14,8 @@ public interface PollDb {
|
||||
|
||||
Poll loadPoll(String id);
|
||||
|
||||
Map<Integer,Integer> loadSelections(Poll poll, UmbrellaUser user);
|
||||
|
||||
Poll save(Poll poll);
|
||||
void saveSelection(Poll poll, Map<Integer,Integer> optionsToWeights, String guestName);
|
||||
|
||||
|
||||
@@ -96,7 +96,10 @@ public class PollModule extends BaseHandler implements PollService {
|
||||
var poll = pollDb.loadPoll(pollId);
|
||||
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);
|
||||
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 {
|
||||
@@ -207,7 +210,7 @@ public class PollModule extends BaseHandler implements PollService {
|
||||
var poll = pollDb.loadPoll(id);
|
||||
if (SELECT.equals(head)) {
|
||||
if (user == null && poll.isPrivate()) return unauthorized(ex);
|
||||
return postSelection(ex, poll, null);
|
||||
return postSelection(ex, poll, 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);
|
||||
|
||||
@@ -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
|
||||
public Poll save(Poll 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) {
|
||||
|
||||
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()){
|
||||
var optionId = entry.getKey();
|
||||
var weight = entry.getValue();
|
||||
|
||||
Reference in New Issue
Block a user