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

@@ -32,8 +32,7 @@ public record Note(long id, String module, long entityId, long authorId, String
MODULE,module, MODULE,module,
ENTITY_ID,entityId, ENTITY_ID,entityId,
USER_ID,authorId, USER_ID,authorId,
TEXT,text, TEXT,Map.of(RENDERED,markdown(text),SOURCE,text),
RENDERED,markdown(text),
TIMESTAMP,timestamp.withNano(0) TIMESTAMP,timestamp.withNano(0)
); );
} }

View File

@@ -16,6 +16,18 @@
entity_id = null entity_id = null
} = $props(); } = $props();
async function load(){
const url = api(`notes/${module}/${entity_id}`);
const resp = await fetch(url,{credentials:'include'});
if (resp.ok){
const data = await resp.json();
notes = data.notes;
authors = data.authors;
} else {
error = await resp.text();
}
}
async function saveNote(){ async function saveNote(){
const url = api(`notes/${module}/${entity_id}`); const url = api(`notes/${module}/${entity_id}`);
const resp = await fetch(url,{ const resp = await fetch(url,{
@@ -36,13 +48,16 @@
} }
} }
async function load(){ async function update(nid,src){
const url = api(`notes/${module}/${entity_id}`); const url = api(`notes/${nid}`);
const resp = await fetch(url,{credentials:'include'}); const resp = await fetch(url,{
if (resp.ok){ credentials : 'include',
const data = await resp.json(); method : 'PATCH',
notes = data.notes; body : src
authors = data.authors; });
if (resp.ok) {
error = false;
return true;
} else { } else {
error = await resp.text(); error = await resp.text();
} }
@@ -67,11 +82,11 @@
<span class="error">{error}</span> <span class="error">{error}</span>
{/if} {/if}
{#if notes} {#if notes}
{#each Object.entries(notes) as [a,b]} {#each Object.entries(notes) as [nid,note]}
<fieldset> <fieldset>
<legend class="author">{authors[b.user_id].name}</legend> <legend class="author">{authors[note.user_id].name}</legend>
<legend class="time">{b.timestamp.replace('T',' ')}</legend> <legend class="time">{note.timestamp.replace('T',' ')}</legend>
{@html b.rendered} <Editor value={note.text} onSet={(newVal) => update(nid,newVal)} editable={user.id == note.user_id} />
</fieldset> </fieldset>
{/each} {/each}
{/if} {/if}

View File

@@ -89,6 +89,28 @@ public class NoteModule extends BaseHandler implements NoteService {
return sendEmptyResponse(HTTP_OK,ex); 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 @Override
public boolean doPost(Path path, HttpExchange ex) throws IOException { public boolean doPost(Path path, HttpExchange ex) throws IOException {
addCors(ex); addCors(ex);

View File

@@ -12,5 +12,7 @@ public interface NotesDb {
Map<Long, Note> list(String module, long entityId); Map<Long, Note> list(String module, long entityId);
Note load(long noteId);
Note save(Note note); 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 @Override
public Note save(Note note) { public Note save(Note note) {
try { try {
if (note.id() == 0) { 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)) .values(note.authorId(), note.module(), note.entityId(), note.text(), note.timestamp().toEpochSecond(UTC))
.execute(db) .execute(db)
.getGeneratedKeys(); .getGeneratedKeys();
@@ -141,10 +154,13 @@ CREATE TABLE IF NOT EXISTS "{0}" (
if (rs.next()) id = rs.getLong(1); if (rs.next()) id = rs.getLong(1);
rs.close(); rs.close();
if (id != 0) return new Note(id,note.module(),note.entityId(),note.authorId(),note.text(),note.timestamp()); if (id != 0) return new Note(id,note.module(),note.entityId(),note.authorId(),note.text(),note.timestamp());
return note;
} else { } 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){ } catch (SQLException e){
throw new UmbrellaException("Failed to save note: {0}",note.text()); throw new UmbrellaException("Failed to save note: {0}",note.text());
} }