Skip to content

Commit

Permalink
Merge pull request #80 from metakgp/route/list_papers
Browse files Browse the repository at this point in the history
list Unapproved Question Papers
  • Loading branch information
harshkhandeparkar authored Aug 26, 2024
2 parents 909197c + 2f38558 commit dde0b24
Showing 1 changed file with 38 additions and 29 deletions.
67 changes: 38 additions & 29 deletions backend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@ type contextKey string
const claimsKey = contextKey("claims")

type QuestionPaper struct {
ID int `json:"id"`
CourseCode string `json:"course_code"`
CourseName string `json:"course_name"`
Year int `json:"year"`
Exam string `json:"exam"`
FileLink string `json:"filelink"`
FromLibrary bool `json:"from_library"`
UploadTimestamp string `json:"upload_timestamp"`
ApproveStatus bool `json:"approve_status"`
CourseDetails string `json:"course_details"`
ID int `json:"id,omitempty"`
CourseCode string `json:"course_code,omitempty"`
CourseName string `json:"course_name,omitempty"`
Year int `json:"year,omitempty"`
Exam string `json:"exam,omitempty"`
FileLink string `json:"filelink,omitempty"`
FromLibrary bool `json:"from_library,omitempty"`
UploadTimestamp string `json:"upload_timestamp,omitempty"`
ApproveStatus bool `json:"approve_status,omitempty"`
CourseDetails string `json:"course_details,omitempty"`
}

type uploadEndpointRes struct {
Expand Down Expand Up @@ -189,6 +189,32 @@ func search(w http.ResponseWriter, r *http.Request) {
}
}

func listUnapprovedPapers(w http.ResponseWriter, r *http.Request) {
rows, err := db.Query("SELECT course_code, course_name, year, exam, filelink FROM qp WHERE approve_status = false ORDER BY upload_timestamp ASC")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer rows.Close()

var qps []QuestionPaper = make([]QuestionPaper, 0)
for rows.Next() {
qp := QuestionPaper{}
err := rows.Scan(&qp.CourseCode, &qp.CourseName, &qp.Year, &qp.Exam, &qp.FileLink)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
qps = append(qps, qp)
}
http.Header.Add(w.Header(), "content-type", "application/json")
err = json.NewEncoder(w).Encode(&qps)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}

func upload(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
Expand Down Expand Up @@ -499,23 +525,6 @@ func JWTMiddleware(handler http.Handler) http.Handler {
})
}

func getClaims(r *http.Request) jwt.MapClaims {
if claims, ok := r.Context().Value(claimsKey).(jwt.MapClaims); ok {
return claims
}
return nil
}

// func protectedRoute(w http.ResponseWriter, r *http.Request) {
// claims := getClaims(r)

// if claims != nil {
// fmt.Fprintf(w, "Hello, %s", claims["username"])
// } else {
// http.Error(w, "No claims found", http.StatusUnauthorized)
// }
// }

func CheckError(err error) {
if err != nil {
panic(err)
Expand Down Expand Up @@ -552,7 +561,7 @@ func LoadGhEnv() {
}

func main() {
err := godotenv.Load()
godotenv.Load()
host := os.Getenv("DB_HOST")
port, err := strconv.Atoi(os.Getenv("DB_PORT"))
CheckError(err)
Expand Down Expand Up @@ -587,7 +596,7 @@ func main() {
http.HandleFunc("/library", library)
http.HandleFunc("POST /upload", upload)
http.HandleFunc("POST /oauth", GhAuth)
// http.Handle("/protected", JWTMiddleware(http.HandlerFunc(protectedRoute)))
http.Handle("GET /unapproved", JWTMiddleware(http.HandlerFunc(listUnapprovedPapers)))

c := cors.New(cors.Options{
AllowedOrigins: []string{"https://qp.metakgp.org", "http://localhost:3000"},
Expand Down

0 comments on commit dde0b24

Please sign in to comment.