implemented editing members of company
This commit is contained in:
@@ -3,8 +3,8 @@ package de.srsoftware.umbrella.company;
|
||||
|
||||
import static de.srsoftware.umbrella.company.Constants.CONFIG_DATABASE;
|
||||
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.MEMBERS;
|
||||
import static de.srsoftware.umbrella.core.Paths.LIST;
|
||||
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.exceptions.UmbrellaException;
|
||||
import de.srsoftware.umbrella.core.model.*;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.*;
|
||||
|
||||
@@ -61,7 +60,7 @@ public class CompanyModule extends BaseHandler implements CompanyService {
|
||||
var head = path.pop();
|
||||
return switch (head) {
|
||||
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) {
|
||||
return send(ex,invalidFieldException(ID,"ID (Long)"));
|
||||
@@ -133,11 +132,31 @@ public class CompanyModule extends BaseHandler implements CompanyService {
|
||||
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);
|
||||
if (!membership(companyId,user.id())) throw forbidden("You are mot a member of company {0}",company.name());
|
||||
|
||||
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 {
|
||||
|
||||
@@ -13,14 +13,13 @@ import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.databaseE
|
||||
import de.srsoftware.umbrella.company.api.CompanyDb;
|
||||
import de.srsoftware.umbrella.core.exceptions.UmbrellaException;
|
||||
import de.srsoftware.umbrella.core.model.Company;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import org.json.JSONObject;
|
||||
|
||||
public class SqliteDb implements CompanyDb {
|
||||
|
||||
@@ -43,6 +42,15 @@ public class SqliteDb implements CompanyDb {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void dropUser(long company_id, long user_id) {
|
||||
try {
|
||||
delete().from(TABLE_COMPANIES_USERS).where(COMPANY_ID,equal(company_id)).where(USER_ID,equal(user_id)).execute(db);
|
||||
} catch (SQLException e) {
|
||||
throw databaseException("Failed to assign user {0} to company {1}");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<Long> getMembers(long companyId) throws UmbrellaException {
|
||||
try {
|
||||
|
||||
@@ -9,6 +9,8 @@ import java.util.Map;
|
||||
public interface CompanyDb {
|
||||
void addUser(long company_id, long user_id);
|
||||
|
||||
void dropUser(long company_id, long user_id);
|
||||
|
||||
Collection<Long> getMembers(long companyId) throws UmbrellaException;
|
||||
|
||||
Map<Long,Company> listCompaniesOf(long id) throws UmbrellaException;
|
||||
|
||||
Reference in New Issue
Block a user