From 086fd5b7bf7c5d052813190bdd35aaddaa775e82 Mon Sep 17 00:00:00 2001 From: andrewcaplan1 Date: Mon, 25 Mar 2024 16:55:20 -0400 Subject: [PATCH] feat: update task yes --- backend/schema/tasks/routes.go | 11 +--------- backend/schema/tasks/transactions.go | 32 ++++++++++++++++++++++------ 2 files changed, 26 insertions(+), 17 deletions(-) diff --git a/backend/schema/tasks/routes.go b/backend/schema/tasks/routes.go index 35b6364..0ee7a5a 100644 --- a/backend/schema/tasks/routes.go +++ b/backend/schema/tasks/routes.go @@ -2,7 +2,6 @@ package tasks import ( "carewallet/models" - "encoding/json" "fmt" "net/http" "strconv" @@ -302,16 +301,8 @@ func (pg *PgModel) UpdateTaskInfo(c *gin.Context) { return } - // Convert TaskInfo to json.RawMessage - taskInfoRaw, err := json.Marshal(requestBody.TaskInfo) - if err != nil { - fmt.Println("error converting TaskInfo to json.RawMessage:", err) - c.JSON(http.StatusBadRequest, err.Error()) - return - } - // Update the task_info field in the database - if err := UpdateTaskInfoInDB(pg.Conn, taskID, taskInfoRaw); err != nil { + if err := UpdateTaskInfoInDB(pg.Conn, taskID, requestBody); err != nil { fmt.Println("error updating task info in the database:", err) c.JSON(http.StatusBadRequest, err.Error()) return diff --git a/backend/schema/tasks/transactions.go b/backend/schema/tasks/transactions.go index 9e7f04d..10f36fd 100644 --- a/backend/schema/tasks/transactions.go +++ b/backend/schema/tasks/transactions.go @@ -3,7 +3,6 @@ package tasks import ( "carewallet/models" "context" - "encoding/json" "fmt" "strconv" "time" @@ -197,12 +196,31 @@ func DeleteTaskInDB(pool *pgxpool.Pool, taskID int) error { return err } -// UpdateTaskInfoInDB updates the task_info field in the database -func UpdateTaskInfoInDB(pool *pgxpool.Pool, taskID int, taskInfo json.RawMessage) error { - // Assuming "task" table structure, adjust the query based on your schema - query := "UPDATE task SET task_info = $1 WHERE task_id = $2" - - _, err := pool.Exec(context.Background(), query, taskInfo, taskID) +// UpdateTaskInfoInDB updates all fields of a task in the database +func UpdateTaskInfoInDB(pool *pgxpool.Pool, taskID int, taskFields models.Task) error { + // Construct the SQL query dynamically based on the task_fields parameter + query := ` + UPDATE task SET task_title = $1, group_id = $2, created_by = $3, created_date = $4, start_date = $5, end_date = $6, quick_task = $7, notes = $8, repeating = $9, repeating_interval = $10, repeating_end_date = $11, task_status = $12, task_type = $13, task_info = $14 WHERE task_id = $15 + ` + + // Execute the SQL query with the provided task fields and task ID + _, err := pool.Exec(context.Background(), query, + taskFields.TaskTitle, + taskFields.GroupID, + taskFields.CreatedBy, + taskFields.CreatedDate, + taskFields.StartDate, + taskFields.EndDate, + taskFields.QuickTask, + taskFields.Notes, + taskFields.Repeating, + taskFields.RepeatingInterval, + taskFields.RepeatingEndDate, + taskFields.TaskStatus, + taskFields.TaskType, + taskFields.TaskInfo, + taskID, + ) return err }