Rename endpoints (#42)

Closes #39

Reviewed-on: #42
This commit was merged in pull request #42.
This commit is contained in:
2025-05-14 17:14:58 +02:00
parent 790ee3c622
commit 1589bc9422
6 changed files with 125 additions and 125 deletions

View File

@@ -76,7 +76,7 @@ func getUser(c *gin.Context) {
c.IndentedJSON(http.StatusOK, user)
}
func getUserGoals(c *gin.Context) {
func getUserAllowance(c *gin.Context) {
userIdStr := c.Param("userId")
userId, err := strconv.Atoi(userIdStr)
if err != nil {
@@ -97,16 +97,16 @@ func getUserGoals(c *gin.Context) {
return
}
goals, err := db.GetUserGoals(userId)
allowances, err := db.GetUserAllowances(userId)
if err != nil {
log.Printf("Error getting user goals: %v", err)
log.Printf("Error getting user allowance: %v", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": ErrInternalServerError})
return
}
c.IndentedJSON(http.StatusOK, goals)
c.IndentedJSON(http.StatusOK, allowances)
}
func createUserGoal(c *gin.Context) {
func createUserAllowance(c *gin.Context) {
userIdStr := c.Param("userId")
userId, err := strconv.Atoi(userIdStr)
if err != nil {
@@ -116,7 +116,7 @@ func createUserGoal(c *gin.Context) {
}
// Parse request body
var goalRequest CreateGoalRequest
var goalRequest CreateAllowanceRequest
if err := c.ShouldBindJSON(&goalRequest); err != nil {
log.Printf("Error parsing request body: %v", err)
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request body"})
@@ -125,12 +125,12 @@ func createUserGoal(c *gin.Context) {
// Validate request
if goalRequest.Name == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "Goal name cannot be empty"})
c.JSON(http.StatusBadRequest, gin.H{"error": "Allowance name cannot be empty"})
return
}
// Create goal in database
goalId, err := db.CreateGoal(userId, &goalRequest)
goalId, err := db.CreateAllowance(userId, &goalRequest)
if err != nil {
log.Printf("Error creating goal: %v", err)
if err.Error() == "user does not exist" {
@@ -146,9 +146,9 @@ func createUserGoal(c *gin.Context) {
c.IndentedJSON(http.StatusCreated, response)
}
func deleteUserGoal(c *gin.Context) {
func deleteUserAllowance(c *gin.Context) {
userIdStr := c.Param("userId")
goalIdStr := c.Param("goalId")
allowanceIdStr := c.Param("allowanceId")
userId, err := strconv.Atoi(userIdStr)
if err != nil {
@@ -157,10 +157,10 @@ func deleteUserGoal(c *gin.Context) {
return
}
goalId, err := strconv.Atoi(goalIdStr)
allowanceId, err := strconv.Atoi(allowanceIdStr)
if err != nil {
log.Printf("Invalid goal ID: %v", err)
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid goal ID"})
log.Printf("Invalid allowance ID: %v", err)
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid allowance ID"})
return
}
@@ -175,18 +175,18 @@ func deleteUserGoal(c *gin.Context) {
return
}
err = db.DeleteGoal(userId, goalId)
err = db.DeleteAllowance(userId, allowanceId)
if err != nil {
if err.Error() == "goal not found" {
c.JSON(http.StatusNotFound, gin.H{"error": "Goal not found"})
if err.Error() == "allowance not found" {
c.JSON(http.StatusNotFound, gin.H{"error": "History not found"})
} else {
log.Printf("Error deleting goal: %v", err)
log.Printf("Error deleting allowance: %v", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": ErrInternalServerError})
}
return
}
c.IndentedJSON(http.StatusOK, gin.H{"message": "Goal deleted successfully"})
c.IndentedJSON(http.StatusOK, gin.H{"message": "History deleted successfully"})
}
func createTask(c *gin.Context) {
@@ -290,7 +290,7 @@ func putTask(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"message": "Task updated successfully"})
}
func postAllowance(c *gin.Context) {
func postHistory(c *gin.Context) {
userIdStr := c.Param("userId")
userId, err := strconv.Atoi(userIdStr)
if err != nil {
@@ -299,8 +299,8 @@ func postAllowance(c *gin.Context) {
return
}
var allowanceRequest PostAllowance
if err := c.ShouldBindJSON(&allowanceRequest); err != nil {
var historyRequest PostHistory
if err := c.ShouldBindJSON(&historyRequest); err != nil {
log.Printf("Error parsing request body: %v", err)
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request body"})
return
@@ -317,13 +317,13 @@ func postAllowance(c *gin.Context) {
return
}
err = db.AddAllowance(userId, &allowanceRequest)
err = db.AddHistory(userId, &historyRequest)
if err != nil {
log.Printf("Error updating allowance: %v", err)
log.Printf("Error updating history: %v", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": ErrInternalServerError})
return
}
c.JSON(http.StatusOK, gin.H{"message": "Allowance updated successfully"})
c.JSON(http.StatusOK, gin.H{"message": "History updated successfully"})
}
func getHistory(c *gin.Context) {
@@ -359,11 +359,11 @@ func start(ctx context.Context, config *ServerConfig) {
}))
router.GET("/api/users", getUsers)
router.GET("/api/user/:userId", getUser)
router.POST("/api/user/:userId/allowance", postAllowance)
router.POST("/api/user/:userId/history", postHistory)
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)
router.GET("/api/user/:userId/allowance", getUserAllowance)
router.POST("/api/user/:userId/allowance", createUserAllowance)
router.DELETE("/api/user/:userId/allowance/:allowanceId", deleteUserAllowance)
router.POST("/api/tasks", createTask)
router.GET("/api/tasks", getTasks)
router.GET("/api/task/:taskId", getTask)