diff --git a/backend/api/GetAllOperation.go b/backend/api/GetAllOperation.go index 8e03bc8..a9139c6 100644 --- a/backend/api/GetAllOperation.go +++ b/backend/api/GetAllOperation.go @@ -1,13 +1,17 @@ package api import ( - "backend/internal/operation" + "backend/database" "github.com/gofiber/fiber/v2" + "github.com/golang-jwt/jwt/v5" ) func GetAllOperation(c *fiber.Ctx) error { - operations, err := operation.GetAllOperation() + user := c.Locals("user").(*jwt.Token) + claims := user.Claims.(jwt.MapClaims) + userid := claims["id"].(string) + operations, err := database.GetAllOperations(userid) if err != nil { return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{ "error": err.Error(), diff --git a/backend/database/db.go b/backend/database/db.go index 35bf102..8e68c99 100644 --- a/backend/database/db.go +++ b/backend/database/db.go @@ -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]) + } + } } } } diff --git a/backend/database/operations.go b/backend/database/operations.go new file mode 100644 index 0000000..d66e7b8 --- /dev/null +++ b/backend/database/operations.go @@ -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 +} diff --git a/backend/default_operations.json b/backend/default_operations.json new file mode 100644 index 0000000..d6ff419 --- /dev/null +++ b/backend/default_operations.json @@ -0,0 +1,45 @@ +[ + { + "title": "Operation 1", + "amount": 100, + "date": "2022-01-01T00:00:00Z", + "type": "income", + "icon": "😀", + "categoryid": "8f8b5f62-5d30-44a0-8de0-0485200f53a5" + }, + { + "title": "Operation 2", + "amount": 200, + "date": "2022-01-02T00:00:00Z", + "type": "expense", + "icon": "😂" + }, + { + "title": "Operation 2", + "amount": 200, + "date": "2022-01-02T00:00:00Z", + "type": "expense", + "icon": "😂" + }, + { + "title": "Operation 1", + "amount": 100, + "date": "2022-01-02T00:00:00Z", + "type": "income", + "icon": "😀" + }, + { + "title": "Operation 2", + "amount": 200, + "date": "2022-06-02T00:00:00Z", + "type": "expense", + "icon": "😂" + }, + { + "title": "Operation 2", + "amount": 200, + "date": "2022-06-22T00:00:00Z", + "type": "expense", + "icon": "😂" + } +] \ No newline at end of file diff --git a/backend/model/Operation.go b/backend/model/Operation.go index f6bf85b..dbf40a0 100644 --- a/backend/model/Operation.go +++ b/backend/model/Operation.go @@ -2,15 +2,15 @@ package model type Operation struct { BaseModel - Title string `json:"title"` - Amount float64 `json:"amount"` - Note string `json:"note,omitempty"` - Icon string `json:"icon"` - Type string `json:"type"` - Date string `json:"date"` - UserID string - CategoryID string - SubcategoryID string + Title string `json:"title"` + Amount float64 `json:"amount"` + Note string `json:"note,omitempty"` + Icon string `json:"icon"` + Type string `json:"type"` + Date string `json:"date"` + UserID string `json:"userid"` + CategoryID string `json:"categoryid"` + SubcategoryID string `json:"subcategoryid"` Category Category `gorm:"foreignKey:CategoryID"` Subcategory Subcategory `gorm:"foreignKey:SubcategoryID"` } diff --git a/backend/wallet.conf b/backend/wallet.conf index 1b6c9ac..f96f06f 100644 --- a/backend/wallet.conf +++ b/backend/wallet.conf @@ -5,4 +5,5 @@ CERT_FILE=/home/tola/certs/localhost/localhost.crt KEY_FILE=/home/tola/certs/localhost/localhost.key USERS_FILE=users.json DEFAULT_CATEGORIES_FILE=default_categories.json +DEFAULT_OPERATIONS_FILE=default_operations.json DATABASE=wallet.sqlite