implemented /bookmark/<ID>/view
This commit is contained in:
@@ -19,11 +19,10 @@ 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 org.json.JSONArray;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import org.json.JSONArray;
|
||||
|
||||
public class BookmarkApi extends BaseHandler {
|
||||
private final BookmarkDb db;
|
||||
@@ -47,7 +46,11 @@ public class BookmarkApi extends BaseHandler {
|
||||
var head = path.pop();
|
||||
return switch (head) {
|
||||
case LIST -> getUserBookmarks(user.get(),ex);
|
||||
case null, default -> super.doPost(path,ex);
|
||||
case null -> super.doPost(path,ex);
|
||||
default -> {
|
||||
var id = Long.parseLong(head);
|
||||
yield getBookmark(user.get(),id,ex);
|
||||
}
|
||||
};
|
||||
} catch (NumberFormatException e){
|
||||
return sendContent(ex,HTTP_BAD_REQUEST,"Invalid project id");
|
||||
@@ -56,6 +59,12 @@ public class BookmarkApi extends BaseHandler {
|
||||
}
|
||||
}
|
||||
|
||||
private boolean getBookmark(UmbrellaUser user, long id, HttpExchange ex) throws IOException {
|
||||
var bookmark = db.load(id,user.id());
|
||||
tags.getTags(BOOKMARK, id, user).forEach(bookmark.tags()::add);
|
||||
return sendContent(ex,bookmark);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean doPost(Path path, HttpExchange ex) throws IOException {
|
||||
addCors(ex);
|
||||
|
||||
@@ -5,7 +5,9 @@ import de.srsoftware.umbrella.core.model.Bookmark;
|
||||
import java.util.Map;
|
||||
|
||||
public interface BookmarkDb {
|
||||
Bookmark save(String url, String comment, long userId);
|
||||
Map<Long, Bookmark> list(long userId);
|
||||
|
||||
Map<Long, Bookmark> list(long id);
|
||||
Bookmark load(long id, long userId);
|
||||
|
||||
Bookmark save(String url, String comment, long userId);
|
||||
}
|
||||
|
||||
@@ -1,22 +1,20 @@
|
||||
/* © SRSoftware 2025 */
|
||||
package de.srsoftware.umbrella.bookmarks;
|
||||
|
||||
import static de.srsoftware.tools.jdbc.Condition.equal;
|
||||
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.Constants.ERROR_FAILED_CREATE_TABLE;
|
||||
import static de.srsoftware.umbrella.core.Util.sha1;
|
||||
import static java.lang.System.Logger.Level.ERROR;
|
||||
import static java.text.MessageFormat.format;
|
||||
import static java.time.ZoneOffset.UTC;
|
||||
|
||||
import de.srsoftware.tools.jdbc.Condition;
|
||||
import de.srsoftware.umbrella.core.BaseDb;
|
||||
import de.srsoftware.umbrella.core.exceptions.UmbrellaException;
|
||||
import de.srsoftware.umbrella.core.model.Bookmark;
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.SQLException;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.HashMap;
|
||||
@@ -73,7 +71,7 @@ CREATE TABLE IF NOT EXISTS {0} (
|
||||
public Map<Long, Bookmark> list(long userId) {
|
||||
try {
|
||||
var map = new HashMap<Long,Bookmark>();
|
||||
var rs = select(ALL).from(TABLE_URL_COMMENTS).leftJoin(URL_ID,TABLE_URLS,ID).where(USER_ID, Condition.equal(userId)).exec(db);
|
||||
var rs = select(ALL).from(TABLE_URL_COMMENTS).leftJoin(URL_ID,TABLE_URLS,ID).where(USER_ID, equal(userId)).exec(db);
|
||||
while (rs.next()){
|
||||
var bookmark = Bookmark.of(rs);
|
||||
map.put(bookmark.id(),bookmark);
|
||||
@@ -85,11 +83,25 @@ CREATE TABLE IF NOT EXISTS {0} (
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Bookmark load(long id, long userId) {
|
||||
try {
|
||||
Bookmark result = null;
|
||||
var rs = select(ALL).from(TABLE_URLS).leftJoin(ID,TABLE_URL_COMMENTS,URL_ID).where(ID,equal(id)).where(USER_ID,equal(userId)).exec(db);
|
||||
if (rs.next()) result = Bookmark.of(rs);
|
||||
rs.close();
|
||||
if (result != null) return result;
|
||||
throw UmbrellaException.notFound("No bookmark with id {0}",id);
|
||||
} catch (SQLException e) {
|
||||
throw new UmbrellaException("Failed to load bookmark");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Bookmark save(String url, String comment, long userId) {
|
||||
try {
|
||||
var timestamp = LocalDateTime.now();
|
||||
var rs = select(ID).from(TABLE_URLS).where(URL,Condition.equal(url)).exec(db);
|
||||
var rs = select(ID).from(TABLE_URLS).where(URL, equal(url)).exec(db);
|
||||
var id = 0L;
|
||||
if (rs.next()) id = rs.getLong(ID);
|
||||
rs.close();
|
||||
|
||||
Reference in New Issue
Block a user