Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update task #61

Merged
merged 1 commit into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 1 addition & 10 deletions backend/schema/tasks/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package tasks

import (
"carewallet/models"
"encoding/json"
"fmt"
"net/http"
"strconv"
Expand Down Expand Up @@ -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
Expand Down
32 changes: 25 additions & 7 deletions backend/schema/tasks/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package tasks
import (
"carewallet/models"
"context"
"encoding/json"
"fmt"
"strconv"
"time"
Expand Down Expand Up @@ -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
}

Expand Down
Loading