aboutsummaryrefslogtreecommitdiffstats
path: root/backend/handlers
diff options
context:
space:
mode:
Diffstat (limited to 'backend/handlers')
-rw-r--r--backend/handlers/goal_handler.go25
1 files changed, 25 insertions, 0 deletions
diff --git a/backend/handlers/goal_handler.go b/backend/handlers/goal_handler.go
index dd2791c..53a3d6e 100644
--- a/backend/handlers/goal_handler.go
+++ b/backend/handlers/goal_handler.go
@@ -1,6 +1,7 @@
package handlers
import (
+ "log"
"net/http"
"strconv"
"time"
@@ -54,6 +55,7 @@ func (h *GoalHandler) GetGoals(c *gin.Context) {
}
if err := query.Find(&goals).Error; err != nil {
+ log.Printf("Error fetching goals for user %d: %v", userID, err)
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to get goals"})
return
}
@@ -66,12 +68,14 @@ func (h *GoalHandler) GetGoal(c *gin.Context) {
userID := c.MustGet("userID").(uint)
goalID, err := strconv.ParseUint(c.Param("id"), 10, 32)
if err != nil {
+ log.Printf("Error parsing goal ID: %v", err)
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid goal ID"})
return
}
var goal models.Goal
if err := database.DB.Where("id = ? AND user_id = ?", goalID, userID).First(&goal).Error; err != nil {
+ log.Printf("Error fetching goal ID %d for user %d: %v", goalID, userID, err)
c.JSON(http.StatusNotFound, gin.H{"error": "Goal not found"})
return
}
@@ -85,6 +89,7 @@ func (h *GoalHandler) CreateGoal(c *gin.Context) {
var input CreateGoalInput
if err := c.ShouldBindJSON(&input); err != nil {
+ log.Printf("Error binding JSON for goal creation: %v", err)
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
@@ -100,6 +105,7 @@ func (h *GoalHandler) CreateGoal(c *gin.Context) {
if input.TargetDate != "" {
parsedDate, err := time.Parse("2006-01-02", input.TargetDate)
if err != nil {
+ log.Printf("Error parsing target date for goal creation: %v", err)
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid date format for targetDate. Use YYYY-MM-DD"})
return
}
@@ -120,10 +126,12 @@ func (h *GoalHandler) CreateGoal(c *gin.Context) {
}
if err := database.DB.Create(&goal).Error; err != nil {
+ log.Printf("Error creating goal for user %d: %v", userID, err)
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create goal"})
return
}
+ log.Printf("Goal created successfully for user %d: %s (ID: %d)", userID, goal.Name, goal.ID)
c.JSON(http.StatusCreated, goal)
}
@@ -132,18 +140,21 @@ func (h *GoalHandler) UpdateGoal(c *gin.Context) {
userID := c.MustGet("userID").(uint)
goalID, err := strconv.ParseUint(c.Param("id"), 10, 32)
if err != nil {
+ log.Printf("Error parsing goal ID for update: %v", err)
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid goal ID"})
return
}
var goal models.Goal
if err := database.DB.Where("id = ? AND user_id = ?", goalID, userID).First(&goal).Error; err != nil {
+ log.Printf("Error fetching goal ID %d for user %d in update: %v", goalID, userID, err)
c.JSON(http.StatusNotFound, gin.H{"error": "Goal not found"})
return
}
var input UpdateGoalInput
if err := c.ShouldBindJSON(&input); err != nil {
+ log.Printf("Error binding JSON for goal update: %v", err)
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
@@ -161,6 +172,7 @@ func (h *GoalHandler) UpdateGoal(c *gin.Context) {
if input.TargetDate != "" {
parsedDate, err := time.Parse("2006-01-02", input.TargetDate)
if err != nil {
+ log.Printf("Error parsing target date for goal update: %v", err)
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid date format for targetDate. Use YYYY-MM-DD"})
return
}
@@ -173,13 +185,16 @@ func (h *GoalHandler) UpdateGoal(c *gin.Context) {
// Check if goal has been achieved
if goal.CurrentAmount >= goal.TargetAmount {
goal.Status = "Achieved"
+ log.Printf("Goal ID %d for user %d automatically marked as Achieved", goalID, userID)
}
if err := database.DB.Save(&goal).Error; err != nil {
+ log.Printf("Error saving updated goal ID %d for user %d: %v", goalID, userID, err)
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to update goal"})
return
}
+ log.Printf("Goal ID %d updated successfully for user %d", goalID, userID)
c.JSON(http.StatusOK, goal)
}
@@ -188,18 +203,21 @@ func (h *GoalHandler) UpdateGoalProgress(c *gin.Context) {
userID := c.MustGet("userID").(uint)
goalID, err := strconv.ParseUint(c.Param("id"), 10, 32)
if err != nil {
+ log.Printf("Error parsing goal ID for progress update: %v", err)
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid goal ID"})
return
}
var goal models.Goal
if err := database.DB.Where("id = ? AND user_id = ?", goalID, userID).First(&goal).Error; err != nil {
+ log.Printf("Error fetching goal ID %d for user %d in progress update: %v", goalID, userID, err)
c.JSON(http.StatusNotFound, gin.H{"error": "Goal not found"})
return
}
var input UpdateGoalProgressInput
if err := c.ShouldBindJSON(&input); err != nil {
+ log.Printf("Error binding JSON for goal progress update: %v", err)
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
@@ -209,13 +227,16 @@ func (h *GoalHandler) UpdateGoalProgress(c *gin.Context) {
// Check if goal has been achieved
if goal.CurrentAmount >= goal.TargetAmount {
goal.Status = "Achieved"
+ log.Printf("Goal ID %d for user %d automatically marked as Achieved during progress update", goalID, userID)
}
if err := database.DB.Save(&goal).Error; err != nil {
+ log.Printf("Error saving progress update for goal ID %d for user %d: %v", goalID, userID, err)
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to update goal progress"})
return
}
+ log.Printf("Goal ID %d progress updated successfully for user %d: %d/%d", goalID, userID, goal.CurrentAmount, goal.TargetAmount)
c.JSON(http.StatusOK, goal)
}
@@ -224,20 +245,24 @@ func (h *GoalHandler) DeleteGoal(c *gin.Context) {
userID := c.MustGet("userID").(uint)
goalID, err := strconv.ParseUint(c.Param("id"), 10, 32)
if err != nil {
+ log.Printf("Error parsing goal ID for deletion: %v", err)
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid goal ID"})
return
}
var goal models.Goal
if err := database.DB.Where("id = ? AND user_id = ?", goalID, userID).First(&goal).Error; err != nil {
+ log.Printf("Error fetching goal ID %d for user %d for deletion: %v", goalID, userID, err)
c.JSON(http.StatusNotFound, gin.H{"error": "Goal not found"})
return
}
if err := database.DB.Delete(&goal).Error; err != nil {
+ log.Printf("Error deleting goal ID %d for user %d: %v", goalID, userID, err)
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to delete goal"})
return
}
+ log.Printf("Goal ID %d deleted successfully for user %d", goalID, userID)
c.JSON(http.StatusOK, gin.H{"message": "Goal deleted successfully"})
}