93 lines
2.0 KiB
Go

package items
import (
"dk/internal/database"
"fmt"
"zombiezen.com/go/sqlite"
)
// Builder provides a fluent interface for creating items
type Builder struct {
item *Item
}
// NewBuilder creates a new item builder
func NewBuilder() *Builder {
return &Builder{
item: &Item{},
}
}
// WithType sets the item type
func (b *Builder) WithType(itemType int) *Builder {
b.item.Type = itemType
return b
}
// WithName sets the item name
func (b *Builder) WithName(name string) *Builder {
b.item.Name = name
return b
}
// WithValue sets the item value
func (b *Builder) WithValue(value int) *Builder {
b.item.Value = value
return b
}
// WithAtt sets the item attack/defense value
func (b *Builder) WithAtt(att int) *Builder {
b.item.Att = att
return b
}
// WithSpecial sets the item special attributes
func (b *Builder) WithSpecial(special string) *Builder {
b.item.Special = special
return b
}
// Create saves the item to the database and returns it
func (b *Builder) Create() (*Item, error) {
// Use a transaction to ensure we can get the ID
var item *Item
err := database.Transaction(func(tx *database.Tx) error {
query := `INSERT INTO items (type, name, value, att, special)
VALUES (?, ?, ?, ?, ?)`
if err := tx.Exec(query, b.item.Type, b.item.Name, b.item.Value, b.item.Att, b.item.Special); err != nil {
return fmt.Errorf("failed to insert item: %w", err)
}
// Get the last inserted ID within the same transaction
var lastID int
err := tx.Query("SELECT last_insert_rowid()", func(stmt *sqlite.Stmt) error {
lastID = stmt.ColumnInt(0)
return nil
})
if err != nil {
return fmt.Errorf("failed to get last insert ID: %w", err)
}
// Create the item with the ID
item = &Item{
ID: lastID,
Type: b.item.Type,
Name: b.item.Name,
Value: b.item.Value,
Att: b.item.Att,
Special: b.item.Special,
}
return nil
})
if err != nil {
return nil, fmt.Errorf("failed to create item: %w", err)
}
return item, nil
}