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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
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
}
|