155 lines
3.4 KiB
Go

package drops
import (
"fmt"
"dk/internal/database"
"zombiezen.com/go/sqlite"
)
// Drop represents a drop item in the database
type Drop struct {
ID int `json:"id"`
Name string `json:"name"`
Level int `json:"level"`
Type int `json:"type"`
Att string `json:"att"`
}
// DropType constants for drop types
const (
TypeConsumable = 1
)
// Find retrieves a drop by ID
func Find(id int) (*Drop, error) {
drop := &Drop{}
query := "SELECT id, name, level, type, att FROM drops WHERE id = ?"
err := database.Query(query, func(stmt *sqlite.Stmt) error {
drop.ID = stmt.ColumnInt(0)
drop.Name = stmt.ColumnText(1)
drop.Level = stmt.ColumnInt(2)
drop.Type = stmt.ColumnInt(3)
drop.Att = stmt.ColumnText(4)
return nil
}, id)
if err != nil {
return nil, fmt.Errorf("failed to find drop: %w", err)
}
if drop.ID == 0 {
return nil, fmt.Errorf("drop with ID %d not found", id)
}
return drop, nil
}
// All retrieves all drops
func All() ([]*Drop, error) {
var drops []*Drop
query := "SELECT id, name, level, type, att FROM drops ORDER BY id"
err := database.Query(query, func(stmt *sqlite.Stmt) error {
drop := &Drop{
ID: stmt.ColumnInt(0),
Name: stmt.ColumnText(1),
Level: stmt.ColumnInt(2),
Type: stmt.ColumnInt(3),
Att: stmt.ColumnText(4),
}
drops = append(drops, drop)
return nil
})
if err != nil {
return nil, fmt.Errorf("failed to retrieve all drops: %w", err)
}
return drops, nil
}
// ByLevel retrieves drops by minimum level requirement
func ByLevel(minLevel int) ([]*Drop, error) {
var drops []*Drop
query := "SELECT id, name, level, type, att FROM drops WHERE level <= ? ORDER BY level, id"
err := database.Query(query, func(stmt *sqlite.Stmt) error {
drop := &Drop{
ID: stmt.ColumnInt(0),
Name: stmt.ColumnText(1),
Level: stmt.ColumnInt(2),
Type: stmt.ColumnInt(3),
Att: stmt.ColumnText(4),
}
drops = append(drops, drop)
return nil
}, minLevel)
if err != nil {
return nil, fmt.Errorf("failed to retrieve drops by level: %w", err)
}
return drops, nil
}
// ByType retrieves drops by type
func ByType(dropType int) ([]*Drop, error) {
var drops []*Drop
query := "SELECT id, name, level, type, att FROM drops WHERE type = ? ORDER BY level, id"
err := database.Query(query, func(stmt *sqlite.Stmt) error {
drop := &Drop{
ID: stmt.ColumnInt(0),
Name: stmt.ColumnText(1),
Level: stmt.ColumnInt(2),
Type: stmt.ColumnInt(3),
Att: stmt.ColumnText(4),
}
drops = append(drops, drop)
return nil
}, dropType)
if err != nil {
return nil, fmt.Errorf("failed to retrieve drops by type: %w", err)
}
return drops, nil
}
// Save updates an existing drop in the database
func (d *Drop) Save() error {
if d.ID == 0 {
return fmt.Errorf("cannot save drop without ID")
}
query := `UPDATE drops SET name = ?, level = ?, type = ?, att = ? WHERE id = ?`
return database.Exec(query, d.Name, d.Level, d.Type, d.Att, d.ID)
}
// Delete removes the drop from the database
func (d *Drop) Delete() error {
if d.ID == 0 {
return fmt.Errorf("cannot delete drop without ID")
}
return database.Exec("DELETE FROM drops WHERE id = ?", d.ID)
}
// IsConsumable returns true if the drop is a consumable item
func (d *Drop) IsConsumable() bool {
return d.Type == TypeConsumable
}
// TypeName returns the string representation of the drop type
func (d *Drop) TypeName() string {
switch d.Type {
case TypeConsumable:
return "Consumable"
default:
return "Unknown"
}
}