|
|
|
@ -3,8 +3,8 @@ package de.srsoftware.umbrella.company; |
|
|
|
|
|
|
|
|
|
|
|
import static de.srsoftware.umbrella.company.Constants.CONFIG_DATABASE; |
|
|
|
import static de.srsoftware.umbrella.company.Constants.CONFIG_DATABASE; |
|
|
|
import static de.srsoftware.umbrella.core.ConnectionProvider.connect; |
|
|
|
import static de.srsoftware.umbrella.core.ConnectionProvider.connect; |
|
|
|
import static de.srsoftware.umbrella.core.Constants.COMPANY; |
|
|
|
|
|
|
|
import static de.srsoftware.umbrella.core.Constants.ID; |
|
|
|
import static de.srsoftware.umbrella.core.Constants.ID; |
|
|
|
|
|
|
|
import static de.srsoftware.umbrella.core.Constants.MEMBERS; |
|
|
|
import static de.srsoftware.umbrella.core.Paths.LIST; |
|
|
|
import static de.srsoftware.umbrella.core.Paths.LIST; |
|
|
|
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.*; |
|
|
|
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.*; |
|
|
|
|
|
|
|
|
|
|
|
@ -18,7 +18,6 @@ import de.srsoftware.umbrella.core.api.CompanyService; |
|
|
|
import de.srsoftware.umbrella.core.api.UserService; |
|
|
|
import de.srsoftware.umbrella.core.api.UserService; |
|
|
|
import de.srsoftware.umbrella.core.exceptions.UmbrellaException; |
|
|
|
import de.srsoftware.umbrella.core.exceptions.UmbrellaException; |
|
|
|
import de.srsoftware.umbrella.core.model.*; |
|
|
|
import de.srsoftware.umbrella.core.model.*; |
|
|
|
|
|
|
|
|
|
|
|
import java.io.IOException; |
|
|
|
import java.io.IOException; |
|
|
|
import java.util.*; |
|
|
|
import java.util.*; |
|
|
|
|
|
|
|
|
|
|
|
@ -61,7 +60,7 @@ public class CompanyModule extends BaseHandler implements CompanyService { |
|
|
|
var head = path.pop(); |
|
|
|
var head = path.pop(); |
|
|
|
return switch (head) { |
|
|
|
return switch (head) { |
|
|
|
case null -> super.doGet(path, ex); |
|
|
|
case null -> super.doGet(path, ex); |
|
|
|
default -> patchProject(Long.parseLong(head), user.get(), ex); |
|
|
|
default -> patchCompany(Long.parseLong(head), user.get(), ex); |
|
|
|
}; |
|
|
|
}; |
|
|
|
} catch (NumberFormatException n) { |
|
|
|
} catch (NumberFormatException n) { |
|
|
|
return send(ex,invalidFieldException(ID,"ID (Long)")); |
|
|
|
return send(ex,invalidFieldException(ID,"ID (Long)")); |
|
|
|
@ -133,11 +132,31 @@ public class CompanyModule extends BaseHandler implements CompanyService { |
|
|
|
return companyDb.getMembers(companyId).contains(userId); |
|
|
|
return companyDb.getMembers(companyId).contains(userId); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private boolean patchProject(long companyId, UmbrellaUser user, HttpExchange ex) throws IOException { |
|
|
|
private boolean patchCompany(long companyId, UmbrellaUser user, HttpExchange ex) throws IOException { |
|
|
|
var company = get(companyId); |
|
|
|
var company = get(companyId); |
|
|
|
if (!membership(companyId,user.id())) throw forbidden("You are mot a member of company {0}",company.name()); |
|
|
|
if (!membership(companyId,user.id())) throw forbidden("You are mot a member of company {0}",company.name()); |
|
|
|
|
|
|
|
|
|
|
|
var json = json(ex); |
|
|
|
var json = json(ex); |
|
|
|
return sendContent(ex,companyDb.save(company.patch(json))); |
|
|
|
company = companyDb.save(company.patch(json)); |
|
|
|
|
|
|
|
if (json.has(MEMBERS)){ |
|
|
|
|
|
|
|
var wantedMembers = new HashSet<Long>(); |
|
|
|
|
|
|
|
wantedMembers.add(user.id()); // user may not delete itself
|
|
|
|
|
|
|
|
for (var o : json.getJSONArray(MEMBERS).toList()){ |
|
|
|
|
|
|
|
if (o instanceof Number uid) wantedMembers.add(uid.longValue()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
loadMembers(List.of(company)); // load current members
|
|
|
|
|
|
|
|
var currentMembers = company.members().keySet(); |
|
|
|
|
|
|
|
for (var currentMember : currentMembers){ |
|
|
|
|
|
|
|
if (!wantedMembers.contains(currentMember)) companyDb.dropUser(companyId,currentMember); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
for (var wantedMember : wantedMembers){ |
|
|
|
|
|
|
|
if (!currentMembers.contains(wantedMember)) companyDb.addUser(companyId,wantedMember); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
company.members().clear(); |
|
|
|
|
|
|
|
loadMembers(List.of(company)); // load new members
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return sendContent(ex,company); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private boolean postNewCompany(UmbrellaUser user, HttpExchange ex) throws IOException { |
|
|
|
private boolean postNewCompany(UmbrellaUser user, HttpExchange ex) throws IOException { |
|
|
|
|