refactored project.members to be map of userId → member
This commit is contained in:
@@ -12,7 +12,7 @@ import java.util.*;
|
|||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
|
|
||||||
public class Project implements Mappable {
|
public class Project implements Mappable {
|
||||||
private final Collection<Member> members;
|
private final Map<Long,Member> members;
|
||||||
private final boolean showClosed;
|
private final boolean showClosed;
|
||||||
private final Long companyId;
|
private final Long companyId;
|
||||||
private Status status;
|
private Status status;
|
||||||
@@ -21,7 +21,7 @@ public class Project implements Mappable {
|
|||||||
private String description;
|
private String description;
|
||||||
private final Set<String> dirtyFields = new HashSet<>();
|
private final Set<String> dirtyFields = new HashSet<>();
|
||||||
|
|
||||||
public Project(long id, String name, String description, Status status, Long companyId, boolean showClosed, Collection<Member> members) {
|
public Project(long id, String name, String description, Status status, Long companyId, boolean showClosed, Map<Long,Member> members) {
|
||||||
this.id = id;
|
this.id = id;
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.description = description;
|
this.description = description;
|
||||||
@@ -44,17 +44,14 @@ public class Project implements Mappable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public boolean hasMember(UmbrellaUser user) {
|
public boolean hasMember(UmbrellaUser user) {
|
||||||
for (var member : members){
|
return members.containsKey(user.id());
|
||||||
if (member.user().id() == user.id()) return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public long id(){
|
public long id(){
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Collection<Member> members(){
|
public Map<Long,Member> members(){
|
||||||
return members;
|
return members;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -64,7 +61,7 @@ public class Project implements Mappable {
|
|||||||
|
|
||||||
public static Project of(ResultSet rs) throws SQLException {
|
public static Project of(ResultSet rs) throws SQLException {
|
||||||
var companyId = rs.getLong(COMPANY_ID);
|
var companyId = rs.getLong(COMPANY_ID);
|
||||||
return new Project(rs.getLong(ID),rs.getString(NAME),rs.getString(DESCRIPTION),Status.of(rs.getInt(STATUS)),companyId == 0 ? null : companyId,rs.getBoolean(SHOW_CLOSED),new ArrayList<>());
|
return new Project(rs.getLong(ID),rs.getString(NAME),rs.getString(DESCRIPTION),Status.of(rs.getInt(STATUS)),companyId == 0 ? null : companyId,rs.getBoolean(SHOW_CLOSED),new HashMap<>());
|
||||||
}
|
}
|
||||||
|
|
||||||
public Project patch(JSONObject json) {
|
public Project patch(JSONObject json) {
|
||||||
@@ -91,13 +88,15 @@ public class Project implements Mappable {
|
|||||||
@Override
|
@Override
|
||||||
public Map<String, Object> toMap() {
|
public Map<String, Object> toMap() {
|
||||||
var map = new HashMap<String, Object>();
|
var map = new HashMap<String, Object>();
|
||||||
|
var memberMap = new HashMap<Long,Map<String,Object>>();
|
||||||
|
for (var entry : members.entrySet()) memberMap.put(entry.getKey(),entry.getValue().toMap());
|
||||||
map.put(ID,id);
|
map.put(ID,id);
|
||||||
map.put(NAME,name);
|
map.put(NAME,name);
|
||||||
map.put(DESCRIPTION,Map.of(SOURCE,description,RENDERED,markdown(description)));
|
map.put(DESCRIPTION,Map.of(SOURCE,description,RENDERED,markdown(description)));
|
||||||
map.put(STATUS,Map.of(STATUS_CODE,status.code(), NAME,status.name()));
|
map.put(STATUS,Map.of(STATUS_CODE,status.code(), NAME,status.name()));
|
||||||
map.put(COMPANY_ID,companyId);
|
map.put(COMPANY_ID,companyId);
|
||||||
map.put(SHOW_CLOSED,showClosed);
|
map.put(SHOW_CLOSED,showClosed);
|
||||||
map.put(MEMBERS,members == null ? List.of() : members.stream().map(Member::toMap).toList());
|
map.put(MEMBERS,memberMap);
|
||||||
return map;
|
return map;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -108,7 +108,7 @@
|
|||||||
<td>
|
<td>
|
||||||
<ul>
|
<ul>
|
||||||
{#each Object.entries(project.members) as [uid,member]}
|
{#each Object.entries(project.members) as [uid,member]}
|
||||||
<li>{member.user.name}: {t('permission.'+member.permission)}</li>
|
<li>{member.user.name}: {t('permission.'+member.permission.name)}</li>
|
||||||
{/each}
|
{/each}
|
||||||
</ul>
|
</ul>
|
||||||
</td>
|
</td>
|
||||||
|
|||||||
@@ -152,7 +152,7 @@ public class ProjectModule extends BaseHandler implements ProjectService {
|
|||||||
var userId = entry.getKey();
|
var userId = entry.getKey();
|
||||||
var permission = entry.getValue();
|
var permission = entry.getValue();
|
||||||
var user = userMap.computeIfAbsent(userId,k -> users.loadUser(userId));
|
var user = userMap.computeIfAbsent(userId,k -> users.loadUser(userId));
|
||||||
project.members().add(new Member(user,permission));
|
project.members().put(userId,new Member(user,permission));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return projectList;
|
return projectList;
|
||||||
@@ -187,7 +187,8 @@ public class ProjectModule extends BaseHandler implements ProjectService {
|
|||||||
if (json.has(SETTINGS) && json.get(SETTINGS) instanceof JSONObject settingsJson){
|
if (json.has(SETTINGS) && json.get(SETTINGS) instanceof JSONObject settingsJson){
|
||||||
showClosed = settingsJson.has(SHOW_CLOSED) && settingsJson.get(SHOW_CLOSED) == TRUE;
|
showClosed = settingsJson.has(SHOW_CLOSED) && settingsJson.get(SHOW_CLOSED) == TRUE;
|
||||||
}
|
}
|
||||||
var prj = new Project(0,name,description, OPEN,companyId,showClosed, List.of(new Member(user, OWNER)));
|
var owner = Map.of(user.id(),new Member(user,OWNER));
|
||||||
|
var prj = new Project(0,name,description, OPEN,companyId,showClosed, owner);
|
||||||
prj = projects.save(prj);
|
prj = projects.save(prj);
|
||||||
return sendContent(ex,prj);
|
return sendContent(ex,prj);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -197,7 +197,7 @@ CREATE TABLE IF NOT EXISTS {0} ( {1} VARCHAR(255) PRIMARY KEY, {2} VARCHAR(255)
|
|||||||
if (id != null){
|
if (id != null){
|
||||||
if (!prj.members().isEmpty()) {
|
if (!prj.members().isEmpty()) {
|
||||||
var query = insertInto(TABLE_PROJECT_USERS, PROJECT_ID, USER_ID, PERMISSIONS);
|
var query = insertInto(TABLE_PROJECT_USERS, PROJECT_ID, USER_ID, PERMISSIONS);
|
||||||
for (var member : prj.members()) query.values(id, member.user().id(), member.permission().code());
|
for (var member : prj.members().entrySet()) query.values(id, member.getKey(), member.getValue().permission().code());
|
||||||
query.execute(db).close();
|
query.execute(db).close();
|
||||||
}
|
}
|
||||||
return new Project(id, prj.name(), prj.description(),prj.status(),prj.companyId().orElse(null),prj.showClosed(),prj.members());
|
return new Project(id, prj.name(), prj.description(),prj.status(),prj.companyId().orElse(null),prj.showClosed(),prj.members());
|
||||||
|
|||||||
Reference in New Issue
Block a user