Add ShopDao

This commit is contained in:
P0nk
2023-03-17 21:28:23 +01:00
parent ab0239ba84
commit a496c0f2b4
4 changed files with 57 additions and 1 deletions

View File

@@ -6,6 +6,7 @@ import database.maker.MakerIngredientRowMapper;
import database.maker.MakerReagentRowMapper;
import database.maker.MakerRecipeRowMapper;
import database.note.NoteRowMapper;
import database.shop.ShopItemRowMapper;
import org.jdbi.v3.core.Jdbi;
import javax.sql.DataSource;
@@ -20,6 +21,7 @@ public final class JdbiConfig {
.registerRowMapper(new MakerRecipeRowMapper())
.registerRowMapper(new MakerIngredientRowMapper())
.registerRowMapper(new MonsterDropRowMapper())
.registerRowMapper(new GlobalMonsterDropRowMapper());
.registerRowMapper(new GlobalMonsterDropRowMapper())
.registerRowMapper(new ShopItemRowMapper());
}
}

View File

@@ -0,0 +1,31 @@
package database.shop;
import database.DaoException;
import database.PgDatabaseConnection;
import org.jdbi.v3.core.Handle;
import org.jdbi.v3.core.JdbiException;
import java.util.List;
public class ShopDao {
private final PgDatabaseConnection connection;
public ShopDao(PgDatabaseConnection connection) {
this.connection = connection;
}
public List<ShopItem> getShopItems(int shopId) {
try (Handle handle = connection.getHandle()) {
return handle.createQuery("""
SELECT *
FROM shop_item
WHERE shop = ?
ORDER BY position DESC;""")
.bind(0, shopId)
.mapTo(ShopItem.class)
.list();
} catch (JdbiException e) {
throw new DaoException("Failed to get shop items for shop %d".formatted(shopId), e);
}
}
}

View File

@@ -0,0 +1,4 @@
package database.shop;
public record ShopItem(int itemId, int price, Integer pitch, int position) {
}

View File

@@ -0,0 +1,19 @@
package database.shop;
import org.jdbi.v3.core.mapper.RowMapper;
import org.jdbi.v3.core.statement.StatementContext;
import java.sql.ResultSet;
import java.sql.SQLException;
public class ShopItemRowMapper implements RowMapper<ShopItem> {
@Override
public ShopItem map(ResultSet rs, StatementContext ctx) throws SQLException {
final int itemId = rs.getInt("item_id");
final int price = rs.getInt("price");
final Integer pitch = rs.getObject("pitch", Integer.class);
final int position = rs.getInt("position");
return new ShopItem(itemId, price, pitch, position);
}
}