feat(storage): favorites

This commit is contained in:
ineanto 2025-06-08 09:11:45 +02:00
parent 8c04009c66
commit 7cacb36fec
Signed by: ineanto
GPG key ID: E511F9CAA2F9CE84
2 changed files with 22 additions and 5 deletions

View file

@ -1,5 +1,8 @@
package xyz.ineanto.nicko.storage.mariadb; package xyz.ineanto.nicko.storage.mariadb;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import xyz.ineanto.nicko.appearance.ActionResult; import xyz.ineanto.nicko.appearance.ActionResult;
import xyz.ineanto.nicko.appearance.Appearance; import xyz.ineanto.nicko.appearance.Appearance;
import xyz.ineanto.nicko.config.Configuration; import xyz.ineanto.nicko.config.Configuration;
@ -12,6 +15,7 @@ import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.Collections; import java.util.Collections;
import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.UUID; import java.util.UUID;
import java.util.logging.Logger; import java.util.logging.Logger;
@ -19,6 +23,7 @@ import java.util.logging.Logger;
public class MariaDBStorage extends Storage { public class MariaDBStorage extends Storage {
private final Logger logger = Logger.getLogger("SQLStorage"); private final Logger logger = Logger.getLogger("SQLStorage");
private final Configuration configuration; private final Configuration configuration;
private final Gson gson = new GsonBuilder().serializeNulls().create();
private MariaDBStorageProvider provider; private MariaDBStorageProvider provider;
@ -86,15 +91,17 @@ public class MariaDBStorage extends Storage {
String skin = ""; String skin = "";
String locale = ""; String locale = "";
boolean randomSkin = false; boolean randomSkin = false;
List<Appearance> favorites = Collections.emptyList();
while (resultSet.next()) { while (resultSet.next()) {
name = resultSet.getString("name"); name = resultSet.getString("name");
skin = resultSet.getString("skin"); skin = resultSet.getString("skin");
locale = resultSet.getString("locale"); locale = resultSet.getString("locale");
randomSkin = resultSet.getBoolean("randomskin"); randomSkin = resultSet.getBoolean("randomskin");
favorites = gson.fromJson(resultSet.getString("favorites"), new TypeToken<List<Appearance>>() { }.getType());
} }
// TODO (Ineanto, 17/05/2025): Retrieve favorites // TODO (Ineanto, 17/05/2025): Retrieve favorites
final NickoProfile profile = new NickoProfile(new Appearance(name, skin), Language.fromCode(locale), randomSkin, Collections.emptyList()); final NickoProfile profile = new NickoProfile(new Appearance(name, skin), Language.fromCode(locale), randomSkin, favorites);
return Optional.of(profile); return Optional.of(profile);
} catch (SQLException e) { } catch (SQLException e) {
logger.warning("Couldn't fetch profile: " + e.getMessage()); logger.warning("Couldn't fetch profile: " + e.getMessage());
@ -120,13 +127,14 @@ public class MariaDBStorage extends Storage {
} }
private PreparedStatement getInsertStatement(Connection connection, UUID uuid, NickoProfile profile) throws SQLException { private PreparedStatement getInsertStatement(Connection connection, UUID uuid, NickoProfile profile) throws SQLException {
final String sql = "INSERT IGNORE INTO nicko.DATA (`uuid`, `name`, `skin`, `locale`, `randomskin`) VALUES (?, ?, ?, ?, ?)"; final String sql = "INSERT IGNORE INTO nicko.DATA (`uuid`, `name`, `skin`, `locale`, `randomskin`, `favorites`) VALUES (?, ?, ?, ?, ?, ?)";
final PreparedStatement statement = connection.prepareStatement(sql); final PreparedStatement statement = connection.prepareStatement(sql);
statement.setString(1, uuid.toString()); statement.setString(1, uuid.toString());
statement.setString(2, profile.getName() == null ? null : profile.getName()); statement.setString(2, profile.getName() == null ? null : profile.getName());
statement.setString(3, profile.getSkin() == null ? null : profile.getSkin()); statement.setString(3, profile.getSkin() == null ? null : profile.getSkin());
statement.setString(4, profile.getLocale().getCode()); statement.setString(4, profile.getLocale().getCode());
statement.setBoolean(5, profile.isRandomSkin()); statement.setBoolean(5, profile.isRandomSkin());
statement.setString(6, gson.toJson(profile.getFavorites(), new TypeToken<List<Appearance>>() { }.getRawType()));
return statement; return statement;
} }

View file

@ -1,5 +1,8 @@
package xyz.ineanto.nicko.storage.mysql; package xyz.ineanto.nicko.storage.mysql;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import xyz.ineanto.nicko.appearance.ActionResult; import xyz.ineanto.nicko.appearance.ActionResult;
import xyz.ineanto.nicko.appearance.Appearance; import xyz.ineanto.nicko.appearance.Appearance;
import xyz.ineanto.nicko.config.Configuration; import xyz.ineanto.nicko.config.Configuration;
@ -12,6 +15,7 @@ import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.Collections; import java.util.Collections;
import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.UUID; import java.util.UUID;
import java.util.logging.Logger; import java.util.logging.Logger;
@ -19,6 +23,7 @@ import java.util.logging.Logger;
public class MySQLStorage extends Storage { public class MySQLStorage extends Storage {
private final Logger logger = Logger.getLogger("SQLStorage"); private final Logger logger = Logger.getLogger("SQLStorage");
private final Configuration configuration; private final Configuration configuration;
private final Gson gson = new GsonBuilder().serializeNulls().create();
private MySQLStorageProvider provider; private MySQLStorageProvider provider;
@ -86,15 +91,16 @@ public class MySQLStorage extends Storage {
String skin = ""; String skin = "";
String locale = ""; String locale = "";
boolean randomSkin = false; boolean randomSkin = false;
List<Appearance> favorites = Collections.emptyList();
while (resultSet.next()) { while (resultSet.next()) {
name = resultSet.getString("name"); name = resultSet.getString("name");
skin = resultSet.getString("skin"); skin = resultSet.getString("skin");
locale = resultSet.getString("locale"); locale = resultSet.getString("locale");
randomSkin = resultSet.getBoolean("randomskin"); randomSkin = resultSet.getBoolean("randomskin");
favorites = gson.fromJson(resultSet.getString("favorites"), new TypeToken<List<Appearance>>() { }.getType());
} }
// TODO (Ineanto, 17/05/2025): Retrieve favorites final NickoProfile profile = new NickoProfile(new Appearance(name, skin), Language.fromCode(locale), randomSkin, favorites);
final NickoProfile profile = new NickoProfile(new Appearance(name, skin), Language.fromCode(locale), randomSkin, Collections.emptyList());
return Optional.of(profile); return Optional.of(profile);
} catch (SQLException e) { } catch (SQLException e) {
logger.warning("Couldn't fetch profile: " + e.getMessage()); logger.warning("Couldn't fetch profile: " + e.getMessage());
@ -120,13 +126,16 @@ public class MySQLStorage extends Storage {
} }
private PreparedStatement getInsertStatement(Connection connection, UUID uuid, NickoProfile profile) throws SQLException { private PreparedStatement getInsertStatement(Connection connection, UUID uuid, NickoProfile profile) throws SQLException {
final String sql = "INSERT IGNORE INTO nicko.DATA (`uuid`, `name`, `skin`, `locale`, `randomskin`) VALUES (?, ?, ?, ?, ?)"; final String sql = "INSERT IGNORE INTO nicko.DATA (`uuid`, `name`, `skin`, `locale`, `randomskin`, `favorites`) VALUES (?, ?, ?, ?, ?, ?)";
final PreparedStatement statement = connection.prepareStatement(sql); final PreparedStatement statement = connection.prepareStatement(sql);
statement.setString(1, uuid.toString()); statement.setString(1, uuid.toString());
statement.setString(2, profile.getName() == null ? null : profile.getName()); statement.setString(2, profile.getName() == null ? null : profile.getName());
statement.setString(3, profile.getSkin() == null ? null : profile.getSkin()); statement.setString(3, profile.getSkin() == null ? null : profile.getSkin());
statement.setString(4, profile.getLocale().getCode()); statement.setString(4, profile.getLocale().getCode());
statement.setBoolean(5, profile.isRandomSkin()); statement.setBoolean(5, profile.isRandomSkin());
// Ineanto, 08/06/2025: this will never fucking work
statement.setString(6, gson.toJson(profile.getFavorites(), new TypeToken<List<Appearance>>() { }.getRawType()));
return statement; return statement;
} }