Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added many2many #93

Merged
merged 15 commits into from
Jun 25, 2023
22 changes: 12 additions & 10 deletions controller/form.controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package controller

import (
"encoding/json"
"fmt"
"io"
"net/http"

Expand All @@ -10,15 +11,15 @@ import (
)

func (h Handler) CreateForm(c *gin.Context) {
var issues []models.Issue
var form models.Form
var issues models.Issue

// Clear previous errors if any
errList := map[string]string{}
var form = models.Form{}
// Accessing the issue ID from request params
id := c.Query("id")
ID := c.Query("id")
// Next, need to verify whether the issue with this ID exists or not
if result := h.DB.Find(&issues, id); result.Error != nil {
if result := h.DB.Find(&issues, ID); result.Error != nil {
c.JSON(http.StatusUnprocessableEntity, gin.H{
"status": http.StatusInternalServerError,
"error": "couldn't find the issue",
Expand All @@ -36,7 +37,6 @@ func (h Handler) CreateForm(c *gin.Context) {
})
return
}

err = json.Unmarshal(body, &form)
if err != nil {
errList["Invalid_body"] = "Invalid data provided"
Expand All @@ -60,9 +60,11 @@ func (h Handler) CreateForm(c *gin.Context) {

// Creating data in the database
// If there's an error, send the error to the client

if err := h.DB.Create(&form).Error; err != nil {
fmt.Println(err)
c.JSON(http.StatusUnprocessableEntity, gin.H{
"status": http.StatusInternalServerError,
"status": http.StatusUnprocessableEntity,
"error": "couldn't save your data",
"ok": false,
})
Expand Down Expand Up @@ -98,8 +100,8 @@ func (h Handler) GetAllForm(c *gin.Context) {

func (h Handler) GetFormByID(c *gin.Context) {
var form models.Form
id := c.Param("id")
if result := h.DB.Find(&form, id); result.Error != nil {
ID := c.Param("id")
if result := h.DB.Find(&form, ID).Preload("Asignees"); result.Error != nil {
c.JSON(http.StatusUnprocessableEntity, gin.H{
"status": http.StatusInternalServerError,
"error": "couldn't find the data",
Expand All @@ -118,9 +120,9 @@ func (h Handler) GetFormByID(c *gin.Context) {

func (h Handler) CompleteIssue(c *gin.Context) {
// Get the issue ID from the request URL
id := c.Param("id")
ID := c.Param("id")
var Form = models.Form{}
if result := h.DB.Find(&Form, id); result.Error != nil {
if result := h.DB.Find(&Form, ID); result.Error != nil {
c.JSON(http.StatusNotFound, gin.H{
"status": http.StatusNotFound,
"error": "User not found",
Expand Down
10 changes: 5 additions & 5 deletions controller/issue.controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (h Handler) CreateIssue(c *gin.Context) {
errList := map[string]string{}
var issue = models.Issue{}
body, err := io.ReadAll(c.Request.Body)
// if error in marsheling body
// if error in marsheling body``
if err != nil {
errList["Invalid_body"] = "error in reading user data"
c.JSON(http.StatusUnprocessableEntity, gin.H{
Expand Down Expand Up @@ -103,8 +103,8 @@ func (h Handler) GetAllIssues(c *gin.Context) {
// @Router /issue/:id [get]
func (h Handler) GetIssueByID(c *gin.Context) {
var issues []models.Issue
id := c.Param("id")
if result := h.DB.Find(&issues, id); result.Error != nil {
ID := c.Param("id")
if result := h.DB.Find(&issues, ID).Preload("form"); result.Error != nil {
c.JSON(http.StatusUnprocessableEntity, gin.H{
"status": http.StatusInternalServerError,
"error": "couldn't find the issue",
Expand Down Expand Up @@ -156,9 +156,9 @@ func (h Handler) SearchIssueByPostNumber(c *gin.Context) {
}

func (h Handler) GetIssueWithFormHandler(c *gin.Context) {
id := c.Param("id")
ID := c.Param("id")
var issue models.Issue
if result := h.DB.Preload("Form").First(&issue, id); result.Error != nil {
if result := h.DB.Preload("Form").First(&issue, ID); result.Error != nil {
c.JSON(http.StatusUnprocessableEntity, gin.H{
"status": http.StatusInternalServerError,
"error": "couldn't find the issue",
Expand Down
52 changes: 23 additions & 29 deletions controller/officials.controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,39 +207,13 @@ func (h Handler) GetFormsByOfficialID(c *gin.Context) {
}

func (h Handler) VerifyUser(c *gin.Context) {
// Fetch the session object and read the userID
sessionContainer := session.GetSessionFromRequestContext(c.Request.Context())
userId := sessionContainer.GetUserID()

// Find the user by auth_id
var Official models.Officials
var User models.Officials
if result := h.DB.Where("auth_id = ?", userId).First(&Official); result.Error != nil {
c.JSON(http.StatusNotFound, gin.H{
"status": http.StatusNotFound,
"error": "User not found",
"ok": false,
})
return
}

// Get the role of the user

// Check if the user's role matches the required role
// if Official.Role != "AE" {
// c.JSON(http.StatusForbidden, gin.H{
// "status": http.StatusForbidden,
// "error": "Unauthorized access",
// "ok": false,
// })
// return
// }

// Get the user ID from the request parameters
id := c.Param("id")
ID := c.Query("id")

// Find the user by ID
if result := h.DB.Find(&User, id); result.Error != nil {
if result := h.DB.Find(&User, ID); result.Error != nil {
c.JSON(http.StatusNotFound, gin.H{
"status": http.StatusNotFound,
"error": "User not found",
Expand All @@ -260,6 +234,26 @@ func (h Handler) VerifyUser(c *gin.Context) {
"status": http.StatusOK,
"response": "User verified successfully",
"ok": true,
"data": Official,
"data": User,
})
}

func (h Handler) GetOfficialByID(c *gin.Context) {
var user models.Officials
ID := c.Param("id")
if result := h.DB.Find(&user, ID); result.Error != nil {
c.JSON(http.StatusUnprocessableEntity, gin.H{
"status": http.StatusInternalServerError,
"error": "couldn't find the data",
"ok": false,
})
return
}

c.JSON(http.StatusOK, gin.H{
"status": http.StatusOK,
"response": "Data read successfully",
"ok": true,
"data": user,
})
}
7 changes: 4 additions & 3 deletions db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"log"
"os"

"github.com/sreehari2003/kseb/models"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)
Expand All @@ -16,14 +15,16 @@ func Init() *gorm.DB {
db_name := os.Getenv("DB_NAME")
db_port_host := os.Getenv("DB_PORT_HOST")
dbURL := "postgresql://" + db_user + ":" + pass + db_port_host + db_name
db, err := gorm.Open(postgres.Open(dbURL), &gorm.Config{})
fmt.Println(dbURL)
db, err := gorm.Open(postgres.Open(dbURL), &gorm.Config{
DisableForeignKeyConstraintWhenMigrating: true,
})

if err != nil {
log.Fatalln(err)
}
// if db connection fails this wont run
fmt.Println("server connected with db successfully")
db.AutoMigrate(&models.Issue{}, &models.Officials{}, &models.Form{})

return db
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ require (
require (
github.com/bytedance/sonic v1.8.8 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/pgx/v5 v5.3.0 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/klauspost/cpuid/v2 v2.2.4 // indirect
github.com/nedpals/postgrest-go v0.1.3 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
golang.org/x/arch v0.3.0 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
Expand Down
7 changes: 7 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,10 @@ github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5y
github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw=
github.com/golang/protobuf v1.5.0 h1:LUVKkCeviFUMKqHa4tXIIij/lbhnMbP7Fn5wKdKkRh4=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
Expand Down Expand Up @@ -158,6 +161,10 @@ github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjY
github.com/mvrilo/go-redoc v0.1.2 h1:qQWTa9YPbE3jcq9XwYRVy0rOUSSskO0cteVyN9tIUYA=
github.com/mvrilo/go-redoc v0.1.2/go.mod h1:GIPJDv4/44PLH8WGb8o5/6xS10hoa4tF6eTUJTd2uTo=
github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32/go.mod h1:9wM+0iRr9ahx58uYLpLIr5fm8diHn0JbqRycJi6w0Ms=
github.com/nedpals/postgrest-go v0.1.3 h1:ZC3aPPx9rDTWQWzvnWI60lJWjAqgCCD/U6hcHp3NL0w=
github.com/nedpals/postgrest-go v0.1.3/go.mod h1:RGinB2OXsnGLcZMu5avS0U+b9npyZmk+ecK74UDi/xY=
github.com/nedpals/supabase-go v0.3.0 h1:qeLOiW758NZb/eC1SKxUuVeONTT0FrGDtHGB0U4sfkI=
github.com/nedpals/supabase-go v0.3.0/go.mod h1:rscvF0tYsD6gJYKMYZy8e6YWspVIaGnBb13PlU6HFcU=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/nyaruka/phonenumbers v1.0.73 h1:bP2WN8/NUP8tQebR+WCIejFaibwYMHOaB7MQVayclUo=
github.com/nyaruka/phonenumbers v1.0.73/go.mod h1:3aiS+PS3DuYwkbK3xdcmRwMiPNECZ0oENH8qUT1lY7Q=
Expand Down
44 changes: 22 additions & 22 deletions middlewares/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,30 +34,30 @@ func Cors() gin.HandlerFunc {
}

func VerifyUser(h controller.Handler) gin.HandlerFunc {
var official models.Officials
return func(c *gin.Context) {
// Get the user ID from the query parameter
id := c.Query("id")
// Retrieve the user by ID from the database
if result := h.DB.Find(&official, id); result.Error != nil {
c.JSON(http.StatusNotFound, gin.H{
"status": http.StatusNotFound,
"error": "User not found",
"ok": false,
})
c.Abort()
return
}

// Fetching the session object and reading the userID
// sessionContainer := session.GetSessionFromRequestContext(c.Request.Context())
// userId := sessionContainer.GetUserID()
var official models.Officials
// if result := h.DB.Where("auth_id = ?", userId).First(&official); result.Error != nil {
// c.JSON(http.StatusNotFound, gin.H{
// "status": http.StatusNotFound,
// "error": "couldn't find the user",
// "ok": false,
// })
// return
// }
// Check if the user is verified
if official.IsVerified == false {
c.JSON(http.StatusForbidden, gin.H{
"status": http.StatusForbidden,
"error": "Unauthoried Person",
"ok": false,
})
c.Abort()
return
}
// if official.IsVerified == false {
// c.JSON(http.StatusForbidden, gin.H{
// "status": http.StatusForbidden,
// "error": "Unauthoried Person",
// "ok": false,
// })
// c.Abort()
// return
// }
// Pass the user object to the next handler
c.Set("user", official)
c.Next()
Expand Down
Loading
Loading