Skip to content

Commit

Permalink
handlers updated
Browse files Browse the repository at this point in the history
  • Loading branch information
kasyap1234 committed Apr 11, 2024
1 parent a62614a commit 7942564
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 2 deletions.
24 changes: 23 additions & 1 deletion handlers/company_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down
59 changes: 58 additions & 1 deletion handlers/question_handlers.go
Original file line number Diff line number Diff line change
@@ -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) {

}

0 comments on commit 7942564

Please sign in to comment.