working on management of poll weights

Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
This commit is contained in:
2026-02-25 15:34:01 +01:00
parent 4a2c49c42c
commit 5336384e0d
3 changed files with 20 additions and 8 deletions

View File

@@ -43,9 +43,7 @@
async function patch_option(option, field, newVal){ async function patch_option(option, field, newVal){
let url = api(`poll/${id}/option/${option.id}`); let url = api(`poll/${id}/option/${option.id}`);
let data = {} let res = await patch(url,{[field]: newVal});
data[field] = newVal;
let res = await patch(url,data);
if (res.ok) { if (res.ok) {
yikes(); yikes();
const json = await res.json(); const json = await res.json();
@@ -64,7 +62,14 @@
if (res.ok) { if (res.ok) {
yikes(); yikes();
const json = await res.json(); const json = await res.json();
console.log(json); const weights = json.weights;
for (let weight of Object.keys(data)){
let desc = data[weight];
console.log(weight, desc);
if (desc) {
poll.weights[weight] = desc;
} else delete poll.weights[weight]; // TODO: this corrupts the display of the following element!
}
return true; return true;
} }
error(res); error(res);
@@ -153,7 +158,7 @@
<input type="number" value={weight} /> <input type="number" value={weight} />
</td> </td>
<td> <td>
<LineEditor value={descr} onSet={desc => patch_weight({weight: desc})} /> <LineEditor editable={true} value={descr} onSet={desc => patch_weight({[weight]: desc})} />
</td> </td>
</tr> </tr>
{/each} {/each}

View File

@@ -137,8 +137,15 @@ public class PollModule extends BaseHandler implements PollService {
} }
private boolean patchWeights(HttpExchange ex, Poll poll) throws IOException { private boolean patchWeights(HttpExchange ex, Poll poll) throws IOException {
// TODO var json = json(ex);
return notFound(ex); for (var key : json.keySet()) try {
var weight = Integer.parseInt(key);
if (!(json.get(key) instanceof String description)) throw invalidField(Field.DESCRIPTION,Text.STRING);
poll.setWeight(weight,description);
} catch (NumberFormatException nfe) {
throw invalidField(Text.WEIGHT,Text.NUMBER);
}
return sendContent(ex,pollDb.save(poll));
} }
private boolean patchPollOptions(HttpExchange ex, Path path, Poll poll) throws IOException { private boolean patchPollOptions(HttpExchange ex, Path path, Poll poll) throws IOException {

View File

@@ -112,7 +112,7 @@ public class SqliteDb extends BaseDb implements PollDb {
private void dropWeight(Poll poll, int weight){ private void dropWeight(Poll poll, int weight){
try { try {
delete().from(TABLE_WEIGHTS).where(POLL_ID, equal(poll.id())).where(ID, equal(weight)).execute(db); delete().from(TABLE_WEIGHTS).where(POLL_ID, equal(poll.id())).where(WEIGHT, equal(weight)).execute(db);
poll.weights().remove(weight); poll.weights().remove(weight);
} catch (SQLException e){ } catch (SQLException e){
throw failedToDropObject(Text.WEIGHT); throw failedToDropObject(Text.WEIGHT);