implemented bookmark search
Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
This commit is contained in:
@@ -5,6 +5,7 @@ import static de.srsoftware.umbrella.bookmarks.Constants.*;
|
||||
import static de.srsoftware.umbrella.core.ConnectionProvider.connect;
|
||||
import static de.srsoftware.umbrella.core.Constants.*;
|
||||
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.missingFieldException;
|
||||
import static java.net.HttpURLConnection.HTTP_BAD_REQUEST;
|
||||
@@ -21,6 +22,7 @@ import de.srsoftware.umbrella.core.model.Token;
|
||||
import de.srsoftware.umbrella.core.model.UmbrellaUser;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Optional;
|
||||
import org.json.JSONArray;
|
||||
|
||||
@@ -71,6 +73,7 @@ public class BookmarkApi extends BaseHandler implements BookmarkService {
|
||||
if (user.isEmpty()) return unauthorized(ex);
|
||||
var head = path.pop();
|
||||
return switch (head) {
|
||||
case SEARCH -> postSearch(user.get(),ex);
|
||||
case null -> postBookmark(user.get(),ex);
|
||||
default -> super.doPost(path,ex);
|
||||
};
|
||||
@@ -118,4 +121,13 @@ public class BookmarkApi extends BaseHandler implements BookmarkService {
|
||||
}
|
||||
return sendContent(ex,bookmark);
|
||||
}
|
||||
|
||||
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 bookmarks = db.find(user.id(),keys);
|
||||
return sendContent(ex,mapValues(bookmarks));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,7 +7,9 @@ import java.util.Collection;
|
||||
import java.util.Map;
|
||||
|
||||
public interface BookmarkDb {
|
||||
Map<Long, Bookmark> list(long userId, long offset, long limit);
|
||||
Map<Long, Bookmark> find(long userId, Collection<String> key);
|
||||
|
||||
Map<Long, Bookmark> list(long userId, Long offset, Long limit);
|
||||
|
||||
Bookmark load(long id, long userId);
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ package de.srsoftware.umbrella.bookmarks;
|
||||
|
||||
public class Constants {
|
||||
public static final String CONFIG_DATABASE = "umbrella.modules.bookmark.database";
|
||||
public static final String FULLTEXT = "fulltext";
|
||||
public static final String SHARE = "share";
|
||||
public static final String TABLE_TOKENS = "tokens";
|
||||
public static final String TABLE_URLS = "urls";
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
package de.srsoftware.umbrella.bookmarks;
|
||||
|
||||
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.bookmarks.Constants.*;
|
||||
@@ -69,7 +70,26 @@ CREATE TABLE IF NOT EXISTS {0} (
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<Long, Bookmark> list(long userId, long offset, long limit) {
|
||||
public Map<Long, Bookmark> find(long userId, Collection<String> keys) {
|
||||
try {
|
||||
var map = new HashMap<Long,Bookmark>();
|
||||
var query = select(ALL).from(TABLE_URL_COMMENTS).leftJoin(URL_ID,TABLE_URLS,ID)
|
||||
.where(USER_ID, equal(userId));
|
||||
for (var key : keys) query.where(COMMENT,like("%"+key+"%"));
|
||||
var rs = query.sort(format("{0} DESC",TIMESTAMP)).exec(db);
|
||||
while (rs.next()){
|
||||
var bookmark = Bookmark.of(rs);
|
||||
map.put(bookmark.urlId(),bookmark);
|
||||
}
|
||||
rs.close();;
|
||||
return map;
|
||||
} catch (SQLException e) {
|
||||
throw new UmbrellaException("Failed to load bookmark list");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<Long, Bookmark> list(long userId, Long offset, Long limit) {
|
||||
try {
|
||||
var map = new HashMap<Long,Bookmark>();
|
||||
var rs = select(ALL).from(TABLE_URL_COMMENTS).leftJoin(URL_ID,TABLE_URLS,ID).where(USER_ID, equal(userId)).sort(format("{0} DESC",TIMESTAMP)).skip(offset).limit(limit).exec(db);
|
||||
|
||||
Reference in New Issue
Block a user