implemented search in wiki pages
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
package de.srsoftware.umbrella.wiki;
|
||||
|
||||
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.*;
|
||||
@@ -151,7 +152,25 @@ public class SqliteDb extends BaseDb implements WikiDb {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@Override
|
||||
public Map<Long, WikiPage> find(long userId, List<String> keys, boolean fulltext) {
|
||||
try {
|
||||
var query = select(ALL).from(TABLE_PAGES).leftJoin(ID,TABLE_PAGES_USERS,PAGE_ID).where(USER_ID,equal(userId));
|
||||
for (var key : keys) query.where(TITLE,like("%"+key+"%"));
|
||||
var rs = query.exec(db);
|
||||
var map = new HashMap<Long,WikiPage>();
|
||||
while (rs.next()) {
|
||||
var page = WikiPage.of(rs);
|
||||
map.put(page.id(),page);
|
||||
}
|
||||
rs.close();
|
||||
return map;
|
||||
} catch (SQLException e) {
|
||||
throw databaseException("Failed to search \"{0}\" in wiki pages",String.join(" "+keys));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAvailable(String title) {
|
||||
if (title==null||title.isBlank())return false;
|
||||
try {
|
||||
|
||||
@@ -11,6 +11,8 @@ public interface WikiDb {
|
||||
|
||||
WikiPage drop(WikiPage page);
|
||||
|
||||
Map<Long,WikiPage> find(long userId, List<String> keys, boolean fulltext);
|
||||
|
||||
long getNextId();
|
||||
|
||||
boolean isAvailable(String title);
|
||||
|
||||
@@ -2,10 +2,11 @@
|
||||
package de.srsoftware.umbrella.wiki;
|
||||
|
||||
import static de.srsoftware.umbrella.core.ConnectionProvider.connect;
|
||||
import static de.srsoftware.umbrella.core.Constants.VERSION;
|
||||
import static de.srsoftware.umbrella.core.Constants.*;
|
||||
import static de.srsoftware.umbrella.core.Constants.FULLTEXT;
|
||||
import static de.srsoftware.umbrella.core.ModuleRegistry.userService;
|
||||
import static de.srsoftware.umbrella.core.Paths.AVAILABLE;
|
||||
import static de.srsoftware.umbrella.core.Paths.PAGE;
|
||||
import static de.srsoftware.umbrella.core.Paths.*;
|
||||
import static de.srsoftware.umbrella.core.Util.mapValues;
|
||||
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.*;
|
||||
import static de.srsoftware.umbrella.core.model.Permission.EDIT;
|
||||
import static de.srsoftware.umbrella.wiki.Constants.*;
|
||||
@@ -20,7 +21,9 @@ import de.srsoftware.umbrella.core.exceptions.UmbrellaException;
|
||||
import de.srsoftware.umbrella.core.model.*;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class WikiModule extends BaseHandler implements WikiService {
|
||||
private final WikiDb wikiDb;
|
||||
@@ -105,15 +108,25 @@ public class WikiModule extends BaseHandler implements WikiService {
|
||||
Optional<Token> token = SessionToken.from(ex).map(Token::of);
|
||||
var user = userService().loadUser(token);
|
||||
if (user.isEmpty()) return unauthorized(ex);
|
||||
var title = path.pop();
|
||||
var head = path.pop();
|
||||
if (!path.empty()) return super.doPost(path,ex);
|
||||
return postNewPage(title,user.get(),ex);
|
||||
if (SEARCH.equals(head)) return postSearch(user.get(),ex);
|
||||
return postNewPage(head,user.get(),ex);
|
||||
} catch (UmbrellaException e){
|
||||
return send(ex,e);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean getAvailability(Path path, HttpExchange ex) throws IOException {
|
||||
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 pages = wikiDb.find(user.id(),keys,fulltext);
|
||||
return sendContent(ex,mapValues(pages));
|
||||
}
|
||||
|
||||
private boolean getAvailability(Path path, HttpExchange ex) throws IOException {
|
||||
return sendContent(ex,wikiDb.isAvailable(path.pop()));
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user