implemented search in companies
Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
This commit is contained in:
@@ -3,9 +3,11 @@ 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.ID;
|
import static de.srsoftware.umbrella.core.Constants.*;
|
||||||
import static de.srsoftware.umbrella.core.Constants.MEMBERS;
|
import static de.srsoftware.umbrella.core.Constants.FULLTEXT;
|
||||||
import static de.srsoftware.umbrella.core.Paths.LIST;
|
import static de.srsoftware.umbrella.core.Paths.LIST;
|
||||||
|
import static de.srsoftware.umbrella.core.Paths.SEARCH;
|
||||||
|
import static de.srsoftware.umbrella.core.Util.mapValues;
|
||||||
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.*;
|
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.*;
|
||||||
|
|
||||||
import com.sun.net.httpserver.HttpExchange;
|
import com.sun.net.httpserver.HttpExchange;
|
||||||
@@ -105,6 +107,7 @@ public class CompanyModule extends BaseHandler implements CompanyService {
|
|||||||
if (user.isEmpty()) return unauthorized(ex);
|
if (user.isEmpty()) return unauthorized(ex);
|
||||||
var head = path.pop();
|
var head = path.pop();
|
||||||
return switch (head) {
|
return switch (head) {
|
||||||
|
case SEARCH -> postSearch(user.get(),ex);
|
||||||
case null -> postNewCompany(user.get(),ex);
|
case null -> postNewCompany(user.get(),ex);
|
||||||
default -> super.doPost(path,ex);
|
default -> super.doPost(path,ex);
|
||||||
};
|
};
|
||||||
@@ -193,4 +196,13 @@ public class CompanyModule extends BaseHandler implements CompanyService {
|
|||||||
companyDb.addUser(company.id(),user.id());
|
companyDb.addUser(company.id(),user.id());
|
||||||
return sendContent(ex,company);
|
return sendContent(ex,company);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean postSearch(UmbrellaUser user, HttpExchange ex) throws IOException {
|
||||||
|
var json = json(ex);
|
||||||
|
if (!(json.has(KEY) && json.get(KEY) instanceof String key)) throw missingFieldException(KEY);
|
||||||
|
var keys = Arrays.asList(key.split(" "));
|
||||||
|
var fulltext = json.has(FULLTEXT) && json.get(FULLTEXT) instanceof Boolean val && val;
|
||||||
|
var companies = companyDb.find(user.id(),keys);
|
||||||
|
return sendContent(ex,mapValues(companies));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ public class Constants {
|
|||||||
private Constants(){}
|
private Constants(){}
|
||||||
|
|
||||||
public static final String CONFIG_DATABASE = "umbrella.modules.company.database";
|
public static final String CONFIG_DATABASE = "umbrella.modules.company.database";
|
||||||
|
public static final String DISTINCT = "DISTINCT *";
|
||||||
public static final String TABLE_COMPANIES_USERS = "companies_users";
|
public static final String TABLE_COMPANIES_USERS = "companies_users";
|
||||||
public static final String TABLE_COMPANIES = "companies";
|
public static final String TABLE_COMPANIES = "companies";
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,14 +2,15 @@
|
|||||||
package de.srsoftware.umbrella.company;
|
package de.srsoftware.umbrella.company;
|
||||||
|
|
||||||
import static de.srsoftware.tools.jdbc.Condition.equal;
|
import static de.srsoftware.tools.jdbc.Condition.equal;
|
||||||
|
import static de.srsoftware.tools.jdbc.Condition.like;
|
||||||
import static de.srsoftware.tools.jdbc.Query.*;
|
import static de.srsoftware.tools.jdbc.Query.*;
|
||||||
import static de.srsoftware.tools.jdbc.Query.Dialect.SQLITE;
|
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.SelectQuery.ALL;
|
||||||
import static de.srsoftware.umbrella.company.Constants.TABLE_COMPANIES;
|
import static de.srsoftware.umbrella.company.Constants.*;
|
||||||
import static de.srsoftware.umbrella.company.Constants.TABLE_COMPANIES_USERS;
|
|
||||||
import static de.srsoftware.umbrella.core.Constants.*;
|
import static de.srsoftware.umbrella.core.Constants.*;
|
||||||
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.databaseException;
|
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.databaseException;
|
||||||
import static java.lang.System.Logger.Level.ERROR;
|
import static java.lang.System.Logger.Level.ERROR;
|
||||||
|
import static java.text.MessageFormat.format;
|
||||||
|
|
||||||
import de.srsoftware.umbrella.company.api.CompanyDb;
|
import de.srsoftware.umbrella.company.api.CompanyDb;
|
||||||
import de.srsoftware.umbrella.core.BaseDb;
|
import de.srsoftware.umbrella.core.BaseDb;
|
||||||
@@ -17,10 +18,8 @@ import de.srsoftware.umbrella.core.exceptions.UmbrellaException;
|
|||||||
import de.srsoftware.umbrella.core.model.Company;
|
import de.srsoftware.umbrella.core.model.Company;
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.util.Collection;
|
import java.util.*;
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.HashSet;
|
|
||||||
import java.util.Map;
|
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
|
|
||||||
public class SqliteDb extends BaseDb implements CompanyDb {
|
public class SqliteDb extends BaseDb implements CompanyDb {
|
||||||
@@ -126,6 +125,26 @@ CREATE TABLE IF NOT EXISTS "companies" (
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public HashMap<Long, Company> find(long userId, Collection<String> keys) {
|
||||||
|
try {
|
||||||
|
var query = select(DISTINCT).from(TABLE_COMPANIES).leftJoin(ID,TABLE_COMPANIES_USERS,COMPANY_ID)
|
||||||
|
.where(USER_ID,equal(userId));
|
||||||
|
for (var key : keys){
|
||||||
|
query.where(format("CONCAT({0},\" \",{1},\" \",{2},\" \",{3},\" \",{4})",NAME,ADDRESS,EMAIL,FIELD_PHONE,FIELD_BANK_ACCOUNT),like("%"+key+"%"));
|
||||||
|
}
|
||||||
|
var rs = query.exec(db);
|
||||||
|
var companies = new HashMap<Long,Company>();
|
||||||
|
while (rs.next()) {
|
||||||
|
var company = Company.of(rs);
|
||||||
|
companies.put(company.id(),company);
|
||||||
|
}
|
||||||
|
rs.close();
|
||||||
|
return companies;
|
||||||
|
} catch (SQLException ex){
|
||||||
|
throw databaseException("Failed to search in company database!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Map<Long,Company> listCompaniesOf(long userId) throws UmbrellaException {
|
public Map<Long,Company> listCompaniesOf(long userId) throws UmbrellaException {
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ package de.srsoftware.umbrella.company.api;
|
|||||||
import de.srsoftware.umbrella.core.exceptions.UmbrellaException;
|
import de.srsoftware.umbrella.core.exceptions.UmbrellaException;
|
||||||
import de.srsoftware.umbrella.core.model.Company;
|
import de.srsoftware.umbrella.core.model.Company;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
public interface CompanyDb {
|
public interface CompanyDb {
|
||||||
@@ -13,6 +14,8 @@ public interface CompanyDb {
|
|||||||
|
|
||||||
void dropUser(long company_id, long user_id);
|
void dropUser(long company_id, long user_id);
|
||||||
|
|
||||||
|
HashMap<Long, Company> find(long userId, Collection<String> keys);
|
||||||
|
|
||||||
Collection<Long> getMembers(long companyId) throws UmbrellaException;
|
Collection<Long> getMembers(long companyId) throws UmbrellaException;
|
||||||
|
|
||||||
Map<Long,Company> listCompaniesOf(long id) throws UmbrellaException;
|
Map<Long,Company> listCompaniesOf(long id) throws UmbrellaException;
|
||||||
|
|||||||
@@ -9,6 +9,7 @@
|
|||||||
const router = useTinyRouter();
|
const router = useTinyRouter();
|
||||||
console.log(router);
|
console.log(router);
|
||||||
let bookmarks = $state(null);
|
let bookmarks = $state(null);
|
||||||
|
let companies = $state(null);
|
||||||
let error = $state(null);
|
let error = $state(null);
|
||||||
let fulltext = false;
|
let fulltext = false;
|
||||||
let key = $state(router.getQueryParam('key'));
|
let key = $state(router.getQueryParam('key'));
|
||||||
@@ -39,6 +40,7 @@
|
|||||||
body: JSON.stringify(data)
|
body: JSON.stringify(data)
|
||||||
};
|
};
|
||||||
fetch(api('bookmark/search'),options).then(handleBookmarks);
|
fetch(api('bookmark/search'),options).then(handleBookmarks);
|
||||||
|
fetch(api('company/search'),options).then(handleCompanies);
|
||||||
fetch(api('project/search'),options).then(handleProjects);
|
fetch(api('project/search'),options).then(handleProjects);
|
||||||
fetch(api('task/search'),options).then(handleTasks);
|
fetch(api('task/search'),options).then(handleTasks);
|
||||||
}
|
}
|
||||||
@@ -57,6 +59,15 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function handleCompanies(resp){
|
||||||
|
if (resp.ok){
|
||||||
|
const json = await resp.json();
|
||||||
|
companies = Object.keys(json).length ? json : null;
|
||||||
|
} else {
|
||||||
|
error = await resp.text();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async function handleProjects(resp){
|
async function handleProjects(resp){
|
||||||
if (resp.ok){
|
if (resp.ok){
|
||||||
const res = await resp.json();
|
const res = await resp.json();
|
||||||
@@ -141,3 +152,17 @@
|
|||||||
</ul>
|
</ul>
|
||||||
</fieldset>
|
</fieldset>
|
||||||
{/if}
|
{/if}
|
||||||
|
{#if companies}
|
||||||
|
<fieldset>
|
||||||
|
<legend>
|
||||||
|
{t('companies')}
|
||||||
|
</legend>
|
||||||
|
<ul>
|
||||||
|
{#each Object.values(companies) as company}
|
||||||
|
<li>
|
||||||
|
<a href="#" onclick={e=>go(`/company/${company.id}/view`)} >{company.name}</a>
|
||||||
|
</li>
|
||||||
|
{/each}
|
||||||
|
</ul>
|
||||||
|
</fieldset>
|
||||||
|
{/if}
|
||||||
|
|||||||
Reference in New Issue
Block a user