Add delete goal endpoint (#27)
Closes #18 Reviewed-on: #27
This commit was merged in pull request #27.
This commit is contained in:
@@ -16,6 +16,13 @@ import (
|
||||
var migrations embed.FS
|
||||
var db *Db
|
||||
|
||||
const (
|
||||
ErrInternalServerError = "Internal Server Error"
|
||||
ErrInvalidUserID = "Invalid user ID"
|
||||
ErrUserNotFound = "User not found"
|
||||
ErrCheckingUserExist = "Error checking user existence: %v"
|
||||
)
|
||||
|
||||
// ServerConfig holds configuration for the server.
|
||||
type ServerConfig struct {
|
||||
// The datasource to the SQLite database.
|
||||
@@ -34,7 +41,7 @@ func getUsers(c *gin.Context) {
|
||||
users, err := db.GetUsers()
|
||||
if err != nil {
|
||||
log.Printf("Error getting users: %v", err)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Internal Server Error"})
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": ErrInternalServerError})
|
||||
return
|
||||
}
|
||||
c.IndentedJSON(http.StatusOK, users)
|
||||
@@ -44,19 +51,19 @@ func getUser(c *gin.Context) {
|
||||
userIdStr := c.Param("userId")
|
||||
userId, err := strconv.Atoi(userIdStr)
|
||||
if err != nil {
|
||||
log.Printf("Invalid user ID: %v", err)
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid user ID"})
|
||||
log.Printf(ErrInvalidUserID+": %v", err)
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": ErrInvalidUserID})
|
||||
return
|
||||
}
|
||||
|
||||
user, err := db.GetUser(userId)
|
||||
if err != nil {
|
||||
log.Printf("Error getting user: %v", err)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Internal Server Error"})
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": ErrInternalServerError})
|
||||
return
|
||||
}
|
||||
if user == nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "User not found"})
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": ErrUserNotFound})
|
||||
return
|
||||
}
|
||||
|
||||
@@ -67,27 +74,27 @@ func getUserGoals(c *gin.Context) {
|
||||
userIdStr := c.Param("userId")
|
||||
userId, err := strconv.Atoi(userIdStr)
|
||||
if err != nil {
|
||||
log.Printf("Invalid user ID: %v", err)
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid user ID"})
|
||||
log.Printf(ErrInvalidUserID+": %v", err)
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": ErrInvalidUserID})
|
||||
return
|
||||
}
|
||||
|
||||
exists, err := db.UserExists(userId)
|
||||
if err != nil {
|
||||
log.Printf("Error checking user existence: %v", err)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Internal Server Error"})
|
||||
log.Printf(ErrCheckingUserExist, err)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": ErrInternalServerError})
|
||||
return
|
||||
}
|
||||
if !exists {
|
||||
log.Printf("Error checking user existence: %v", err)
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "User not found"})
|
||||
log.Printf(ErrCheckingUserExist, err)
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": ErrUserNotFound})
|
||||
return
|
||||
}
|
||||
|
||||
goals, err := db.GetUserGoals(userId)
|
||||
if err != nil {
|
||||
log.Printf("Error getting user goals: %v", err)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Internal Server Error"})
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": ErrInternalServerError})
|
||||
return
|
||||
}
|
||||
c.IndentedJSON(http.StatusOK, goals)
|
||||
@@ -97,8 +104,8 @@ func createUserGoal(c *gin.Context) {
|
||||
userIdStr := c.Param("userId")
|
||||
userId, err := strconv.Atoi(userIdStr)
|
||||
if err != nil {
|
||||
log.Printf("Invalid user ID: %v", err)
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid user ID"})
|
||||
log.Printf(ErrInvalidUserID+": %v", err)
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": ErrInvalidUserID})
|
||||
return
|
||||
}
|
||||
|
||||
@@ -121,7 +128,7 @@ func createUserGoal(c *gin.Context) {
|
||||
if err != nil {
|
||||
log.Printf("Error creating goal: %v", err)
|
||||
if err.Error() == "user does not exist" {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "User not found"})
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": ErrUserNotFound})
|
||||
} else {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Could not create goal"})
|
||||
}
|
||||
@@ -133,6 +140,49 @@ func createUserGoal(c *gin.Context) {
|
||||
c.IndentedJSON(http.StatusCreated, response)
|
||||
}
|
||||
|
||||
func deleteUserGoal(c *gin.Context) {
|
||||
userIdStr := c.Param("userId")
|
||||
goalIdStr := c.Param("goalId")
|
||||
|
||||
userId, err := strconv.Atoi(userIdStr)
|
||||
if err != nil {
|
||||
log.Printf(ErrInvalidUserID+": %v", err)
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": ErrInvalidUserID})
|
||||
return
|
||||
}
|
||||
|
||||
goalId, err := strconv.Atoi(goalIdStr)
|
||||
if err != nil {
|
||||
log.Printf("Invalid goal ID: %v", err)
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid goal ID"})
|
||||
return
|
||||
}
|
||||
|
||||
exists, err := db.UserExists(userId)
|
||||
if err != nil {
|
||||
log.Printf(ErrCheckingUserExist, err)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": ErrInternalServerError})
|
||||
return
|
||||
}
|
||||
if !exists {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": ErrUserNotFound})
|
||||
return
|
||||
}
|
||||
|
||||
err = db.DeleteGoal(userId, goalId)
|
||||
if err != nil {
|
||||
if err.Error() == "goal not found" {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Goal not found"})
|
||||
} else {
|
||||
log.Printf("Error deleting goal: %v", err)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": ErrInternalServerError})
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"message": "Goal deleted successfully"})
|
||||
}
|
||||
|
||||
/*
|
||||
*
|
||||
Initialises the database, and then starts the server.
|
||||
@@ -147,6 +197,7 @@ func start(ctx context.Context, config *ServerConfig) {
|
||||
router.GET("/api/user/:userId", getUser)
|
||||
router.GET("/api/user/:userId/goals", getUserGoals)
|
||||
router.POST("/api/user/:userId/goals", createUserGoal)
|
||||
router.DELETE("/api/user/:userId/goal/:goalId", deleteUserGoal)
|
||||
|
||||
srv := &http.Server{
|
||||
Addr: ":" + config.Port,
|
||||
|
||||
Reference in New Issue
Block a user