From bca2d67b02369dff745df2ffc92b01d1e45ecc3c Mon Sep 17 00:00:00 2001 From: Sebastiaan de Schaetzen Date: Wed, 14 May 2025 15:40:10 +0200 Subject: [PATCH] Working on more renames --- backend/api_test.go | 14 +++++++------- backend/db.go | 4 ++-- backend/dto.go | 2 +- backend/main.go | 32 ++++++++++++++++---------------- 4 files changed, 26 insertions(+), 26 deletions(-) diff --git a/backend/api_test.go b/backend/api_test.go index 39e71d2..0afe0e3 100644 --- a/backend/api_test.go +++ b/backend/api_test.go @@ -391,9 +391,9 @@ func TestPutTaskInvalidTaskId(t *testing.T) { func TestPostAllowance(t *testing.T) { e := startServer(t) - e.POST("/user/1/history").WithJSON(PostAllowance{Allowance: 100}).Expect().Status(200) - e.POST("/user/1/history").WithJSON(PostAllowance{Allowance: 20}).Expect().Status(200) - e.POST("/user/1/history").WithJSON(PostAllowance{Allowance: -10}).Expect().Status(200) + e.POST("/user/1/history").WithJSON(PostHistory{Allowance: 100}).Expect().Status(200) + e.POST("/user/1/history").WithJSON(PostHistory{Allowance: 20}).Expect().Status(200) + e.POST("/user/1/history").WithJSON(PostHistory{Allowance: -10}).Expect().Status(200) response := e.GET("/user/1").Expect().Status(200).JSON().Object() response.Value("allowance").Number().IsEqual(100 + 20 - 10) @@ -402,16 +402,16 @@ func TestPostAllowance(t *testing.T) { func TestPostAllowanceInvalidUserId(t *testing.T) { e := startServer(t) - e.POST("/user/999/history").WithJSON(PostAllowance{Allowance: 100}).Expect(). + e.POST("/user/999/history").WithJSON(PostHistory{Allowance: 100}).Expect(). Status(404) } func TestGetHistory(t *testing.T) { e := startServer(t) - e.POST("/user/1/history").WithJSON(PostAllowance{Allowance: 100}).Expect().Status(200) - e.POST("/user/1/history").WithJSON(PostAllowance{Allowance: 20}).Expect().Status(200) - e.POST("/user/1/history").WithJSON(PostAllowance{Allowance: -10}).Expect().Status(200) + e.POST("/user/1/history").WithJSON(PostHistory{Allowance: 100}).Expect().Status(200) + e.POST("/user/1/history").WithJSON(PostHistory{Allowance: 20}).Expect().Status(200) + e.POST("/user/1/history").WithJSON(PostHistory{Allowance: -10}).Expect().Status(200) response := e.GET("/user/1/history").Expect().Status(200).JSON().Array() response.Length().IsEqual(3) diff --git a/backend/db.go b/backend/db.go index 120143b..17cc17d 100644 --- a/backend/db.go +++ b/backend/db.go @@ -67,7 +67,7 @@ func (db *Db) UserExists(userId int) (bool, error) { return count > 0, nil } -func (db *Db) GetUserGoals(userId int) ([]Goal, error) { +func (db *Db) GetUserAllowances(userId int) ([]Goal, error) { goals := make([]Goal, 0) var err error @@ -236,7 +236,7 @@ func (db *Db) UpdateTask(id int, task *CreateTaskRequest) error { return tx.Commit() } -func (db *Db) AddAllowance(userId int, allowance *PostAllowance) error { +func (db *Db) AddHistory(userId int, allowance *PostHistory) error { tx, err := db.db.Begin() if err != nil { return err diff --git a/backend/dto.go b/backend/dto.go index 225e28d..e44e4ea 100644 --- a/backend/dto.go +++ b/backend/dto.go @@ -18,7 +18,7 @@ type Allowance struct { Timestamp time.Time `json:"timestamp"` } -type PostAllowance struct { +type PostHistory struct { Allowance int `json:"allowance"` } diff --git a/backend/main.go b/backend/main.go index 25ea271..11933a1 100644 --- a/backend/main.go +++ b/backend/main.go @@ -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 { @@ -146,7 +146,7 @@ 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") @@ -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/:allowance", deleteUserAllowance) router.POST("/api/tasks", createTask) router.GET("/api/tasks", getTasks) router.GET("/api/task/:taskId", getTask)