Add put task endpoint (#33)

Closes #23

Reviewed-on: #33
This commit was merged in pull request #33.
This commit is contained in:
2025-05-13 13:01:29 +02:00
parent 0521710032
commit 44d85fc155
4 changed files with 89 additions and 4 deletions

View File

@@ -257,6 +257,38 @@ func getTask(c *gin.Context) {
c.JSON(http.StatusOK, &response)
}
func putTask(c *gin.Context) {
taskIdStr := c.Param("taskId")
taskId, err := strconv.Atoi(taskIdStr)
if err != nil {
log.Printf("Invalid task ID: %v", err)
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid task ID"})
return
}
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
}
_, err = db.GetTask(taskId)
if errors.Is(err, mysqlite.ErrNoRows) {
c.JSON(http.StatusNotFound, gin.H{"error": "Task not found"})
return
}
err = db.UpdateTask(taskId, &taskRequest)
if err != nil {
log.Printf("Error updating task: %v", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": ErrInternalServerError})
return
}
c.JSON(http.StatusOK, gin.H{"message": "Task updated successfully"})
}
/*
*
Initialises the database, and then starts the server.
@@ -275,6 +307,7 @@ func start(ctx context.Context, config *ServerConfig) {
router.POST("/api/tasks", createTask)
router.GET("/api/tasks", getTasks)
router.GET("/api/task/:taskId", getTask)
router.PUT("/api/task/:taskId", putTask)
srv := &http.Server{
Addr: config.Addr,