implemented sharing of bookmarks
This commit is contained in:
@@ -20,7 +20,7 @@ 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.List;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Optional;
|
||||
import org.json.JSONArray;
|
||||
|
||||
@@ -74,8 +74,8 @@ public class BookmarkApi extends BaseHandler {
|
||||
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);
|
||||
case null -> postBookmark(user.get(),ex);
|
||||
default -> super.doPost(path,ex);
|
||||
};
|
||||
} catch (NumberFormatException e){
|
||||
return sendContent(ex,HTTP_BAD_REQUEST,"Invalid project id");
|
||||
@@ -93,10 +93,19 @@ public class BookmarkApi extends BaseHandler {
|
||||
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 bookmark = db.save(url,comment, user.id());
|
||||
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");
|
||||
userList.add(uid.longValue());
|
||||
}
|
||||
}
|
||||
var bookmark = db.save(url,comment, userList);
|
||||
|
||||
if (json.has(TAGS) && json.get(TAGS) instanceof JSONArray tagList){
|
||||
var list = tagList.toList().stream().map(Object::toString).toList();
|
||||
tags.save(BOOKMARK,bookmark.id(), List.of(user.id()),list);
|
||||
tags.save(BOOKMARK,bookmark.id(), userList, list);
|
||||
}
|
||||
return sendContent(ex,bookmark);
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
package de.srsoftware.umbrella.bookmarks;
|
||||
|
||||
import de.srsoftware.umbrella.core.model.Bookmark;
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
|
||||
public interface BookmarkDb {
|
||||
@@ -9,5 +10,5 @@ public interface BookmarkDb {
|
||||
|
||||
Bookmark load(long id, long userId);
|
||||
|
||||
Bookmark save(String url, String comment, long userId);
|
||||
Bookmark save(String url, String comment, Collection<Long> userIds);
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package de.srsoftware.umbrella.bookmarks;
|
||||
|
||||
public class Constants {
|
||||
public static final String CONFIG_DATABASE = "umbrella.modules.bookmark.database";
|
||||
public static final String SHARE = "share";
|
||||
public static final String SAVE = "save";
|
||||
public static final String TABLE_URLS = "urls";
|
||||
public static final String TABLE_URL_COMMENTS = "url_comments";
|
||||
|
||||
@@ -17,6 +17,7 @@ import de.srsoftware.umbrella.core.model.Bookmark;
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -98,7 +99,7 @@ CREATE TABLE IF NOT EXISTS {0} (
|
||||
}
|
||||
|
||||
@Override
|
||||
public Bookmark save(String url, String comment, long userId) {
|
||||
public Bookmark save(String url, String comment, Collection<Long> userIds) {
|
||||
try {
|
||||
var timestamp = LocalDateTime.now();
|
||||
var rs = select(ID).from(TABLE_URLS).where(URL, equal(url)).exec(db);
|
||||
@@ -112,9 +113,9 @@ CREATE TABLE IF NOT EXISTS {0} (
|
||||
rs.close();
|
||||
stmt.close();
|
||||
}
|
||||
replaceInto(TABLE_URL_COMMENTS,URL_ID,USER_ID,COMMENT, TIMESTAMP)
|
||||
.values(id,userId,comment,timestamp.toEpochSecond(UTC))
|
||||
.execute(db).close();
|
||||
var query = replaceInto(TABLE_URL_COMMENTS,URL_ID,USER_ID,COMMENT, TIMESTAMP);
|
||||
for (long userId : userIds) query.values(id,userId,comment,timestamp.toEpochSecond(UTC));
|
||||
query.execute(db).close();
|
||||
return Bookmark.of(id,url,comment,timestamp);
|
||||
} catch (SQLException e) {
|
||||
throw new UmbrellaException("Failed to store url");
|
||||
|
||||
Reference in New Issue
Block a user