implemented storing of bookmarks
Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
This commit is contained in:
@@ -1,13 +1,24 @@
|
||||
/* © SRSoftware 2025 */
|
||||
package de.srsoftware.umbrella.bookmarks;
|
||||
|
||||
import static de.srsoftware.umbrella.bookmarks.Constants.CONFIG_DATABASE;
|
||||
import static de.srsoftware.umbrella.bookmarks.Constants.*;
|
||||
import static de.srsoftware.umbrella.core.ConnectionProvider.connect;
|
||||
import static de.srsoftware.umbrella.core.Constants.URL;
|
||||
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.missingFieldException;
|
||||
import static java.net.HttpURLConnection.HTTP_BAD_REQUEST;
|
||||
import static java.net.HttpURLConnection.HTTP_NOT_IMPLEMENTED;
|
||||
|
||||
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.api.UserService;
|
||||
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;
|
||||
|
||||
public class BookmarkApi extends BaseHandler {
|
||||
private final BookmarkDb db;
|
||||
@@ -18,4 +29,31 @@ public class BookmarkApi extends BaseHandler {
|
||||
db = new SqliteDb(connect(dbFile));
|
||||
users = userService;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean doPost(Path path, HttpExchange ex) throws IOException {
|
||||
addCors(ex);
|
||||
try {
|
||||
Optional<Token> token = SessionToken.from(ex).map(Token::of);
|
||||
var user = users.loadUser(token);
|
||||
if (user.isEmpty()) return unauthorized(ex);
|
||||
var head = path.pop();
|
||||
return switch (head) {
|
||||
case SAVE -> postBookmark(user.get(),ex);
|
||||
case null, default -> super.doPost(path,ex);
|
||||
};
|
||||
} catch (NumberFormatException e){
|
||||
return sendContent(ex,HTTP_BAD_REQUEST,"Invalid project id");
|
||||
} catch (UmbrellaException e){
|
||||
return send(ex,e);
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
var urlHash = db.save(url,comment, user.id());
|
||||
return sendContent(ex,urlHash);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,4 +2,5 @@
|
||||
package de.srsoftware.umbrella.bookmarks;
|
||||
|
||||
public interface BookmarkDb {
|
||||
String save(String url, String comment, long userId);
|
||||
}
|
||||
|
||||
@@ -2,7 +2,10 @@
|
||||
package de.srsoftware.umbrella.bookmarks;
|
||||
|
||||
public class Constants {
|
||||
public static final String COMMENT = "comment";
|
||||
public static final String CONFIG_DATABASE = "umbrella.modules.bookmark.database";
|
||||
public static final String HASH = "hash";
|
||||
public static final String SAVE = "save";
|
||||
public static final String TABLE_URLS = "urls";
|
||||
public static final String TABLE_URL_COMMENTS = "url_comments";
|
||||
|
||||
|
||||
@@ -1,13 +1,16 @@
|
||||
/* © SRSoftware 2025 */
|
||||
package de.srsoftware.umbrella.bookmarks;
|
||||
|
||||
import static de.srsoftware.umbrella.bookmarks.Constants.TABLE_URLS;
|
||||
import static de.srsoftware.umbrella.bookmarks.Constants.TABLE_URL_COMMENTS;
|
||||
import static de.srsoftware.tools.jdbc.Query.insertInto;
|
||||
import static de.srsoftware.tools.jdbc.Query.replaceInto;
|
||||
import static de.srsoftware.umbrella.bookmarks.Constants.*;
|
||||
import static de.srsoftware.umbrella.core.Constants.*;
|
||||
import static de.srsoftware.umbrella.core.Constants.ERROR_FAILED_CREATE_TABLE;
|
||||
import static de.srsoftware.umbrella.core.Util.sha1;
|
||||
import static java.lang.System.Logger.Level.ERROR;
|
||||
|
||||
import de.srsoftware.umbrella.core.BaseDb;
|
||||
import de.srsoftware.umbrella.core.exceptions.UmbrellaException;
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
|
||||
@@ -30,10 +33,10 @@ public class SqliteDb extends BaseDb implements BookmarkDb {
|
||||
private void createUrlCommentsTable() {
|
||||
var sql = """
|
||||
CREATE TABLE IF NOT EXISTS "url_comments" (
|
||||
`url_hash` VARCHAR ( 255 ) NOT NULL,
|
||||
`hash` VARCHAR ( 255 ) NOT NULL,
|
||||
`user_id` LONG NOT NULL,
|
||||
`comment` TEXT NOT NULL,
|
||||
PRIMARY KEY (`url_hash`,`user_id`)
|
||||
PRIMARY KEY (`hash`,`user_id`)
|
||||
)""";
|
||||
try {
|
||||
var stmt = db.prepareStatement(sql);
|
||||
@@ -56,4 +59,19 @@ CREATE TABLE IF NOT EXISTS "url_comments" (
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String save(String url, String comment, long userId) {
|
||||
var hash = sha1(url);
|
||||
try {
|
||||
replaceInto(TABLE_URLS,HASH,URL)
|
||||
.values(hash,url).execute(db).close();
|
||||
replaceInto(TABLE_URL_COMMENTS,HASH,USER_ID,COMMENT)
|
||||
.values(hash,userId,comment)
|
||||
.execute(db).close();
|
||||
return hash;
|
||||
} catch (SQLException e) {
|
||||
throw new UmbrellaException("Failed to store url");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user