mirror of
https://github.com/Uttkarsh-raj/Plannerly.git
synced 2024-11-24 08:02:18 +02:00
create a new task api created
This commit is contained in:
parent
8ead18b94b
commit
792e7ae232
@ -1,4 +1,3 @@
|
|||||||
PORT=8000
|
PORT=8000
|
||||||
MONGODB_URL="mongodb+srv://uttkarsh:OQ4W6JoirmMLpUIm@cluster0.p62gfpi.mongodb.net/"
|
MONGODB_URL="mongodb+srv://uttkarsh:OQ4W6JoirmMLpUIm@cluster0.p62gfpi.mongodb.net/"
|
||||||
SECRET_KEY= "MHcCAQEEIP8dCJ/YyI9qEhGpZnMbceoOqexyjPfrZPPl9kTk+PzjoAoGCCqGSM49AwEHoUQDQgAExVZopSiLjT5fHbeD6Nk51XsLquW6k/I+/nWiIuF+W7zpn7W0kHuHXj1M4xrWv1azJkskFwZt9lLW/wx8R5v+XQ=="
|
SECRET_KEY= "MHcCAQEEIP8dCJ/YyI9qEhGpZnMbceoOqexyjPfrZPPl9kTk+PzjoAoGCCqGSM49AwEHoUQDQgAExVZopSiLjT5fHbeD6Nk51XsLquW6k/I+/nWiIuF+W7zpn7W0kHuHXj1M4xrWv1azJkskFwZt9lLW/wx8R5v+XQ=="
|
||||||
|
|
48
Backend/controllers/tasks_controller.go
Normal file
48
Backend/controllers/tasks_controller.go
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
package controllers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/Uttkarsh-raj/To_Do_App/database"
|
||||||
|
"github.com/Uttkarsh-raj/To_Do_App/models"
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||||
|
"go.mongodb.org/mongo-driver/mongo"
|
||||||
|
)
|
||||||
|
|
||||||
|
var taskCollection *mongo.Collection = database.OpenCollection(database.Client, "tasks")
|
||||||
|
|
||||||
|
func AddNewTask() gin.HandlerFunc {
|
||||||
|
return func(c *gin.Context) {
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Second)
|
||||||
|
var task models.Task
|
||||||
|
|
||||||
|
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)
|
||||||
|
if validationError != nil {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": validationError.Error()})
|
||||||
|
}
|
||||||
|
resultInsertionNumber, insertErr := taskCollection.InsertOne(ctx, task)
|
||||||
|
if insertErr != nil {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"success": false, "error": "Task creation Failed!!"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
log.Print(resultInsertionNumber)
|
||||||
|
defer cancel()
|
||||||
|
response := gin.H{
|
||||||
|
"success": true,
|
||||||
|
"message": "Task created successfully",
|
||||||
|
"data": task, // Include the task data in the response.
|
||||||
|
}
|
||||||
|
c.JSON(http.StatusOK, response)
|
||||||
|
}
|
||||||
|
}
|
@ -19,6 +19,7 @@ func main() {
|
|||||||
router := gin.New()
|
router := gin.New()
|
||||||
router.Use(gin.Logger())
|
router.Use(gin.Logger())
|
||||||
routes.UserRoutes(router)
|
routes.UserRoutes(router)
|
||||||
|
routes.TaskRoutes(router)
|
||||||
log.Print(os.Getenv("SECRET_KEY"))
|
log.Print(os.Getenv("SECRET_KEY"))
|
||||||
|
|
||||||
router.Use(middleware.Authentication())
|
router.Use(middleware.Authentication())
|
||||||
|
15
Backend/models/task_model.go
Normal file
15
Backend/models/task_model.go
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
package models
|
||||||
|
|
||||||
|
import (
|
||||||
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Task struct {
|
||||||
|
ID primitive.ObjectID `bson:"_id"`
|
||||||
|
User_id *string `json:"user_id"`
|
||||||
|
Title *string `json:"title"`
|
||||||
|
Description *string `json:"desc"`
|
||||||
|
Time *string `json:"time"`
|
||||||
|
Date *string `json:"date"`
|
||||||
|
Status *string `json:"status"`
|
||||||
|
}
|
12
Backend/routes/tasks_routes.go
Normal file
12
Backend/routes/tasks_routes.go
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
package routes
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/Uttkarsh-raj/To_Do_App/controllers"
|
||||||
|
"github.com/Uttkarsh-raj/To_Do_App/middleware"
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TaskRoutes(incomingRoutes *gin.Engine) {
|
||||||
|
incomingRoutes.Use(middleware.Authentication())
|
||||||
|
incomingRoutes.POST("/addTask", controllers.AddNewTask())
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user