aboutsummaryrefslogtreecommitdiffstats
path: root/backend/internal/models/models.go
diff options
context:
space:
mode:
Diffstat (limited to 'backend/internal/models/models.go')
-rw-r--r--backend/internal/models/models.go97
1 files changed, 97 insertions, 0 deletions
diff --git a/backend/internal/models/models.go b/backend/internal/models/models.go
new file mode 100644
index 0000000..c984012
--- /dev/null
+++ b/backend/internal/models/models.go
@@ -0,0 +1,97 @@
+package models
+
+import (
+ "time"
+
+ "golang.org/x/crypto/bcrypt"
+ "gorm.io/gorm"
+)
+
+// User represents a user in the system
+// We'll store a hashed password, not the plaintext one.
+type User struct {
+ gorm.Model // Includes ID, CreatedAt, UpdatedAt, DeletedAt
+ Name string `gorm:"not null"`
+ Email string `gorm:"uniqueIndex;not null"` // Unique email
+ PasswordHash string `gorm:"not null"` // Store hashed password
+}
+
+// SetPassword hashes the given password and sets it on the user model.
+func (u *User) SetPassword(password string) error {
+ hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
+ if err != nil {
+ return err
+ }
+ u.PasswordHash = string(hashedPassword)
+ return nil
+}
+
+// CheckPassword compares a given password with the user's hashed password.
+// Returns nil if the password is correct, otherwise returns an error.
+func (u *User) CheckPassword(password string) error {
+ return bcrypt.CompareHashAndPassword([]byte(u.PasswordHash), []byte(password))
+}
+
+// Account represents a financial account (e.g., bank account, credit card, cash)
+// It belongs to a User.
+type Account struct {
+ gorm.Model
+ UserID uint `gorm:"not null;index"` // Foreign key to User
+ User User // Belongs To relationship
+ Name string `gorm:"not null"`
+ Type string `gorm:"not null;index"` // e.g., "Bank", "Credit Card", "Cash", "Loan", "Income Source"
+ // Balance is stored in the smallest currency unit (e.g., cents)
+ // to avoid floating point issues.
+ Balance int64 `gorm:"not null;default:0"`
+}
+
+// Transaction represents an income or expense event.
+// It belongs to a User and is usually associated with an Account.
+type Transaction struct {
+ gorm.Model
+ UserID uint `gorm:"not null;index"` // Foreign key to User
+ User User // Belongs To relationship
+ AccountID *uint `gorm:"index"` // Foreign key to Account (nullable, e.g., for cash transactions not tied to a bank)
+ Account *Account // Belongs To relationship (pointer for nullable FK)
+ Description string `gorm:"not null"`
+ // Amount stored in the smallest currency unit (e.g., cents)
+ Amount int64 `gorm:"not null"`
+ Type string `gorm:"not null;index"` // "Income" or "Expense"
+ Date time.Time `gorm:"not null;index"` // Date of the transaction
+ Category string `gorm:"index"` // e.g., "Salary", "Groceries", "Utilities"
+}
+
+// Loan represents a loan taken by the user.
+// It belongs to a User and may be linked to an Account representing the loan source/payments.
+type Loan struct {
+ gorm.Model
+ UserID uint `gorm:"not null;index"` // Foreign key to User
+ User User // Belongs To relationship
+ AccountID *uint `gorm:"index"` // Optional FK to the related Account (e.g., where payments are made from/to)
+ Account *Account // Belongs To relationship
+ Name string `gorm:"not null"` // e.g., "Car Loan", "Student Loan"
+ OriginalAmount int64 `gorm:"not null"` // In smallest currency unit
+ CurrentBalance int64 `gorm:"not null"` // In smallest currency unit
+ InterestRate float64 // Annual interest rate (e.g., 0.05 for 5%)
+ StartDate time.Time
+ EndDate time.Time // Expected payoff date
+ // Add fields for payment frequency, next due date etc. if needed later
+}
+
+// Goal represents a financial goal the user is working towards.
+// It belongs to a User.
+type Goal struct {
+ gorm.Model
+ UserID uint `gorm:"not null;index"` // Foreign key to User
+ User User // Belongs To relationship
+ Name string `gorm:"not null"` // e.g., "Save for Down Payment", "Pay Off Credit Card"
+ TargetAmount int64 `gorm:"not null"` // In smallest currency unit
+ CurrentAmount int64 `gorm:"not null;default:0"` // In smallest currency unit
+ TargetDate time.Time // Optional target date for the goal
+ Status string `gorm:"not null;default:'Active';index"` // e.g., "Active", "Achieved", "Cancelled"
+ // Link to specific accounts or loans if needed (e.g., goal to pay off a specific loan)
+ // RelatedAccountID *uint
+ // RelatedLoanID *uint
+}
+
+// Add other models below (Goal)