added operations
Build and Test / build-and-test-backend (push) Successful in 1m2s
Build and Test / build-and-test-frontend (push) Successful in 34s

This commit is contained in:
wt committed 2025-09-02 18:25:06 +07:00
1 parent 59f5a2cbc6
commit e16edd7744
6 files changed
+130 -11

No files matched your search

+7
View File
@@ -51,6 +51,13 @@ func InitDatabase() {
CreateCategory(&default_categories[j])
}
}
if os.Getenv("DEFAULT_OPERATIONS_FILE") != "" {
default_operations := GetOperationsFromFile(os.Getenv("DEFAULT_OPERATIONS_FILE"))
for j := range default_operations {
default_operations[j].UserID = allusers[i].ID
CreateOperation(&default_operations[j])
}
}
}
}
}
+62
View File
@@ -0,0 +1,62 @@
package database
import (
"backend/model"
"encoding/json"
"fmt"
"os"
"strings"
)
func CreateOperation(operation *model.Operation) error {
e := Db.Where(model.Operation{
Title: operation.Title,
Amount: operation.Amount,
Note: operation.Note,
Icon: operation.Icon,
Type: operation.Type,
Date: operation.Date,
UserID: operation.UserID,
CategoryID: operation.CategoryID,
SubcategoryID: operation.SubcategoryID,
}).FirstOrCreate(&operation).Error
if e != nil {
return e
}
return nil
}
func GetOperationsFromFile(filename string) []model.Operation {
data, e := os.ReadFile(filename)
if e != nil {
panic(e)
}
decoder := json.NewDecoder(strings.NewReader(string(data)))
var res []model.Operation
decoder.Decode(&res)
return res
}
func GetAllOperations(userid string) ([]model.Operation, error) {
var operations []model.Operation
e := Db.Where(model.Operation{UserID: userid}).Find(&operations).Error
if e != nil {
return nil, e
}
for i := range operations {
var category model.Category
e := Db.First(&category, "ID = ?", operations[i].CategoryID).Error
if e != nil {
fmt.Println(e)
return nil, e
}
operations[i].Category = category
// // var subcategories []model.Subcategory
// // e := Db.Where(model.Subcategory{CategoryID: categories[i].ID}).Find(&subcategories).Error
// // if e != nil {
// // return nil, e
// // }
// categories[i].Subcategories = subcategories
}
return operations, nil
}