Implement POST task (#28)
Closes #21 Reviewed-on: #28
This commit was merged in pull request #28.
This commit is contained in:
@@ -183,6 +183,44 @@ func deleteUserGoal(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, gin.H{"message": "Goal deleted successfully"})
|
||||
}
|
||||
|
||||
func createTask(c *gin.Context) {
|
||||
var taskRequest CreateTaskRequest
|
||||
if err := c.ShouldBindJSON(&taskRequest); err != nil {
|
||||
log.Printf("Error parsing request body: %v", err)
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request body"})
|
||||
return
|
||||
}
|
||||
|
||||
if taskRequest.Name == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Task name cannot be empty"})
|
||||
return
|
||||
}
|
||||
|
||||
// If assigned is not nil, check if user exists
|
||||
if taskRequest.Assigned != nil {
|
||||
exists, err := db.UserExists(*taskRequest.Assigned)
|
||||
if err != nil {
|
||||
log.Printf("Error checking user existence: %v", err)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": ErrInternalServerError})
|
||||
return
|
||||
}
|
||||
if !exists {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": ErrUserNotFound})
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
taskId, err := db.CreateTask(&taskRequest)
|
||||
if err != nil {
|
||||
log.Printf("Error creating task: %v", err)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Could not create task"})
|
||||
return
|
||||
}
|
||||
|
||||
response := CreateTaskResponse{ID: taskId}
|
||||
c.IndentedJSON(http.StatusCreated, response)
|
||||
}
|
||||
|
||||
/*
|
||||
*
|
||||
Initialises the database, and then starts the server.
|
||||
@@ -198,6 +236,7 @@ func start(ctx context.Context, config *ServerConfig) {
|
||||
router.GET("/api/user/:userId/goals", getUserGoals)
|
||||
router.POST("/api/user/:userId/goals", createUserGoal)
|
||||
router.DELETE("/api/user/:userId/goal/:goalId", deleteUserGoal)
|
||||
router.POST("/api/tasks", createTask)
|
||||
|
||||
srv := &http.Server{
|
||||
Addr: ":" + config.Port,
|
||||
|
||||
Reference in New Issue
Block a user