Add get task/id (#30)

Closes #22

Reviewed-on: #30
This commit was merged in pull request #30.
This commit is contained in:
2025-05-13 11:47:05 +02:00
parent 2c133e0d85
commit c5efe585d6
3 changed files with 58 additions and 4 deletions

View File

@@ -3,6 +3,8 @@ package main
import (
"context"
"embed"
"errors"
"gitea.seeseepuff.be/seeseemelk/mysqlite"
"log"
"net"
"net/http"
@@ -234,6 +236,27 @@ func getTasks(c *gin.Context) {
c.JSON(http.StatusOK, &response)
}
func getTask(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
}
response, err := db.GetTask(taskId)
if errors.Is(err, mysqlite.ErrNoRows) {
c.JSON(http.StatusNotFound, gin.H{"error": "Task not found"})
return
}
if err != nil {
log.Printf("Error getting task: %v", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": ErrInternalServerError})
return
}
c.JSON(http.StatusOK, &response)
}
/*
*
Initialises the database, and then starts the server.
@@ -251,6 +274,7 @@ func start(ctx context.Context, config *ServerConfig) {
router.DELETE("/api/user/:userId/goal/:goalId", deleteUserGoal)
router.POST("/api/tasks", createTask)
router.GET("/api/tasks", getTasks)
router.GET("/api/task/:taskId", getTask)
srv := &http.Server{
Addr: config.Addr,