diff --git a/Backend/controllers/authController.go b/Backend/controllers/authController.go index d14f6cb..5d5464e 100644 --- a/Backend/controllers/authController.go +++ b/Backend/controllers/authController.go @@ -54,7 +54,7 @@ func SignUp() gin.HandlerFunc { return func(c *gin.Context) { var ctx, cancel = context.WithTimeout(context.Background(), 100*time.Second) var user models.User - + defer cancel() if err := c.BindJSON(&user); err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return @@ -119,7 +119,7 @@ func Login() gin.HandlerFunc { var ctx, cancel = context.WithTimeout(context.Background(), 100*time.Second) var user models.User var foundUser models.User - + defer cancel() if err := c.BindJSON(&user); err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return diff --git a/Backend/controllers/tasks_controller.go b/Backend/controllers/tasks_controller.go index bf0121b..bc727c4 100644 --- a/Backend/controllers/tasks_controller.go +++ b/Backend/controllers/tasks_controller.go @@ -23,12 +23,11 @@ func AddNewTask() gin.HandlerFunc { return func(c *gin.Context) { ctx, cancel := context.WithTimeout(context.Background(), 100*time.Second) var task models.Task - + defer cancel() if err := c.BindJSON(&task); err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } - defer cancel() log.Print(task) task.ID = primitive.NewObjectID() validationError := validate.Struct(task) @@ -53,8 +52,52 @@ func AddNewTask() gin.HandlerFunc { func UpdateTask() gin.HandlerFunc { return func(c *gin.Context) { - // ctx,cancel:=context.WithTimeout(context.Background(),100*time.Second) + taskId := c.Param("id") + var updateFields map[string]interface{} + if err := c.BindJSON(&updateFields); err != nil { + c.JSON(http.StatusBadRequest, gin.H{"success": false, "error": err.Error()}) + return + } + + ctx, cancel := context.WithTimeout(context.Background(), 100*time.Second) + defer cancel() + + objID, err := primitive.ObjectIDFromHex(taskId) + if err != nil { + c.JSON(http.StatusBadRequest, gin.H{"success": false, "error": "Invalid ID format"}) + return + } + + update := bson.M{ + "$set": updateFields, + } + + // Check if the task exists before attempting to update it + var existingTask models.Task + err = taskCollection.FindOne(ctx, bson.M{"_id": objID}).Decode(&existingTask) + if err != nil { + c.JSON(http.StatusNotFound, gin.H{"success": false, "error": "Task not found"}) + return + } + + result, err := taskCollection.UpdateOne(ctx, bson.M{"_id": objID}, update) + if err != nil { + c.JSON(http.StatusInternalServerError, gin.H{"success": false, "error": err.Error()}) + return + } + err = taskCollection.FindOne(ctx, bson.M{"_id": objID}).Decode(&existingTask) + if err != nil { + c.JSON(http.StatusInternalServerError, gin.H{"success": false, "error": err.Error()}) + return + } + + if result.ModifiedCount == 0 { + c.JSON(http.StatusOK, gin.H{"success": true, "data": existingTask}) + return + } + + c.JSON(http.StatusOK, gin.H{"success": true, "data": existingTask}) } } @@ -65,6 +108,7 @@ func GetTaskById() gin.HandlerFunc { idParam := c.Param("id") objID, err := primitive.ObjectIDFromHex(idParam) + defer cancel() if err != nil { c.JSON(http.StatusBadRequest, gin.H{"success": false, "error": "Invalid ID format"}) return diff --git a/Backend/main.go b/Backend/main.go index a9182be..4572f6e 100644 --- a/Backend/main.go +++ b/Backend/main.go @@ -1,36 +1,21 @@ package main import ( - "fmt" "log" "os" - "github.com/Uttkarsh-raj/To_Do_App/middleware" "github.com/Uttkarsh-raj/To_Do_App/routes" "github.com/gin-gonic/gin" ) func main() { - fmt.Println("Hello gophers") port := os.Getenv("PORT") if port == "" { port = "8000" } router := gin.New() - router.Use(gin.Logger()) - routes.UserRoutes(router) - routes.TaskRoutes(router) - log.Print(os.Getenv("SECRET_KEY")) - - router.Use(middleware.Authentication()) - - router.GET("/api-1", func(ctx *gin.Context) { - ctx.JSON(200, gin.H{"success": "Access granted for api-1"}) - }) - - router.GET("/api-2", func(ctx *gin.Context) { - ctx.JSON(200, gin.H{"success": "Access granted for api-2"}) - }) - + router.Use(gin.Logger()) //logs + routes.UserRoutes(router) //user auth routes + routes.TaskRoutes(router) //tasks routes log.Fatal(router.Run(":" + port)) } diff --git a/Backend/routes/tasks_routes.go b/Backend/routes/tasks_routes.go index 57a6a25..bff1a03 100644 --- a/Backend/routes/tasks_routes.go +++ b/Backend/routes/tasks_routes.go @@ -9,7 +9,7 @@ import ( func TaskRoutes(incomingRoutes *gin.Engine) { incomingRoutes.Use(middleware.Authentication()) incomingRoutes.POST("/addTask", controllers.AddNewTask()) - incomingRoutes.PATCH("/update", controllers.UpdateTask()) + incomingRoutes.PATCH("/update/:id", controllers.UpdateTask()) incomingRoutes.GET("/getTasks", controllers.GetAllTask()) incomingRoutes.GET("/getTask/:id", controllers.GetTaskById()) }