aboutsummaryrefslogtreecommitdiffstats
path: root/backend/internal/api/handlers/loan_handler.go
blob: 3edb5596caedb4bca08aff002daf5235b6a2a7f0 (plain) (blame)
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package handlers

import (
	"log"
	"net/http"
	"strconv"
	"time"

	"finance/backend/internal/database"
	"finance/backend/internal/models"

	"github.com/gin-gonic/gin"
)

// LoanHandler handles loan-related operations
type LoanHandler struct {
}

// NewLoanHandler creates and returns a new LoanHandler instance
func NewLoanHandler() *LoanHandler {
	return &LoanHandler{}
}

// GetLoans retrieves all loans for the authenticated user
func (h *LoanHandler) GetLoans(c *gin.Context) {
	userID := c.MustGet("userID").(uint)
	var loans []models.Loan

	if err := database.DB.Where("user_id = ?", userID).Find(&loans).Error; err != nil {
		log.Printf("Error fetching loans for user %d: %v", userID, err)
		c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to get loans"})
		return
	}

	c.JSON(http.StatusOK, loans)
}

// GetLoanByID retrieves a specific loan by ID for the authenticated user
func (h *LoanHandler) GetLoanByID(c *gin.Context) {
	userID := c.MustGet("userID").(uint)
	loanID, err := strconv.ParseUint(c.Param("id"), 10, 32)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid loan ID"})
		return
	}

	var loan models.Loan
	if err := database.DB.Where("id = ? AND user_id = ?", loanID, userID).First(&loan).Error; err != nil {
		c.JSON(http.StatusNotFound, gin.H{"error": "Loan not found"})
		return
	}

	c.JSON(http.StatusOK, loan)
}

// CreateLoan creates a new loan for the authenticated user
func (h *LoanHandler) CreateLoan(c *gin.Context) {
	userID := c.MustGet("userID").(uint)

	// Define input structure
	type CreateLoanInput struct {
		Name           string  `json:"name" binding:"required"`
		OriginalAmount int64   `json:"originalAmount" binding:"required"`
		CurrentBalance int64   `json:"currentBalance" binding:"required"`
		InterestRate   float64 `json:"interestRate"`
		StartDate      string  `json:"startDate"`
		EndDate        string  `json:"endDate"`
		AccountID      *uint   `json:"accountId"`
	}

	var input CreateLoanInput
	if err := c.ShouldBindJSON(&input); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	// Parse dates if provided
	var startDate, endDate time.Time
	var err error

	if input.StartDate != "" {
		startDate, err = time.Parse("2006-01-02", input.StartDate)
		if err != nil {
			c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid start date format. Use YYYY-MM-DD"})
			return
		}
	}

	if input.EndDate != "" {
		endDate, err = time.Parse("2006-01-02", input.EndDate)
		if err != nil {
			c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid end date format. Use YYYY-MM-DD"})
			return
		}
	}

	// Validate account if provided
	if input.AccountID != nil {
		var account models.Account
		if err := database.DB.Where("id = ? AND user_id = ?", *input.AccountID, userID).First(&account).Error; err != nil {
			c.JSON(http.StatusBadRequest, gin.H{"error": "Associated account not found or doesn't belong to user"})
			return
		}
	}

	// Create loan
	loan := models.Loan{
		UserID:         userID,
		AccountID:      input.AccountID,
		Name:           input.Name,
		OriginalAmount: input.OriginalAmount,
		CurrentBalance: input.CurrentBalance,
		InterestRate:   input.InterestRate,
	}

	// Only set dates if they were provided
	if !startDate.IsZero() {
		loan.StartDate = startDate
	}
	if !endDate.IsZero() {
		loan.EndDate = endDate
	}

	if err := database.DB.Create(&loan).Error; err != nil {
		log.Printf("Error creating loan for user %d: %v", userID, err)
		c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create loan"})
		return
	}

	c.JSON(http.StatusCreated, loan)
}

// UpdateLoan updates an existing loan for the authenticated user
func (h *LoanHandler) UpdateLoan(c *gin.Context) {
	userID := c.MustGet("userID").(uint)
	loanID, err := strconv.ParseUint(c.Param("id"), 10, 32)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid loan ID"})
		return
	}

	// Find loan
	var loan models.Loan
	if err := database.DB.Where("id = ? AND user_id = ?", loanID, userID).First(&loan).Error; err != nil {
		c.JSON(http.StatusNotFound, gin.H{"error": "Loan not found"})
		return
	}

	// Define update structure
	type UpdateLoanInput struct {
		Name           string  `json:"name"`
		OriginalAmount int64   `json:"originalAmount"`
		CurrentBalance int64   `json:"currentBalance"`
		InterestRate   float64 `json:"interestRate"`
		StartDate      string  `json:"startDate"`
		EndDate        string  `json:"endDate"`
		AccountID      *uint   `json:"accountId"`
	}

	var input UpdateLoanInput
	if err := c.ShouldBindJSON(&input); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	// Update fields if provided
	if input.Name != "" {
		loan.Name = input.Name
	}
	if input.OriginalAmount != 0 {
		loan.OriginalAmount = input.OriginalAmount
	}
	if input.CurrentBalance != 0 {
		loan.CurrentBalance = input.CurrentBalance
	}
	if input.InterestRate != 0 {
		loan.InterestRate = input.InterestRate
	}
	if input.StartDate != "" {
		startDate, err := time.Parse("2006-01-02", input.StartDate)
		if err != nil {
			c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid start date format. Use YYYY-MM-DD"})
			return
		}
		loan.StartDate = startDate
	}
	if input.EndDate != "" {
		endDate, err := time.Parse("2006-01-02", input.EndDate)
		if err != nil {
			c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid end date format. Use YYYY-MM-DD"})
			return
		}
		loan.EndDate = endDate
	}
	if input.AccountID != nil {
		// Validate account if provided
		var account models.Account
		if err := database.DB.Where("id = ? AND user_id = ?", *input.AccountID, userID).First(&account).Error; err != nil {
			c.JSON(http.StatusBadRequest, gin.H{"error": "Associated account not found or doesn't belong to user"})
			return
		}
		loan.AccountID = input.AccountID
	}

	// Save changes
	if err := database.DB.Save(&loan).Error; err != nil {
		log.Printf("Error updating loan ID %d for user %d: %v", loanID, userID, err)
		c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to update loan"})
		return
	}

	c.JSON(http.StatusOK, loan)
}

// DeleteLoan deletes a loan belonging to the authenticated user
func (h *LoanHandler) DeleteLoan(c *gin.Context) {
	userID := c.MustGet("userID").(uint)
	loanID, err := strconv.ParseUint(c.Param("id"), 10, 32)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid loan ID"})
		return
	}

	// Check if loan exists and belongs to user
	var loan models.Loan
	if err := database.DB.Where("id = ? AND user_id = ?", loanID, userID).First(&loan).Error; err != nil {
		c.JSON(http.StatusNotFound, gin.H{"error": "Loan not found"})
		return
	}

	// Delete loan
	if err := database.DB.Delete(&loan).Error; err != nil {
		log.Printf("Error deleting loan ID %d for user %d: %v", loanID, userID, err)
		c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to delete loan"})
		return
	}

	c.JSON(http.StatusOK, gin.H{"message": "Loan deleted successfully"})
}