Rename endpoints

This commit is contained in:
Sebastiaan de Schaetzen 2025-05-14 17:12:47 +02:00
parent bca2d67b02
commit 895a6be140
5 changed files with 43 additions and 43 deletions

View File

@ -9,7 +9,7 @@ import (
) )
const ( const (
TestAllowanceName = "Test Allowance" TestAllowanceName = "Test History"
) )
func startServer(t *testing.T) *httpexpect.Expect { func startServer(t *testing.T) *httpexpect.Expect {
@ -201,7 +201,7 @@ func TestDeleteUserAllowance(t *testing.T) {
e.DELETE("/user/1/allowance/" + strconv.Itoa(int(allowanceId))). e.DELETE("/user/1/allowance/" + strconv.Itoa(int(allowanceId))).
Expect(). Expect().
Status(200). Status(200).
JSON().Object().Value("message").IsEqual("Allowance deleted successfully") JSON().Object().Value("message").IsEqual("History deleted successfully")
// Verify the allowance no longer exists // Verify the allowance no longer exists
allowances := e.GET("/user/1/allowance"). allowances := e.GET("/user/1/allowance").
@ -218,7 +218,7 @@ func TestDeleteUserAllowanceNotFound(t *testing.T) {
e.DELETE("/user/1/allowance/999"). e.DELETE("/user/1/allowance/999").
Expect(). Expect().
Status(404). Status(404).
JSON().Object().Value("error").IsEqual("Allowance not found") JSON().Object().Value("error").IsEqual("History not found")
} }
func TestDeleteUserAllowanceInvalidId(t *testing.T) { func TestDeleteUserAllowanceInvalidId(t *testing.T) {

View File

@ -67,27 +67,27 @@ func (db *Db) UserExists(userId int) (bool, error) {
return count > 0, nil return count > 0, nil
} }
func (db *Db) GetUserAllowances(userId int) ([]Goal, error) { func (db *Db) GetUserAllowances(userId int) ([]Allowance, error) {
goals := make([]Goal, 0) allowances := make([]Allowance, 0)
var err error var err error
for row := range db.db.Query("select id, name, target, progress, weight from goals where user_id = ?"). for row := range db.db.Query("select id, name, target, progress, weight from allowances where user_id = ?").
Bind(userId).Range(&err) { Bind(userId).Range(&err) {
goal := Goal{} allowance := Allowance{}
err = row.Scan(&goal.ID, &goal.Name, &goal.Target, &goal.Progress, &goal.Weight) err = row.Scan(&allowance.ID, &allowance.Name, &allowance.Target, &allowance.Progress, &allowance.Weight)
if err != nil { if err != nil {
return nil, err return nil, err
} }
goals = append(goals, goal) allowances = append(allowances, allowance)
} }
if err != nil { if err != nil {
return nil, err return nil, err
} }
return goals, nil return allowances, nil
} }
func (db *Db) CreateGoal(userId int, goal *CreateGoalRequest) (int, error) { func (db *Db) CreateAllowance(userId int, allowance *CreateAllowanceRequest) (int, error) {
// Check if user exists before attempting to create a goal // Check if user exists before attempting to create an allowance
exists, err := db.UserExists(userId) exists, err := db.UserExists(userId)
if err != nil { if err != nil {
return 0, err return 0, err
@ -102,9 +102,9 @@ func (db *Db) CreateGoal(userId int, goal *CreateGoalRequest) (int, error) {
} }
defer tx.MustRollback() defer tx.MustRollback()
// Insert the new goal // Insert the new allowance
err = tx.Query("insert into goals (user_id, name, target, progress, weight) values (?, ?, ?, 0, ?)"). err = tx.Query("insert into allowances (user_id, name, target, progress, weight) values (?, ?, ?, 0, ?)").
Bind(userId, goal.Name, goal.Target, goal.Weight). Bind(userId, allowance.Name, allowance.Target, allowance.Weight).
Exec() Exec()
if err != nil { if err != nil {
@ -127,21 +127,21 @@ func (db *Db) CreateGoal(userId int, goal *CreateGoalRequest) (int, error) {
return lastId, nil return lastId, nil
} }
func (db *Db) DeleteGoal(userId int, goalId int) error { func (db *Db) DeleteAllowance(userId int, allowanceId int) error {
// Check if the goal exists for the user // Check if the allowance exists for the user
count := 0 count := 0
err := db.db.Query("select count(*) from goals where id = ? and user_id = ?"). err := db.db.Query("select count(*) from allowances where id = ? and user_id = ?").
Bind(goalId, userId).ScanSingle(&count) Bind(allowanceId, userId).ScanSingle(&count)
if err != nil { if err != nil {
return err return err
} }
if count == 0 { if count == 0 {
return errors.New("goal not found") return errors.New("allowance not found")
} }
// Delete the goal // Delete the allowance
err = db.db.Query("delete from goals where id = ? and user_id = ?"). err = db.db.Query("delete from allowances where id = ? and user_id = ?").
Bind(goalId, userId).Exec() Bind(allowanceId, userId).Exec()
if err != nil { if err != nil {
return err return err
} }
@ -252,13 +252,13 @@ func (db *Db) AddHistory(userId int, allowance *PostHistory) error {
return tx.Commit() return tx.Commit()
} }
func (db *Db) GetHistory(userId int) ([]Allowance, error) { func (db *Db) GetHistory(userId int) ([]History, error) {
history := make([]Allowance, 0) history := make([]History, 0)
var err error var err error
for row := range db.db.Query("select amount, `timestamp` from history where user_id = ? order by `timestamp` desc"). for row := range db.db.Query("select amount, `timestamp` from history where user_id = ? order by `timestamp` desc").
Bind(userId).Range(&err) { Bind(userId).Range(&err) {
allowance := Allowance{} allowance := History{}
var timestamp int64 var timestamp int64
err = row.Scan(&allowance.Allowance, &timestamp) err = row.Scan(&allowance.Allowance, &timestamp)
if err != nil { if err != nil {

View File

@ -13,7 +13,7 @@ type UserWithAllowance struct {
Allowance int `json:"allowance"` Allowance int `json:"allowance"`
} }
type Allowance struct { type History struct {
Allowance int `json:"allowance"` Allowance int `json:"allowance"`
Timestamp time.Time `json:"timestamp"` Timestamp time.Time `json:"timestamp"`
} }
@ -30,7 +30,7 @@ type Task struct {
Assigned *int `json:"assigned"` // Pointer to allow null Assigned *int `json:"assigned"` // Pointer to allow null
} }
type Goal struct { type Allowance struct {
ID int `json:"id"` ID int `json:"id"`
Name string `json:"name"` Name string `json:"name"`
Target int `json:"target"` Target int `json:"target"`
@ -38,7 +38,7 @@ type Goal struct {
Weight int `json:"weight"` Weight int `json:"weight"`
} }
type CreateGoalRequest struct { type CreateAllowanceRequest struct {
Name string `json:"name"` Name string `json:"name"`
Target int `json:"target"` Target int `json:"target"`
Weight int `json:"weight"` Weight int `json:"weight"`

View File

@ -116,7 +116,7 @@ func createUserAllowance(c *gin.Context) {
} }
// Parse request body // Parse request body
var goalRequest CreateGoalRequest var goalRequest CreateAllowanceRequest
if err := c.ShouldBindJSON(&goalRequest); err != nil { if err := c.ShouldBindJSON(&goalRequest); 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"})
@ -125,12 +125,12 @@ func createUserAllowance(c *gin.Context) {
// Validate request // Validate request
if goalRequest.Name == "" { 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 return
} }
// Create goal in database // Create goal in database
goalId, err := db.CreateGoal(userId, &goalRequest) goalId, err := db.CreateAllowance(userId, &goalRequest)
if err != nil { if err != nil {
log.Printf("Error creating goal: %v", err) log.Printf("Error creating goal: %v", err)
if err.Error() == "user does not exist" { if err.Error() == "user does not exist" {
@ -148,7 +148,7 @@ func createUserAllowance(c *gin.Context) {
func deleteUserAllowance(c *gin.Context) { func deleteUserAllowance(c *gin.Context) {
userIdStr := c.Param("userId") userIdStr := c.Param("userId")
goalIdStr := c.Param("goalId") allowanceIdStr := c.Param("allowanceId")
userId, err := strconv.Atoi(userIdStr) userId, err := strconv.Atoi(userIdStr)
if err != nil { if err != nil {
@ -157,10 +157,10 @@ func deleteUserAllowance(c *gin.Context) {
return return
} }
goalId, err := strconv.Atoi(goalIdStr) allowanceId, err := strconv.Atoi(allowanceIdStr)
if err != nil { if err != nil {
log.Printf("Invalid goal ID: %v", err) log.Printf("Invalid allowance ID: %v", err)
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid goal ID"}) c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid allowance ID"})
return return
} }
@ -175,18 +175,18 @@ func deleteUserAllowance(c *gin.Context) {
return return
} }
err = db.DeleteGoal(userId, goalId) err = db.DeleteAllowance(userId, allowanceId)
if err != nil { if err != nil {
if err.Error() == "goal not found" { if err.Error() == "allowance not found" {
c.JSON(http.StatusNotFound, gin.H{"error": "Goal not found"}) c.JSON(http.StatusNotFound, gin.H{"error": "History not found"})
} else { } else {
log.Printf("Error deleting goal: %v", err) log.Printf("Error deleting 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, gin.H{"message": "Goal deleted successfully"}) c.IndentedJSON(http.StatusOK, gin.H{"message": "History deleted successfully"})
} }
func createTask(c *gin.Context) { func createTask(c *gin.Context) {
@ -363,7 +363,7 @@ func start(ctx context.Context, config *ServerConfig) {
router.GET("/api/user/:userId/history", getHistory) router.GET("/api/user/:userId/history", getHistory)
router.GET("/api/user/:userId/allowance", getUserAllowance) router.GET("/api/user/:userId/allowance", getUserAllowance)
router.POST("/api/user/:userId/allowance", createUserAllowance) router.POST("/api/user/:userId/allowance", createUserAllowance)
router.DELETE("/api/user/:userId/allowance/:allowance", deleteUserAllowance) router.DELETE("/api/user/:userId/allowance/:allowanceId", 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)

View File

@ -12,7 +12,7 @@ create table history
amount integer not null amount integer not null
); );
create table goals create table allowances
( (
id integer primary key, id integer primary key,
user_id integer not null, user_id integer not null,