-
Notifications
You must be signed in to change notification settings - Fork 0
/
add.go
47 lines (42 loc) · 1.36 KB
/
add.go
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
package cmd
import (
"fmt"
"log/slog"
"github.com/elliot40404/acc/pkg/database"
"github.com/spf13/cobra"
)
var addCmd = &cobra.Command{
Use: "add",
Short: "Add a new income or expense",
Example: `acc add -t income -d "Paycheck" -a 1000`,
Run: Add,
}
func init() {
RootCmd.AddCommand(addCmd)
addCmd.Flags().StringP("type", "t", "", "Type of transaction (income or expense)")
addCmd.Flags().StringP("description", "d", "", "Description")
addCmd.Flags().Float64P("amount", "a", 0, "Amount")
addCmd.MarkFlagRequired("type")
addCmd.MarkFlagRequired("description")
addCmd.MarkFlagRequired("amount")
}
func Add(cmd *cobra.Command, args []string) {
transactionType, _ := cmd.Flags().GetString("type")
description, _ := cmd.Flags().GetString("description")
amount, _ := cmd.Flags().GetFloat64("amount")
if transactionType != "income" && transactionType != "expense" {
slog.Error("Invalid transaction type", "Type", transactionType)
fmt.Println("Supported transaction types: income, expense")
return
}
fmt.Printf("Adding %s transaction: %s for $%.2f\n", transactionType, description, amount)
db := database.NewTransactionRepository()
err := db.CreateTransaction(database.Transaction{
Type: transactionType,
Description: description,
Amount: amount,
})
if err != nil {
slog.Error("Failed to create transaction", "Error", err.Error())
}
}