Implement PUT /user/{userId}/allowance/{allowanceId} (#52)

Closes #17

Reviewed-on: #52
This commit was merged in pull request #52.
This commit is contained in:
2025-05-17 16:20:05 +02:00
parent d1774c1ce0
commit 238aedb5c9
5 changed files with 109 additions and 1 deletions

View File

@@ -232,6 +232,52 @@ func deleteUserAllowance(c *gin.Context) {
c.IndentedJSON(http.StatusOK, gin.H{"message": "History deleted successfully"})
}
func putUserAllowance(c *gin.Context) {
userIdStr := c.Param("userId")
allowanceIdStr := c.Param("allowanceId")
userId, err := strconv.Atoi(userIdStr)
if err != nil {
log.Printf(ErrInvalidUserID+": %v", err)
c.JSON(http.StatusBadRequest, gin.H{"error": ErrInvalidUserID})
return
}
allowanceId, err := strconv.Atoi(allowanceIdStr)
if err != nil {
log.Printf("Invalid allowance ID: %v", err)
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid allowance ID"})
return
}
exists, err := db.UserExists(userId)
if err != nil {
log.Printf(ErrCheckingUserExist, err)
c.JSON(http.StatusInternalServerError, gin.H{"error": ErrInternalServerError})
return
}
if !exists {
c.JSON(http.StatusNotFound, gin.H{"error": ErrUserNotFound})
return
}
var allowanceRequest UpdateAllowanceRequest
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.UpdateAllowance(userId, allowanceId, &allowanceRequest)
if err != nil {
log.Printf("Error updating allowance: %v", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": ErrInternalServerError})
return
}
c.IndentedJSON(http.StatusOK, gin.H{"message": "Allowance updated successfully"})
}
func createTask(c *gin.Context) {
var taskRequest CreateTaskRequest
if err := c.ShouldBindJSON(&taskRequest); err != nil {
@@ -439,6 +485,7 @@ func start(ctx context.Context, config *ServerConfig) {
router.POST("/api/user/:userId/allowance", createUserAllowance)
router.GET("/api/user/:userId/allowance/:allowanceId", getUserAllowanceById)
router.DELETE("/api/user/:userId/allowance/:allowanceId", deleteUserAllowance)
router.PUT("/api/user/:userId/allowance/:allowanceId", putUserAllowance)
router.POST("/api/tasks", createTask)
router.GET("/api/tasks", getTasks)
router.GET("/api/task/:taskId", getTask)