feat(update): holy shit it worked first try
This commit is contained in:
1 parent
d3b4431228
commit
72a95c96f3
2 files changed
+88
-27
No files matched your search
+10
-4
@@ -13,8 +13,8 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"id": 3,
|
"id": 3,
|
||||||
"string": "Gas Station ",
|
"string": "Groceries ",
|
||||||
"amount": 22123,
|
"amount": 100,
|
||||||
"date": "2026-11-13T13:16:56.0525-05:00"
|
"date": "2026-11-13T13:16:56.0525-05:00"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -25,8 +25,14 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"id": 5,
|
"id": 5,
|
||||||
"string": "Gas Station ",
|
"string": "Food ",
|
||||||
"amount": 22.3143,
|
"amount": 40,
|
||||||
"date": "2026-11-13T13:17:02.100182-05:00"
|
"date": "2026-11-13T13:17:02.100182-05:00"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": 6,
|
||||||
|
"string": "Groceries",
|
||||||
|
"amount": 100,
|
||||||
|
"date": "2026-11-13T13:35:58.552665-05:00"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
@@ -54,12 +54,6 @@ func list(fileName string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
$ expense-tracker delete --id 2
|
|
||||||
# Expense deleted successfully
|
|
||||||
|
|
||||||
takes Id and loops over loaded tasks to remove object
|
|
||||||
*/
|
|
||||||
func delete(fileName string, idx int) error {
|
func delete(fileName string, idx int) error {
|
||||||
exp, err := loadExpenses(fileName)
|
exp, err := loadExpenses(fileName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -95,7 +89,30 @@ $ expense-tracker update --id 2 --description "new desc" --amount 10
|
|||||||
takes in an id, a string for desc (if "" then it is blank and dont update), a new amount (if -1 then it is blank and dont update)
|
takes in an id, a string for desc (if "" then it is blank and dont update), a new amount (if -1 then it is blank and dont update)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
func update() {
|
func update(fileName string, idx int, desc string, amount float64) error {
|
||||||
|
|
||||||
|
exp, err := loadExpenses(fileName)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
var expense Expense
|
||||||
|
|
||||||
|
for c, i := range exp {
|
||||||
|
if i.Id == idx {
|
||||||
|
expense = i
|
||||||
|
if desc != "" {
|
||||||
|
expense.Desc = desc
|
||||||
|
}
|
||||||
|
|
||||||
|
if amount != -1 {
|
||||||
|
expense.Amount = amount
|
||||||
|
}
|
||||||
|
exp[c] = expense
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return saveExpenses(exp, fileName)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -120,7 +137,6 @@ func summary() {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Saves expenses table to a json file <expenses.json>
|
|
||||||
func saveExpenses(exp []Expense, fileName string) error {
|
func saveExpenses(exp []Expense, fileName string) error {
|
||||||
file, err := os.Create(fileName)
|
file, err := os.Create(fileName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -159,8 +175,56 @@ func main() {
|
|||||||
var fileName string = "expenses.json"
|
var fileName string = "expenses.json"
|
||||||
|
|
||||||
if args[0] == "add" {
|
if args[0] == "add" {
|
||||||
|
var amount float64
|
||||||
|
var desc string
|
||||||
|
for i := range args {
|
||||||
|
if args[i] == "--description" {
|
||||||
|
if len(args) > i {
|
||||||
|
var build_desc strings.Builder
|
||||||
|
for j := i + 1; j < len(args); j++ {
|
||||||
|
if strings.HasPrefix(args[j], "--") {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
build_desc.WriteString(args[j])
|
||||||
|
build_desc.WriteString(" ")
|
||||||
|
}
|
||||||
|
desc = strings.Trim(build_desc.String(), " ")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if args[i] == "--amount" {
|
||||||
|
if len(args) > i {
|
||||||
|
a, err := strconv.ParseFloat(args[i+1], 64)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
amount = a
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
add(fileName, desc, amount)
|
||||||
|
} else if args[0] == "list" {
|
||||||
|
err := list(fileName)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
} else if args[0] == "delete" {
|
||||||
|
var id int
|
||||||
|
for i := range args {
|
||||||
|
if args[i] == "--id" {
|
||||||
|
if len(args) > i {
|
||||||
|
id, _ = strconv.Atoi(args[i+1])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ok := delete(fileName, id)
|
||||||
|
if ok != nil {
|
||||||
|
panic(ok)
|
||||||
|
}
|
||||||
|
} else if args[0] == "update" {
|
||||||
var amount float64
|
var amount float64
|
||||||
var desc strings.Builder
|
var desc strings.Builder
|
||||||
|
var id int
|
||||||
for i := range args {
|
for i := range args {
|
||||||
if args[i] == "--description" {
|
if args[i] == "--description" {
|
||||||
if len(args) > i {
|
if len(args) > i {
|
||||||
@@ -182,24 +246,15 @@ func main() {
|
|||||||
amount = a
|
amount = a
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
for i := range args {
|
||||||
add(fileName, desc.String(), amount)
|
if args[i] == "--id" {
|
||||||
} else if args[0] == "list" {
|
if len(args) > i {
|
||||||
err := list(fileName)
|
id, _ = strconv.Atoi(args[i+1])
|
||||||
if err != nil {
|
}
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
} else if args[0] == "delete" {
|
|
||||||
var id int
|
|
||||||
for i := range args {
|
|
||||||
if args[i] == "--id" {
|
|
||||||
if len(args) > i {
|
|
||||||
id, _ = strconv.Atoi(args[i+1])
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
ok := update(fileName, id, desc.String(), amount)
|
||||||
ok := delete(fileName, id)
|
|
||||||
if ok != nil {
|
if ok != nil {
|
||||||
panic(ok)
|
panic(ok)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user