implemented editing of notes

Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
This commit is contained in:
2025-07-30 18:48:29 +02:00
parent fccec9865a
commit e08bcf17a5
5 changed files with 70 additions and 16 deletions

View File

@@ -89,6 +89,28 @@ public class NoteModule extends BaseHandler implements NoteService {
return sendEmptyResponse(HTTP_OK,ex);
}
@Override
public boolean doPatch(Path path, HttpExchange ex) throws IOException {
addCors(ex);
try {
Optional<Token> token = SessionToken.from(ex).map(Token::of);
var user = users.loadUser(token);
if (user.isEmpty()) return unauthorized(ex);
var head = path.pop();
long noteId = Long.parseLong(head);
String text = body(ex);
if (text.isBlank()) throw missingFieldException("Note text");
var note = notesDb.load(noteId);
note = new Note(note.id(),note.module(),note.entityId(),note.authorId(),text,LocalDateTime.now());
note = save(note);
return sendContent(ex,note);
} catch (NumberFormatException e) {
return sendContent(ex, HTTP_UNPROCESSABLE, "Entity id missing in path.");
} catch (UmbrellaException e){
return send(ex,e);
}
}
@Override
public boolean doPost(Path path, HttpExchange ex) throws IOException {
addCors(ex);

View File

@@ -12,5 +12,7 @@ public interface NotesDb {
Map<Long, Note> list(String module, long entityId);
Note load(long noteId);
Note save(Note note);
}

View File

@@ -129,11 +129,24 @@ CREATE TABLE IF NOT EXISTS "{0}" (
}
}
@Override
public Note load(long noteId) {
try {
Note note = null;
var rs = select(ALL).from(TABLE_NOTES).where(ID,equal(noteId)).exec(db);
if (rs.next()) note = Note.of(rs);
rs.close();
return note;
} catch (SQLException e) {
throw new UmbrellaException("Failed to load note {0}",noteId);
}
}
@Override
public Note save(Note note) {
try {
if (note.id() == 0) {
var rs = replaceInto(TABLE_NOTES, USER_ID, MODULE, ENTITY_ID, NOTE, TIMESTAMP)
var rs = insertInto(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();
@@ -141,10 +154,13 @@ CREATE TABLE IF NOT EXISTS "{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");
replaceInto(TABLE_NOTES, ID, USER_ID, MODULE, ENTITY_ID, NOTE, TIMESTAMP)
.values(note.id(), note.authorId(), note.module(), note.entityId(), note.text(), note.timestamp().toEpochSecond(UTC))
.execute(db).close();
}
return note;
} catch (SQLException e){
throw new UmbrellaException("Failed to save note: {0}",note.text());
}