Move notes to the Postgres database
This commit is contained in:
@@ -5,7 +5,6 @@ import database.PgDatabaseConnection;
|
|||||||
import model.Note;
|
import model.Note;
|
||||||
import org.jdbi.v3.core.Handle;
|
import org.jdbi.v3.core.Handle;
|
||||||
import org.jdbi.v3.core.JdbiException;
|
import org.jdbi.v3.core.JdbiException;
|
||||||
import tools.DatabaseConnection;
|
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
@@ -18,13 +17,13 @@ public class NoteDao {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void save(Note note) {
|
public void save(Note note) {
|
||||||
try (Handle handle = DatabaseConnection.getHandle()) {
|
try (Handle handle = connection.getHandle()) {
|
||||||
handle.createUpdate("""
|
handle.createUpdate("""
|
||||||
INSERT INTO notes (`message`, `from`, `to`, `timestamp`, `fame`, `deleted`)
|
INSERT INTO note (message, sender, receiver, timestamp, fame, deleted)
|
||||||
VALUES (?, ?, ?, ?, ?, ?)""")
|
VALUES (?, ?, ?, ?, ?, ?)""")
|
||||||
.bind(0, note.message())
|
.bind(0, note.message())
|
||||||
.bind(1, note.from())
|
.bind(1, note.sender())
|
||||||
.bind(2, note.to())
|
.bind(2, note.receiver())
|
||||||
.bind(3, note.timestamp())
|
.bind(3, note.timestamp())
|
||||||
.bind(4, note.fame())
|
.bind(4, note.fame())
|
||||||
.bind(5, 0)
|
.bind(5, 0)
|
||||||
@@ -35,12 +34,12 @@ public class NoteDao {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public List<Note> findAllByTo(String to) {
|
public List<Note> findAllByTo(String to) {
|
||||||
try (Handle handle = DatabaseConnection.getHandle()) {
|
try (Handle handle = connection.getHandle()) {
|
||||||
return handle.createQuery("""
|
return handle.createQuery("""
|
||||||
SELECT *
|
SELECT *
|
||||||
FROM notes
|
FROM note
|
||||||
WHERE `deleted` = 0
|
WHERE deleted = 0
|
||||||
AND `to` = ?""")
|
AND receiver = ?""")
|
||||||
.bind(0, to)
|
.bind(0, to)
|
||||||
.mapTo(Note.class)
|
.mapTo(Note.class)
|
||||||
.list();
|
.list();
|
||||||
@@ -50,7 +49,7 @@ public class NoteDao {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public Optional<Note> delete(int id) {
|
public Optional<Note> delete(int id) {
|
||||||
try (Handle handle = DatabaseConnection.getHandle()) {
|
try (Handle handle = connection.getHandle()) {
|
||||||
Optional<Note> note = findById(handle, id);
|
Optional<Note> note = findById(handle, id);
|
||||||
if (note.isEmpty()) {
|
if (note.isEmpty()) {
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
@@ -68,9 +67,9 @@ public class NoteDao {
|
|||||||
try {
|
try {
|
||||||
note = handle.createQuery("""
|
note = handle.createQuery("""
|
||||||
SELECT *
|
SELECT *
|
||||||
FROM notes
|
FROM note
|
||||||
WHERE `deleted` = 0
|
WHERE deleted = 0
|
||||||
AND `id` = ?""")
|
AND id = ?""")
|
||||||
.bind(0, id)
|
.bind(0, id)
|
||||||
.mapTo(Note.class)
|
.mapTo(Note.class)
|
||||||
.findOne();
|
.findOne();
|
||||||
@@ -83,9 +82,9 @@ public class NoteDao {
|
|||||||
private void deleteById(Handle handle, int id) {
|
private void deleteById(Handle handle, int id) {
|
||||||
try {
|
try {
|
||||||
handle.createUpdate("""
|
handle.createUpdate("""
|
||||||
UPDATE notes
|
UPDATE note
|
||||||
SET `deleted` = 1
|
SET deleted = 1
|
||||||
WHERE `id` = ?""")
|
WHERE id = ?""")
|
||||||
.bind(0, id)
|
.bind(0, id)
|
||||||
.execute();
|
.execute();
|
||||||
} catch (JdbiException e) {
|
} catch (JdbiException e) {
|
||||||
|
|||||||
@@ -13,10 +13,10 @@ public class NoteRowMapper implements RowMapper<Note> {
|
|||||||
public Note map(ResultSet rs, StatementContext ctx) throws SQLException {
|
public Note map(ResultSet rs, StatementContext ctx) throws SQLException {
|
||||||
int id = rs.getInt("id");
|
int id = rs.getInt("id");
|
||||||
String message = rs.getString("message");
|
String message = rs.getString("message");
|
||||||
String from = rs.getString("from");
|
String sender = rs.getString("sender");
|
||||||
String to = rs.getString("to");
|
String receiver = rs.getString("receiver");
|
||||||
long timestamp = rs.getLong("timestamp");
|
long timestamp = rs.getLong("timestamp");
|
||||||
int fame = rs.getInt("fame");
|
int fame = rs.getInt("fame");
|
||||||
return new Note(id, message, from, to, timestamp, fame);
|
return new Note(id, message, sender, receiver, timestamp, fame);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,20 +2,20 @@ package model;
|
|||||||
|
|
||||||
import java.util.Objects;
|
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;
|
private static final int PLACEHOLDER_ID = -1;
|
||||||
|
|
||||||
public Note {
|
public Note {
|
||||||
Objects.requireNonNull(message);
|
Objects.requireNonNull(message);
|
||||||
Objects.requireNonNull(from);
|
Objects.requireNonNull(sender);
|
||||||
Objects.requireNonNull(to);
|
Objects.requireNonNull(receiver);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Note createNormal(String message, String from, String to, long timestamp) {
|
public static Note createNormal(String message, String sender, String receiver, long timestamp) {
|
||||||
return new Note(PLACEHOLDER_ID, message, from, to, timestamp, 0);
|
return new Note(PLACEHOLDER_ID, message, sender, receiver, timestamp, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Note createGift(String message, String from, String to, long timestamp) {
|
public static Note createGift(String message, String sender, String receiver, long timestamp) {
|
||||||
return new Note(PLACEHOLDER_ID, message, from, to, timestamp, 1);
|
return new Note(PLACEHOLDER_ID, message, sender, receiver, timestamp, 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ public final class ShowNotesPacket extends ByteBufOutPacket {
|
|||||||
|
|
||||||
private void writeNote(Note note) {
|
private void writeNote(Note note) {
|
||||||
writeInt(note.id());
|
writeInt(note.id());
|
||||||
writeString(note.from() + " "); //Stupid nexon forgot space lol
|
writeString(note.sender() + " "); //Stupid nexon forgot space lol
|
||||||
writeString(note.message());
|
writeString(note.message());
|
||||||
writeLong(getTime(note.timestamp()));
|
writeLong(getTime(note.timestamp()));
|
||||||
writeByte(note.fame());
|
writeByte(note.fame());
|
||||||
|
|||||||
11
src/main/resources/db/migration/postgresql/V0.2__note.sql
Normal file
11
src/main/resources/db/migration/postgresql/V0.2__note.sql
Normal 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};
|
||||||
@@ -46,8 +46,8 @@ class NoteServiceTest {
|
|||||||
verify(noteDao).save(noteCaptor.capture());
|
verify(noteDao).save(noteCaptor.capture());
|
||||||
var note = noteCaptor.getValue();
|
var note = noteCaptor.getValue();
|
||||||
assertEquals(message, note.message());
|
assertEquals(message, note.message());
|
||||||
assertEquals(from, note.from());
|
assertEquals(from, note.sender());
|
||||||
assertEquals(to, note.to());
|
assertEquals(to, note.receiver());
|
||||||
assertEquals(0, note.fame());
|
assertEquals(0, note.fame());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -64,8 +64,8 @@ class NoteServiceTest {
|
|||||||
verify(noteDao).save(noteCaptor.capture());
|
verify(noteDao).save(noteCaptor.capture());
|
||||||
var note = noteCaptor.getValue();
|
var note = noteCaptor.getValue();
|
||||||
assertEquals(message, note.message());
|
assertEquals(message, note.message());
|
||||||
assertEquals(from, note.from());
|
assertEquals(from, note.sender());
|
||||||
assertEquals(to, note.to());
|
assertEquals(to, note.receiver());
|
||||||
assertEquals(1, note.fame());
|
assertEquals(1, note.fame());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user