implemented searching in notes

Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
This commit is contained in:
2025-09-02 22:46:11 +02:00
parent cff4360a37
commit bee33e84a0
5 changed files with 77 additions and 13 deletions

View File

@@ -2,8 +2,9 @@
package de.srsoftware.umbrella.notes;
import static de.srsoftware.umbrella.core.ConnectionProvider.connect;
import static de.srsoftware.umbrella.core.Constants.LIMIT;
import static de.srsoftware.umbrella.core.Constants.OFFSET;
import static de.srsoftware.umbrella.core.Constants.*;
import static de.srsoftware.umbrella.core.Constants.FULLTEXT;
import static de.srsoftware.umbrella.core.Paths.SEARCH;
import static de.srsoftware.umbrella.core.ResponseCode.HTTP_UNPROCESSABLE;
import static de.srsoftware.umbrella.core.Util.mapValues;
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.*;
@@ -22,6 +23,7 @@ import de.srsoftware.umbrella.core.model.Token;
import de.srsoftware.umbrella.core.model.UmbrellaUser;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
@@ -139,8 +141,10 @@ public class NoteModule extends BaseHandler implements NoteService {
var user = userService().loadUser(token);
if (user.isEmpty()) return unauthorized(ex);
var module = path.pop();
if (SEARCH.equals(module)) return postSearch(ex,user.get());
if (module == null) throw unprocessable("Module missing in path.");
var entityId = path.pop();
if (entityId == null || entityId.isBlank()) throw missingFieldException(ENTITY_ID);
String text = body(ex);
if (text.isBlank()) throw missingFieldException("Note text");
var note = new Note(0,module,entityId,user.get().id(),text, LocalDateTime.now());
@@ -162,6 +166,15 @@ public class NoteModule extends BaseHandler implements NoteService {
return notesDb.list(module, entityId);
}
private boolean postSearch(HttpExchange ex, UmbrellaUser user) 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 notes = notesDb.find(user.id(),keys,fulltext);
return sendContent(ex,mapValues(notes));
}
@Override
public Note save(Note note) {
return notesDb.save(note);

View File

@@ -2,6 +2,8 @@
package de.srsoftware.umbrella.notes;
import de.srsoftware.umbrella.core.model.Note;
import java.util.List;
import java.util.Map;
public interface NotesDb {
@@ -9,6 +11,8 @@ public interface NotesDb {
void deleteEntity(String module, String entityId);
Map<Long, Note> find(long userId, List<String> keys, boolean fulltext);
/**
* get all lists of a person
* @return

View File

@@ -2,6 +2,7 @@
package de.srsoftware.umbrella.notes;
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.SelectQuery.ALL;
import static de.srsoftware.umbrella.core.Constants.*;
@@ -14,9 +15,12 @@ import de.srsoftware.tools.jdbc.Query;
import de.srsoftware.umbrella.core.BaseDb;
import de.srsoftware.umbrella.core.exceptions.UmbrellaException;
import de.srsoftware.umbrella.core.model.Note;
import de.srsoftware.umbrella.core.model.Project;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class SqliteDb extends BaseDb implements NotesDb {
@@ -161,9 +165,22 @@ CREATE TABLE IF NOT EXISTS "{0}" (
}
}
private void init(){
var version = createTables();
LOG.log(INFO,"Updated task db to version {0}",version);
@Override
public Map<Long, Note> find(long userId, List<String> keys, boolean fulltext) {
try {
var notes = new HashMap<Long, Note>();
var query = select(ALL).from(TABLE_NOTES).where(USER_ID, equal(userId));
for (var key : keys) query.where(NOTE,like("%"+key+"%"));
var rs = query.exec(db);
while (rs.next()){
var note = Note.of(rs);
notes.put(note.id(),note);
}
rs.close();
return notes;
} catch (SQLException e) {
throw new UmbrellaException("Failed to search notes");
}
}
@Override