Skip to content

Commit

Permalink
refactor: Remove unnecessary concurrency and formatting adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
xDeFc0nx committed Jan 25, 2025
1 parent ed0b64d commit 36a0ae7
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 98 deletions.
18 changes: 0 additions & 18 deletions handlers/Accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package handlers

import (
"encoding/json"
"sync"

"github.com/gofiber/contrib/websocket"
"github.com/google/uuid"
Expand Down Expand Up @@ -51,23 +50,6 @@ func GetAccounts(ws *websocket.Conn, data json.RawMessage, userID string) {
return
}

var mu sync.Mutex
var wg sync.WaitGroup
for i := range accounts {
wg.Add(1)
go func(a *types.Accounts) {
defer wg.Done()
if err := GetAccountBalance(ws, a.ID); err != nil {
Send_Error(ws, "failed to get account balance", err)
}
mu.Lock()
defer mu.Unlock()
if err := db.DB.Where("id = ?", a.ID).First(a).Error; err != nil {
Send_Error(ws, "Failed to fetch updated account", err)
}
}(&accounts[i])
}
wg.Wait()
if err := db.DB.Where("user_id = ?", userID).Find(&accounts).Error; err != nil {
Send_Error(ws, "Accounts not found", err)
}
Expand Down
53 changes: 9 additions & 44 deletions handlers/Transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,25 +155,21 @@ func CreateTransaction(
}
}

if err := GetAccountBalance(ws, account.ID); err != nil {
Send_Error(ws, "failed to get account balance", err)
return
}
if err := db.DB.Where("id = ?", account.ID).First(&account).Error; err != nil {
Send_Error(ws, "Failed to fetch updated account", err)
return
}
response := map[string]interface{}{
"transaction": map[string]interface{}{
"ID": transaction.ID,
"UserID": transaction.UserID,
"AccountID": transaction.AccountID,
"Amount": transaction.Amount,
"Description": transaction.Description,
"IsRecurring": transaction.IsRecurring,
"Frequency": recurring.Frequency,
"CreatedAt": recurring.CreatedAt.Format(time.RFC3339),
"AccountBalance": account.Balance,
"ID": transaction.ID,
"UserID": transaction.UserID,
"AccountID": transaction.AccountID,
"Type": transaction.Type,
"Amount": transaction.Amount,
"Description": transaction.Description,
"IsRecurring": transaction.IsRecurring,
"Frequency": recurring.Frequency,
"CreatedAt": recurring.CreatedAt.Format(time.RFC3339),
},
}

Expand Down Expand Up @@ -384,34 +380,3 @@ func DeleteTransaction(
responseData, _ := json.Marshal(response)
Send_Message(ws, string(responseData))
}

func GetAccountBalance(ws *websocket.Conn, accountID string) error {
transactions := []types.Transaction{}
account := new(types.Accounts)

account.ID = accountID

if err := db.DB.Where(" id =?", account.ID).First(&account).Error; err != nil {
Send_Error(ws, "Account not found", err)
return err
}

if err := db.DB.Where("account_id = ?", account.ID).Find(&transactions).Error; err != nil {
Send_Error(ws, "Could not get transactions", err)
return err
}

totalBalance := float64(0)
for _, t := range transactions {
totalBalance += t.Amount
}

account.Balance = totalBalance

if err := db.DB.Save(account).Error; err != nil {
Send_Error(ws, "Failed to save", err)
return err
}

return nil
}
75 changes: 39 additions & 36 deletions types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,58 +8,61 @@ type WebSocketConnection struct {
ID string `gorm:"primaryKey"`
ConnectionID string `gorm:"connectionID"`
UserID string `gorm:"not null;index"`
IsActive bool `json:"isActive"`
LastPing time.Time `json:"lastPing"`
CreatedAt time.Time `json:"createdAt"`
IsActive bool ` json:"isActive"`
LastPing time.Time ` json:"lastPing"`
CreatedAt time.Time ` json:"createdAt"`
}

type User struct {
ID string `gorm:"primaryKey;index"`
FirstName string `json:"FirstName"`
LastName string `json:"lastName"`
FirstName string ` json:"FirstName"`
LastName string ` json:"lastName"`
Email string `gorm:"type:varchar(100);unique_index"`
Password string `json:"Password"`
Country string `json:"country"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"-"`
Password string ` json:"Password"`
Country string ` json:"country"`
CreatedAt time.Time ` json:"createdAt"`
UpdatedAt time.Time ` json:"-"`
Accounts []Accounts `gorm:"foreignKey:UserID;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
WebSocketConnections []WebSocketConnection `gorm:"foreignKey:UserID;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
}

type Accounts struct {
ID string `gorm:"primaryKey;column:id"`
UserID string `gorm:"not null;column:user_id"`
User User `gorm:"foreignKey:UserID;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
Type string `json:"type"`
Balance float64 `json:"balance"`
Type string ` json:"type"`
Balance float64 ` json:"balance"`
Transactions []Transaction `gorm:"foreignKey:AccountID;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"-"`
CreatedAt time.Time ` json:"createdAt"`
UpdatedAt time.Time ` json:"-"`
}

type Transaction struct {
ID string `gorm:"primaryKey;column:id"`
UserID string `gorm:"not null;column:user_id"`
User User `gorm:"foreignKey:UserID;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
AccountID string `gorm:"not null;column:account_id"`
Account Accounts `gorm:"foreignKey:AccountID;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
Amount float64 `json:"amount"`
ID string `gorm:"primaryKey;column:id"`
UserID string `gorm:"not null;column:user_id"`
User User `gorm:"foreignKey:UserID;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
AccountID string `gorm:"not null;column:account_id"`
Account Accounts `gorm:"foreignKey:AccountID;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
Type string ` json:"type"`
Amount float64 ` json:"amount"`

Description string `json:"description"`
IsRecurring bool `gorm:"default:false"`
Recurring Recurring `gorm:"foreignKey:TransactionID;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
IsRecurring bool ` gorm:"default:false"`
Recurring Recurring ` gorm:"foreignKey:TransactionID;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"-"`
}

type Recurring struct {
ID string `gorm:"primaryKey"`
TransactionID string `gorm:"not null"`
Amount float64 `json:"amount"`
Frequency string `json:"frequency"`
StartDate time.Time `json:"startDate"`
NextDate time.Time `json:"nextDate"`
EndDate *time.Time `json:"endDate"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
Amount float64 ` json:"amount"`
Frequency string ` json:"frequency"`
StartDate time.Time ` json:"startDate"`
NextDate time.Time ` json:"nextDate"`
EndDate *time.Time ` json:"endDate"`
CreatedAt time.Time ` json:"createdAt"`
UpdatedAt time.Time ` json:"updatedAt"`
}

type Budget struct {
Expand All @@ -68,11 +71,11 @@ type Budget struct {
User User `gorm:"foreignKey:UserID;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
AccountID string `gorm:"not null;column:account_id"`
Account Accounts `gorm:"foreignKey:AccountID;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
TotalSpent float64 `gorm:"column:total_spent" json:"totalSpent"`
Limit float64 `gorm:"column:limit" json:"limit"`
Description string `json:"description"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"-"`
TotalSpent float64 `gorm:"column:total_spent" json:"totalSpent"`
Limit float64 `gorm:"column:limit" json:"limit"`
Description string ` json:"description"`
CreatedAt time.Time ` json:"createdAt"`
UpdatedAt time.Time ` json:"-"`
}

type Goal struct {
Expand All @@ -83,7 +86,7 @@ type Goal struct {
Account Accounts `gorm:"foreignKey:AccountID;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
GoalAmount float64 `gorm:"goal_amount"`
Amount float64 `gorm:"amount"`
Description string `json:"description"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"-"`
Description string ` json:"description"`
CreatedAt time.Time ` json:"createdAt"`
UpdatedAt time.Time ` json:"-"`
}

0 comments on commit 36a0ae7

Please sign in to comment.