preparing search in stock items

This commit is contained in:
2025-12-03 09:34:46 +01:00
parent fdffad6022
commit 4cb9c6bd2f
4 changed files with 61 additions and 16 deletions

View File

@@ -1,7 +1,7 @@
<script>
import { onMount } from 'svelte';
import { useTinyRouter } from 'svelte-tiny-router';
import { api, target } from '../../urls.svelte.js';
import { api, post, target } from '../../urls.svelte.js';
import { error, yikes } from '../../warn.svelte';
import { t } from '../../translations.svelte.js';
import { display } from '../../time.svelte';
@@ -18,6 +18,7 @@
let notes = $state(null);
let pages = $state(null);
let projects = $state(null);
let stock = $state(null);
let tasks = $state(null);
let times = $state(null);
@@ -38,19 +39,16 @@
window.history.replaceState(history.state, '', url);
const data = { key : key, fulltext : fulltext };
const options = {
credentials:'include',
method: 'POST',
body: JSON.stringify(data)
};
fetch(api('bookmark/search'),options).then(handleBookmarks);
fetch(api('company/search'),options).then(handleCompanies);
fetch(api('document/search'),options).then(handleDocuments);
fetch(api('notes/search'),options).then(handleNotes);
fetch(api('project/search'),options).then(handleProjects);
fetch(api('task/search'),options).then(handleTasks);
fetch(api('time/search'),options).then(handleTimes);
fetch(api('wiki/search'),options).then(handleWikiPages);
post(api('bookmark/search'),data).then(handleBookmarks);
post(api('company/search '),data).then(handleCompanies);
post(api('document/search'),data).then(handleDocuments);
post(api('notes/search' ),data).then(handleNotes);
post(api('project/search' ),data).then(handleProjects);
post(api('task/search' ),data).then(handleTasks);
post(api('stock/search' ),data).then(handleStock);
post(api('time/search' ),data).then(handleTimes);
post(api('wiki/search' ),data).then(handleWikiPages);
}
function onclick(e){
@@ -107,6 +105,15 @@
}
}
async function handleStock(resp){
if (resp.ok){
const res = await resp.json();
stock = Object.keys(res).length ? res : null;
} else {
error(resp);
}
}
async function handleTasks(resp){
if (resp.ok){
const res = await resp.json();

View File

@@ -3,8 +3,8 @@ package de.srsoftware.umbrella.stock;
import static de.srsoftware.tools.Optionals.is0;
import static de.srsoftware.tools.Optionals.nullIfEmpty;
import static de.srsoftware.tools.jdbc.Condition.equal;
import static de.srsoftware.tools.jdbc.Condition.isNull;
import static de.srsoftware.tools.jdbc.Condition.*;
import static de.srsoftware.tools.jdbc.Condition.like;
import static de.srsoftware.tools.jdbc.Query.*;
import static de.srsoftware.tools.jdbc.Query.SelectQuery.ALL;
import static de.srsoftware.umbrella.core.Constants.*;
@@ -17,6 +17,7 @@ import static java.text.MessageFormat.format;
import de.srsoftware.tools.jdbc.Query;
import de.srsoftware.umbrella.core.BaseDb;
import de.srsoftware.umbrella.core.api.Owner;
import de.srsoftware.umbrella.core.exceptions.UmbrellaException;
import de.srsoftware.umbrella.core.model.*;
import de.srsoftware.umbrella.core.model.Location;
import java.sql.Connection;
@@ -189,6 +190,28 @@ public class SqliteDb extends BaseDb implements StockDb {
}
}
@Override
public Map<Long, Item> find(long userId, Collection<String> keys, boolean fulltext) {
try {
var items = new HashMap<Long,Item>();
var query = select(ALL).from(TABLE_ITEMS).where(USER_ID, equal(userId));
if (fulltext) {
// TODO: implement full-text search
} else {
for (var key : keys) query.where(NAME,like("%"+key+"%"));
}
var rs = query.exec(db);
while (rs.next()){
var project = Item.of(rs);
items.put(project.id(),project);
}
rs.close();
return items;
} catch (SQLException e) {
throw new UmbrellaException("Failed to load items from database");
}
}
@Override
public Collection<DbLocation> listChildLocations(long parentId) {
try {

View File

@@ -5,11 +5,13 @@ import de.srsoftware.umbrella.core.api.Owner;
import de.srsoftware.umbrella.core.model.*;
import de.srsoftware.umbrella.core.model.Location;
import java.util.Collection;
import java.util.List;
import java.util.Map;
public interface StockDb {
Property addNewProperty(long itemId, String name, Object value, String unit);
Location delete(DbLocation location);
Map<Long, Item> find(long userId, Collection<String> keys, boolean fulltext);
Collection<DbLocation> listChildLocations(long parentId);
Collection<DbLocation> listCompanyLocations(Company company);
Collection<Item> listItemsAt(Location location);
@@ -24,5 +26,6 @@ public interface StockDb {
Map<String,Object> pathToLocation(Location location);
DbLocation save(DbLocation location);
Item save(Item item);
Property setProperty(long itemId, long existingPropId, Object value);
}

View File

@@ -9,6 +9,8 @@ import static de.srsoftware.umbrella.core.Field.ITEM;
import static de.srsoftware.umbrella.core.ModuleRegistry.companyService;
import static de.srsoftware.umbrella.core.ModuleRegistry.userService;
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.stock.Constants.*;
import static java.lang.System.Logger.Level.WARNING;
@@ -174,6 +176,7 @@ public class StockModule extends BaseHandler implements StockService {
case LIST -> postItemList(user.get(), path, ex);
case LOCATION -> postLocation(user.get(),ex);
case PROPERTY -> postProperty(user.get(),ex);
case SEARCH -> postSearch(user.get(),ex);
case null, default -> super.doPost(path,ex);
};
} catch (UmbrellaException e){
@@ -389,6 +392,15 @@ public class StockModule extends BaseHandler implements StockService {
return sendContent(ex,property);
}
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 items = stockDb.find(user.id(),keys,fulltext);
return sendContent(ex,mapValues(items));
}
@Override
public Collection<Object> redefineMe(long company_id) {
// TODO