Working on more renames
This commit is contained in:
parent
274675d02a
commit
bca2d67b02
@ -391,9 +391,9 @@ func TestPutTaskInvalidTaskId(t *testing.T) {
|
|||||||
func TestPostAllowance(t *testing.T) {
|
func TestPostAllowance(t *testing.T) {
|
||||||
e := startServer(t)
|
e := startServer(t)
|
||||||
|
|
||||||
e.POST("/user/1/history").WithJSON(PostAllowance{Allowance: 100}).Expect().Status(200)
|
e.POST("/user/1/history").WithJSON(PostHistory{Allowance: 100}).Expect().Status(200)
|
||||||
e.POST("/user/1/history").WithJSON(PostAllowance{Allowance: 20}).Expect().Status(200)
|
e.POST("/user/1/history").WithJSON(PostHistory{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: -10}).Expect().Status(200)
|
||||||
|
|
||||||
response := e.GET("/user/1").Expect().Status(200).JSON().Object()
|
response := e.GET("/user/1").Expect().Status(200).JSON().Object()
|
||||||
response.Value("allowance").Number().IsEqual(100 + 20 - 10)
|
response.Value("allowance").Number().IsEqual(100 + 20 - 10)
|
||||||
@ -402,16 +402,16 @@ func TestPostAllowance(t *testing.T) {
|
|||||||
func TestPostAllowanceInvalidUserId(t *testing.T) {
|
func TestPostAllowanceInvalidUserId(t *testing.T) {
|
||||||
e := startServer(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)
|
Status(404)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGetHistory(t *testing.T) {
|
func TestGetHistory(t *testing.T) {
|
||||||
e := startServer(t)
|
e := startServer(t)
|
||||||
|
|
||||||
e.POST("/user/1/history").WithJSON(PostAllowance{Allowance: 100}).Expect().Status(200)
|
e.POST("/user/1/history").WithJSON(PostHistory{Allowance: 100}).Expect().Status(200)
|
||||||
e.POST("/user/1/history").WithJSON(PostAllowance{Allowance: 20}).Expect().Status(200)
|
e.POST("/user/1/history").WithJSON(PostHistory{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: -10}).Expect().Status(200)
|
||||||
|
|
||||||
response := e.GET("/user/1/history").Expect().Status(200).JSON().Array()
|
response := e.GET("/user/1/history").Expect().Status(200).JSON().Array()
|
||||||
response.Length().IsEqual(3)
|
response.Length().IsEqual(3)
|
||||||
|
@ -67,7 +67,7 @@ func (db *Db) UserExists(userId int) (bool, error) {
|
|||||||
return count > 0, nil
|
return count > 0, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *Db) GetUserGoals(userId int) ([]Goal, error) {
|
func (db *Db) GetUserAllowances(userId int) ([]Goal, error) {
|
||||||
goals := make([]Goal, 0)
|
goals := make([]Goal, 0)
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
@ -236,7 +236,7 @@ func (db *Db) UpdateTask(id int, task *CreateTaskRequest) error {
|
|||||||
return tx.Commit()
|
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()
|
tx, err := db.db.Begin()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -18,7 +18,7 @@ type Allowance struct {
|
|||||||
Timestamp time.Time `json:"timestamp"`
|
Timestamp time.Time `json:"timestamp"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type PostAllowance struct {
|
type PostHistory struct {
|
||||||
Allowance int `json:"allowance"`
|
Allowance int `json:"allowance"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -76,7 +76,7 @@ func getUser(c *gin.Context) {
|
|||||||
c.IndentedJSON(http.StatusOK, user)
|
c.IndentedJSON(http.StatusOK, user)
|
||||||
}
|
}
|
||||||
|
|
||||||
func getUserGoals(c *gin.Context) {
|
func getUserAllowance(c *gin.Context) {
|
||||||
userIdStr := c.Param("userId")
|
userIdStr := c.Param("userId")
|
||||||
userId, err := strconv.Atoi(userIdStr)
|
userId, err := strconv.Atoi(userIdStr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -97,16 +97,16 @@ func getUserGoals(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
goals, err := db.GetUserGoals(userId)
|
allowances, err := db.GetUserAllowances(userId)
|
||||||
if err != nil {
|
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})
|
c.JSON(http.StatusInternalServerError, gin.H{"error": ErrInternalServerError})
|
||||||
return
|
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")
|
userIdStr := c.Param("userId")
|
||||||
userId, err := strconv.Atoi(userIdStr)
|
userId, err := strconv.Atoi(userIdStr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -146,7 +146,7 @@ func createUserGoal(c *gin.Context) {
|
|||||||
c.IndentedJSON(http.StatusCreated, response)
|
c.IndentedJSON(http.StatusCreated, response)
|
||||||
}
|
}
|
||||||
|
|
||||||
func deleteUserGoal(c *gin.Context) {
|
func deleteUserAllowance(c *gin.Context) {
|
||||||
userIdStr := c.Param("userId")
|
userIdStr := c.Param("userId")
|
||||||
goalIdStr := c.Param("goalId")
|
goalIdStr := c.Param("goalId")
|
||||||
|
|
||||||
@ -290,7 +290,7 @@ func putTask(c *gin.Context) {
|
|||||||
c.JSON(http.StatusOK, gin.H{"message": "Task updated successfully"})
|
c.JSON(http.StatusOK, gin.H{"message": "Task updated successfully"})
|
||||||
}
|
}
|
||||||
|
|
||||||
func postAllowance(c *gin.Context) {
|
func postHistory(c *gin.Context) {
|
||||||
userIdStr := c.Param("userId")
|
userIdStr := c.Param("userId")
|
||||||
userId, err := strconv.Atoi(userIdStr)
|
userId, err := strconv.Atoi(userIdStr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -299,8 +299,8 @@ func postAllowance(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var allowanceRequest PostAllowance
|
var historyRequest PostHistory
|
||||||
if err := c.ShouldBindJSON(&allowanceRequest); err != nil {
|
if err := c.ShouldBindJSON(&historyRequest); err != nil {
|
||||||
log.Printf("Error parsing request body: %v", err)
|
log.Printf("Error parsing request body: %v", err)
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request body"})
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request body"})
|
||||||
return
|
return
|
||||||
@ -317,13 +317,13 @@ func postAllowance(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = db.AddAllowance(userId, &allowanceRequest)
|
err = db.AddHistory(userId, &historyRequest)
|
||||||
if err != nil {
|
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})
|
c.JSON(http.StatusInternalServerError, gin.H{"error": ErrInternalServerError})
|
||||||
return
|
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) {
|
func getHistory(c *gin.Context) {
|
||||||
@ -359,11 +359,11 @@ func start(ctx context.Context, config *ServerConfig) {
|
|||||||
}))
|
}))
|
||||||
router.GET("/api/users", getUsers)
|
router.GET("/api/users", getUsers)
|
||||||
router.GET("/api/user/:userId", getUser)
|
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/history", getHistory)
|
||||||
router.GET("/api/user/:userId/goals", getUserGoals)
|
router.GET("/api/user/:userId/allowance", getUserAllowance)
|
||||||
router.POST("/api/user/:userId/goals", createUserGoal)
|
router.POST("/api/user/:userId/allowance", createUserAllowance)
|
||||||
router.DELETE("/api/user/:userId/goal/:goalId", deleteUserGoal)
|
router.DELETE("/api/user/:userId/allowance/:allowance", deleteUserAllowance)
|
||||||
router.POST("/api/tasks", createTask)
|
router.POST("/api/tasks", createTask)
|
||||||
router.GET("/api/tasks", getTasks)
|
router.GET("/api/tasks", getTasks)
|
||||||
router.GET("/api/task/:taskId", getTask)
|
router.GET("/api/task/:taskId", getTask)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user