parent
2c133e0d85
commit
27c29c54da
@ -313,15 +313,17 @@ func TestGetTaskWhenNoTasks(t *testing.T) {
|
|||||||
result.Length().IsEqual(0)
|
result.Length().IsEqual(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGetTaskWhenTasks(t *testing.T) {
|
func createTestTask(e *httpexpect.Expect) {
|
||||||
e := startServer(t)
|
|
||||||
|
|
||||||
// Create a new task
|
|
||||||
requestBody := map[string]interface{}{
|
requestBody := map[string]interface{}{
|
||||||
"name": "Test Task",
|
"name": "Test Task",
|
||||||
"reward": 100,
|
"reward": 100,
|
||||||
}
|
}
|
||||||
e.POST("/tasks").WithJSON(requestBody).Expect().Status(201)
|
e.POST("/tasks").WithJSON(requestBody).Expect().Status(201)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetTaskSWhenTasks(t *testing.T) {
|
||||||
|
e := startServer(t)
|
||||||
|
createTestTask(e)
|
||||||
|
|
||||||
// Get the task
|
// Get the task
|
||||||
result := e.GET("/tasks").Expect().Status(200).JSON().Array()
|
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("reward").IsEqual(100)
|
||||||
item.Value("assigned").IsNull()
|
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)
|
||||||
|
}
|
||||||
|
@ -200,3 +200,14 @@ func (db *Db) GetTasks() ([]Task, error) {
|
|||||||
}
|
}
|
||||||
return tasks, nil
|
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
|
||||||
|
}
|
||||||
|
@ -3,6 +3,8 @@ package main
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"embed"
|
"embed"
|
||||||
|
"errors"
|
||||||
|
"gitea.seeseepuff.be/seeseemelk/mysqlite"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
@ -234,6 +236,27 @@ func getTasks(c *gin.Context) {
|
|||||||
c.JSON(http.StatusOK, &response)
|
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.
|
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.DELETE("/api/user/:userId/goal/:goalId", deleteUserGoal)
|
||||||
router.POST("/api/tasks", createTask)
|
router.POST("/api/tasks", createTask)
|
||||||
router.GET("/api/tasks", getTasks)
|
router.GET("/api/tasks", getTasks)
|
||||||
|
router.GET("/api/task/:taskId", getTask)
|
||||||
|
|
||||||
srv := &http.Server{
|
srv := &http.Server{
|
||||||
Addr: config.Addr,
|
Addr: config.Addr,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user