Add put task endpoint
This commit is contained in:
parent
0521710032
commit
dc661a5c92
@ -357,3 +357,31 @@ func TestGetTaskBadId(t *testing.T) {
|
|||||||
createTestTask(e)
|
createTestTask(e)
|
||||||
e.GET("/task/invalid").Expect().Status(400)
|
e.GET("/task/invalid").Expect().Status(400)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestPutTaskModifiesTask(t *testing.T) {
|
||||||
|
e := startServer(t)
|
||||||
|
createTestTask(e)
|
||||||
|
requestBody := map[string]interface{}{
|
||||||
|
"name": "Updated Task",
|
||||||
|
"reward": 100,
|
||||||
|
}
|
||||||
|
e.PUT("/task/1").WithJSON(requestBody).Expect().
|
||||||
|
Status(200).
|
||||||
|
JSON().Object()
|
||||||
|
|
||||||
|
// Verify the task is updated
|
||||||
|
result := e.GET("/task/1").Expect().Status(200).JSON().Object()
|
||||||
|
result.Value("id").IsEqual(1)
|
||||||
|
result.Value("name").IsEqual("Updated Task")
|
||||||
|
result.Value("reward").IsEqual(100)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPutTaskInvalidTaskId(t *testing.T) {
|
||||||
|
e := startServer(t)
|
||||||
|
createTestTask(e)
|
||||||
|
|
||||||
|
requestBody := map[string]interface{}{
|
||||||
|
"name": "Updated Task",
|
||||||
|
}
|
||||||
|
e.PUT("/task/999").WithJSON(requestBody).Expect().Status(404)
|
||||||
|
}
|
||||||
|
@ -102,7 +102,7 @@ func (db *Db) CreateGoal(userId int, goal *CreateGoalRequest) (int, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
defer tx.Rollback()
|
defer tx.MustRollback()
|
||||||
|
|
||||||
// Insert the new goal
|
// Insert the new goal
|
||||||
err = tx.Query("insert into goals (user_id, name, target, progress, weight) values (?, ?, ?, 0, ?)").
|
err = tx.Query("insert into goals (user_id, name, target, progress, weight) values (?, ?, ?, 0, ?)").
|
||||||
@ -156,7 +156,7 @@ func (db *Db) CreateTask(task *CreateTaskRequest) (int, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
defer tx.Rollback()
|
defer tx.MustRollback()
|
||||||
|
|
||||||
// Insert the new task
|
// Insert the new task
|
||||||
err = tx.Query("insert into tasks (name, reward, assigned) values (?, ?, ?)").
|
err = tx.Query("insert into tasks (name, reward, assigned) values (?, ?, ?)").
|
||||||
@ -211,3 +211,29 @@ func (db *Db) GetTask(id int) (Task, error) {
|
|||||||
}
|
}
|
||||||
return task, nil
|
return task, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (db *Db) HasTask(id int) (bool, error) {
|
||||||
|
count := 0
|
||||||
|
err := db.db.Query("select count(*) from tasks where id = ?").
|
||||||
|
Bind(id).ScanSingle(&count)
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
return count > 0, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (db *Db) UpdateTask(id int, task *CreateTaskRequest) error {
|
||||||
|
tx, err := db.db.Begin()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer tx.MustRollback()
|
||||||
|
|
||||||
|
err = tx.Query("update tasks set name=?, reward=?, assigned=? where id = ?").
|
||||||
|
Bind(task.Name, task.Reward, task.Assigned, id).
|
||||||
|
Exec()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return tx.Commit()
|
||||||
|
}
|
||||||
|
@ -36,14 +36,12 @@ type CreateGoalResponse struct {
|
|||||||
ID int `json:"id"`
|
ID int `json:"id"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// CreateTaskRequest represents the request body for creating a new task.
|
|
||||||
type CreateTaskRequest struct {
|
type CreateTaskRequest struct {
|
||||||
Name string `json:"name" binding:"required"`
|
Name string `json:"name" binding:"required"`
|
||||||
Reward int `json:"reward"`
|
Reward int `json:"reward"`
|
||||||
Assigned *int `json:"assigned"`
|
Assigned *int `json:"assigned"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// CreateTaskResponse represents the response body after creating a new task.
|
|
||||||
type CreateTaskResponse struct {
|
type CreateTaskResponse struct {
|
||||||
ID int `json:"id"`
|
ID int `json:"id"`
|
||||||
}
|
}
|
||||||
|
@ -257,6 +257,38 @@ func getTask(c *gin.Context) {
|
|||||||
c.JSON(http.StatusOK, &response)
|
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.
|
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.POST("/api/tasks", createTask)
|
||||||
router.GET("/api/tasks", getTasks)
|
router.GET("/api/tasks", getTasks)
|
||||||
router.GET("/api/task/:taskId", getTask)
|
router.GET("/api/task/:taskId", getTask)
|
||||||
|
router.PUT("/api/task/:taskId", putTask)
|
||||||
|
|
||||||
srv := &http.Server{
|
srv := &http.Server{
|
||||||
Addr: config.Addr,
|
Addr: config.Addr,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user