Skip to content

Commit

Permalink
added new verification server, routes and its handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
AkshatGupta15 committed Jul 3, 2024
1 parent 0f5329c commit c1fabf6
Show file tree
Hide file tree
Showing 12 changed files with 306 additions and 12 deletions.
66 changes: 63 additions & 3 deletions application/db.pvf.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,28 @@
package application

import "github.com/gin-gonic/gin"
import (
"github.com/gin-gonic/gin"
"gorm.io/gorm/clause"
)

func createPVF(ctx *gin.Context, pvf *PVF) error {
tx := db.WithContext(ctx).Create(pvf)
return tx.Error
}

func fetchPvfForStudent(ctx *gin.Context, sid uint, rid uint, jps *[]PVF) error {
func fetchPVF(ctx *gin.Context, pid uint, jp *PVF) error {
tx := db.WithContext(ctx).Where("id = ?", pid).First(jp)
return tx.Error
}

func updatePVF(ctx *gin.Context, jp *PVF) error {
tx := db.WithContext(ctx).Where("id = ?", jp.ID).Updates(jp)
return tx.Error
}

func fetchAllPvfForStudent(ctx *gin.Context, sid uint, rid uint, jps *[]PVF) error {
tx := db.WithContext(ctx).
// Where("student_recruitment_cycle_id = ? AND recruitment_cycle_id = ?", sid, rid).
Where("student_recruitment_cycle_id = ? AND recruitment_cycle_id = ?", sid, rid).
Select(
"id",
"company_university_name",
Expand All @@ -20,8 +33,55 @@ func fetchPvfForStudent(ctx *gin.Context, sid uint, rid uint, jps *[]PVF) error
"mentor_designation",
"mentor_email",
"is_verified",
"file_name",
).
Order("id ASC").
Find(jps)
return tx.Error
}
func fetchPvfForStudent(ctx *gin.Context, sid uint, rid uint, pid uint, jps *[]PVF) error {

Check failure on line 42 in application/db.pvf.go

View workflow job for this annotation

GitHub Actions / lint

func `fetchPvfForStudent` is unused (unused)
tx := db.WithContext(ctx).
Where("student_recruitment_cycle_id = ? AND recruitment_cycle_id = ? AND id = ?", sid, rid, pid).
Select(
"id",
"company_university_name",
"role",
"duration",
"description",
"mentor_name",
"mentor_designation",
"mentor_email",
"is_verified",
"file_name",
).
Find(jps)
return tx.Error
}

func fetchPvfForVerification(ctx *gin.Context, id uint, rid uint, jps *PVF) error {
tx := db.WithContext(ctx).
Where("id = ? AND recruitment_cycle_id = ?", id, rid).
Select(
"id",
"company_university_name",
"role",
"duration",
"description",
"mentor_name",
"mentor_designation",
"mentor_email",
"is_verified",
"is_approved",
"file_name",
).
Find(jps)
return tx.Error
}

func verifyPvf(ctx *gin.Context, pvf *PVF) (bool, error) {

Check failure on line 81 in application/db.pvf.go

View workflow job for this annotation

GitHub Actions / lint

func `verifyPvf` is unused (unused)
tx := db.WithContext(ctx).Model(&pvf).
Clauses(clause.Returning{}).
Where("id = ?", pvf.ID).
Updates(map[string]interface{}{"is_verified": pvf.IsVerified})
return tx.RowsAffected > 0, tx.Error
}
1 change: 1 addition & 0 deletions application/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,5 @@ type PVF struct {
IsApproved sql.NullBool `json:"is_approved" gorm:"index;default:NULL"`
IsVerified sql.NullBool `json:"is_verified" gorm:"index;default:NULL"`
RecruitmentCycleID uint `json:"recruitment_cycle_id" gorm:"index;->;<-:create"`
FileName string `json:"filename"`
}
13 changes: 12 additions & 1 deletion application/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ func StudentRouter(mail_channel chan mail.Mail, r *gin.Engine) {
student.GET("/proforma/:pid/event", getEventsByProformaForStudentHandler)

student.POST("/pvf", postPvfForStudentHandler)
student.GET("/pvf", getPvfForStudentHandler)
student.GET("/pvf", getAllPvfForStudentHandler)
student.GET("/pvf/:pid", getPvfForStudentHandler)

student.GET("/opening", getProformasForEligibleStudentHandler)
student.GET("/opening/:pid", getApplicationHandler)
Expand Down Expand Up @@ -99,3 +100,13 @@ func CompanyRouter(r *gin.Engine) {
company.GET("/event/:eid/student", getStudentsByEventForCompanyHandler)
}
}

func PvfVerificationRouter(r *gin.Engine) {
pvf := r.Group("/api/verification/application/rc/:rid")
pvf.Use()
{
pvf.GET("/pvf/:pid", getPvfForVerificationHandler)
pvf.PUT("pvf/:pid/verify", verifyPvfHandler)
pvf.PUT("/pvf", putPVFHandler)
}
}
26 changes: 23 additions & 3 deletions application/student.pvf.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ func postPvfForStudentHandler(ctx *gin.Context) {
}
user := middleware.GetUserID(ctx)

logrus.Infof("%v created \a proforma with id %d", user, pvf.ID)
logrus.Infof("%v created \a pvf with id %d", user, pvf.ID)
ctx.JSON(http.StatusOK, gin.H{"pid": pvf.ID})

}

func getPvfForStudentHandler(ctx *gin.Context) {
func getAllPvfForStudentHandler(ctx *gin.Context) {
sid := getStudentRCID(ctx)
if sid == 0 {
ctx.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "SRCID not found"})
Expand All @@ -46,7 +46,7 @@ func getPvfForStudentHandler(ctx *gin.Context) {
return
}
var jps []PVF
err = fetchPvfForStudent(ctx, sid, rid, &jps)
err = fetchAllPvfForStudent(ctx, sid, rid, &jps)
if err != nil {
ctx.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
Expand All @@ -55,3 +55,23 @@ func getPvfForStudentHandler(ctx *gin.Context) {
ctx.JSON(http.StatusOK, jps)

}
func getPvfForStudentHandler(ctx *gin.Context) {
rid, err := util.ParseUint(ctx.Param("rid"))
if err != nil {
ctx.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
id, err := util.ParseUint(ctx.Param("pid"))
if err != nil {
ctx.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
var jps PVF
err = fetchPvfForVerification(ctx, id, rid, &jps)
if err != nil {
ctx.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}

ctx.JSON(http.StatusOK, jps)
}
1 change: 1 addition & 0 deletions application/util.pvf_verification.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package application
120 changes: 120 additions & 0 deletions application/verify.pvf.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package application

import (
"net/http"

"github.com/gin-gonic/gin"
"github.com/spo-iitk/ras-backend/util"
)

func getPvfForVerificationHandler(ctx *gin.Context) {
rid, err := util.ParseUint(ctx.Param("rid"))
if err != nil {
ctx.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
id, err := util.ParseUint("1") // id to be upadated
if err != nil {
ctx.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
var jps PVF
err = fetchPvfForVerification(ctx, id, rid, &jps)
if err != nil {
ctx.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}

ctx.JSON(http.StatusOK, jps)
}

func verifyPvfHandler(ctx *gin.Context) {
// var verifyPvfRequest PVF
// if err := ctx.ShouldBindJSON(&verifyPvfRequest); err != nil {
// ctx.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()})
// return
// }
// pid, err := util.ParseUint(ctx.Param("pid"))

// if err != nil {
// ctx.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()})
// return
// }

// verifyPvfRequest.ID = pid
// updated, err := verifyPvf(ctx, &verifyPvfRequest)
// if err != nil {
// ctx.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()})
// return
// }

// if !updated {
// ctx.AbortWithStatusJSON(http.StatusNotFound, gin.H{"error": "PVF not found"})
// return
// }

// if verifyPvfRequest.IsVerified {
// logrus.Infof("A PVF with id %d is verified", verifyPvfRequest.ID)
// ctx.JSON(http.StatusOK, gin.H{"status": "Successfully verified"})
// } else {
// logrus.Infof("A PVF with id %d is unverified", verifyPvfRequest.ID)
// ctx.JSON(http.StatusOK, gin.H{"status": "Successfully unverified"})
// }

}

func putPVFHandler(ctx *gin.Context) {
var jp PVF

err := ctx.ShouldBindJSON(&jp)
if err != nil {
ctx.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}

if jp.ID == 0 {
ctx.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "id is required"})
return
}

var oldJp PVF
err = fetchPVF(ctx, jp.ID, &oldJp)
if err != nil {
ctx.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}

// jp.ActionTakenBy = middleware.GetUserID(ctx)

// publishNotice := oldJp.Deadline == 0 && jp.Deadline != 0

err = updatePVF(ctx, &jp)
if err != nil {
ctx.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
ctx.JSON(http.StatusOK, gin.H{"status": "Updated PVF with id " + util.ParseString(jp.ID)})
// user := middleware.GetUserID(ctx)

// logrus.Infof("%v edited a proforma with id %d", user, jp.ID)

// if publishNotice {
// logrus.Infof("%v published a proforma with id %d", user, jp.ID)

// err = rc.CreateNotice(ctx, oldJp.RecruitmentCycleID, &rc.Notice{
// Title: fmt.Sprintf("[%s] | New Job Opening for %s", jp.CompanyName, jp.Profile),
// Description: fmt.Sprintf(
// "A new opening has been created for the profile of %s in the company %s",
// jp.Profile, jp.CompanyName),
// Tags: fmt.Sprintf("opening,%s,%s", jp.Role, jp.CompanyName),
// })
// if err != nil {
// ctx.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()})
// return
// }

// ctx.JSON(http.StatusOK, gin.H{"status": "Proforma with id " + util.ParseString(jp.ID) + " has been published"})
// } else {
// ctx.JSON(http.StatusOK, gin.H{"status": "Updated proforma with id " + util.ParseString(jp.ID)})
// }
}
3 changes: 3 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ func main() {
g.Go(func() error {
return adminCompanyServer().ListenAndServe()
})
g.Go(func() error {
return verificationServer().ListenAndServe()
})

log.Println("Starting Server...")
if err := g.Wait(); err != nil {
Expand Down
31 changes: 31 additions & 0 deletions cmd/verification.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package main

import (
"fmt"
"net/http"

"github.com/gin-gonic/gin"
"github.com/spf13/viper"
"github.com/spo-iitk/ras-backend/application"
"github.com/spo-iitk/ras-backend/middleware"
)

func verificationServer() *http.Server {
PORT := viper.GetString("PORT.VERIFICATION")
fmt.Print(PORT)
engine := gin.New()
engine.Use(middleware.CORS())
engine.Use(gin.CustomRecovery(recoveryHandler))
engine.Use(gin.Logger())

application.PvfVerificationRouter(engine)

server := &http.Server{
Addr: ":" + PORT,
Handler: engine,
ReadTimeout: readTimeout,
WriteTimeout: writeTimeout,
}

return server
}
3 changes: 2 additions & 1 deletion config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ PORT:
AUTH: 3475
STUDENT: 3480
COMPANY: 3485
ADMIN:
VERIFICATION: 3505
ADMIN:
RC: 3490
APP: 3492
COMPANY: 3495
Expand Down
3 changes: 3 additions & 0 deletions container/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,7 @@ server{
location /api/admin/student {
proxy_pass http://localhost:3500;
}
location /api/verification {
proxy_pass http://localhost:3505;
}
}
Loading

0 comments on commit c1fabf6

Please sign in to comment.