working on notes

This commit is contained in:
2025-07-28 21:31:26 +02:00
parent 6600b6fd27
commit 325dffa3fe
9 changed files with 102 additions and 40 deletions

View File

@@ -7,5 +7,4 @@ public class Constants {
public static final String CONFIG_DATABASE = "umbrella.modules.notes.database";
public static final String DB_VERSION = "notes_db_version";
public static final String TABLE_NOTES = "notes";
public static final String NOTE = "note";
}

View File

@@ -2,12 +2,11 @@
package de.srsoftware.umbrella.notes;
import static de.srsoftware.umbrella.core.ConnectionProvider.connect;
import static de.srsoftware.umbrella.core.Constants.USER_LIST;
import static de.srsoftware.umbrella.core.ResponseCode.HTTP_UNPROCESSABLE;
import static de.srsoftware.umbrella.core.Util.mapValues;
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.missingFieldException;
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.unprocessable;
import static de.srsoftware.umbrella.notes.Constants.CONFIG_DATABASE;
import static de.srsoftware.umbrella.notes.Constants.NOTE;
import static java.net.HttpURLConnection.HTTP_OK;
import com.sun.net.httpserver.HttpExchange;
@@ -16,18 +15,16 @@ import de.srsoftware.tools.Path;
import de.srsoftware.tools.SessionToken;
import de.srsoftware.umbrella.core.BaseHandler;
import de.srsoftware.umbrella.core.api.NoteService;
import de.srsoftware.umbrella.core.api.TagService;
import de.srsoftware.umbrella.core.api.UserService;
import de.srsoftware.umbrella.core.exceptions.UmbrellaException;
import de.srsoftware.umbrella.core.model.Note;
import de.srsoftware.umbrella.core.model.Token;
import de.srsoftware.umbrella.core.model.UmbrellaUser;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.json.JSONArray;
public class NoteModule extends BaseHandler implements NoteService {
private final NotesDb notesDb;
@@ -74,7 +71,7 @@ public class NoteModule extends BaseHandler implements NoteService {
if (module == null) throw unprocessable("Module missing in path.");
var head = path.pop();
long entityId = Long.parseLong(head);
return sendContent(ex, getNotes(module,entityId));
return sendContent(ex, mapValues(getNotes(module,entityId)));
} catch (NumberFormatException e){
return sendContent(ex,HTTP_UNPROCESSABLE,"Entity id missing in path.");
} catch (UmbrellaException e){
@@ -99,20 +96,8 @@ public class NoteModule extends BaseHandler implements NoteService {
if (module == null) throw unprocessable("Module missing in path.");
var head = path.pop();
long entityId = Long.parseLong(head);
var json = json(ex);
if (!(json.has(NOTE) && json.get(NOTE) instanceof String text)) throw missingFieldException(NOTE);
List<Long> userList = null;
if (!json.has(USER_LIST)) throw missingFieldException(USER_LIST);
var ul = json.isNull(USER_LIST) ? null : json.get(USER_LIST);
if (ul instanceof JSONArray arr){
userList = arr.toList().stream()
.filter(elem -> elem instanceof Number)
.map(Number.class::cast)
.map(Number::longValue)
.toList();
} else if (ul != null) throw unprocessable("User list must be NULL or array of user ids!");
// userList == null → tag is shared with all users
if (userList != null && userList.isEmpty()) throw unprocessable("User list must not be an empty list!");
String text = body(ex);
if (text.isBlank()) throw missingFieldException("Note text");
var note = new Note(0,module,entityId,user.get().id(),text, LocalDateTime.now());
note = save(note);
return sendContent(ex, note);
@@ -123,7 +108,7 @@ public class NoteModule extends BaseHandler implements NoteService {
}
}
public Collection<Note> getNotes(String module, long entityId) throws UmbrellaException{
public Map<Long,Note> getNotes(String module, long entityId) throws UmbrellaException{
return notesDb.list(module,entityId);
}

View File

@@ -3,15 +3,14 @@ package de.srsoftware.umbrella.notes;
import de.srsoftware.umbrella.core.model.Note;
import java.util.Collection;
import java.util.Set;
import java.util.Map;
public interface NotesDb {
long delete(long noteId, long userId);
void deleteEntity(String module, long entityId);
Set<Note> list(String module, long entityId);
Map<Long, Note> list(String module, long entityId);
Note save(Note note);
}

View File

@@ -8,15 +8,16 @@ import static de.srsoftware.umbrella.core.Constants.*;
import static de.srsoftware.umbrella.notes.Constants.*;
import static java.lang.System.Logger.Level.*;
import static java.text.MessageFormat.format;
import static java.time.ZoneOffset.UTC;
import de.srsoftware.tools.jdbc.Query;
import de.srsoftware.umbrella.core.exceptions.UmbrellaException;
import de.srsoftware.umbrella.core.model.Note;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
import java.util.Map;
public class SqliteDb implements NotesDb {
private static final int INITIAL_DB_VERSION = 1;
@@ -113,25 +114,37 @@ CREATE TABLE IF NOT EXISTS "{0}" (
}
@Override
public Set<Note> list(String module, long entityId) {
public Map<Long, Note> list(String module, long entityId) {
try {
var tags = new HashSet<Note>();
var notes = new HashMap<Long, Note>();
var rs = select(ALL).from(TABLE_NOTES).where(MODULE,equal(module)).where(ENTITY_ID,equal(entityId)).exec(db);
while (rs.next()) tags.add(Note.of(rs));
while (rs.next()) {
var note = Note.of(rs);
notes.put(note.id(),note);
}
rs.close();
return tags;
return notes;
} catch (SQLException e) {
throw new UmbrellaException("Failed to load tags");
throw new UmbrellaException("Failed to load notes");
}
}
@Override
public Note save(Note note) {
try {
replaceInto(TABLE_NOTES,USER_ID,MODULE,ENTITY_ID,NOTE,TIMESTAMP)
.values(note.authorId(),note.module(),note.entityId(),note.text(),note.timestamp())
.execute(db).close();
return note;
if (note.id() == 0) {
var rs = replaceInto(TABLE_NOTES, USER_ID, MODULE, ENTITY_ID, NOTE, TIMESTAMP)
.values(note.authorId(), note.module(), note.entityId(), note.text(), note.timestamp().toEpochSecond(UTC))
.execute(db)
.getGeneratedKeys();
long id = 0;
if (rs.next()) id = rs.getLong(1);
rs.close();
if (id != 0) return new Note(id,note.module(),note.entityId(),note.authorId(),note.text(),note.timestamp());
return note;
} else {
throw new RuntimeException("note.update not implemented");
}
} catch (SQLException e){
throw new UmbrellaException("Failed to save note: {0}",note.text());
}