From 741aefb2d307e95cd2c756620e5d886a06b7c4de Mon Sep 17 00:00:00 2001 From: Biswa Kalyan Bhuyan Date: Fri, 25 Apr 2025 14:58:40 +0530 Subject: finance/backend: feat: wrote unit/integration tests for core API endpoints --- backend/handlers/goal_handler.go | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) (limited to 'backend/handlers/goal_handler.go') diff --git a/backend/handlers/goal_handler.go b/backend/handlers/goal_handler.go index 53a3d6e..09bea74 100644 --- a/backend/handlers/goal_handler.go +++ b/backend/handlers/goal_handler.go @@ -12,6 +12,7 @@ import ( "github.com/gin-gonic/gin" ) +// CreateGoalInput defines the structure for creating a new financial goal type CreateGoalInput struct { Name string `json:"name" binding:"required"` TargetAmount int64 `json:"targetAmount" binding:"required"` @@ -20,6 +21,7 @@ type CreateGoalInput struct { Status string `json:"status"` } +// UpdateGoalInput defines the structure for updating an existing financial goal type UpdateGoalInput struct { Name string `json:"name"` TargetAmount int64 `json:"targetAmount"` @@ -28,20 +30,22 @@ type UpdateGoalInput struct { Status string `json:"status"` } +// UpdateGoalProgressInput defines the structure for updating just the progress of a goal type UpdateGoalProgressInput struct { CurrentAmount int64 `json:"currentAmount" binding:"required"` } -// GoalHandler handles goal-related operations +// GoalHandler handles all goal-related operations in the API type GoalHandler struct { } -// NewGoalHandler creates a new goal handler +// NewGoalHandler creates and returns a new GoalHandler instance func NewGoalHandler() *GoalHandler { return &GoalHandler{} } -// GetGoals gets all goals for the current user +// GetGoals retrieves all goals for the authenticated user +// Optionally filtered by status if provided as a query parameter func (h *GoalHandler) GetGoals(c *gin.Context) { userID := c.MustGet("userID").(uint) var goals []models.Goal @@ -63,7 +67,7 @@ func (h *GoalHandler) GetGoals(c *gin.Context) { c.JSON(http.StatusOK, goals) } -// GetGoal gets a specific goal by ID +// GetGoal retrieves a specific goal by ID for the authenticated user func (h *GoalHandler) GetGoal(c *gin.Context) { userID := c.MustGet("userID").(uint) goalID, err := strconv.ParseUint(c.Param("id"), 10, 32) @@ -83,7 +87,7 @@ func (h *GoalHandler) GetGoal(c *gin.Context) { c.JSON(http.StatusOK, goal) } -// CreateGoal creates a new goal for the current user +// CreateGoal creates a new financial goal for the authenticated user func (h *GoalHandler) CreateGoal(c *gin.Context) { userID := c.MustGet("userID").(uint) var input CreateGoalInput @@ -135,7 +139,7 @@ func (h *GoalHandler) CreateGoal(c *gin.Context) { c.JSON(http.StatusCreated, goal) } -// UpdateGoal updates an existing goal +// UpdateGoal updates an existing goal for the authenticated user func (h *GoalHandler) UpdateGoal(c *gin.Context) { userID := c.MustGet("userID").(uint) goalID, err := strconv.ParseUint(c.Param("id"), 10, 32) @@ -240,7 +244,7 @@ func (h *GoalHandler) UpdateGoalProgress(c *gin.Context) { c.JSON(http.StatusOK, goal) } -// DeleteGoal deletes a goal +// DeleteGoal deletes a goal belonging to the authenticated user func (h *GoalHandler) DeleteGoal(c *gin.Context) { userID := c.MustGet("userID").(uint) goalID, err := strconv.ParseUint(c.Param("id"), 10, 32) -- cgit v1.2.3-59-g8ed1b