implemented deletion and updating of notes

This commit is contained in:
2025-07-30 21:00:18 +02:00
parent 3dab95691b
commit 8446f1e08f
8 changed files with 71 additions and 20 deletions

View File

@@ -77,10 +77,14 @@ public class NoteModule extends BaseHandler implements NoteService {
var user = users.refreshSession(ex);
if (user.isEmpty()) return unauthorized(ex);
var module = path.pop();
if (module == null) throw unprocessable("Module missing in path.");
var head = path.pop();
long entityId = Long.parseLong(head);
var notes = getNotes(module,entityId);
Map<Long,Note> notes = null;
if (module == null) {
notes = notesDb.list(user.get().id());
} else {
var head = path.pop();
long entityId = Long.parseLong(head);
notes = getNotes(module, entityId);
}
var authors = notes.values().stream().map(Note::authorId).distinct().map(users::loadUser).collect(Collectors.toMap(UmbrellaUser::id,UmbrellaUser::toMap));
return sendContent(ex, Map.of("notes",mapValues(notes),"authors",authors));
} catch (NumberFormatException e){
@@ -108,6 +112,7 @@ public class NoteModule extends BaseHandler implements NoteService {
String text = body(ex);
if (text.isBlank()) throw missingFieldException("Note text");
var note = notesDb.load(noteId);
if (note.authorId() != user.get().id()) throw forbidden("You are not allowed to edit notes of another user!");
note = new Note(note.id(),note.module(),note.entityId(),note.authorId(),text,LocalDateTime.now());
note = save(note);
return sendContent(ex,note);

View File

@@ -10,6 +10,18 @@ public interface NotesDb {
void deleteEntity(String module, long entityId);
/**
* get all lists of a person
* @return
*/
Map<Long, Note> list(long authorId);
/**
* get the notes related to a specific entity
* @param module
* @param entityId
* @return
*/
Map<Long, Note> list(String module, long entityId);
Note load(long noteId);

View File

@@ -16,7 +16,6 @@ import de.srsoftware.umbrella.core.model.Note;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
public class SqliteDb implements NotesDb {
@@ -112,6 +111,22 @@ CREATE TABLE IF NOT EXISTS "{0}" (
LOG.log(INFO,"Updated task db to version {0}",version);
}
@Override
public Map<Long, Note> list(long authorId) {
try {
var notes = new HashMap<Long, Note>();
var rs = select(ALL).from(TABLE_NOTES).where(USER_ID,equal(authorId)).exec(db);
while (rs.next()) {
var note = Note.of(rs);
notes.put(note.id(),note);
}
rs.close();
return notes;
} catch (SQLException e) {
throw new UmbrellaException("Failed to load notes");
}
}
@Override
public Map<Long, Note> list(String module, long entityId) {
try {