1
0
mirror of https://github.com/Uttkarsh-raj/Plannerly.git synced 2024-11-24 08:02:18 +02:00

patch/edit task api completed

This commit is contained in:
Uttkarsh-raj 2023-10-28 17:55:58 +05:30
parent 1e5b02f92b
commit 6071aa3c49
4 changed files with 53 additions and 24 deletions

View File

@ -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

View File

@ -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

View File

@ -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))
}

View File

@ -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())
}