Add endpoint to get allowance history (#37)

Closes #12

Reviewed-on: #37
This commit was merged in pull request #37.
This commit is contained in:
2025-05-14 15:27:29 +02:00
parent 2486bbf1ec
commit 4355e1b1b7
8 changed files with 106 additions and 39 deletions

View File

@@ -185,7 +185,7 @@ func deleteUserGoal(c *gin.Context) {
return
}
c.JSON(http.StatusOK, gin.H{"message": "Goal deleted successfully"})
c.IndentedJSON(http.StatusOK, gin.H{"message": "Goal deleted successfully"})
}
func createTask(c *gin.Context) {
@@ -233,7 +233,7 @@ func getTasks(c *gin.Context) {
c.JSON(http.StatusInternalServerError, gin.H{"error": ErrInternalServerError})
return
}
c.JSON(http.StatusOK, &response)
c.IndentedJSON(http.StatusOK, &response)
}
func getTask(c *gin.Context) {
@@ -325,8 +325,26 @@ func postAllowance(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"message": "Allowance updated successfully"})
}
func getHistory(c *gin.Context) {
userIdStr := c.Param("userId")
userId, err := strconv.Atoi(userIdStr)
if err != nil {
log.Printf("Invalid user ID: %v", err)
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid user ID"})
return
}
history, err := db.GetHistory(userId)
if err != nil {
log.Printf("Error getting history: %v", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": ErrInternalServerError})
return
}
c.IndentedJSON(http.StatusOK, history)
}
/*
*
Initialises the database, and then starts the server.
If the context gets cancelled, the server is shutdown and the database is closed.
*/
@@ -337,6 +355,8 @@ func start(ctx context.Context, config *ServerConfig) {
router := gin.Default()
router.GET("/api/users", getUsers)
router.GET("/api/user/:userId", getUser)
router.POST("/api/user/:userId/allowance", postAllowance)
router.GET("/api/user/:userId/history", getHistory)
router.GET("/api/user/:userId/goals", getUserGoals)
router.POST("/api/user/:userId/goals", createUserGoal)
router.DELETE("/api/user/:userId/goal/:goalId", deleteUserGoal)
@@ -344,7 +364,6 @@ func start(ctx context.Context, config *ServerConfig) {
router.GET("/api/tasks", getTasks)
router.GET("/api/task/:taskId", getTask)
router.PUT("/api/task/:taskId", putTask)
router.POST("/api/user/:userId/allowance", postAllowance)
srv := &http.Server{
Addr: config.Addr,