Skip to content

Commit

Permalink
Merge pull request #52 from Ratnesh-Team/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
ratnesh-maurya authored Oct 2, 2024
2 parents 8cbaf79 + 22f6962 commit 5c56bc7
Show file tree
Hide file tree
Showing 10 changed files with 552 additions and 506 deletions.
175 changes: 100 additions & 75 deletions Backend/controllers/NMK_controllers.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"go.mongodb.org/mongo-driver/bson/primitive"
)

// GetNMKCodes is a handler to get all NMK codes.
// GetNMK is a handler to get all NMK codes.
// It fetches all NMK codes from the repository and returns them as a response.
// @Summary Get all NMK codes
// @Description Get all NMK codes
Expand All @@ -21,26 +21,35 @@ import (
// @Router /NMK [get]
func GetNMK(nmkRepo repository.MongoRepository) gin.HandlerFunc {
return func(c *gin.Context) {

//extract the role from the context

// Extract the role from the context
role, _ := c.Get("role")
if (role != "superadmin" && role != "admin" && role != "user") {
c.JSON(http.StatusUnauthorized, gin.H{
"status": http.StatusUnauthorized,
"message": "Unauthorized: You do not have permission to access this resource.",
"data": nil,
})
return
}

var nmkList []models.NMK
queryParams := c.Request.URL.Query()
filter := bson.M{}
if role := queryParams.Get("role"); role == "superadmin"{
filter["IsVerified"] = false
} else if role == "admin"{
}else {
filter["IsVerified"] = true

// Set filter based on user role
if role == "superadmin" {
filter["IsVerified"] = false // Superadmins can see unverified NMK codes
} else if role == "admin" {
filter["IsVerified"] = true // Admins can only see verified NMK codes
} else {
filter["IsVerified"] = true // Users can only see verified NMK codes
}

if Email := queryParams.Get("email"); Email != "" {
Email := queryParams.Get("email")
filter["Email"] = Email
}
if NMK_Code := queryParams.Get("NMK_Code"); NMK_Code != "" {
id := queryParams.Get("NMK_Code")
ObjectID, err := primitive.ObjectIDFromHex(id)
id, err := primitive.ObjectIDFromHex(NMK_Code)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"status": http.StatusBadRequest,
Expand All @@ -49,7 +58,7 @@ func GetNMK(nmkRepo repository.MongoRepository) gin.HandlerFunc {
})
return
}
filter["_id"] = ObjectID
filter["_id"] = id
}

// Fetch all NMK codes from the repository
Expand All @@ -64,7 +73,6 @@ func GetNMK(nmkRepo repository.MongoRepository) gin.HandlerFunc {
}
defer cursor.Close(c.Request.Context())

// Decode each document and append to the NMK list
for cursor.Next(c.Request.Context()) {
var nmk models.NMK
if err := cursor.Decode(&nmk); err != nil {
Expand All @@ -78,76 +86,93 @@ func GetNMK(nmkRepo repository.MongoRepository) gin.HandlerFunc {
nmkList = append(nmkList, nmk)
}

c.JSON(http.StatusOK, gin.H{
"status": http.StatusOK,
"message": "NMK codes fetched successfully",
"data": nmkList,
})
}
c.JSON(http.StatusOK, gin.H{
"status": http.StatusOK,
"message": "NMK codes fetched successfully",
"data": nmkList,
})
}
}

// AddNMK adds a new NMK code.
// @Summary Add a new NMK code
// @Description Add a new NMK code to the repository
// @Tags NMK
// @Accept json
// @Produce json
// @Param nmk body models.NMK true "New NMK Code"
// @Success 200 {object} models.NMK
// @Router /NMK [post]
func AddNMK(nmkRepo repository.MongoRepository) gin.HandlerFunc {
return func(c *gin.Context) {
var nmk models.NMK
if err := c.BindJSON(&nmk); err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"status": http.StatusBadRequest,
"message": "Failed to bind NMK data",
"data": nil,
})
return
}
nmk.IsVerified = false
var nmk models.NMK
if err := c.BindJSON(&nmk); err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"status": http.StatusBadRequest,
"message": "Failed to bind NMK data",
"data": nil,
})
return
}
nmk.IsVerified = false

// Insert NMK data into the repository
if _, err := nmkRepo.InsertOne(nmk); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"status": http.StatusInternalServerError,
"message": "Failed to insert NMK data",
"data": nil,
})
return
}
// Insert NMK data into the repository
if _, err := nmkRepo.InsertOne(nmk); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"status": http.StatusInternalServerError,
"message": "Failed to insert NMK data",
"data": nil,
})
return
}

c.JSON(http.StatusOK, gin.H{
"status": http.StatusOK,
"message": "NMK data inserted successfully",
"data": nmk,
})
}
c.JSON(http.StatusOK, gin.H{
"status": http.StatusOK,
"message": "NMK data inserted successfully",
"data": nmk,
})
}
}

// /NMK/${id}/approve
// ApproveNMK approves an existing NMK code.
// @Summary Approve an existing NMK code by ID.
// @Description Approves an existing NMK code and sets IsVerified to true.
// @Tags NMK
// @Accept json
// @Produce json
// @Param id path string true "NMK ID"
// @Success 200 {object} models.NMKApprovalResponse
// @Router /NMK/{id}/approve [post]
func ApproveNMK(nmkRepo repository.MongoRepository) gin.HandlerFunc {
return func(c *gin.Context) {
queryParams := c.Request.URL.Query()
id := queryParams.Get("id")
ObjectID, err := primitive.ObjectIDFromHex(id)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"status": http.StatusBadRequest,
"message": "Invalid NMK code",
"data": nil,
})
return
}
queryParams := c.Request.URL.Query()
id := queryParams.Get("id")
ObjectID, err := primitive.ObjectIDFromHex(id)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"status": http.StatusBadRequest,
"message": "Invalid NMK code",
"data": nil,
})
return
}

// Update the NMK code in the repository
filter := bson.M{"_id": ObjectID}
update := bson.M{"$set": bson.M{"IsVerified": true}}
if err := nmkRepo.UpdateOne(filter, update,nil); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"status": http.StatusInternalServerError,
"message": "Failed to approve NMK code",
"data": nil,
})
return
}
// Update the NMK code in the repository
filter := bson.M{"_id": ObjectID}
update := bson.M{"$set": bson.M{"IsVerified": true}}
if err := nmkRepo.UpdateOne(filter, update,nil); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"status": http.StatusInternalServerError,
"message": "Failed to approve NMK code",
"data": nil,
})
return
}

c.JSON(http.StatusOK, gin.H{
"status": http.StatusOK,
"message": "NMK code approved successfully",
"data": nil,
})
}
c.JSON(http.StatusOK, gin.H{
"status": http.StatusOK,
"message": "NMK code approved successfully",
"data": nil,
})
}
}
4 changes: 0 additions & 4 deletions Backend/controllers/home_remedies.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package controllers

import (
"fmt"
"net/http"
"strconv"

Expand All @@ -25,9 +24,6 @@ func GetHomeremediesDetails(HomeremediesRepo repository.MongoRepository) gin.Han
var HomeremediesList []models.Homeremedies

role, _ := c.Get("role")
// here role is interface and store as key value pair get role now
fmt.Println(role)

if role != "superadmin" && role != "admin" && role != "user" {
c.JSON(http.StatusUnauthorized, gin.H{
"status": http.StatusUnauthorized,
Expand Down
4 changes: 2 additions & 2 deletions Backend/routes/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ func RehabifyRoutes(router *gin.Engine) {
DoctorDB := config.GetRepoCollection("DoctorDB")

router.GET("/users",middleware.AuthMiddleware(),controllers.GetUsers(user))
router.GET("/NMK", controllers.GetNMK(NMK))
router.GET("/NMK",middleware.AuthMiddleware(), controllers.GetNMK(NMK))
router.GET("/home-remedies",middleware.AuthMiddleware(), controllers.GetHomeremediesDetails(home_remedies))
router.POST("/signUp", controllers.AddUser(AuthDB))
router.POST("/signIn", controllers.VerifyUser(AuthDB))
router.GET("/doctor", controllers.GetDoctor(DoctorDB))
router.GET("/doctor", middleware.AuthMiddleware(), controllers.GetDoctor(DoctorDB))
router.POST("/addNmk", controllers.AddNMK(NMK))
router.POST("/addDoctor", controllers.AddDoctor(DoctorDB))
router.POST("/addPatient", controllers.AddPatient(user))
Expand Down
Loading

0 comments on commit 5c56bc7

Please sign in to comment.