implemented creation of companies
Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
This commit is contained in:
@@ -3,6 +3,7 @@ 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.Paths.LIST;
|
||||
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.*;
|
||||
@@ -70,11 +71,23 @@ public class CompanyModule extends BaseHandler implements CompanyService {
|
||||
}
|
||||
}
|
||||
|
||||
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 boolean doPost(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 -> postNewCompany(user.get(),ex);
|
||||
default -> super.doPost(path,ex);
|
||||
};
|
||||
} catch (NumberFormatException n) {
|
||||
return send(ex,invalidFieldException(ID,"ID (Long)"));
|
||||
} catch (UmbrellaException e) {
|
||||
return send(ex,e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -88,8 +101,6 @@ public class CompanyModule extends BaseHandler implements CompanyService {
|
||||
return sendContent(ex,result);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public Collection<UmbrellaUser> getMembers(long companyId) throws UmbrellaException {
|
||||
var members = new HashSet<UmbrellaUser>();
|
||||
@@ -107,6 +118,21 @@ public class CompanyModule extends BaseHandler implements CompanyService {
|
||||
return companyDb.getMembers(companyId).contains(userId);
|
||||
}
|
||||
|
||||
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)));
|
||||
}
|
||||
|
||||
private boolean postNewCompany(UmbrellaUser user, HttpExchange ex) throws IOException {
|
||||
var json = json(ex);
|
||||
json.put(ID,0);
|
||||
var company = companyDb.save(Company.of(json));
|
||||
companyDb.addUser(company.id(),user.id());
|
||||
return sendContent(ex,company);
|
||||
}
|
||||
|
||||
@Override
|
||||
public UserService userService() {
|
||||
return users;
|
||||
|
||||
@@ -2,9 +2,9 @@
|
||||
package de.srsoftware.umbrella.company;
|
||||
|
||||
import static de.srsoftware.tools.jdbc.Condition.equal;
|
||||
import static de.srsoftware.tools.jdbc.Query.*;
|
||||
import static de.srsoftware.tools.jdbc.Query.Dialect.SQLITE;
|
||||
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.*;
|
||||
@@ -13,6 +13,8 @@ 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;
|
||||
@@ -28,6 +30,19 @@ public class SqliteDb implements CompanyDb {
|
||||
db = connection;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addUser(long company_id, long user_id) {
|
||||
try {
|
||||
insertInto(TABLE_COMPANIES_USERS,COMPANY_ID, USER_ID)
|
||||
.values(company_id, user_id)
|
||||
.ignoreDuplicates(SQLITE)
|
||||
.execute(db)
|
||||
.close();
|
||||
} catch (SQLException e) {
|
||||
throw databaseException("Failed to assign user {0} to company {1}");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<Long> getMembers(long companyId) throws UmbrellaException {
|
||||
try {
|
||||
@@ -76,7 +91,19 @@ public class SqliteDb implements CompanyDb {
|
||||
public Company save(Company company) {
|
||||
try {
|
||||
if (company.id() == 0){ // new
|
||||
throw new RuntimeException("Not implemented");
|
||||
long id = 0;
|
||||
var rs = insertInto(TABLE_COMPANIES,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)
|
||||
.values(company.name(),company.address(),company.email(),company.phone(),company.bankAccount(),company.court(),company.currency(),company.taxId(),company.decimals(),company.decimalSeparator(),company.thousandsSeparator(),0,company.customerNumberPrefix())
|
||||
.execute(db)
|
||||
.getGeneratedKeys();
|
||||
if (rs.next()) id = rs.getLong(1);
|
||||
rs.close();
|
||||
if (id != 0){
|
||||
var json = new JSONObject(company.toMap());
|
||||
json.put(ID,id);
|
||||
return Company.of(json);
|
||||
}
|
||||
return company;
|
||||
} else { // update
|
||||
if (company.isDirty()) {
|
||||
update(TABLE_COMPANIES)
|
||||
|
||||
@@ -7,11 +7,13 @@ import java.util.Collection;
|
||||
import java.util.Map;
|
||||
|
||||
public interface CompanyDb {
|
||||
void addUser(long company_id, long user_id);
|
||||
|
||||
Collection<Long> getMembers(long companyId) throws UmbrellaException;
|
||||
|
||||
Map<Long,Company> listCompaniesOf(long id) throws UmbrellaException;
|
||||
|
||||
Company load(long companyId) throws UmbrellaException;
|
||||
|
||||
Company load(long companyId) throws UmbrellaException;
|
||||
Company save(Company company);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user