overhauling constants, working on translations
Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
This commit is contained in:
@@ -3,11 +3,13 @@ package de.srsoftware.umbrella.bookmarks;
|
||||
|
||||
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 de.srsoftware.umbrella.core.constants.Field.*;
|
||||
import static de.srsoftware.umbrella.core.constants.Field.TAGS;
|
||||
import static de.srsoftware.umbrella.core.constants.Module.BOOKMARK;
|
||||
import static de.srsoftware.umbrella.core.constants.Path.LIST;
|
||||
import static de.srsoftware.umbrella.core.constants.Path.SEARCH;
|
||||
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.*;
|
||||
import static java.net.HttpURLConnection.HTTP_BAD_REQUEST;
|
||||
|
||||
import com.sun.net.httpserver.HttpExchange;
|
||||
@@ -32,9 +34,9 @@ public class BookmarkApi extends BaseHandler implements BookmarkService {
|
||||
|
||||
public BookmarkApi(Configuration config) {
|
||||
super();
|
||||
var dbFile = config.get(CONFIG_DATABASE).orElseThrow(() -> missingFieldException(CONFIG_DATABASE));
|
||||
var dbFile = config.get(CONFIG_DATABASE).orElseThrow(() -> missingField(CONFIG_DATABASE));
|
||||
db = new SqliteDb(connect(dbFile));
|
||||
ModuleRegistry.add(this);
|
||||
ModuleRegistry.add(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -110,13 +112,13 @@ public class BookmarkApi extends BaseHandler implements BookmarkService {
|
||||
|
||||
private boolean postBookmark(UmbrellaUser user, HttpExchange ex) throws IOException {
|
||||
var json = json(ex);
|
||||
if (!(json.has(URL) && json.get(URL) instanceof String url)) throw missingFieldException(URL);
|
||||
if (!(json.has(COMMENT) && json.get(COMMENT) instanceof String comment)) throw missingFieldException(COMMENT);
|
||||
if (!(json.has(URL) && json.get(URL) instanceof String url)) throw missingField(URL);
|
||||
if (!(json.has(COMMENT) && json.get(COMMENT) instanceof String comment)) throw missingField(COMMENT);
|
||||
var userList = new ArrayList<Long>();
|
||||
userList.add(user.id());
|
||||
if (json.has(SHARE) && json.get(SHARE) instanceof JSONArray arr){
|
||||
for (Object o : arr.toList()) {
|
||||
if (!(o instanceof Number uid)) throw UmbrellaException.invalidFieldException(SHARE,"Array of ids");
|
||||
if (!(o instanceof Number uid)) throw invalidField(SHARE,"Array of ids");
|
||||
userList.add(uid.longValue());
|
||||
}
|
||||
}
|
||||
@@ -131,7 +133,7 @@ public class BookmarkApi extends BaseHandler implements BookmarkService {
|
||||
|
||||
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);
|
||||
if (!(json.has(KEY) && json.get(KEY) instanceof String key)) throw missingField(KEY);
|
||||
var keys = Arrays.asList(key.split(" "));
|
||||
var fulltext = json.has(FULLTEXT) && json.get(FULLTEXT) instanceof Boolean val && val;
|
||||
var bookmarks = db.findUrls(user.id(),keys);
|
||||
|
||||
@@ -6,15 +6,16 @@ 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.*;
|
||||
import static de.srsoftware.umbrella.core.Constants.*;
|
||||
import static de.srsoftware.umbrella.core.Errors.*;
|
||||
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.databaseException;
|
||||
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.notFound;
|
||||
import static de.srsoftware.umbrella.core.constants.Field.*;
|
||||
import static de.srsoftware.umbrella.core.constants.Text.BOOKMARK;
|
||||
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.*;
|
||||
import static java.text.MessageFormat.format;
|
||||
import static java.time.ZoneOffset.UTC;
|
||||
|
||||
import de.srsoftware.umbrella.core.BaseDb;
|
||||
import de.srsoftware.umbrella.core.model.Bookmark;
|
||||
import de.srsoftware.umbrella.core.model.Translatable;
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.time.LocalDateTime;
|
||||
@@ -48,7 +49,7 @@ CREATE TABLE IF NOT EXISTS {0} (
|
||||
PRIMARY KEY (`{1}`,`{2}`)
|
||||
)""";
|
||||
try {
|
||||
var stmt = db.prepareStatement(format(sql,TABLE_URL_COMMENTS,URL_ID,USER_ID,COMMENT,TIMESTAMP));
|
||||
var stmt = db.prepareStatement(format(sql,TABLE_URL_COMMENTS,URL_ID, USER_ID, COMMENT,TIMESTAMP));
|
||||
stmt.execute();
|
||||
stmt.close();
|
||||
} catch (SQLException e) {
|
||||
@@ -63,7 +64,7 @@ CREATE TABLE IF NOT EXISTS {0} (
|
||||
stmt.execute();
|
||||
stmt.close();
|
||||
} catch (SQLException e) {
|
||||
throw databaseException(FAILED_TO_CREATE_TABLE,TABLE_URLS).causedBy(e);
|
||||
throw failedToCreateTable(TABLE_URLS).causedBy(e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -76,7 +77,7 @@ CREATE TABLE IF NOT EXISTS {0} (
|
||||
rs.close();;
|
||||
return map;
|
||||
} catch (SQLException e) {
|
||||
throw databaseException(FAILED_TO_LOAD_ENTITY,"bookmark list").causedBy(e);
|
||||
throw failedToLoadObject("bookmark list").causedBy(e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -84,7 +85,7 @@ CREATE TABLE IF NOT EXISTS {0} (
|
||||
public Map<Long, Bookmark> findUrls(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)
|
||||
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);
|
||||
@@ -95,7 +96,7 @@ CREATE TABLE IF NOT EXISTS {0} (
|
||||
rs.close();;
|
||||
return map;
|
||||
} catch (SQLException e) {
|
||||
throw databaseException(FAILED_TO_DROP_ENTITY,"bookmark list").causedBy(e);
|
||||
throw failedToDropObject("bookmark list").causedBy(e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -103,7 +104,7 @@ CREATE TABLE IF NOT EXISTS {0} (
|
||||
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);
|
||||
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);
|
||||
while (rs.next()){
|
||||
var bookmark = Bookmark.of(rs);
|
||||
map.put(bookmark.urlId(),bookmark);
|
||||
@@ -111,7 +112,7 @@ CREATE TABLE IF NOT EXISTS {0} (
|
||||
rs.close();;
|
||||
return map;
|
||||
} catch (SQLException e) {
|
||||
throw databaseException(FAILED_TO_LOAD_ENTITY,"bookmark list").causedBy(e);
|
||||
throw failedToLoadObject("bookmark list").causedBy(e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -123,9 +124,9 @@ CREATE TABLE IF NOT EXISTS {0} (
|
||||
if (rs.next()) result = Bookmark.of(rs);
|
||||
rs.close();
|
||||
if (result != null) return result;
|
||||
throw notFound(NO_BOOKMARK_FOR_URLID,id);
|
||||
throw failedToLoadObject(Translatable.t(BOOKMARK),id);
|
||||
} catch (SQLException e) {
|
||||
throw databaseException(FAILED_TO_LOAD_ENTITY,"bookmark").causedBy(e);
|
||||
throw failedToLoadObject(Translatable.t(BOOKMARK),id).causedBy(e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -143,12 +144,12 @@ CREATE TABLE IF NOT EXISTS {0} (
|
||||
rs.close();
|
||||
stmt.close();
|
||||
}
|
||||
var query = replaceInto(TABLE_URL_COMMENTS,URL_ID,USER_ID,COMMENT, TIMESTAMP);
|
||||
var query = replaceInto(TABLE_URL_COMMENTS,URL_ID, USER_ID, COMMENT, TIMESTAMP);
|
||||
for (long userId : userIds) query.values(urlId,userId,comment,timestamp.toEpochSecond(UTC));
|
||||
query.execute(db).close();
|
||||
return Bookmark.of(urlId,url,comment,timestamp);
|
||||
} catch (SQLException e) {
|
||||
throw databaseException(FAILED_TO_STORE_ENTITY,"url").causedBy(e);
|
||||
throw failedToStoreObject(this).causedBy(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user