Add endpoint to create allowance (#34)

Closes #11

Reviewed-on: #34
This commit was merged in pull request #34.
This commit is contained in:
2025-05-13 13:52:10 +02:00
parent 44d85fc155
commit 62f43d7eb3
6 changed files with 81 additions and 4 deletions

View File

@@ -289,6 +289,31 @@ func putTask(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"message": "Task updated successfully"})
}
func postAllowance(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
}
var allowanceRequest PostAllowance
if err := c.ShouldBindJSON(&allowanceRequest); err != nil {
log.Printf("Error parsing request body: %v", err)
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request body"})
return
}
err = db.AddAllowance(userId, &allowanceRequest)
if err != nil {
log.Printf("Error updating allowance: %v", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": ErrInternalServerError})
return
}
c.JSON(http.StatusOK, gin.H{"message": "Allowance updated successfully"})
}
/*
*
Initialises the database, and then starts the server.
@@ -308,6 +333,7 @@ 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,