From 79425643036d7a0e4e9c773b8a7412308c8a4d0b Mon Sep 17 00:00:00 2001 From: kasyap1234 Date: Thu, 11 Apr 2024 12:25:40 +0530 Subject: [PATCH] handlers updated --- handlers/company_handlers.go | 24 +++++++++++++- handlers/question_handlers.go | 59 ++++++++++++++++++++++++++++++++++- 2 files changed, 81 insertions(+), 2 deletions(-) diff --git a/handlers/company_handlers.go b/handlers/company_handlers.go index f5cc450..c094e80 100644 --- a/handlers/company_handlers.go +++ b/handlers/company_handlers.go @@ -7,6 +7,8 @@ import ( "github.com/gin-gonic/gin" "github.com/kasyap1234/eduhub_backend_golang/database" model "github.com/kasyap1234/eduhub_backend_golang/models" + "go.mongodb.org/mongo-driver/bson/primitive" + "gopkg.in/mgo.v2/bson" ) func GetAllCompanies(c *gin.Context) { @@ -33,7 +35,27 @@ func AddCompany(c *gin.Context) error { return err } -func GetCompaniesByID() { +func GetCompaniesByID(c *gin.Context) { + companyId :=c.Param("companyId"); + objID,err :=primitive.ObjectIDFromHex(companyID); + collection :=databaase.GetMongoClient().Database("college").Collection("companies") + + if err!= nil { + c.JSON(http.StatusBadRequest,gin.H{"error":"invalid company ID"}) + return + } + filter :=bson.D{{"_id",objID}}; + company,err :=database.FindOneById(collection, filter); + if err !=nil { + c.JSON(http.StatusInternalServerError,gin.H{"error": "failed to fetch company by ID "}) +return + } + if company ==nil { + c.JSON(http.StatusNotFound,gin.H{"error": "Company Not found"}) + return + } + c.JSON(http.StatusOK,company); + } func UpdateCompany(c *gin.Context) { diff --git a/handlers/question_handlers.go b/handlers/question_handlers.go index 0d37735..7e43eac 100644 --- a/handlers/question_handlers.go +++ b/handlers/question_handlers.go @@ -1,24 +1,81 @@ package handlers import ( + "log" + "net/http" + "github.com/gin-gonic/gin" "github.com/kasyap1234/eduhub_backend_golang/database" + model "github.com/kasyap1234/eduhub_backend_golang/models" + "go.mongodb.org/mongo-driver/bson/primitive" + "gopkg.in/mgo.v2/bson" ) func GetAllQuestions(c *gin.Context) { collection := database.GetMongoClient().Database("college").Collection("questions") - + questions, err := database.FindAll(collection) + if err != nil { + c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to fetch questions"}) + return + } + c.JSON(http.StatusOK, questions) } func GetQuestionByID(c *gin.Context) { + questionID := c.Param("questionId") + objID, err := primitive.ObjectIDFromHex(questionID) + collection := database.GetMongoClient().Database("college").collection("questions") + if err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": "incorrect request body "}) + return + } + filter := bson.D{{"_id": objID}} + question, err := database.FindOneById(collection, filter) + if err != nil { + c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to fetch question by ID "}) + return + } + if question == nil { + c.JSON(http.StatusNotFound, gin.H{"error": "question not found"}) + return + } + return } func AddQuestion(c *gin.Context) { + var newQuestion model.Question + if err := c.BindJSON(&newQuestion); err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": "invalid request body"}) + return + } + collection := database.GetMongoClient().Database("college").collection("questions") + err := database.InsertOne(collection, newQuestion) + if err != nil { + c.JSON(http.StatusInternalServerError, gin.H{"error": "unable to add question"}) + log.Fatal(err) + + } +return } func UpdateQuestion(c *gin.Context) { + var updatedQuestion model.Question + if err := c.BindJSON(&updatedCompany); err != nil { + c.JSON(http.StatusBadRequest, gin.H{{"error": "invalid request body"}}) + return + } +questionID :=c.param("questionId"); +filter := bson.D{{"questionId", questionID}} +update :=bson.D{{"$set",bson.D{{"Title",updatedQuestion.Title}, {"Text",updatedQuestion.Text},{"Answer",updatedQuestion.Answer}}} +_,err := database.UpdateOne(collection,filter, update) +if err !=nil { + c.JSON(http.StatusInternalServerError,gin.H{"error": "unable to update question"}) + return +} +c.JSON(http.StatusOK,gin.H{"message": "question updated successfully "}); } + func DeleteQuestion(c *gin.Context) { }