Files
certctl/internal/repository/postgres/team.go
T
shankar0123 21aeed4f4e legal: addlicense headers + normalize legacy variants (Phase 0 RED-4)
Phase 0 closure (Path B2, post-rewrite):

addlicense sweep — adds the canonical certctl LLC copyright + BUSL-1.1
SPDX header to every production Go file. Template:

  // Copyright 2026 certctl LLC. All rights reserved.
  // SPDX-License-Identifier: BUSL-1.1

Coverage: 338 / 338 production Go files (cmd/ + internal/, excluding
*_test.go and **/testdata/**). Pre-sweep coverage was 22 / 338 (6.5%);
post-sweep is 338 / 338 (100%).

Normalized 22 pre-existing legacy headers (`// Copyright (c) certctl`
+ `// SPDX-License-Identifier: BSL-1.1`) and 1 file using a
`Certctl Contributors` attribution. The legacy SPDX ID `BSL-1.1`
is non-standard; the official SPDX identifier for Business Source
License 1.1 is `BUSL-1.1` (capital U). All 338 files now share the
canonical form.

Generated via:
  addlicense -c "certctl LLC" -y 2026 \
    -f cowork/legal/copyright-header.tpl \
    -ignore '**/testdata/**' -ignore '**/*_test.go' \
    cmd/ internal/

Verification:
  find cmd internal -name '*.go' -not -name '*_test.go' \
    -not -path '*/testdata/*' \
    -exec grep -L '^// Copyright 2026 certctl LLC' {} \; | wc -l

  Returns: 0

gofmt clean. Header additions are comments only, no compile impact.

Closes: cowork/certctl-architecture-diligence-audit.html#fix-RED-4
2026-05-13 21:23:35 +00:00

140 lines
3.3 KiB
Go

// Copyright 2026 certctl LLC. All rights reserved.
// SPDX-License-Identifier: BUSL-1.1
package postgres
import (
"context"
"database/sql"
"fmt"
"github.com/certctl-io/certctl/internal/repository"
"github.com/certctl-io/certctl/internal/domain"
"github.com/google/uuid"
)
// TeamRepository implements repository.TeamRepository
type TeamRepository struct {
db *sql.DB
}
// NewTeamRepository creates a new TeamRepository
func NewTeamRepository(db *sql.DB) *TeamRepository {
return &TeamRepository{db: db}
}
// List returns all teams
func (r *TeamRepository) List(ctx context.Context) ([]*domain.Team, error) {
rows, err := r.db.QueryContext(ctx, `
SELECT id, name, description, created_at, updated_at
FROM teams
ORDER BY created_at DESC
`)
if err != nil {
return nil, fmt.Errorf("failed to query teams: %w", err)
}
defer rows.Close()
var teams []*domain.Team
for rows.Next() {
var team domain.Team
if err := rows.Scan(&team.ID, &team.Name, &team.Description,
&team.CreatedAt, &team.UpdatedAt); err != nil {
return nil, fmt.Errorf("failed to scan team: %w", err)
}
teams = append(teams, &team)
}
if err := rows.Err(); err != nil {
return nil, fmt.Errorf("error iterating team rows: %w", err)
}
return teams, nil
}
// Get retrieves a team by ID
func (r *TeamRepository) Get(ctx context.Context, id string) (*domain.Team, error) {
var team domain.Team
err := r.db.QueryRowContext(ctx, `
SELECT id, name, description, created_at, updated_at
FROM teams
WHERE id = $1
`, id).Scan(&team.ID, &team.Name, &team.Description,
&team.CreatedAt, &team.UpdatedAt)
if err != nil {
if err == sql.ErrNoRows {
return nil, fmt.Errorf("team not found: %w", repository.ErrNotFound)
}
return nil, fmt.Errorf("failed to query team: %w", err)
}
return &team, nil
}
// Create stores a new team
func (r *TeamRepository) Create(ctx context.Context, team *domain.Team) error {
if team.ID == "" {
team.ID = uuid.New().String()
}
err := r.db.QueryRowContext(ctx, `
INSERT INTO teams (id, name, description, created_at, updated_at)
VALUES ($1, $2, $3, $4, $5)
RETURNING id
`, team.ID, team.Name, team.Description, team.CreatedAt, team.UpdatedAt).Scan(&team.ID)
if err != nil {
return fmt.Errorf("failed to create team: %w", err)
}
return nil
}
// Update modifies an existing team
func (r *TeamRepository) Update(ctx context.Context, team *domain.Team) error {
result, err := r.db.ExecContext(ctx, `
UPDATE teams SET
name = $1,
description = $2,
updated_at = $3
WHERE id = $4
`, team.Name, team.Description, team.UpdatedAt, team.ID)
if err != nil {
return fmt.Errorf("failed to update team: %w", err)
}
rows, err := result.RowsAffected()
if err != nil {
return fmt.Errorf("failed to get rows affected: %w", err)
}
if rows == 0 {
return fmt.Errorf("team not found: %w", repository.ErrNotFound)
}
return nil
}
// Delete removes a team
func (r *TeamRepository) Delete(ctx context.Context, id string) error {
result, err := r.db.ExecContext(ctx, "DELETE FROM teams WHERE id = $1", id)
if err != nil {
return fmt.Errorf("failed to delete team: %w", err)
}
rows, err := result.RowsAffected()
if err != nil {
return fmt.Errorf("failed to get rows affected: %w", err)
}
if rows == 0 {
return fmt.Errorf("team not found: %w", repository.ErrNotFound)
}
return nil
}