diff options
Diffstat (limited to 'internal/actions')
-rw-r--r-- | internal/actions/actions.go | 2 | ||||
-rw-r--r-- | internal/actions/clients.go | 31 | ||||
-rw-r--r-- | internal/actions/projects.go | 23 |
3 files changed, 50 insertions, 6 deletions
diff --git a/internal/actions/actions.go b/internal/actions/actions.go index 61d007b..7f707d3 100644 --- a/internal/actions/actions.go +++ b/internal/actions/actions.go @@ -16,10 +16,12 @@ type Actions interface { // Client operations CreateClient(ctx context.Context, name, email string, billableRate *float64) (*queries.Client, error) + EditClient(ctx context.Context, id int64, name, email string, billableRate *float64) (*queries.Client, error) FindClient(ctx context.Context, nameOrID string) (*queries.Client, error) // Project operations CreateProject(ctx context.Context, name, client string, billableRate *float64) (*queries.Project, error) + EditProject(ctx context.Context, id int64, name string, billableRate *float64) (*queries.Project, error) FindProject(ctx context.Context, nameOrID string) (*queries.Project, error) } diff --git a/internal/actions/clients.go b/internal/actions/clients.go index 1a99d59..10e8e7d 100644 --- a/internal/actions/clients.go +++ b/internal/actions/clients.go @@ -22,9 +22,8 @@ func (a *actions) CreateClient(ctx context.Context, name, email string, billable } var billableRateParam sql.NullInt64 - if billableRate != nil && *billableRate > 0 { - rate := int64(*billableRate * 100) // Convert dollars to cents - billableRateParam = sql.NullInt64{Int64: rate, Valid: true} + if billableRate != nil { + billableRateParam = sql.NullInt64{Int64: int64(*billableRate * 100), Valid: true} } client, err := a.queries.CreateClient(ctx, queries.CreateClientParams{ @@ -39,6 +38,32 @@ func (a *actions) CreateClient(ctx context.Context, name, email string, billable return &client, nil } +func (a *actions) EditClient(ctx context.Context, id int64, name, email string, billableRate *float64) (*queries.Client, error) { + finalName, finalEmail := parseNameAndEmail(name, email) + + var emailParam sql.NullString + if finalEmail != "" { + emailParam = sql.NullString{String: finalEmail, Valid: true} + } + + var rateParam sql.NullInt64 + if billableRate != nil { + rateParam = sql.NullInt64{Int64: int64(*billableRate * 100), Valid: true} + } + + client, err := a.queries.UpdateClient(ctx, queries.UpdateClientParams{ + Name: finalName, + Email: emailParam, + BillableRate: rateParam, + ID: id, + }) + if err != nil { + return nil, fmt.Errorf("failed to update client: %w", err) + } + + return &client, nil +} + // FindClient finds a client by name or ID func (a *actions) FindClient(ctx context.Context, nameOrID string) (*queries.Client, error) { // Parse as ID if possible, otherwise use 0 diff --git a/internal/actions/projects.go b/internal/actions/projects.go index 4cb4638..d36780b 100644 --- a/internal/actions/projects.go +++ b/internal/actions/projects.go @@ -18,9 +18,8 @@ func (a *actions) CreateProject(ctx context.Context, name, client string, billab } var billableRateParam sql.NullInt64 - if billableRate != nil && *billableRate > 0 { - rate := int64(*billableRate * 100) // Convert dollars to cents - billableRateParam = sql.NullInt64{Int64: rate, Valid: true} + if billableRate != nil { + billableRateParam = sql.NullInt64{Int64: int64(*billableRate * 100), Valid: true} } project, err := a.queries.CreateProject(ctx, queries.CreateProjectParams{ @@ -35,6 +34,24 @@ func (a *actions) CreateProject(ctx context.Context, name, client string, billab 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 |