working on company editing
Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
This commit is contained in:
@@ -3,8 +3,9 @@ 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.ID;
|
||||
import static de.srsoftware.umbrella.core.Paths.LIST;
|
||||
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.missingFieldException;
|
||||
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.*;
|
||||
|
||||
import com.sun.net.httpserver.HttpExchange;
|
||||
import de.srsoftware.configuration.Configuration;
|
||||
@@ -50,6 +51,32 @@ public class CompanyModule extends BaseHandler implements CompanyService {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean doPatch(Path path, HttpExchange ex) throws IOException {
|
||||
addCors(ex);
|
||||
try {
|
||||
Optional<Token> token = SessionToken.from(ex).map(Token::of);
|
||||
var user = users.loadUser(token);
|
||||
if (user.isEmpty()) return unauthorized(ex);
|
||||
var head = path.pop();
|
||||
return switch (head) {
|
||||
case null -> super.doGet(path, ex);
|
||||
default -> patchProject(Long.parseLong(head), user.get(), ex);
|
||||
};
|
||||
} catch (NumberFormatException n) {
|
||||
return send(ex,invalidFieldException(ID,"ID (Long)"));
|
||||
} catch (UmbrellaException e) {
|
||||
return send(ex,e);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean patchProject(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)));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Company get(long companyId) throws UmbrellaException {
|
||||
return companyDb.load(companyId);
|
||||
|
||||
@@ -4,6 +4,7 @@ package de.srsoftware.umbrella.company;
|
||||
import static de.srsoftware.tools.jdbc.Condition.equal;
|
||||
import static de.srsoftware.tools.jdbc.Query.SelectQuery.ALL;
|
||||
import static de.srsoftware.tools.jdbc.Query.select;
|
||||
import static de.srsoftware.tools.jdbc.Query.update;
|
||||
import static de.srsoftware.umbrella.company.Constants.TABLE_COMPANIES;
|
||||
import static de.srsoftware.umbrella.company.Constants.TABLE_COMPANIES_USERS;
|
||||
import static de.srsoftware.umbrella.core.Constants.*;
|
||||
@@ -40,6 +41,7 @@ public class SqliteDb implements CompanyDb {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Map<Long,Company> listCompaniesOf(long userId) throws UmbrellaException {
|
||||
try {
|
||||
@@ -69,4 +71,24 @@ public class SqliteDb implements CompanyDb {
|
||||
throw databaseException("Could not load company {0}",companyId);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Company save(Company company) {
|
||||
try {
|
||||
if (company.id() == 0){ // new
|
||||
throw new RuntimeException("Not implemented");
|
||||
} else { // update
|
||||
if (company.isDirty()) {
|
||||
update(TABLE_COMPANIES)
|
||||
.set(NAME, ADDRESS, EMAIL, FIELD_PHONE, FIELD_BANK_ACCOUNT, FIELD_COURT, FIELD_CURRENCY, FIELD_TAX_NUMBER, DECIMALS, DECIMAL_SEPARATOR, THOUSANDS_SEPARATOR, LAST_CUSTOMER_NUMBER, CUSTOMER_NUMBER_PREFIX)
|
||||
.where(ID,equal(company.id())).prepare(db)
|
||||
.apply(company.name(), company.address(), company.email(), company.phone(), company.bankAccount(), company.court(), company.currency(), company.taxId(), company.decimals(), company.decimalSeparator(), company.thousandsSeparator(), company.lastCustomerNumber(), company.customerNumberPrefix())
|
||||
.close();
|
||||
}
|
||||
return company;
|
||||
}
|
||||
} catch (SQLException e){
|
||||
throw UmbrellaException.databaseException("Failed to save {0}…",company.name());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,4 +12,6 @@ public interface CompanyDb {
|
||||
Map<Long,Company> listCompaniesOf(long id) throws UmbrellaException;
|
||||
|
||||
Company load(long companyId) throws UmbrellaException;
|
||||
|
||||
Company save(Company company);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user