Add NoteService to handle note operations

NoteService should be the only class with access to NoteDao;
nowhere else should NoteDao be accessed directly.

Channel dependencies are static in PacketProcessor, for now.
Ideally they would be injected in the constructor,
but since the constructor is private and I don't want to open
up that can of worms, I'll leave it like this.
At the very least, now we have a way of injecting services into
the handlers. This will make further restructuring way easier.
This commit is contained in:
P0nk
2022-12-27 10:34:55 +01:00
parent 5f1f5b7dcd
commit 389b3ad2a4
15 changed files with 199 additions and 110 deletions

View File

@@ -1,5 +1,6 @@
package database;
package database.note;
import database.DaoException;
import model.Note;
import org.jdbi.v3.core.Handle;
import org.jdbi.v3.core.JdbiException;
@@ -27,7 +28,7 @@ public class NoteDao {
}
}
public static List<Note> findAllByTo(String to) {
public List<Note> findAllByTo(String to) {
try (Handle handle = DatabaseConnection.getHandle()) {
return handle.createQuery("""
SELECT *
@@ -38,11 +39,11 @@ public class NoteDao {
.mapTo(Note.class)
.list();
} catch (JdbiException e) {
throw new DaoException("Failed to find notes with \"to\": %s".formatted(to), e);
throw new DaoException("Failed to find notes sent to: %s".formatted(to), e);
}
}
public static Optional<Note> delete(int id) {
public Optional<Note> delete(int id) {
try (Handle handle = DatabaseConnection.getHandle()) {
Optional<Note> note = findById(handle, id);
if (note.isEmpty()) {