implemented adding of new options
Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
This commit is contained in:
@@ -45,6 +45,8 @@ public class Text {
|
|||||||
public static final String NOTE_WITH_ID = "note ({id})";
|
public static final String NOTE_WITH_ID = "note ({id})";
|
||||||
public static final String NUMBER = "number";
|
public static final String NUMBER = "number";
|
||||||
|
|
||||||
|
public static final Object OPTION = "option"
|
||||||
|
;
|
||||||
public static final String PATH = "path";
|
public static final String PATH = "path";
|
||||||
public static final String POLL = "poll";
|
public static final String POLL = "poll";
|
||||||
public static final String POLLS = "polls";
|
public static final String POLLS = "polls";
|
||||||
|
|||||||
@@ -8,16 +8,53 @@ import de.srsoftware.umbrella.core.constants.Field;
|
|||||||
|
|
||||||
import java.sql.ResultSet;
|
import java.sql.ResultSet;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.util.ArrayList;
|
import java.util.*;
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
import static de.srsoftware.tools.Optionals.is0;
|
||||||
import static de.srsoftware.umbrella.core.constants.Field.*;
|
import static de.srsoftware.umbrella.core.constants.Field.*;
|
||||||
|
import static java.text.MessageFormat.format;
|
||||||
|
|
||||||
public record Poll(String id, Owner owner, String name, String description, boolean isPrivate, List<Option> options, Map<Integer,String> weights, Map<UmbrellaUser,Long> shares) implements Mappable {
|
public record Poll(String id, Owner owner, String name, String description, boolean isPrivate, List<Option> options, Map<Integer,String> weights, Map<UmbrellaUser,Long> shares) implements Mappable {
|
||||||
public record Option(Integer id, String name, String description, Integer status) implements Mappable{
|
public static class Option implements Mappable{
|
||||||
|
private int id;
|
||||||
|
Integer status;
|
||||||
|
private final String description, name;
|
||||||
|
private final Set<String> dirtyFields = new HashSet<>();
|
||||||
|
|
||||||
|
public Option(int id, String name, String description, Integer status) {
|
||||||
|
this.id = id;
|
||||||
|
this.name = name;
|
||||||
|
this.description = description;
|
||||||
|
this.status = status;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String description(){
|
||||||
|
return description;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int id(){
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Option id(Integer newVal){
|
||||||
|
this.id = newVal;
|
||||||
|
dirtyFields.add(ID);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isDirty(){
|
||||||
|
return !dirtyFields.isEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isNew(){
|
||||||
|
return dirtyFields.contains(ID);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String name(){
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
public static Option of(ResultSet rs) throws SQLException {
|
public static Option of(ResultSet rs) throws SQLException {
|
||||||
var id = rs.getInt(ID);
|
var id = rs.getInt(ID);
|
||||||
var name = rs.getString(NAME);
|
var name = rs.getString(NAME);
|
||||||
@@ -27,6 +64,16 @@ public record Poll(String id, Owner owner, String name, String description, bool
|
|||||||
return new Option(id,name,description,status);
|
return new Option(id,name,description,status);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Integer status(){
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Option status(int newValue){
|
||||||
|
this.status = newValue;
|
||||||
|
dirtyFields.add(STATUS);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Map<String, Object> toMap() {
|
public Map<String, Object> toMap() {
|
||||||
return Map.of(
|
return Map.of(
|
||||||
@@ -39,6 +86,22 @@ public record Poll(String id, Owner owner, String name, String description, bool
|
|||||||
STATUS,status
|
STATUS,status
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return format("Option \"{0}\"",name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Map<Long, Map<String, Object>> mapShares() {
|
||||||
|
var result = new HashMap<Long,Map<String,Object>>();
|
||||||
|
for (var entry : shares.entrySet()){
|
||||||
|
var user = entry.getKey();
|
||||||
|
var data = user.toMap();
|
||||||
|
data.put(Field.PERMISSION,entry.getValue());
|
||||||
|
result.put(user.id(),data);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Poll of(ResultSet rs) throws SQLException {
|
public static Poll of(ResultSet rs) throws SQLException {
|
||||||
@@ -52,6 +115,7 @@ public record Poll(String id, Owner owner, String name, String description, bool
|
|||||||
return new Poll(id,owner,name,description,isPrivate,new ArrayList<>(),new HashMap<>(),new HashMap<>());
|
return new Poll(id,owner,name,description,isPrivate,new ArrayList<>(),new HashMap<>(),new HashMap<>());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Map<String, Object> toMap() {
|
public Map<String, Object> toMap() {
|
||||||
return Map.of(
|
return Map.of(
|
||||||
@@ -68,15 +132,4 @@ public record Poll(String id, Owner owner, String name, String description, bool
|
|||||||
Field.WEIGHTS, weights
|
Field.WEIGHTS, weights
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Map<Long, Map<String, Object>> mapShares() {
|
|
||||||
var result = new HashMap<Long,Map<String,Object>>();
|
|
||||||
for (var entry : shares.entrySet()){
|
|
||||||
var user = entry.getKey();
|
|
||||||
var data = user.toMap();
|
|
||||||
data.put(Field.PERMISSION,entry.getValue());
|
|
||||||
result.put(user.id(),data);
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,6 +28,9 @@
|
|||||||
let res = await post(url,new_option);
|
let res = await post(url,new_option);
|
||||||
if (res.ok){
|
if (res.ok){
|
||||||
yikes();
|
yikes();
|
||||||
|
const json = await res.json();
|
||||||
|
console.log(json);
|
||||||
|
poll.options = json.options;
|
||||||
} else error(res);
|
} else error(res);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -109,7 +109,10 @@ public class PollModule extends BaseHandler implements PollService {
|
|||||||
if (val instanceof JSONObject j && j.has(Field.SOURCE)) val = j.get(Field.SOURCE);
|
if (val instanceof JSONObject j && j.has(Field.SOURCE)) val = j.get(Field.SOURCE);
|
||||||
if (val instanceof String d) description = d;
|
if (val instanceof String d) description = d;
|
||||||
}
|
}
|
||||||
var option = new Poll.Option(0, name, description, 0);
|
var options = poll.options();
|
||||||
|
int newId = options.stream().map(Poll.Option::id).max(Integer::compareTo).orElse(0) + 1;
|
||||||
|
|
||||||
|
var option = new Poll.Option(0, name, description, 0).id(newId);
|
||||||
poll.options().add(option);
|
poll.options().add(option);
|
||||||
return sendContent(ex,pollDb.save(poll));
|
return sendContent(ex,pollDb.save(poll));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,10 +3,8 @@ package de.srsoftware.umbrella.poll;
|
|||||||
|
|
||||||
import static de.srsoftware.tools.Optionals.is0;
|
import static de.srsoftware.tools.Optionals.is0;
|
||||||
import static de.srsoftware.tools.jdbc.Condition.equal;
|
import static de.srsoftware.tools.jdbc.Condition.equal;
|
||||||
import static de.srsoftware.tools.jdbc.Condition.isNull;
|
import static de.srsoftware.tools.jdbc.Query.*;
|
||||||
import static de.srsoftware.tools.jdbc.Query.SelectQuery.ALL;
|
import static de.srsoftware.tools.jdbc.Query.SelectQuery.ALL;
|
||||||
import static de.srsoftware.tools.jdbc.Query.insertInto;
|
|
||||||
import static de.srsoftware.tools.jdbc.Query.select;
|
|
||||||
import static de.srsoftware.umbrella.core.ModuleRegistry.userService;
|
import static de.srsoftware.umbrella.core.ModuleRegistry.userService;
|
||||||
import static de.srsoftware.umbrella.core.constants.Field.*;
|
import static de.srsoftware.umbrella.core.constants.Field.*;
|
||||||
import static de.srsoftware.umbrella.core.constants.Field.DESCRIPTION;
|
import static de.srsoftware.umbrella.core.constants.Field.DESCRIPTION;
|
||||||
@@ -18,6 +16,7 @@ import static java.text.MessageFormat.format;
|
|||||||
import de.srsoftware.umbrella.core.BaseDb;
|
import de.srsoftware.umbrella.core.BaseDb;
|
||||||
import de.srsoftware.umbrella.core.constants.Field;
|
import de.srsoftware.umbrella.core.constants.Field;
|
||||||
import de.srsoftware.umbrella.core.constants.Text;
|
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.Poll;
|
||||||
import de.srsoftware.umbrella.core.model.UmbrellaUser;
|
import de.srsoftware.umbrella.core.model.UmbrellaUser;
|
||||||
|
|
||||||
@@ -26,7 +25,6 @@ import java.sql.SQLException;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public class SqliteDb extends BaseDb implements PollDb {
|
public class SqliteDb extends BaseDb implements PollDb {
|
||||||
|
|
||||||
@@ -162,27 +160,38 @@ public class SqliteDb extends BaseDb implements PollDb {
|
|||||||
return is0(poll.id()) ? saveNew(poll) : update(poll);
|
return is0(poll.id()) ? saveNew(poll) : update(poll);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Poll.Option saveOption(Long pollId, Poll.Option option){
|
public Poll.Option saveOption(String pollId, Poll.Option option) {
|
||||||
return is0(option.id()) ? saveNew(pollId, option) : update(pollId, option);
|
try {
|
||||||
|
if (option.isNew()) return saveNew(pollId,option);
|
||||||
|
if (option.isDirty()) return update(pollId, option);
|
||||||
|
return option;
|
||||||
|
} catch (SQLException e){
|
||||||
|
throw failedToStoreObject(Text.OPTION);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private Poll saveNew(Poll poll) {
|
private Poll saveNew(Poll poll) {
|
||||||
throw new RuntimeException("Not implemented");
|
throw new RuntimeException("Not implemented");
|
||||||
}
|
}
|
||||||
|
|
||||||
private Poll saveNew(long pollId, Poll.Option option) {
|
private Poll.Option saveNew(String pollId, Poll.Option option) throws SQLException {
|
||||||
throw new RuntimeException("Not implemented");
|
insertInto(TABLE_OPTIONS, ID, POLL_ID, NAME, DESCRIPTION, STATUS)
|
||||||
|
.values(option.id(), pollId, option.name(), option.description(), option.status())
|
||||||
|
.execute(db);
|
||||||
|
return option;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Poll update(Poll poll) {
|
private Poll update(Poll poll) {
|
||||||
LOG.log(WARNING,"Updating poll not fully implemented");
|
LOG.log(WARNING,"Updating poll not fully implemented");
|
||||||
for (var option : poll.options()) saveOption(poll.id(),option);
|
for (var option : poll.options()) saveOption(poll.id(),option);
|
||||||
throw new RuntimeException("Not implemented");
|
return poll;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Poll update(long pollId, Poll.Option option) {
|
private Poll.Option update(String pollId, Poll.Option option) throws SQLException {
|
||||||
insertInto(TABLE_OPTIONS) // TODO
|
updateIgnore(TABLE_OPTIONS)
|
||||||
throw new RuntimeException("Not implemented");
|
.set(NAME, DESCRIPTION, STATUS).where(ID, equal(pollId)).prepare(db)
|
||||||
|
.apply(option.name(), option.description(), option.status()).execute();
|
||||||
|
return option;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user