1
0
mirror of https://github.com/Uttkarsh-raj/Plannerly.git synced 2025-11-23 21:54:39 +02:00

get requests for diff types of tasks

This commit is contained in:
Uttkarsh-raj
2023-11-01 20:33:24 +05:30
parent fbbedcce39
commit 49d70f412d
3 changed files with 104 additions and 1 deletions

View File

@@ -127,6 +127,106 @@ func GetTaskById() gin.HandlerFunc {
} }
} }
func GetUrgentTasks() gin.HandlerFunc {
return func(c *gin.Context) {
tokenString := c.GetHeader("token")
if tokenString == "" {
c.JSON(http.StatusBadRequest, gin.H{"success": false, "error": "No token Provided."})
return
}
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
}
if claims, ok := token.Claims.(jwt.MapClaims); ok && token.Valid {
user_id := claims["Uid"].(string)
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Second)
var tasks []models.Task
cursor, err := taskCollection.Find(ctx, bson.M{"user_id": user_id, "urgent": true})
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{"sucess": false, "error": "Invalid token"})
}
}
}
func GetRegularTasks() gin.HandlerFunc {
return func(c *gin.Context) {
tokenString := c.GetHeader("token")
if tokenString == "" {
c.JSON(http.StatusBadRequest, gin.H{"success": false, "error": "No token Provided."})
return
}
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
}
if claims, ok := token.Claims.(jwt.MapClaims); token.Valid && ok {
user_id := claims["Uid"].(string)
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Second)
var tasks []models.Task
cursor, err := taskCollection.Find(ctx, bson.M{"user_id": user_id, "urgent": false})
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{"sucess": false, "error": "Invalid token"})
}
}
}
func GetAllTask() gin.HandlerFunc { func GetAllTask() gin.HandlerFunc {
return func(c *gin.Context) { return func(c *gin.Context) {
tokenString := c.GetHeader("token") tokenString := c.GetHeader("token")
@@ -179,7 +279,7 @@ func GetAllTask() gin.HandlerFunc {
c.JSON(http.StatusOK, response) c.JSON(http.StatusOK, response)
} else { } else {
c.JSON(http.StatusUnauthorized, gin.H{"error": "Invalid token"}) c.JSON(http.StatusUnauthorized, gin.H{"sucess": false, "error": "Invalid token"})
} }
} }

View File

@@ -12,4 +12,5 @@ type Task struct {
Time *string `json:"time"` Time *string `json:"time"`
Date *string `json:"date"` Date *string `json:"date"`
Completed bool `json:"completed"` Completed bool `json:"completed"`
Urgent bool `json:"urgent"`
} }

View File

@@ -12,5 +12,7 @@ func TaskRoutes(incomingRoutes *gin.Engine) {
incomingRoutes.PATCH("/update/:id", controllers.UpdateTask()) incomingRoutes.PATCH("/update/:id", controllers.UpdateTask())
incomingRoutes.GET("/getTasks", controllers.GetAllTask()) incomingRoutes.GET("/getTasks", controllers.GetAllTask())
incomingRoutes.GET("/getTask/:id", controllers.GetTaskById()) incomingRoutes.GET("/getTask/:id", controllers.GetTaskById())
incomingRoutes.GET("/tasks/urgent", controllers.GetUrgentTasks())
incomingRoutes.GET("/tasks/regular", controllers.GetRegularTasks())
incomingRoutes.DELETE("/delete/:id", controllers.DeleteTask()) incomingRoutes.DELETE("/delete/:id", controllers.DeleteTask())
} }