diff --git a/backend/handlers.go b/backend/handlers.go index 58fe9f87..d6c91146 100644 --- a/backend/handlers.go +++ b/backend/handlers.go @@ -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) @@ -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, "_") diff --git a/backend/main.go b/backend/main.go index 4461e0bf..d6ebf502 100644 --- a/backend/main.go +++ b/backend/main.go @@ -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{ diff --git a/backend/pkg/db/db.go b/backend/pkg/db/db.go index 435bee14..e6a4326f 100644 --- a/backend/pkg/db/db.go +++ b/backend/pkg/db/db.go @@ -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, diff --git a/backend/pkg/db/query.go b/backend/pkg/db/query.go index 1fb83194..4f3ccdad 100644 --- a/backend/pkg/db/query.go +++ b/backend/pkg/db/query.go @@ -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)