-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'spo-iitk:main' into pvf
- Loading branch information
Showing
9 changed files
with
284 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package student | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/spo-iitk/ras-backend/mail" | ||
"github.com/spo-iitk/ras-backend/middleware" | ||
"github.com/spo-iitk/ras-backend/util" | ||
) | ||
|
||
type postClarificationRequest struct { | ||
Clarification string `json:"clarification" binding:"required"` | ||
} | ||
|
||
func postClarificationHandler(mail_channel chan mail.Mail) gin.HandlerFunc { | ||
return func(ctx *gin.Context) { | ||
sid, err := util.ParseUint(ctx.Param("sid")) | ||
if err != nil { | ||
ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) | ||
return | ||
} | ||
|
||
var student Student | ||
err = getStudentByID(ctx, &student, sid) | ||
if err != nil { | ||
ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) | ||
return | ||
} | ||
|
||
var request postClarificationRequest | ||
err = ctx.ShouldBindJSON(&request) | ||
if err != nil { | ||
ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) | ||
return | ||
} | ||
|
||
mail_channel <- mail.GenerateMail(student.IITKEmail, "Asking Clarification", request.Clarification) | ||
mail_channel <- mail.GenerateMail( | ||
middleware.GetUserID(ctx), | ||
"Clarification Requested from "+student.Name, | ||
"Dear "+middleware.GetUserID(ctx)+ | ||
"Clarification was requested from "+student.Name+ | ||
"\nSent Mail:\n"+ | ||
request.Clarification) | ||
|
||
ctx.JSON(http.StatusOK, gin.H{"status": "Clarification Mail sent"}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
package student | ||
|
||
import ( | ||
"net/http" | ||
"strconv" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/sirupsen/logrus" | ||
"github.com/spo-iitk/ras-backend/mail" | ||
"github.com/spo-iitk/ras-backend/middleware" | ||
"github.com/spo-iitk/ras-backend/util" | ||
) | ||
|
||
func getDocumentHandler(ctx *gin.Context) { | ||
sid, err := strconv.ParseUint(ctx.Param("sid"), 10, 32) | ||
if err != nil { | ||
ctx.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()}) | ||
return | ||
} | ||
|
||
var documents []StudentDocument | ||
err = getDocumentsByStudentID(ctx, &documents, uint(sid)) | ||
if err != nil { | ||
ctx.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()}) | ||
return | ||
} | ||
|
||
ctx.JSON(http.StatusOK, documents) | ||
} | ||
|
||
type putDocumentVerifyRequest struct { | ||
Verified bool `json:"verified"` | ||
} | ||
|
||
func putDocumentVerifyHandler(mail_channel chan mail.Mail) gin.HandlerFunc { | ||
return func(ctx *gin.Context) { | ||
did, err := util.ParseUint(ctx.Param("docid")) | ||
if err != nil { | ||
ctx.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()}) | ||
return | ||
} | ||
|
||
var req putDocumentVerifyRequest | ||
|
||
err = ctx.BindJSON(&req) | ||
if err != nil { | ||
ctx.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()}) | ||
return | ||
} | ||
|
||
user := middleware.GetUserID(ctx) | ||
|
||
var document StudentDocument | ||
err = getDocumentByID(ctx, &document, uint(did)) | ||
if err != nil { | ||
ctx.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()}) | ||
return | ||
} | ||
|
||
ok, err := updateDocumentVerify(ctx, did, req.Verified, user) | ||
if err != nil { | ||
ctx.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()}) | ||
return | ||
} | ||
|
||
if !ok { | ||
ctx.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Could not verify document"}) | ||
return | ||
} | ||
|
||
var student Student | ||
err = getStudentByID(ctx, &student, document.StudentID) | ||
if err != nil { | ||
ctx.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()}) | ||
return | ||
} | ||
|
||
logrus.Infof("%v verified document with id %d, changed state to %v", user, did, req.Verified) | ||
|
||
// Constructing the email message | ||
msg := "Dear " + student.Name + "\n\n" | ||
msg += "Your document (" + document.Type + ") with id " + strconv.Itoa(int(did)) + " has been " | ||
if req.Verified { | ||
msg += "ACCEPTED." | ||
} else { | ||
msg += "REJECTED." | ||
} | ||
msg += "\n\nBest regards,\nYour Verification Team" | ||
|
||
mail_channel <- mail.GenerateMail(student.PersonalEmail, "Action taken on document", msg) | ||
|
||
ctx.JSON(http.StatusOK, gin.H{"status": true}) | ||
} | ||
} | ||
|
||
|
||
func getAllDocumentHandler(ctx *gin.Context) { | ||
var documents []StudentDocument | ||
err := getAllDocuments(ctx, &documents) | ||
if err != nil { | ||
ctx.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()}) | ||
return | ||
} | ||
|
||
ctx.JSON(http.StatusOK, documents) | ||
} | ||
|
||
func getAllDocumentHandlerByType(ctx *gin.Context) { | ||
docType := ctx.Param("type") | ||
var documents []StudentDocument | ||
err := getDocumentsByType(ctx, &documents, docType) | ||
if err != nil { | ||
ctx.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()}) | ||
return | ||
} | ||
|
||
ctx.JSON(http.StatusOK, documents) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package student | ||
|
||
import ( | ||
"github.com/gin-gonic/gin" | ||
"gorm.io/gorm/clause" | ||
) | ||
|
||
|
||
func saveDocument(ctx *gin.Context, document *StudentDocument) error { | ||
tx := db.WithContext(ctx).Save(document) | ||
return tx.Error | ||
} | ||
|
||
func getDocumentsByStudentID(ctx *gin.Context, documents *[]StudentDocument, studentID uint) error { | ||
tx := db.WithContext(ctx).Where("student_id = ?", studentID).Find(documents) | ||
return tx.Error | ||
} | ||
|
||
func getDocumentByID(ctx *gin.Context, document *StudentDocument, docID uint) error { | ||
tx := db.WithContext(ctx).First(document, docID) | ||
return tx.Error | ||
} | ||
|
||
func getAllDocuments(ctx *gin.Context, documents *[]StudentDocument) error { | ||
tx := db.WithContext(ctx).Find(documents) | ||
return tx.Error | ||
} | ||
|
||
func getDocumentsByType(ctx *gin.Context, documents *[]StudentDocument, docType string) error { | ||
tx := db.WithContext(ctx).Where("type = ?", docType).Find(documents) | ||
return tx.Error | ||
} | ||
|
||
func updateDocumentVerify(ctx *gin.Context, docid uint, verified bool, user string) (bool, error){ | ||
var document StudentDocument | ||
tx := db.WithContext(ctx).Model(&document).Clauses(clause.Returning{}).Where("id = ?", docid).Update("verified", verified).Update("action_taken_by", user) | ||
return tx.RowsAffected == 1, tx.Error | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package student | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/sirupsen/logrus" | ||
"github.com/spo-iitk/ras-backend/middleware" | ||
) | ||
|
||
func postStudentDocumentHandler(ctx *gin.Context) { | ||
var document StudentDocument | ||
email := middleware.GetUserID(ctx) | ||
|
||
if err := ctx.ShouldBindJSON(&document); err != nil { | ||
ctx.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()}) | ||
return | ||
} | ||
var student Student | ||
err := getStudentByEmail(ctx, &student, email) | ||
if err != nil { | ||
ctx.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()}) | ||
return | ||
} | ||
|
||
document.StudentID = student.ID | ||
err = saveDocument(ctx, &document) | ||
if err != nil { | ||
ctx.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()}) | ||
return | ||
} | ||
|
||
logrus.Infof("Document for student %d uploaded", student.ID) | ||
ctx.JSON(http.StatusOK, gin.H{"status": "Successfully uploaded document"}) | ||
} | ||
|
||
func getStudentDocumentHandler(ctx *gin.Context) { | ||
email := middleware.GetUserID(ctx) | ||
var student Student | ||
err := getStudentByEmail(ctx, &student, email) | ||
if err != nil { | ||
ctx.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()}) | ||
return | ||
} | ||
|
||
var documents []StudentDocument | ||
err = getDocumentsByStudentID(ctx, &documents, student.ID) | ||
if err != nil { | ||
ctx.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()}) | ||
return | ||
} | ||
|
||
ctx.JSON(http.StatusOK, documents) | ||
} |