fine-tuning notes in preparation for release
Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
This commit is contained in:
@@ -2,6 +2,8 @@
|
||||
package de.srsoftware.umbrella.notes;
|
||||
|
||||
import static de.srsoftware.umbrella.core.ConnectionProvider.connect;
|
||||
import static de.srsoftware.umbrella.core.Constants.LIMIT;
|
||||
import static de.srsoftware.umbrella.core.Constants.OFFSET;
|
||||
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.*;
|
||||
@@ -74,15 +76,10 @@ public class NoteModule extends BaseHandler implements NoteService {
|
||||
var user = registry.userService().refreshSession(ex);
|
||||
if (user.isEmpty()) return unauthorized(ex);
|
||||
var module = path.pop();
|
||||
Map<Long,Note> notes = null;
|
||||
if (module == null) {
|
||||
notes = notesDb.list(user.get().id());
|
||||
} else {
|
||||
var head = path.pop();
|
||||
notes = getNotes(module, head);
|
||||
}
|
||||
var authors = notes.values().stream().map(Note::authorId).distinct().map(registry.userService()::loadUser).collect(Collectors.toMap(UmbrellaUser::id,UmbrellaUser::toMap));
|
||||
return sendContent(ex, Map.of("notes",mapValues(notes),"authors",authors));
|
||||
return switch (module){
|
||||
case null -> sendContent(ex,getUserNotes(ex,user.get()));
|
||||
default -> sendContent(ex,getEntityNotes(module,path.pop()));
|
||||
};
|
||||
} catch (NumberFormatException e){
|
||||
return sendContent(ex,HTTP_UNPROCESSABLE,"Entity id missing in path.");
|
||||
} catch (UmbrellaException e){
|
||||
@@ -90,6 +87,28 @@ public class NoteModule extends BaseHandler implements NoteService {
|
||||
}
|
||||
}
|
||||
|
||||
private boolean getUserNotes(HttpExchange ex, UmbrellaUser user) throws IOException {
|
||||
var param = queryParam(ex);
|
||||
long offset = switch (param.get(OFFSET)){
|
||||
case String s -> Long.parseLong(s);
|
||||
case Number n -> n.longValue();
|
||||
case null, default -> 0;
|
||||
};
|
||||
long limit = switch (param.get(LIMIT)){
|
||||
case String s -> Long.parseLong(s);
|
||||
case Number n -> n.longValue();
|
||||
case null, default -> 100;
|
||||
};
|
||||
|
||||
var notes = notesDb.list(user.id(),offset,limit);
|
||||
return sendContent(ex,addUsers(notes));
|
||||
}
|
||||
|
||||
private Map<String, Object> addUsers(Map<Long, Note> notes) {
|
||||
var authors = notes.values().stream().map(Note::authorId).distinct().map(registry.userService()::loadUser).collect(Collectors.toMap(UmbrellaUser::id,UmbrellaUser::toMap));
|
||||
return Map.of("notes",mapValues(notes),"authors",authors);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean doPatch(Path path, HttpExchange ex) throws IOException {
|
||||
addCors(ex);
|
||||
@@ -122,8 +141,7 @@ public class NoteModule extends BaseHandler implements NoteService {
|
||||
if (user.isEmpty()) return unauthorized(ex);
|
||||
var module = path.pop();
|
||||
if (module == null) throw unprocessable("Module missing in path.");
|
||||
var head = path.pop();
|
||||
long entityId = Long.parseLong(head);
|
||||
var entityId = path.pop();
|
||||
String text = body(ex);
|
||||
if (text.isBlank()) throw missingFieldException("Note text");
|
||||
var note = new Note(0,module,entityId,user.get().id(),text, LocalDateTime.now());
|
||||
@@ -136,8 +154,13 @@ public class NoteModule extends BaseHandler implements NoteService {
|
||||
}
|
||||
}
|
||||
|
||||
public Map<Long,Note> getNotes(String module, String entityId) throws UmbrellaException{
|
||||
return notesDb.list(module,entityId);
|
||||
public Map<String,Object> getEntityNotes(String module, String entityId) throws UmbrellaException{
|
||||
return addUsers(getNotes(module, entityId));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<Long, Note> getNotes(String module, String entityId) throws UmbrellaException {
|
||||
return notesDb.list(module, entityId);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -13,7 +13,7 @@ public interface NotesDb {
|
||||
* get all lists of a person
|
||||
* @return
|
||||
*/
|
||||
Map<Long, Note> list(long authorId);
|
||||
Map<Long, Note> list(long authorId, long offset, long limit);
|
||||
|
||||
/**
|
||||
* get the notes related to a specific entity
|
||||
|
||||
@@ -167,10 +167,11 @@ CREATE TABLE IF NOT EXISTS "{0}" (
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<Long, Note> list(long authorId) {
|
||||
public Map<Long, Note> list(long authorId, long offset, long limit) {
|
||||
try {
|
||||
var notes = new HashMap<Long, Note>();
|
||||
var rs = select(ALL).from(TABLE_NOTES).where(USER_ID,equal(authorId)).exec(db);
|
||||
var rs = select(ALL).from(TABLE_NOTES).where(USER_ID,equal(authorId))
|
||||
.sort(format("{0} DESC",ID)).skip(offset).limit(limit).exec(db);
|
||||
while (rs.next()) {
|
||||
var note = Note.of(rs);
|
||||
notes.put(note.id(),note);
|
||||
|
||||
Reference in New Issue
Block a user