diff --git a/backend/api_test.go b/backend/api_test.go index ef66dde..9f3c527 100644 --- a/backend/api_test.go +++ b/backend/api_test.go @@ -313,15 +313,17 @@ func TestGetTaskWhenNoTasks(t *testing.T) { result.Length().IsEqual(0) } -func TestGetTaskWhenTasks(t *testing.T) { - e := startServer(t) - - // Create a new task +func createTestTask(e *httpexpect.Expect) { requestBody := map[string]interface{}{ "name": "Test Task", "reward": 100, } e.POST("/tasks").WithJSON(requestBody).Expect().Status(201) +} + +func TestGetTaskSWhenTasks(t *testing.T) { + e := startServer(t) + createTestTask(e) // Get the task result := e.GET("/tasks").Expect().Status(200).JSON().Array() @@ -332,3 +334,20 @@ func TestGetTaskWhenTasks(t *testing.T) { item.Value("reward").IsEqual(100) item.Value("assigned").IsNull() } + +func TestGetTask(t *testing.T) { + e := startServer(t) + createTestTask(e) + + result := e.GET("/task/1").Expect().Status(200).JSON().Object() + result.Value("id").IsEqual(1) + result.Value("name").IsEqual("Test Task") + result.Value("reward").IsEqual(100) + result.Value("assigned").IsNull() +} + +func TestGetTaskInvalidId(t *testing.T) { + e := startServer(t) + createTestTask(e) + e.GET("/task/2").Expect().Status(404) +} diff --git a/backend/db.go b/backend/db.go index 9a7b934..dc9aa36 100644 --- a/backend/db.go +++ b/backend/db.go @@ -200,3 +200,14 @@ func (db *Db) GetTasks() ([]Task, error) { } return tasks, nil } + +func (db *Db) GetTask(id int) (Task, error) { + task := Task{} + + err := db.db.Query("select id, name, reward, assigned from tasks where id = ?"). + Bind(id).ScanSingle(&task.ID, &task.Name, &task.Reward, &task.Assigned) + if err != nil { + return Task{}, err + } + return task, nil +} diff --git a/backend/main.go b/backend/main.go index c8506f9..7bc6355 100644 --- a/backend/main.go +++ b/backend/main.go @@ -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,