package actions import ( "context" "database/sql" "fmt" "strconv" "git.tjp.lol/punchcard/internal/queries" ) // CreateProject creates a new project for the specified client func (a *actions) CreateProject(ctx context.Context, name, client string, billableRate *float64) (*queries.Project, error) { // Find the client first clientRecord, err := a.FindClient(ctx, client) if err != nil { return nil, fmt.Errorf("invalid client: %w", err) } var billableRateParam sql.NullInt64 if billableRate != nil { billableRateParam = sql.NullInt64{Int64: int64(*billableRate * 100), Valid: true} } project, err := a.queries.CreateProject(ctx, queries.CreateProjectParams{ Name: name, ClientID: clientRecord.ID, BillableRate: billableRateParam, }) if err != nil { return nil, fmt.Errorf("failed to create project: %w", err) } return &project, nil } func (a *actions) EditProject(ctx context.Context, id int64, name string, billableRate *float64) (*queries.Project, error) { var rateParam sql.NullInt64 if billableRate != nil { rateParam = sql.NullInt64{Int64: int64(*billableRate * 100), Valid: true} } project, err := a.queries.UpdateProject(ctx, queries.UpdateProjectParams{ Name: name, BillableRate: rateParam, ID: id, }) if err != nil { return nil, fmt.Errorf("failed to update project: %w", err) } return &project, nil } // FindProject finds a project by name or ID func (a *actions) FindProject(ctx context.Context, nameOrID string) (*queries.Project, error) { // Parse as ID if possible, otherwise use 0 var idParam int64 if id, err := strconv.ParseInt(nameOrID, 10, 64); err == nil { idParam = id } // Search by both ID and name projects, err := a.queries.FindProject(ctx, queries.FindProjectParams{ ID: idParam, Name: nameOrID, }) if err != nil { return nil, fmt.Errorf("database error looking up project: %w", err) } // Check results switch len(projects) { case 0: return nil, fmt.Errorf("%w: %s", ErrProjectNotFound, nameOrID) case 1: return &projects[0], nil default: return nil, fmt.Errorf("%w: %s matches multiple projects", ErrAmbiguousProject, nameOrID) } } func (a *actions) ArchiveProject(ctx context.Context, id int64) error { err := a.queries.ArchiveProject(ctx, id) if err != nil { return fmt.Errorf("failed to archive project: %w", err) } return nil } func (a *actions) UnarchiveProject(ctx context.Context, id int64) error { err := a.queries.UnarchiveProject(ctx, id) if err != nil { return fmt.Errorf("failed to unarchive project: %w", err) } return nil }