1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
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 && *billableRate > 0 {
rate := int64(*billableRate * 100) // Convert dollars to cents
billableRateParam = sql.NullInt64{Int64: rate, 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
}
// 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)
}
}
|