Add ShopDao
This commit is contained in:
@@ -6,6 +6,7 @@ import database.maker.MakerIngredientRowMapper;
|
|||||||
import database.maker.MakerReagentRowMapper;
|
import database.maker.MakerReagentRowMapper;
|
||||||
import database.maker.MakerRecipeRowMapper;
|
import database.maker.MakerRecipeRowMapper;
|
||||||
import database.note.NoteRowMapper;
|
import database.note.NoteRowMapper;
|
||||||
|
import database.shop.ShopItemRowMapper;
|
||||||
import org.jdbi.v3.core.Jdbi;
|
import org.jdbi.v3.core.Jdbi;
|
||||||
|
|
||||||
import javax.sql.DataSource;
|
import javax.sql.DataSource;
|
||||||
@@ -20,6 +21,7 @@ public final class JdbiConfig {
|
|||||||
.registerRowMapper(new MakerRecipeRowMapper())
|
.registerRowMapper(new MakerRecipeRowMapper())
|
||||||
.registerRowMapper(new MakerIngredientRowMapper())
|
.registerRowMapper(new MakerIngredientRowMapper())
|
||||||
.registerRowMapper(new MonsterDropRowMapper())
|
.registerRowMapper(new MonsterDropRowMapper())
|
||||||
.registerRowMapper(new GlobalMonsterDropRowMapper());
|
.registerRowMapper(new GlobalMonsterDropRowMapper())
|
||||||
|
.registerRowMapper(new ShopItemRowMapper());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
31
src/main/java/database/shop/ShopDao.java
Normal file
31
src/main/java/database/shop/ShopDao.java
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
4
src/main/java/database/shop/ShopItem.java
Normal file
4
src/main/java/database/shop/ShopItem.java
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
package database.shop;
|
||||||
|
|
||||||
|
public record ShopItem(int itemId, int price, Integer pitch, int position) {
|
||||||
|
}
|
||||||
19
src/main/java/database/shop/ShopItemRowMapper.java
Normal file
19
src/main/java/database/shop/ShopItemRowMapper.java
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user