aboutsummaryrefslogtreecommitdiffstats
path: root/backend/internal/models/models.go
diff options
context:
space:
mode:
authorLibravatarLibravatar Biswa Kalyan Bhuyan <biswa@surgot.in> 2025-04-24 08:18:27 +0530
committerLibravatarLibravatar Biswa Kalyan Bhuyan <biswa@surgot.in> 2025-04-24 08:18:27 +0530
commit50d5e6534f5e593297a09323e683c7c8b850117b (patch)
tree339d6e8b123c5d4caa4129971e2cb1b960b12a89 /backend/internal/models/models.go
parent76066679b5bdab53419492066c4e80d2ed3be518 (diff)
downloadfinance-50d5e6534f5e593297a09323e683c7c8b850117b.tar.gz
finance-50d5e6534f5e593297a09323e683c7c8b850117b.tar.bz2
finance-50d5e6534f5e593297a09323e683c7c8b850117b.zip
feat: added basic backend features to it
- Set up API framework (Gin Gonic) - Set up ORM/DB library (GORM) - Design database schema (Users, Accounts, Transactions, Loans, Goals) - Set up database connection and migrations
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)