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,
ENTITY_ID,entityId,
USER_ID,authorId,
TEXT,text,
RENDERED,markdown(text),
TEXT,Map.of(RENDERED,markdown(text),SOURCE,text),
TIMESTAMP,timestamp.withNano(0)
);
}

View File

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

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());
}