added operations
This commit is contained in:
@@ -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(),
|
||||
|
||||
@@ -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])
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
@@ -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": "😂"
|
||||
}
|
||||
]
|
||||
@@ -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"`
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user