Add ChannelService to handle cc'ing

This commit is contained in:
P0nk
2023-08-04 14:53:20 +02:00
parent f6aa8ceba6
commit 05b7ec77c8
11 changed files with 162 additions and 86 deletions

View File

@@ -5,6 +5,7 @@ import database.drop.MonsterDropRowMapper;
import database.maker.MakerIngredientRowMapper;
import database.maker.MakerReagentRowMapper;
import database.maker.MakerRecipeRowMapper;
import database.monsterbook.MonsterCardRowMapper;
import database.note.NoteRowMapper;
import database.shop.ShopItemRowMapper;
import database.shop.ShopRowMapper;
@@ -32,7 +33,8 @@ public final class JdbiConfig {
new MonsterDropRowMapper(),
new GlobalMonsterDropRowMapper(),
new ShopRowMapper(),
new ShopItemRowMapper()
new ShopItemRowMapper(),
new MonsterCardRowMapper()
);
}
}

View File

@@ -0,0 +1,20 @@
package database.character;
import client.Character;
import database.monsterbook.MonsterCardDao;
public class CharacterSaver {
private final MonsterCardDao monsterCardDao;
public CharacterSaver(MonsterCardDao monsterCardDao) {
this.monsterCardDao = monsterCardDao;
}
public void save(Character chr) {
chr.saveCharToDB(false);
// Saving monster cards to both MySQL and Postgres for now
monsterCardDao.save(chr.getId(), chr.getMonsterBook().getCards());
}
}

View File

@@ -4,6 +4,7 @@ import database.DaoException;
import database.PgDatabaseConnection;
import org.jdbi.v3.core.Handle;
import org.jdbi.v3.core.JdbiException;
import org.jdbi.v3.core.statement.PreparedBatch;
import java.util.List;
@@ -24,7 +25,26 @@ public class MonsterCardDao {
.mapTo(MonsterCard.class)
.list();
} catch (JdbiException e) {
throw new DaoException("Failed to find monster cards for chr id %d".formatted(chrId), e);
throw new DaoException("Failed to find monster cards (chrId %d)".formatted(chrId), e);
}
}
public void save(int chrId, List<MonsterCard> cards) {
try (Handle handle = connection.getHandle()) {
PreparedBatch batch = handle.prepareBatch("""
INSERT INTO monster_card (chr_id, card_id, level)
VALUES (?, ?, ?)
ON CONFLICT (chr_id, card_id)
DO UPDATE SET level = excluded.level;""");
batch.bind(0, chrId);
cards.forEach(card -> {
batch.bind(1, card.cardId());
batch.bind(2, card.level());
batch.add();
});
batch.execute();
} catch (JdbiException e) {
throw new DaoException("Failed to save monster cards (chrId %d)".formatted(chrId), e);
}
}
}