Add NoteDao, used by NoteActionHandler to delete note when read

This commit is contained in:
P0nk
2022-12-26 15:55:28 +01:00
parent 188eb74a70
commit 605f2e212e
6 changed files with 147 additions and 26 deletions

View File

@@ -0,0 +1,10 @@
package database;
import org.jdbi.v3.core.JdbiException;
public class DaoException extends JdbiException {
public DaoException(String message, Throwable cause) {
super(message, cause);
}
}

View File

@@ -0,0 +1,88 @@
package database;
import model.Note;
import org.jdbi.v3.core.Handle;
import org.jdbi.v3.core.JdbiException;
import tools.DatabaseConnection;
import java.util.List;
import java.util.Optional;
public class NoteDao {
public static void save(Note note) {
try (Handle handle = DatabaseConnection.getHandle()) {
handle.createScript("""
INSERT INTO notes (`message`, `from`, `to`, `timestamp`, `fame`, `deleted`)
VALUES (?, ?, ?, ?, ?, ?)""")
.bind(0, note.message())
.bind(1, note.from())
.bind(2, note.to())
.bind(3, note.timestamp())
.bind(4, note.fame())
.bind(5, 0)
.execute();
} catch (JdbiException e) {
throw new DaoException("Failed to save note: %s".formatted(note.toString()), e);
}
}
public static List<Note> findAllByTo(String to) {
try (Handle handle = DatabaseConnection.getHandle()) {
return handle.createQuery("""
SELECT *
FROM notes
WHERE `deleted` = 0
AND `to` = ?""")
.bind(0, to)
.mapTo(Note.class)
.list();
} catch (JdbiException e) {
throw new DaoException("Failed to find notes with \"to\": %s".formatted(to), e);
}
}
public static Optional<Note> delete(int id) {
try (Handle handle = DatabaseConnection.getHandle()) {
Optional<Note> note = findById(handle, id);
if (note.isEmpty()) {
return Optional.empty();
}
deleteById(handle, id);
return note;
} catch (JdbiException e) {
throw new DaoException("Failed to delete note with id: %d".formatted(id), e);
}
}
private static Optional<Note> findById(Handle handle, int id) {
final Optional<Note> note;
try {
note = handle.createQuery("""
SELECT *
FROM notes
WHERE `deleted` = 0
AND `id` = ?""")
.bind(0, id)
.mapTo(Note.class)
.findOne();
} catch (JdbiException e) {
throw new DaoException("Failed find note with id %s".formatted(id), e);
}
return note;
}
private static void deleteById(Handle handle, int id) {
try {
handle.createUpdate("""
UPDATE notes
SET `deleted` = 1
WHERE `id` = ?""")
.bind(0, id)
.execute();
} catch (JdbiException e) {
throw new DaoException("Failed to delete note with id %d".formatted(id), e);
}
}
}

View File

@@ -0,0 +1,22 @@
package database;
import model.Note;
import org.jdbi.v3.core.mapper.RowMapper;
import org.jdbi.v3.core.statement.StatementContext;
import java.sql.ResultSet;
import java.sql.SQLException;
public class NoteRowMapper implements RowMapper<Note> {
@Override
public Note map(ResultSet rs, StatementContext ctx) throws SQLException {
int id = rs.getInt("id");
String message = rs.getString("message");
String from = rs.getString("from");
String to = rs.getString("to");
long timestamp = rs.getLong("timestamp");
int fame = rs.getInt("fame");
return new Note(id, message, from, to, timestamp, fame);
}
}