Skip to content

Commit

Permalink
disapprove papers endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
rajivharlalka committed Oct 1, 2024
1 parent 28f14e0 commit d479c95
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 5 deletions.
25 changes: 24 additions & 1 deletion backend/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func HandleApprovePaper(w http.ResponseWriter, r *http.Request) {
// log line to help which entry was made by deleting which paper for recovery
config.Get().Logger.Infof("HandleApprovePaper: Id %d added against Id %d", id, qpDetails.ID)

err = db.MarkPaperAsSoftDeletedAndUnApprove(qpDetails.ID)
err = db.MarkPaperAsSoftDeletedAndUnApprove(qpDetails.ID, approverUsername)
if err != nil {
utils.DeleteFile(destFile)
sendErrorResponse(w, http.StatusInternalServerError, "error updating paper details!", nil)
Expand Down Expand Up @@ -352,6 +352,29 @@ func HandleFileUpload(w http.ResponseWriter, r *http.Request) {
sendResponse(w, http.StatusAccepted, response)
}

func HandleDeletePaper(w http.ResponseWriter, r *http.Request) {
approverUsername := r.Context().Value(CLAIMS_KEY).(*Claims).Username
db := db.GetDB()
var requestBody struct {
Id int `json:"id"`
}
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&requestBody); err != nil {
sendErrorResponse(w, http.StatusInternalServerError, "Could not find Question Paper, Try Later!", nil)
config.Get().Logger.Errorf("HandleDeletePaper: could not approve paper, invalid Body: %+v", err.Error())
return
}

err := db.MarkPaperAsSoftDeletedAndUnApprove(requestBody.Id, approverUsername)
if err != nil {
sendErrorResponse(w, http.StatusInternalServerError, "error updating paper details!", nil)
config.Get().Logger.Errorf("HandleDeletePaper: error soft-deleting paper: %+v PaperDetails: %d", err.Error(), requestBody.Id)
return
}
config.Get().Logger.Infof("HandleDeletePaper: disapproved paper: %d", requestBody.Id)
sendResponse(w, http.StatusOK, httpResp{Message: "File Disapproved successfully"})
}

func populateDB(filename string) error {
db := db.GetDB()
qpData := strings.Split(filename, "_")
Expand Down
1 change: 1 addition & 0 deletions backend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func main() {
http.Handle("GET /unapproved", JWTMiddleware(http.HandlerFunc(ListUnapprovedPapers)))
http.Handle("GET /all", JWTMiddleware(http.HandlerFunc(ListAllPapers)))
http.Handle("POST /approve", JWTMiddleware(http.HandlerFunc(HandleApprovePaper)))
http.Handle("POST /delete", JWTMiddleware(http.HandlerFunc(HandleDeletePaper)))

logger := config.Get().Logger
c := cors.New(cors.Options{
Expand Down
2 changes: 1 addition & 1 deletion backend/pkg/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const init_db = `CREATE TABLE IF NOT EXISTS iqps (
course_code TEXT NOT NULL DEFAULT '',
course_name TEXT NOT NULL,
year INTEGER NOT NULL,
exam TEXT CHECK (exam IN ('midsem', 'endsem') OR exam = ''),
exam TEXT,
filelink TEXT NOT NULL,
from_library BOOLEAN DEFAULT FALSE,
upload_timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
Expand Down
7 changes: 4 additions & 3 deletions backend/pkg/db/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,11 @@ func (db *db) InsertNewPaper(qpDetails *models.QuestionPaper) (int, error) {
return id, nil
}

func (db *db) MarkPaperAsSoftDeletedAndUnApprove(qpID int) error {
query := "UPDATE iqps set approve_status=false, is_deleted = true where id=@qpID and is_deleted=false"
func (db *db) MarkPaperAsSoftDeletedAndUnApprove(qpID int, approvedBy string) error {
query := "UPDATE iqps set approve_status=false, is_deleted = true, approved_by=@approved_by where id=@qpID and is_deleted=false"
params := pgx.NamedArgs{
"qpID": qpID,
"qpID": qpID,
"approved_by": approvedBy,
}

ct, err := db.Db.Exec(context.Background(), query, params)
Expand Down

0 comments on commit d479c95

Please sign in to comment.