Browse Source

working on notes

module/notes
Stephan Richter 3 months ago
parent
commit
325dffa3fe
  1. 1
      core/src/main/java/de/srsoftware/umbrella/core/Constants.java
  2. 3
      core/src/main/java/de/srsoftware/umbrella/core/api/NoteService.java
  3. 6
      core/src/main/java/de/srsoftware/umbrella/core/model/Note.java
  4. 55
      frontend/src/routes/notes/List.svelte
  5. 7
      frontend/src/routes/task/View.svelte
  6. 1
      notes/src/main/java/de/srsoftware/umbrella/notes/Constants.java
  7. 29
      notes/src/main/java/de/srsoftware/umbrella/notes/NoteModule.java
  8. 5
      notes/src/main/java/de/srsoftware/umbrella/notes/NotesDb.java
  9. 35
      notes/src/main/java/de/srsoftware/umbrella/notes/SqliteDb.java

1
core/src/main/java/de/srsoftware/umbrella/core/Constants.java

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

3
core/src/main/java/de/srsoftware/umbrella/core/api/NoteService.java

@ -6,11 +6,12 @@ import de.srsoftware.umbrella.core.model.Note; @@ -6,11 +6,12 @@ import de.srsoftware.umbrella.core.model.Note;
import de.srsoftware.umbrella.core.model.UmbrellaUser;
import java.util.Collection;
import java.util.Map;
public interface NoteService {
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);
}

6
core/src/main/java/de/srsoftware/umbrella/core/model/Note.java

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

55
frontend/src/routes/notes/List.svelte

@ -0,0 +1,55 @@ @@ -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>

7
frontend/src/routes/task/View.svelte

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

1
notes/src/main/java/de/srsoftware/umbrella/notes/Constants.java

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

29
notes/src/main/java/de/srsoftware/umbrella/notes/NoteModule.java

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

5
notes/src/main/java/de/srsoftware/umbrella/notes/NotesDb.java

@ -3,15 +3,14 @@ package de.srsoftware.umbrella.notes; @@ -3,15 +3,14 @@ package de.srsoftware.umbrella.notes;
import de.srsoftware.umbrella.core.model.Note;
import java.util.Collection;
import java.util.Set;
import java.util.Map;
public interface NotesDb {
long delete(long noteId, long userId);
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);
}

35
notes/src/main/java/de/srsoftware/umbrella/notes/SqliteDb.java

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

Loading…
Cancel
Save