Move notes to the Postgres database

This commit is contained in:
P0nk
2023-02-25 00:40:36 +01:00
parent f6f3c9c3e3
commit 12745c207d
6 changed files with 42 additions and 32 deletions

View File

@@ -5,7 +5,6 @@ import database.PgDatabaseConnection;
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;
@@ -18,13 +17,13 @@ public class NoteDao {
}
public void save(Note note) {
try (Handle handle = DatabaseConnection.getHandle()) {
try (Handle handle = connection.getHandle()) {
handle.createUpdate("""
INSERT INTO notes (`message`, `from`, `to`, `timestamp`, `fame`, `deleted`)
INSERT INTO note (message, sender, receiver, timestamp, fame, deleted)
VALUES (?, ?, ?, ?, ?, ?)""")
.bind(0, note.message())
.bind(1, note.from())
.bind(2, note.to())
.bind(1, note.sender())
.bind(2, note.receiver())
.bind(3, note.timestamp())
.bind(4, note.fame())
.bind(5, 0)
@@ -35,12 +34,12 @@ public class NoteDao {
}
public List<Note> findAllByTo(String to) {
try (Handle handle = DatabaseConnection.getHandle()) {
try (Handle handle = connection.getHandle()) {
return handle.createQuery("""
SELECT *
FROM notes
WHERE `deleted` = 0
AND `to` = ?""")
SELECT *
FROM note
WHERE deleted = 0
AND receiver = ?""")
.bind(0, to)
.mapTo(Note.class)
.list();
@@ -50,7 +49,7 @@ public class NoteDao {
}
public Optional<Note> delete(int id) {
try (Handle handle = DatabaseConnection.getHandle()) {
try (Handle handle = connection.getHandle()) {
Optional<Note> note = findById(handle, id);
if (note.isEmpty()) {
return Optional.empty();
@@ -68,9 +67,9 @@ public class NoteDao {
try {
note = handle.createQuery("""
SELECT *
FROM notes
WHERE `deleted` = 0
AND `id` = ?""")
FROM note
WHERE deleted = 0
AND id = ?""")
.bind(0, id)
.mapTo(Note.class)
.findOne();
@@ -83,9 +82,9 @@ public class NoteDao {
private void deleteById(Handle handle, int id) {
try {
handle.createUpdate("""
UPDATE notes
SET `deleted` = 1
WHERE `id` = ?""")
UPDATE note
SET deleted = 1
WHERE id = ?""")
.bind(0, id)
.execute();
} catch (JdbiException e) {

View File

@@ -13,10 +13,10 @@ public class NoteRowMapper implements RowMapper<Note> {
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");
String sender = rs.getString("sender");
String receiver = rs.getString("receiver");
long timestamp = rs.getLong("timestamp");
int fame = rs.getInt("fame");
return new Note(id, message, from, to, timestamp, fame);
return new Note(id, message, sender, receiver, timestamp, fame);
}
}