From 9ac33440fd4ba9fe1cacd7d890f45433ffe49ff1 Mon Sep 17 00:00:00 2001 From: William Perron Date: Wed, 31 Aug 2022 20:22:11 +0000 Subject: [PATCH] add claims detail --- store.go | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/store.go b/store.go index a75217d..885de2d 100644 --- a/store.go +++ b/store.go @@ -157,3 +157,63 @@ func (s *Store) ListClaims(ctx context.Context) ([]Claim, error) { return claims, nil } + +type ClaimDetail struct { + Claim + Provinces []string +} + +func (cd ClaimDetail) String() string { + sb := strings.Builder{} + sb.WriteString(fmt.Sprintf("%s\n", cd.Claim)) + for _, p := range cd.Provinces { + sb.WriteString(fmt.Sprintf(" - %s\n", p)) + } + return sb.String() +} + +func (s *Store) DescribeClaim(ctx context.Context, ID int) (ClaimDetail, error) { + stmt, err := s.db.PrepareContext(ctx, `SELECT id, player, claim_type, val FROM claims WHERE id = ?`) + if err != nil { + return ClaimDetail{}, fmt.Errorf("failed to get claim: %w", err) + } + + row := stmt.QueryRowContext(ctx, ID) + + c := Claim{} + var rawType string + err = row.Scan(&c.ID, &c.Player, &rawType, &c.Name) + if err != nil { + return ClaimDetail{}, fmt.Errorf("failed to scan row: %w", err) + } + cl, err := ClaimTypeFromString(rawType) + if err != nil { + return ClaimDetail{}, fmt.Errorf("unexpected error converting raw claim type: %w", err) + } + c.Type = cl + + stmt, err = s.db.PrepareContext(ctx, fmt.Sprintf(`SELECT name FROM provinces where provinces.%s = ?`, claimTypeToColumn[cl])) + if err != nil { + return ClaimDetail{}, fmt.Errorf("failed to prepare query: %w", err) + } + + rows, err := stmt.QueryContext(ctx, c.Name) + if err != nil { + return ClaimDetail{}, fmt.Errorf("failed to execute query: %w", err) + } + + provinces := make([]string, 0) + for rows.Next() { + var p string + err = rows.Scan(&p) + if err != nil { + return ClaimDetail{}, fmt.Errorf("failed to scan result set: %w", err) + } + provinces = append(provinces, p) + } + + return ClaimDetail{ + Claim: c, + Provinces: provinces, + }, nil +}