implemented storing of bookmarks

Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
This commit is contained in:
2025-08-02 22:48:49 +02:00
parent d1b8d1a062
commit 61b5a6ffbb
10 changed files with 130 additions and 5 deletions

View File

@@ -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");
}
}
}