1
0
mirror of https://github.com/Uttkarsh-raj/Plannerly.git synced 2025-12-05 22:06:27 +02:00

get tasks apis done

This commit is contained in:
Uttkarsh-raj
2023-10-27 22:34:57 +05:30
parent 792e7ae232
commit 1e5b02f92b
3 changed files with 98 additions and 1 deletions

View File

@@ -4,16 +4,20 @@ import (
"context" "context"
"log" "log"
"net/http" "net/http"
"os"
"time" "time"
"github.com/Uttkarsh-raj/To_Do_App/database" "github.com/Uttkarsh-raj/To_Do_App/database"
"github.com/Uttkarsh-raj/To_Do_App/models" "github.com/Uttkarsh-raj/To_Do_App/models"
"github.com/dgrijalva/jwt-go"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo"
) )
var taskCollection *mongo.Collection = database.OpenCollection(database.Client, "tasks") var taskCollection *mongo.Collection = database.OpenCollection(database.Client, "tasks")
var SECRET_KEY = os.Getenv("SECRET_KEY")
func AddNewTask() gin.HandlerFunc { func AddNewTask() gin.HandlerFunc {
return func(c *gin.Context) { return func(c *gin.Context) {
@@ -46,3 +50,93 @@ func AddNewTask() gin.HandlerFunc {
c.JSON(http.StatusOK, response) c.JSON(http.StatusOK, response)
} }
} }
func UpdateTask() gin.HandlerFunc {
return func(c *gin.Context) {
// ctx,cancel:=context.WithTimeout(context.Background(),100*time.Second)
}
}
func GetTaskById() gin.HandlerFunc {
return func(c *gin.Context) {
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Second)
var task models.Task
idParam := c.Param("id")
objID, err := primitive.ObjectIDFromHex(idParam)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"success": false, "error": "Invalid ID format"})
return
}
err = taskCollection.FindOne(ctx, bson.M{"_id": objID}).Decode(&task)
defer cancel()
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"success": false, "error": err.Error()})
return
}
response := gin.H{
"success": true,
"data": task,
}
c.JSON(http.StatusOK, response)
}
}
func GetAllTask() gin.HandlerFunc {
return func(c *gin.Context) {
tokenString := c.GetHeader("token")
if tokenString == "" {
c.JSON(http.StatusUnauthorized, gin.H{"success": false, "error": "No token Provided"})
return
}
log.Print(tokenString)
token, err := jwt.Parse(tokenString, func(t *jwt.Token) (interface{}, error) { return []byte(SECRET_KEY), nil })
if err != nil {
c.JSON(http.StatusUnauthorized, gin.H{"success": false, "error": "Invalid Token"})
return
}
log.Print(token)
if claims, ok := token.Claims.(jwt.MapClaims); ok && token.Valid {
log.Print("claims\n")
log.Print(claims)
user_id := claims["Uid"].(string)
// Now you have the user_id from the token
log.Print(user_id)
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Second)
var tasks []models.Task
cursor, err := taskCollection.Find(ctx, bson.M{"user_id": user_id})
defer cancel()
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"success": false, "error": err.Error()})
return
}
defer cursor.Close(ctx)
for cursor.Next(ctx) {
var task models.Task
if err := cursor.Decode(&task); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"success": false, "error": err.Error()})
return
}
tasks = append(tasks, task)
}
if err := cursor.Err(); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"success": false, "error": err.Error()})
return
}
response := gin.H{
"success": true,
"data": tasks,
}
c.JSON(http.StatusOK, response)
} else {
c.JSON(http.StatusUnauthorized, gin.H{"error": "Invalid token"})
}
}
}

View File

@@ -11,5 +11,5 @@ type Task struct {
Description *string `json:"desc"` Description *string `json:"desc"`
Time *string `json:"time"` Time *string `json:"time"`
Date *string `json:"date"` Date *string `json:"date"`
Status *string `json:"status"` Completed bool `json:"completed"`
} }

View File

@@ -9,4 +9,7 @@ import (
func TaskRoutes(incomingRoutes *gin.Engine) { func TaskRoutes(incomingRoutes *gin.Engine) {
incomingRoutes.Use(middleware.Authentication()) incomingRoutes.Use(middleware.Authentication())
incomingRoutes.POST("/addTask", controllers.AddNewTask()) incomingRoutes.POST("/addTask", controllers.AddNewTask())
incomingRoutes.PATCH("/update", controllers.UpdateTask())
incomingRoutes.GET("/getTasks", controllers.GetAllTask())
incomingRoutes.GET("/getTask/:id", controllers.GetTaskById())
} }