A product project gets a level (family | product | component) and a parent (migration 0020), so a machine with its robot arm, PLC, edge server and Windows app, or a product family, can be modelled. Topics stay flat; kind is unchanged and old clients get level "product" at the top. The API checks the tree: allowed parents per level, same tenant, no cycles, at most 4 levels, children still fitting after a level change. The database keeps parents within the tenant and refuses to delete a project with children (409 has_children). The list stays flat with level and parent_id. A document's role follows the level of the project it is linked to; no document schema change. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
108 lines
3.0 KiB
Go
108 lines
3.0 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
|
|
"github.com/jackc/pgx/v5"
|
|
)
|
|
|
|
const projectSelect = `
|
|
SELECT id::text, tenant_id::text, name, kind, COALESCE(level, ''), COALESCE(parent_id::text, ''), description, created_by, created_at, updated_at
|
|
FROM projects `
|
|
|
|
func scanProject(row pgx.Row) (*Project, error) {
|
|
var p Project
|
|
if err := row.Scan(&p.ID, &p.TenantID, &p.Name, &p.Kind, &p.Level, &p.ParentID, &p.Description, &p.CreatedBy, &p.CreatedAt, &p.UpdatedAt); err != nil {
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return nil, ErrNotFound
|
|
}
|
|
return nil, err
|
|
}
|
|
return &p, nil
|
|
}
|
|
|
|
func (p *Postgres) ListProjects(ctx context.Context, tenantID string) ([]Project, error) {
|
|
rows, err := p.pool.Query(ctx, projectSelect+`WHERE tenant_id = $1::uuid ORDER BY lower(name), id`, tenantID)
|
|
if isInvalidUUID(err) {
|
|
return []Project{}, nil
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
out := []Project{}
|
|
for rows.Next() {
|
|
pr, err := scanProject(rows)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
out = append(out, *pr)
|
|
}
|
|
return out, rows.Err()
|
|
}
|
|
|
|
func (p *Postgres) GetProject(ctx context.Context, id string) (*Project, error) {
|
|
pr, err := scanProject(p.pool.QueryRow(ctx, projectSelect+`WHERE id = $1::uuid`, id))
|
|
if isInvalidUUID(err) {
|
|
return nil, ErrNotFound
|
|
}
|
|
return pr, err
|
|
}
|
|
|
|
func (p *Postgres) CreateProject(ctx context.Context, pr Project) (*Project, error) {
|
|
var id string
|
|
err := p.pool.QueryRow(ctx, `
|
|
INSERT INTO projects (tenant_id, name, kind, level, parent_id, description, created_by)
|
|
VALUES ($1::uuid, $2, $3, NULLIF($4, ''), NULLIF($5, '')::uuid, $6, $7) RETURNING id::text`,
|
|
pr.TenantID, pr.Name, pr.Kind, pr.Level, pr.ParentID, pr.Description, pr.CreatedBy).Scan(&id)
|
|
switch {
|
|
case err == nil:
|
|
return p.GetProject(ctx, id)
|
|
case isFKViolation(err), isInvalidUUID(err):
|
|
return nil, ErrNotFound
|
|
case isUniqueViolation(err):
|
|
return nil, ErrConflict
|
|
case isCheckViolation(err):
|
|
return nil, ErrInvalidInput
|
|
default:
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
func (p *Postgres) UpdateProject(ctx context.Context, pr Project) (*Project, error) {
|
|
tag, err := p.pool.Exec(ctx, `
|
|
UPDATE projects SET name = $2, kind = $3, description = $4, level = NULLIF($5, ''), parent_id = NULLIF($6, '')::uuid, updated_at = now()
|
|
WHERE id = $1::uuid`, pr.ID, pr.Name, pr.Kind, pr.Description, pr.Level, pr.ParentID)
|
|
switch {
|
|
case isInvalidUUID(err):
|
|
return nil, ErrNotFound
|
|
case isUniqueViolation(err):
|
|
return nil, ErrConflict
|
|
case isCheckViolation(err), isFKViolation(err):
|
|
return nil, ErrInvalidInput
|
|
case err != nil:
|
|
return nil, err
|
|
case tag.RowsAffected() == 0:
|
|
return nil, ErrNotFound
|
|
}
|
|
return p.GetProject(ctx, pr.ID)
|
|
}
|
|
|
|
func (p *Postgres) DeleteProject(ctx context.Context, id string) error {
|
|
tag, err := p.pool.Exec(ctx, `DELETE FROM projects WHERE id = $1::uuid`, id)
|
|
if isInvalidUUID(err) {
|
|
return ErrNotFound
|
|
}
|
|
if isFKViolation(err) {
|
|
return ErrHasChildren
|
|
}
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if tag.RowsAffected() == 0 {
|
|
return ErrNotFound
|
|
}
|
|
return nil
|
|
}
|