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 NUMBER = "number";
|
||||
|
||||
public static final Object OPTION = "option"
|
||||
;
|
||||
public static final String PATH = "path";
|
||||
public static final String POLL = "poll";
|
||||
public static final String POLLS = "polls";
|
||||
|
||||
@@ -8,16 +8,53 @@ import de.srsoftware.umbrella.core.constants.Field;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static de.srsoftware.tools.Optionals.is0;
|
||||
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 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 {
|
||||
var id = rs.getInt(ID);
|
||||
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);
|
||||
}
|
||||
|
||||
public Integer status(){
|
||||
return status;
|
||||
}
|
||||
|
||||
public Option status(int newValue){
|
||||
this.status = newValue;
|
||||
dirtyFields.add(STATUS);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> toMap() {
|
||||
return Map.of(
|
||||
@@ -39,6 +86,22 @@ public record Poll(String id, Owner owner, String name, String description, bool
|
||||
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 {
|
||||
@@ -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<>());
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Map<String, Object> toMap() {
|
||||
return Map.of(
|
||||
@@ -68,15 +132,4 @@ public record Poll(String id, Owner owner, String name, String description, bool
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user