Rename endpoints
This commit is contained in:
parent
bca2d67b02
commit
895a6be140
@ -9,7 +9,7 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
TestAllowanceName = "Test Allowance"
|
||||
TestAllowanceName = "Test History"
|
||||
)
|
||||
|
||||
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))).
|
||||
Expect().
|
||||
Status(200).
|
||||
JSON().Object().Value("message").IsEqual("Allowance deleted successfully")
|
||||
JSON().Object().Value("message").IsEqual("History deleted successfully")
|
||||
|
||||
// Verify the allowance no longer exists
|
||||
allowances := e.GET("/user/1/allowance").
|
||||
@ -218,7 +218,7 @@ func TestDeleteUserAllowanceNotFound(t *testing.T) {
|
||||
e.DELETE("/user/1/allowance/999").
|
||||
Expect().
|
||||
Status(404).
|
||||
JSON().Object().Value("error").IsEqual("Allowance not found")
|
||||
JSON().Object().Value("error").IsEqual("History not found")
|
||||
}
|
||||
|
||||
func TestDeleteUserAllowanceInvalidId(t *testing.T) {
|
||||
|
@ -67,27 +67,27 @@ func (db *Db) UserExists(userId int) (bool, error) {
|
||||
return count > 0, nil
|
||||
}
|
||||
|
||||
func (db *Db) GetUserAllowances(userId int) ([]Goal, error) {
|
||||
goals := make([]Goal, 0)
|
||||
func (db *Db) GetUserAllowances(userId int) ([]Allowance, error) {
|
||||
allowances := make([]Allowance, 0)
|
||||
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) {
|
||||
goal := Goal{}
|
||||
err = row.Scan(&goal.ID, &goal.Name, &goal.Target, &goal.Progress, &goal.Weight)
|
||||
allowance := Allowance{}
|
||||
err = row.Scan(&allowance.ID, &allowance.Name, &allowance.Target, &allowance.Progress, &allowance.Weight)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
goals = append(goals, goal)
|
||||
allowances = append(allowances, allowance)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return goals, nil
|
||||
return allowances, nil
|
||||
}
|
||||
|
||||
func (db *Db) CreateGoal(userId int, goal *CreateGoalRequest) (int, error) {
|
||||
// Check if user exists before attempting to create a goal
|
||||
func (db *Db) CreateAllowance(userId int, allowance *CreateAllowanceRequest) (int, error) {
|
||||
// Check if user exists before attempting to create an allowance
|
||||
exists, err := db.UserExists(userId)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
@ -102,9 +102,9 @@ func (db *Db) CreateGoal(userId int, goal *CreateGoalRequest) (int, error) {
|
||||
}
|
||||
defer tx.MustRollback()
|
||||
|
||||
// Insert the new goal
|
||||
err = tx.Query("insert into goals (user_id, name, target, progress, weight) values (?, ?, ?, 0, ?)").
|
||||
Bind(userId, goal.Name, goal.Target, goal.Weight).
|
||||
// Insert the new allowance
|
||||
err = tx.Query("insert into allowances (user_id, name, target, progress, weight) values (?, ?, ?, 0, ?)").
|
||||
Bind(userId, allowance.Name, allowance.Target, allowance.Weight).
|
||||
Exec()
|
||||
|
||||
if err != nil {
|
||||
@ -127,21 +127,21 @@ func (db *Db) CreateGoal(userId int, goal *CreateGoalRequest) (int, error) {
|
||||
return lastId, nil
|
||||
}
|
||||
|
||||
func (db *Db) DeleteGoal(userId int, goalId int) error {
|
||||
// Check if the goal exists for the user
|
||||
func (db *Db) DeleteAllowance(userId int, allowanceId int) error {
|
||||
// Check if the allowance exists for the user
|
||||
count := 0
|
||||
err := db.db.Query("select count(*) from goals where id = ? and user_id = ?").
|
||||
Bind(goalId, userId).ScanSingle(&count)
|
||||
err := db.db.Query("select count(*) from allowances where id = ? and user_id = ?").
|
||||
Bind(allowanceId, userId).ScanSingle(&count)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if count == 0 {
|
||||
return errors.New("goal not found")
|
||||
return errors.New("allowance not found")
|
||||
}
|
||||
|
||||
// Delete the goal
|
||||
err = db.db.Query("delete from goals where id = ? and user_id = ?").
|
||||
Bind(goalId, userId).Exec()
|
||||
// Delete the allowance
|
||||
err = db.db.Query("delete from allowances where id = ? and user_id = ?").
|
||||
Bind(allowanceId, userId).Exec()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -252,13 +252,13 @@ func (db *Db) AddHistory(userId int, allowance *PostHistory) error {
|
||||
return tx.Commit()
|
||||
}
|
||||
|
||||
func (db *Db) GetHistory(userId int) ([]Allowance, error) {
|
||||
history := make([]Allowance, 0)
|
||||
func (db *Db) GetHistory(userId int) ([]History, error) {
|
||||
history := make([]History, 0)
|
||||
var err error
|
||||
|
||||
for row := range db.db.Query("select amount, `timestamp` from history where user_id = ? order by `timestamp` desc").
|
||||
Bind(userId).Range(&err) {
|
||||
allowance := Allowance{}
|
||||
allowance := History{}
|
||||
var timestamp int64
|
||||
err = row.Scan(&allowance.Allowance, ×tamp)
|
||||
if err != nil {
|
||||
|
@ -13,7 +13,7 @@ type UserWithAllowance struct {
|
||||
Allowance int `json:"allowance"`
|
||||
}
|
||||
|
||||
type Allowance struct {
|
||||
type History struct {
|
||||
Allowance int `json:"allowance"`
|
||||
Timestamp time.Time `json:"timestamp"`
|
||||
}
|
||||
@ -30,7 +30,7 @@ type Task struct {
|
||||
Assigned *int `json:"assigned"` // Pointer to allow null
|
||||
}
|
||||
|
||||
type Goal struct {
|
||||
type Allowance struct {
|
||||
ID int `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Target int `json:"target"`
|
||||
@ -38,7 +38,7 @@ type Goal struct {
|
||||
Weight int `json:"weight"`
|
||||
}
|
||||
|
||||
type CreateGoalRequest struct {
|
||||
type CreateAllowanceRequest struct {
|
||||
Name string `json:"name"`
|
||||
Target int `json:"target"`
|
||||
Weight int `json:"weight"`
|
||||
|
@ -116,7 +116,7 @@ func createUserAllowance(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 createUserAllowance(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" {
|
||||
@ -148,7 +148,7 @@ func createUserAllowance(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 deleteUserAllowance(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 deleteUserAllowance(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) {
|
||||
@ -363,7 +363,7 @@ func start(ctx context.Context, config *ServerConfig) {
|
||||
router.GET("/api/user/:userId/history", getHistory)
|
||||
router.GET("/api/user/:userId/allowance", getUserAllowance)
|
||||
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.GET("/api/tasks", getTasks)
|
||||
router.GET("/api/task/:taskId", getTask)
|
||||
|
@ -12,7 +12,7 @@ create table history
|
||||
amount integer not null
|
||||
);
|
||||
|
||||
create table goals
|
||||
create table allowances
|
||||
(
|
||||
id integer primary key,
|
||||
user_id integer not null,
|
||||
|
Loading…
x
Reference in New Issue
Block a user