1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
|
package accounts
import (
"net/http"
"strconv"
"finance/backend/internal/database"
"finance/backend/internal/models"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
)
// GetAccounts returns all accounts for the authenticated user
func GetAccounts() gin.HandlerFunc {
return func(c *gin.Context) {
// Get user from context (set by auth middleware)
user, exists := c.Get("user")
if !exists {
c.JSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"})
return
}
userObj := user.(models.User)
var accounts []models.Account
// Fetch all accounts for the user
if err := database.DB.Where("user_id = ?", userObj.ID).Find(&accounts).Error; err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to fetch accounts"})
return
}
c.JSON(http.StatusOK, gin.H{"accounts": accounts})
}
}
// GetAccountByID returns a specific account by ID
func GetAccountByID() gin.HandlerFunc {
return func(c *gin.Context) {
// Get user from context (set by auth middleware)
user, exists := c.Get("user")
if !exists {
c.JSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"})
return
}
userObj := user.(models.User)
// Get account ID from URL parameter
accountID, err := strconv.ParseUint(c.Param("id"), 10, 32)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid account ID format"})
return
}
var account models.Account
// Fetch the account and ensure it belongs to the authenticated user
if err := database.DB.Where("id = ? AND user_id = ?", accountID, userObj.ID).First(&account).Error; err != nil {
if err == gorm.ErrRecordNotFound {
c.JSON(http.StatusNotFound, gin.H{"error": "Account not found"})
} else {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to fetch account"})
}
return
}
c.JSON(http.StatusOK, gin.H{"account": account})
}
}
// CreateAccount creates a new account
func CreateAccount() gin.HandlerFunc {
return func(c *gin.Context) {
// Get user from context (set by auth middleware)
user, exists := c.Get("user")
if !exists {
c.JSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"})
return
}
userObj := user.(models.User)
// Define a struct to bind the request JSON
var input struct {
Name string `json:"name" binding:"required"`
Type string `json:"type" binding:"required"` // e.g., "Bank", "Credit Card", "Cash", "Loan", "Income Source"
Balance int64 `json:"balance" binding:"required"`
}
// Bind JSON to struct
if err := c.ShouldBindJSON(&input); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
// Create account object
account := models.Account{
UserID: userObj.ID,
Name: input.Name,
Type: input.Type,
Balance: input.Balance,
}
// Save to database
if err := database.DB.Create(&account).Error; err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create account"})
return
}
c.JSON(http.StatusCreated, gin.H{"account": account})
}
}
// UpdateAccount updates an existing account
func UpdateAccount() gin.HandlerFunc {
return func(c *gin.Context) {
// Get user from context (set by auth middleware)
user, exists := c.Get("user")
if !exists {
c.JSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"})
return
}
userObj := user.(models.User)
// Get account ID from URL parameter
accountID, err := strconv.ParseUint(c.Param("id"), 10, 32)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid account ID format"})
return
}
// Check if the account exists and belongs to the user
var account models.Account
if err := database.DB.Where("id = ? AND user_id = ?", accountID, userObj.ID).First(&account).Error; err != nil {
if err == gorm.ErrRecordNotFound {
c.JSON(http.StatusNotFound, gin.H{"error": "Account not found"})
} else {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to fetch account"})
}
return
}
// Define a struct to bind the request JSON
var input struct {
Name string `json:"name"`
Type string `json:"type"`
Balance int64 `json:"balance"`
}
// Bind JSON to struct
if err := c.ShouldBindJSON(&input); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
// Update fields if provided
if input.Name != "" {
account.Name = input.Name
}
if input.Type != "" {
account.Type = input.Type
}
// For balance, we should allow setting it to 0, so check if it was provided
if c.Request.Method == "PUT" || c.Request.Method == "PATCH" {
if c.PostForm("balance") != "" || c.GetHeader("Content-Type") == "application/json" {
account.Balance = input.Balance
}
}
// Save updates to database
if err := database.DB.Save(&account).Error; err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to update account"})
return
}
c.JSON(http.StatusOK, gin.H{"account": account})
}
}
// DeleteAccount deletes an account
func DeleteAccount() gin.HandlerFunc {
return func(c *gin.Context) {
// Get user from context (set by auth middleware)
user, exists := c.Get("user")
if !exists {
c.JSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"})
return
}
userObj := user.(models.User)
// Get account ID from URL parameter
accountID, err := strconv.ParseUint(c.Param("id"), 10, 32)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid account ID format"})
return
}
// Check if the account exists and belongs to the user
var account models.Account
if err := database.DB.Where("id = ? AND user_id = ?", accountID, userObj.ID).First(&account).Error; err != nil {
if err == gorm.ErrRecordNotFound {
c.JSON(http.StatusNotFound, gin.H{"error": "Account not found"})
} else {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to fetch account"})
}
return
}
// Delete the account
if err := database.DB.Delete(&account).Error; err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to delete account"})
return
}
c.JSON(http.StatusOK, gin.H{"message": "Account deleted successfully"})
}
}
|