Skip to content

Commit

Permalink
feat: support undo in golang with mongo
Browse files Browse the repository at this point in the history
  • Loading branch information
tonitienda committed Apr 25, 2024
1 parent cfe66be commit b7710d9
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
7 changes: 7 additions & 0 deletions backend-golang-rest/pkg/db/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package db

import (
"context"
"fmt"
"log"
"time"

Expand Down Expand Up @@ -72,6 +73,9 @@ func (db *MongoDB) AddTask(task tasks.Task) error {
}

func (db *MongoDB) UpdateTask(task tasks.Task) error {

existingTask, _ := db.GetTask(task.ID)
fmt.Println("Updating task", existingTask, "to", task)
collection := db.client.Database("kadai").Collection("tasks")

_, err := collection.UpdateOne(context.Background(), bson.D{{"id", task.ID}}, bson.D{{"$set", task}})
Expand All @@ -80,6 +84,9 @@ func (db *MongoDB) UpdateTask(task tasks.Task) error {
return err
}

existingTask, _ = db.GetTask(task.ID)
fmt.Println("After updating task", existingTask)

return nil
}

Expand Down
13 changes: 4 additions & 9 deletions backend-golang-rest/pkg/tasks/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,17 +188,12 @@ func (h *TasksHandler) UndoDeletion(c *gin.Context) error {
return err
}

// TODO - See how to reset the "deletedAt"
task2 := Task{
ID: task.ID,
OwnerID: task.OwnerID,
Title: task.Title,
Description: task.Description,
Status: task.Status,
}
task.OwnerID = ""
task.DeletedAt = time.Time{}

fmt.Println("Updating task ")

err = h.datasource.UpdateTask(task2)
err = h.datasource.UpdateTask(task)

if err != nil {
return err
Expand Down

0 comments on commit b7710d9

Please sign in to comment.