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);
}
}

View File

@@ -2,20 +2,20 @@ package model;
import java.util.Objects;
public record Note(int id, String message, String from, String to, long timestamp, int fame) {
public record Note(int id, String message, String sender, String receiver, long timestamp, int fame) {
private static final int PLACEHOLDER_ID = -1;
public Note {
Objects.requireNonNull(message);
Objects.requireNonNull(from);
Objects.requireNonNull(to);
Objects.requireNonNull(sender);
Objects.requireNonNull(receiver);
}
public static Note createNormal(String message, String from, String to, long timestamp) {
return new Note(PLACEHOLDER_ID, message, from, to, timestamp, 0);
public static Note createNormal(String message, String sender, String receiver, long timestamp) {
return new Note(PLACEHOLDER_ID, message, sender, receiver, timestamp, 0);
}
public static Note createGift(String message, String from, String to, long timestamp) {
return new Note(PLACEHOLDER_ID, message, from, to, timestamp, 1);
public static Note createGift(String message, String sender, String receiver, long timestamp) {
return new Note(PLACEHOLDER_ID, message, sender, receiver, timestamp, 1);
}
}

View File

@@ -22,7 +22,7 @@ public final class ShowNotesPacket extends ByteBufOutPacket {
private void writeNote(Note note) {
writeInt(note.id());
writeString(note.from() + " "); //Stupid nexon forgot space lol
writeString(note.sender() + " "); //Stupid nexon forgot space lol
writeString(note.message());
writeLong(getTime(note.timestamp()));
writeByte(note.fame());

View File

@@ -0,0 +1,11 @@
CREATE TABLE note(
id serial NOT NULL,
receiver text NOT NULL,
sender text NOT NULL,
message text NOT NULL,
timestamp bigint NOT NULL,
fame smallint NOT NULL,
deleted smallint NOT NULL,
PRIMARY KEY (id)
);
GRANT SELECT, INSERT, UPDATE ON note TO ${server-username};

View File

@@ -46,8 +46,8 @@ class NoteServiceTest {
verify(noteDao).save(noteCaptor.capture());
var note = noteCaptor.getValue();
assertEquals(message, note.message());
assertEquals(from, note.from());
assertEquals(to, note.to());
assertEquals(from, note.sender());
assertEquals(to, note.receiver());
assertEquals(0, note.fame());
}
@@ -64,8 +64,8 @@ class NoteServiceTest {
verify(noteDao).save(noteCaptor.capture());
var note = noteCaptor.getValue();
assertEquals(message, note.message());
assertEquals(from, note.from());
assertEquals(to, note.to());
assertEquals(from, note.sender());
assertEquals(to, note.receiver());
assertEquals(1, note.fame());
}