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

@@ -73,6 +73,7 @@ public class Constants {
public static final String NEW_MEMBER = "new_member"; public static final String NEW_MEMBER = "new_member";
public static final String MIME = "mime"; public static final String MIME = "mime";
public static final String NO_INDEX = "no_index"; public static final String NO_INDEX = "no_index";
public static final String NOTE = "note";
public static final String NUMBER = "number"; public static final String NUMBER = "number";
public static final String OPTIONAL = "optional"; public static final String OPTIONAL = "optional";

View File

@@ -6,11 +6,12 @@ import de.srsoftware.umbrella.core.model.Note;
import de.srsoftware.umbrella.core.model.UmbrellaUser; import de.srsoftware.umbrella.core.model.UmbrellaUser;
import java.util.Collection; import java.util.Collection;
import java.util.Map;
public interface NoteService { public interface NoteService {
void deleteEntity(String task, long taskId); void deleteEntity(String task, long taskId);
Collection<Note> getNotes(String module, long entityId) throws UmbrellaException; Map<Long,Note> getNotes(String module, long entityId) throws UmbrellaException;
Note save(Note note); Note save(Note note);
} }

View File

@@ -6,10 +6,12 @@ import de.srsoftware.tools.Mappable;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Map; import java.util.Map;
import static de.srsoftware.umbrella.core.Constants.*; import static de.srsoftware.umbrella.core.Constants.*;
import static de.srsoftware.umbrella.core.Util.markdown; import static de.srsoftware.umbrella.core.Util.markdown;
import static java.time.ZoneOffset.UTC;
public record Note(long id, String module, long entityId, long authorId, String text, LocalDateTime timestamp) implements Mappable { public record Note(long id, String module, long entityId, long authorId, String text, LocalDateTime timestamp) implements Mappable {
public static Note of(ResultSet rs) throws SQLException { public static Note of(ResultSet rs) throws SQLException {
@@ -18,8 +20,8 @@ public record Note(long id, String module, long entityId, long authorId, String
rs.getString(MODULE), rs.getString(MODULE),
rs.getLong(ENTITY_ID), rs.getLong(ENTITY_ID),
rs.getLong(USER_ID), rs.getLong(USER_ID),
rs.getString(TEXT), rs.getString(NOTE),
LocalDateTime.parse(rs.getString(TIMESTAMP)) LocalDateTime.ofEpochSecond(rs.getLong(TIMESTAMP),0, UTC)
); );
} }

View File

@@ -0,0 +1,55 @@
<script>
import { onMount } from 'svelte';
import { api } from '../../urls.svelte.js';
import { t } from '../../translations.svelte.js';
import Editor from '../../Components/MarkdownEditor.svelte';
let error = $state(null);
let { module = null, entity_id = null } = $props();
let note = $state({source:null,rendered:null});
let notes = $state(null);
async function onclick(){
alert(note.source);
const url = api(`notes/${module}/${entity_id}`);
const resp = await fetch(url,{
credentials:'include',
method:'POST',
body:note.source
});
if (resp.ok){
} else {
error = await resp.text();
}
}
async function load(){
const url = api(`notes/${module}/${entity_id}`);
const resp = await fetch(url,{credentials:'include'});
if (resp.ok){
notes = await resp.json();
} else {
error = await resp.text();
}
}
onMount(load)
</script>
<h1>{t('notes')}</h1>
{#if error}
<span class="error">{error}</span>
{/if}
{#if notes}
{#each Object.entries(notes) as [a,b]}
<fieldset>
<legend>User {b.user_id} {b.timestamp.replace('T',' ')}</legend>
{@html b.rendered}
</fieldset>
{/each}
{/if}
<Editor simple={true} bind:value={note} />
<button {onclick}>{t('save_note')}</button>

View File

@@ -8,6 +8,7 @@
import LineEditor from '../../Components/LineEditor.svelte'; import LineEditor from '../../Components/LineEditor.svelte';
import MarkdownEditor from '../../Components/MarkdownEditor.svelte'; import MarkdownEditor from '../../Components/MarkdownEditor.svelte';
import MemberEditor from '../../Components/MemberEditor.svelte'; import MemberEditor from '../../Components/MemberEditor.svelte';
import Notes from '../notes/List.svelte';
import StateSelector from '../../Components/StateSelector.svelte'; import StateSelector from '../../Components/StateSelector.svelte';
import TagList from '../tags/TagList.svelte'; import TagList from '../tags/TagList.svelte';
import TaskList from './TaskList.svelte'; import TaskList from './TaskList.svelte';
@@ -293,6 +294,12 @@
<TagList module="task" {id} user_list={Object.keys(task.members).map(id => +id)} /> <TagList module="task" {id} user_list={Object.keys(task.members).map(id => +id)} />
</td> </td>
</tr> </tr>
<tr>
<th>{t('notes')}</th>
<td>
<Notes module="task" entity_id={id} />
</td>
</tr>
</tbody> </tbody>
</table> </table>
{/if} {/if}

View File

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

View File

@@ -2,12 +2,11 @@
package de.srsoftware.umbrella.notes; package de.srsoftware.umbrella.notes;
import static de.srsoftware.umbrella.core.ConnectionProvider.connect; 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.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.missingFieldException;
import static de.srsoftware.umbrella.core.exceptions.UmbrellaException.unprocessable; 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.CONFIG_DATABASE;
import static de.srsoftware.umbrella.notes.Constants.NOTE;
import static java.net.HttpURLConnection.HTTP_OK; import static java.net.HttpURLConnection.HTTP_OK;
import com.sun.net.httpserver.HttpExchange; import com.sun.net.httpserver.HttpExchange;
@@ -16,18 +15,16 @@ import de.srsoftware.tools.Path;
import de.srsoftware.tools.SessionToken; import de.srsoftware.tools.SessionToken;
import de.srsoftware.umbrella.core.BaseHandler; import de.srsoftware.umbrella.core.BaseHandler;
import de.srsoftware.umbrella.core.api.NoteService; 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.api.UserService;
import de.srsoftware.umbrella.core.exceptions.UmbrellaException; import de.srsoftware.umbrella.core.exceptions.UmbrellaException;
import de.srsoftware.umbrella.core.model.Note; import de.srsoftware.umbrella.core.model.Note;
import de.srsoftware.umbrella.core.model.Token; import de.srsoftware.umbrella.core.model.Token;
import de.srsoftware.umbrella.core.model.UmbrellaUser;
import java.io.IOException; import java.io.IOException;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.Map;
import java.util.Optional; import java.util.Optional;
import org.json.JSONArray;
public class NoteModule extends BaseHandler implements NoteService { public class NoteModule extends BaseHandler implements NoteService {
private final NotesDb notesDb; private final NotesDb notesDb;
@@ -74,7 +71,7 @@ public class NoteModule extends BaseHandler implements NoteService {
if (module == null) throw unprocessable("Module missing in path."); if (module == null) throw unprocessable("Module missing in path.");
var head = path.pop(); var head = path.pop();
long entityId = Long.parseLong(head); long entityId = Long.parseLong(head);
return sendContent(ex, getNotes(module,entityId)); return sendContent(ex, mapValues(getNotes(module,entityId)));
} catch (NumberFormatException e){ } catch (NumberFormatException e){
return sendContent(ex,HTTP_UNPROCESSABLE,"Entity id missing in path."); return sendContent(ex,HTTP_UNPROCESSABLE,"Entity id missing in path.");
} catch (UmbrellaException e){ } catch (UmbrellaException e){
@@ -99,20 +96,8 @@ public class NoteModule extends BaseHandler implements NoteService {
if (module == null) throw unprocessable("Module missing in path."); if (module == null) throw unprocessable("Module missing in path.");
var head = path.pop(); var head = path.pop();
long entityId = Long.parseLong(head); long entityId = Long.parseLong(head);
var json = json(ex); String text = body(ex);
if (!(json.has(NOTE) && json.get(NOTE) instanceof String text)) throw missingFieldException(NOTE); if (text.isBlank()) throw missingFieldException("Note text");
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!");
var note = new Note(0,module,entityId,user.get().id(),text, LocalDateTime.now()); var note = new Note(0,module,entityId,user.get().id(),text, LocalDateTime.now());
note = save(note); note = save(note);
return sendContent(ex, 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); return notesDb.list(module,entityId);
} }

View File

@@ -3,15 +3,14 @@ package de.srsoftware.umbrella.notes;
import de.srsoftware.umbrella.core.model.Note; import de.srsoftware.umbrella.core.model.Note;
import java.util.Collection; import java.util.Map;
import java.util.Set;
public interface NotesDb { public interface NotesDb {
long delete(long noteId, long userId); long delete(long noteId, long userId);
void deleteEntity(String module, long entityId); 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); 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 de.srsoftware.umbrella.notes.Constants.*;
import static java.lang.System.Logger.Level.*; import static java.lang.System.Logger.Level.*;
import static java.text.MessageFormat.format; import static java.text.MessageFormat.format;
import static java.time.ZoneOffset.UTC;
import de.srsoftware.tools.jdbc.Query; import de.srsoftware.tools.jdbc.Query;
import de.srsoftware.umbrella.core.exceptions.UmbrellaException; import de.srsoftware.umbrella.core.exceptions.UmbrellaException;
import de.srsoftware.umbrella.core.model.Note; import de.srsoftware.umbrella.core.model.Note;
import java.sql.Connection; import java.sql.Connection;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.Collection; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Map;
public class SqliteDb implements NotesDb { public class SqliteDb implements NotesDb {
private static final int INITIAL_DB_VERSION = 1; private static final int INITIAL_DB_VERSION = 1;
@@ -113,25 +114,37 @@ CREATE TABLE IF NOT EXISTS "{0}" (
} }
@Override @Override
public Set<Note> list(String module, long entityId) { public Map<Long, Note> list(String module, long entityId) {
try { 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); 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(); rs.close();
return tags; return notes;
} catch (SQLException e) { } catch (SQLException e) {
throw new UmbrellaException("Failed to load tags"); throw new UmbrellaException("Failed to load notes");
} }
} }
@Override @Override
public Note save(Note note) { public Note save(Note note) {
try { try {
replaceInto(TABLE_NOTES,USER_ID,MODULE,ENTITY_ID,NOTE,TIMESTAMP) if (note.id() == 0) {
.values(note.authorId(),note.module(),note.entityId(),note.text(),note.timestamp()) var rs = replaceInto(TABLE_NOTES, USER_ID, MODULE, ENTITY_ID, NOTE, TIMESTAMP)
.execute(db).close(); .values(note.authorId(), note.module(), note.entityId(), note.text(), note.timestamp().toEpochSecond(UTC))
return note; .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){ } catch (SQLException e){
throw new UmbrellaException("Failed to save note: {0}",note.text()); throw new UmbrellaException("Failed to save note: {0}",note.text());
} }