diff --git a/claim_type.go b/claim_type.go new file mode 100644 index 0000000..3741ecc --- /dev/null +++ b/claim_type.go @@ -0,0 +1,52 @@ +package themis + +import "fmt" + +type ClaimType string + +func ClaimTypeFromString(s string) (ClaimType, error) { + switch s { + case CLAIM_TYPE_AREA: + return CLAIM_TYPE_AREA, nil + case CLAIM_TYPE_REGION: + return CLAIM_TYPE_REGION, nil + case CLAIM_TYPE_TRADE: + return CLAIM_TYPE_TRADE, nil + } + return "", fmt.Errorf("no claim type matching '%s'", s) +} + +func (ct ClaimType) String() string { + switch ct { + case CLAIM_TYPE_AREA: + return "Area" + case CLAIM_TYPE_REGION: + return "Region" + case CLAIM_TYPE_TRADE: + return "Trade Node" + } + return "" +} + +const ( + CLAIM_TYPE_AREA = "area" + CLAIM_TYPE_REGION = "region" + CLAIM_TYPE_TRADE = "trade" +) + +var claimTypeToColumn = map[ClaimType]string{ + CLAIM_TYPE_AREA: "area", + CLAIM_TYPE_REGION: "region", + CLAIM_TYPE_TRADE: "trade_node", +} + +type Claim struct { + ID int + Player string + Name string + Type ClaimType +} + +func (c Claim) String() string { + return fmt.Sprintf("id=%d player=%s claim_type=%s name=%s", c.ID, c.Player, c.Type, c.Name) +} diff --git a/errors.go b/errors.go new file mode 100644 index 0000000..3e906d8 --- /dev/null +++ b/errors.go @@ -0,0 +1,16 @@ +package themis + +import ( + "errors" + "fmt" +) + +var ErrNoSuchClaim = errors.New("no such claim") + +type ErrConflict struct { + Conflicts []Conflict +} + +func (ec ErrConflict) Error() string { + return fmt.Sprintf("found %d conflicting provinces", len(ec.Conflicts)) +} diff --git a/store.go b/store.go index b5c7a9c..8365540 100644 --- a/store.go +++ b/store.go @@ -4,7 +4,6 @@ import ( "context" "database/sql" _ "embed" - "errors" "fmt" "strings" @@ -14,63 +13,6 @@ import ( //go:embed migrations/init.sql var initScript string -type ClaimType string - -func ClaimTypeFromString(s string) (ClaimType, error) { - switch s { - case CLAIM_TYPE_AREA: - return CLAIM_TYPE_AREA, nil - case CLAIM_TYPE_REGION: - return CLAIM_TYPE_REGION, nil - case CLAIM_TYPE_TRADE: - return CLAIM_TYPE_TRADE, nil - } - return "", fmt.Errorf("no claim type matching '%s'", s) -} - -func (ct ClaimType) String() string { - switch ct { - case CLAIM_TYPE_AREA: - return "Area" - case CLAIM_TYPE_REGION: - return "Region" - case CLAIM_TYPE_TRADE: - return "Trade Node" - } - return "" -} - -const ( - CLAIM_TYPE_AREA = "area" - CLAIM_TYPE_REGION = "region" - CLAIM_TYPE_TRADE = "trade" -) - -var claimTypeToColumn = map[ClaimType]string{ - CLAIM_TYPE_AREA: "area", - CLAIM_TYPE_REGION: "region", - CLAIM_TYPE_TRADE: "trade_node", -} - -type Claim struct { - ID int - Player string - Name string - Type ClaimType -} - -func (c Claim) String() string { - return fmt.Sprintf("id=%d player=%s claim_type=%s name=%s", c.ID, c.Player, c.Type, c.Name) -} - -type ErrConflict struct { - Conflicts []Conflict -} - -func (ec ErrConflict) Error() string { - return fmt.Sprintf("found %d conflicting provinces", len(ec.Conflicts)) -} - type Store struct { db *sql.DB } @@ -274,8 +216,6 @@ func (s *Store) DescribeClaim(ctx context.Context, ID int) (ClaimDetail, error) }, nil } -var ErrNoSuchClaim = errors.New("no such claim") - func (s *Store) DeleteClaim(ctx context.Context, ID int, userId string) error { stmt, err := s.db.PrepareContext(ctx, "DELETE FROM claims WHERE id = ? AND userid = ?") if err != nil {