working on wiki index

Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
This commit is contained in:
2025-09-09 16:19:03 +02:00
parent 3b8e7ed258
commit 3627ccee73
7 changed files with 113 additions and 17 deletions

View File

@@ -9,6 +9,6 @@ public class Constants {
public static final String PAGE_ID = "page_id";
public static final String PERMISSIONS = "permissions";
public static final String TABLE_PAGES = "pages";
public static final String TABLE_PAGES_USERS = "pages_users";
public static final String TABLE_PAGES_USERS = "page_users";
public static final String VERSION = "version";
}

View File

@@ -1,21 +1,16 @@
package de.srsoftware.umbrella.wiki;
import de.srsoftware.tools.jdbc.Condition;
import de.srsoftware.umbrella.core.BaseDb;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.*;
import static de.srsoftware.tools.jdbc.Query.select;
import static de.srsoftware.umbrella.core.Constants.*;
import static de.srsoftware.umbrella.core.Constants.DESCRIPTION;
import static de.srsoftware.umbrella.core.Constants.DUE_DATE;
import static de.srsoftware.umbrella.core.Constants.ERROR_FAILED_CREATE_TABLE;
import static de.srsoftware.umbrella.core.Constants.EST_TIME;
import static de.srsoftware.umbrella.core.Constants.NAME;
import static de.srsoftware.umbrella.core.Constants.NO_INDEX;
import static de.srsoftware.umbrella.core.Constants.SHOW_CLOSED;
import static de.srsoftware.umbrella.core.Constants.START_DATE;
import static de.srsoftware.umbrella.core.Constants.STATUS;
import static de.srsoftware.umbrella.core.model.Status.OPEN;
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.databaseException;
import static de.srsoftware.umbrella.wiki.Constants.*;
import static java.lang.System.Logger.Level.ERROR;
import static java.text.MessageFormat.format;
@@ -63,4 +58,17 @@ public class SqliteDb extends BaseDb implements WikiDb {
throw new RuntimeException(e);
}
}
@Override
public List<String> listUserPages(long userId) {
try {
var rs = select(ID,"MAX(version) AS version").from(TABLE_PAGES).leftJoin(ID,TABLE_PAGES_USERS,PAGE_ID).where(USER_ID, Condition.equal(userId)).groupBy(ID).sort("ID COLLATE NOCASE ASC").exec(db);
var set = new ArrayList<String>();
while (rs.next()) set.add(rs.getString(ID));
rs.close();
return set;
} catch (SQLException e) {
throw databaseException("Failed to list wiki articles for user {0}",userId);
}
}
}

View File

@@ -2,15 +2,9 @@
package de.srsoftware.umbrella.wiki;
import de.srsoftware.umbrella.core.exceptions.UmbrellaException;
import de.srsoftware.umbrella.core.model.Permission;
import de.srsoftware.umbrella.core.model.Task;
import de.srsoftware.umbrella.core.model.UmbrellaUser;
import java.util.Collection;
import java.util.List;
import java.util.Map;
public interface WikiDb {
List<String> listUserPages(long userId);
}

View File

@@ -1,11 +1,21 @@
package de.srsoftware.umbrella.wiki;
import com.sun.net.httpserver.HttpExchange;
import de.srsoftware.configuration.Configuration;
import de.srsoftware.tools.Path;
import de.srsoftware.tools.SessionToken;
import de.srsoftware.umbrella.core.BaseHandler;
import de.srsoftware.umbrella.core.ModuleRegistry;
import de.srsoftware.umbrella.core.api.WikiService;
import de.srsoftware.umbrella.core.exceptions.UmbrellaException;
import de.srsoftware.umbrella.core.model.Token;
import de.srsoftware.umbrella.core.model.UmbrellaUser;
import java.io.IOException;
import java.util.Optional;
import static de.srsoftware.umbrella.core.ConnectionProvider.connect;
import static de.srsoftware.umbrella.core.Util.mapValues;
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.missingFieldException;
import static de.srsoftware.umbrella.wiki.Constants.CONFIG_DATABASE;
@@ -17,4 +27,28 @@ public class WikiModule extends BaseHandler implements WikiService {
var dbFile = config.get(CONFIG_DATABASE).orElseThrow(() -> missingFieldException(CONFIG_DATABASE));
wikiDb = new SqliteDb(connect(dbFile));
}
@Override
public boolean doGet(Path path, HttpExchange ex) throws IOException {
addCors(ex);
try {
Optional<Token> token = SessionToken.from(ex).map(Token::of);
var user = userService().loadUser(token);
if (user.isEmpty()) return unauthorized(ex);
var head = path.pop();
return switch (head) {
case null -> getUserPages(user.get(),ex);
default -> super.doGet(path,ex);
};
} catch (UmbrellaException e){
return send(ex,e);
}
}
private boolean getUserPages(UmbrellaUser user, HttpExchange ex) throws IOException {
var pageList = wikiDb.listUserPages(user.id());
return sendContent(ex,pageList);
}
}