1
0
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:
Uttkarsh-raj 2023-10-26 19:39:14 +05:30
parent 8ead18b94b
commit 792e7ae232
7 changed files with 77 additions and 2 deletions

View File

@ -1,4 +1,3 @@
PORT=8000
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=="

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

View File

@ -19,6 +19,7 @@ func main() {
router := gin.New()
router.Use(gin.Logger())
routes.UserRoutes(router)
routes.TaskRoutes(router)
log.Print(os.Getenv("SECRET_KEY"))
router.Use(middleware.Authentication())

View 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"`
}

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