working on wiki module: adding users+permissions, adding notes, adding tags
adding tag currently does not work due to the tag module not allowing for strings as entity ids
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
package de.srsoftware.umbrella.core.model;
|
||||
|
||||
import de.srsoftware.tools.Mappable;
|
||||
import de.srsoftware.umbrella.core.exceptions.UmbrellaException;
|
||||
import de.srsoftware.umbrella.core.api.UserService;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
@@ -10,6 +10,7 @@ import java.util.*;
|
||||
|
||||
import static de.srsoftware.umbrella.core.Constants.*;
|
||||
import static de.srsoftware.umbrella.core.Util.markdown;
|
||||
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.invalidFieldException;
|
||||
|
||||
public class WikiPage implements Mappable {
|
||||
|
||||
@@ -30,10 +31,33 @@ public class WikiPage implements Mappable {
|
||||
return content;
|
||||
}
|
||||
|
||||
private WikiPage content(String newVal) {
|
||||
if (content.equals(newVal)) return this;
|
||||
content = newVal;
|
||||
version++;
|
||||
dirtyFields.add(CONTENT);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Set<String> dirtyFields(){
|
||||
return Set.copyOf(dirtyFields);
|
||||
}
|
||||
|
||||
public String id(){
|
||||
return id;
|
||||
}
|
||||
|
||||
private WikiPage id(String newVal) {
|
||||
if (id.equals(newVal)) return this;
|
||||
id = newVal;
|
||||
dirtyFields.add(ID);
|
||||
return this;
|
||||
}
|
||||
|
||||
public boolean isDirty(String field) {
|
||||
return dirtyFields.contains(field);
|
||||
}
|
||||
|
||||
public Map<Long,Member> members(){
|
||||
return members;
|
||||
}
|
||||
@@ -42,23 +66,41 @@ public class WikiPage implements Mappable {
|
||||
return new WikiPage(rs.getString(ID),rs.getInt(VERSION),rs.getString(CONTENT));
|
||||
}
|
||||
|
||||
public WikiPage patch(JSONObject json) {
|
||||
public WikiPage patch(JSONObject json, UserService users) {
|
||||
for (var key : json.keySet()){
|
||||
var val = json.get(key);
|
||||
var altered = true;
|
||||
switch (key){
|
||||
case ID:
|
||||
if (!(val instanceof String s)) throw UmbrellaException.invalidFieldException(ID,"String");
|
||||
id = s; break;
|
||||
if (!(val instanceof String s)) throw invalidFieldException(ID,"String");
|
||||
id(s);
|
||||
break;
|
||||
case CONTENT:
|
||||
if (!(val instanceof String s)) throw UmbrellaException.invalidFieldException(CONTENT,"String");
|
||||
content = s; break;
|
||||
default:
|
||||
altered = false;
|
||||
}
|
||||
if (altered) {
|
||||
dirtyFields.add(key);
|
||||
version++;
|
||||
if (!(val instanceof String s)) throw invalidFieldException(CONTENT,"String");
|
||||
content(s);
|
||||
break;
|
||||
case MEMBERS:
|
||||
if (!(val instanceof JSONObject membersJson)) throw invalidFieldException(MEMBERS,"Json");
|
||||
for (var uid : membersJson.keySet()){
|
||||
var userId = Long.parseLong(uid);
|
||||
var user = users.loadUser(userId);
|
||||
if (!(membersJson.get(uid) instanceof JSONObject memberJSON)) throw invalidFieldException(MEMBERS+"."+uid,"Json");
|
||||
var p = memberJSON.get(PERMISSION);
|
||||
if (p == JSONObject.NULL){
|
||||
members.remove(userId);
|
||||
dirtyFields.add(MEMBERS);
|
||||
break;
|
||||
}
|
||||
if (!(p instanceof JSONObject perm)) throw invalidFieldException(String.join(".",MEMBERS,uid,PERMISSION),"Json");
|
||||
if (!(perm.get(NAME) instanceof String permName)) throw invalidFieldException(String.join(".",MEMBERS,uid,PERMISSION,NAME),"String");
|
||||
var permission = Permission.valueOf(permName);
|
||||
var member = new Member(user,permission);
|
||||
var existing = members.get(userId);
|
||||
if (existing == null || existing.permission() != permission) {
|
||||
members.put(userId, member);
|
||||
dirtyFields.add(MEMBERS);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
return this;
|
||||
|
||||
Reference in New Issue
Block a user