Compare commits
No commits in common. "3fd4072c99dc5078b40b5c6a7e75b0a9df805f20" and "2c133e0d85adc2c0bb4b985ec37b0041192e8cef" have entirely different histories.
3fd4072c99
...
2c133e0d85
@ -313,17 +313,15 @@ func TestGetTaskWhenNoTasks(t *testing.T) {
|
|||||||
result.Length().IsEqual(0)
|
result.Length().IsEqual(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
func createTestTask(e *httpexpect.Expect) {
|
func TestGetTaskWhenTasks(t *testing.T) {
|
||||||
|
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()
|
||||||
@ -334,26 +332,3 @@ func TestGetTaskSWhenTasks(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)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestGetTaskBadId(t *testing.T) {
|
|
||||||
e := startServer(t)
|
|
||||||
createTestTask(e)
|
|
||||||
e.GET("/task/invalid").Expect().Status(400)
|
|
||||||
}
|
|
||||||
|
@ -200,14 +200,3 @@ 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,8 +3,6 @@ package main
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"embed"
|
"embed"
|
||||||
"errors"
|
|
||||||
"gitea.seeseepuff.be/seeseemelk/mysqlite"
|
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
@ -236,27 +234,6 @@ 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.
|
||||||
@ -274,7 +251,6 @@ 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