Skip to content

Commit

Permalink
feat: add similar papers api
Browse files Browse the repository at this point in the history
Signed-off-by: Rajiv Harlalka <[email protected]>
  • Loading branch information
rajivharlalka committed Oct 7, 2024
1 parent 2af2724 commit 4177635
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 1 deletion.
40 changes: 40 additions & 0 deletions backend/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,46 @@ func HandleProfile(w http.ResponseWriter, r *http.Request) {
sendResponse(w, http.StatusOK, map[string]string{"username": approverUsername})
}

func HandleFetchSimilarPapers(w http.ResponseWriter, r *http.Request) {
queryParams := r.URL.Query()
db := db.GetDB()

var parsedParams models.QuestionPaper
// parse Query Params
if !queryParams.Has("course_code") {
sendErrorResponse(w, http.StatusBadRequest, "query parameter course_code is mandatory", nil)
config.Get().Logger.Errorf("HandleSimilarPapers: query param 'course_code' not received")
return
}
parsedParams.CourseCode = queryParams.Get("course_code")
if queryParams.Has("year") {
year, err := strconv.Atoi(queryParams.Get("year"))
if err != nil {
sendErrorResponse(w, http.StatusBadRequest, "query parameter year has to be integer", nil)
config.Get().Logger.Errorf("HandleSimilarPapers: query param 'year' received a non-int value")
return
}
parsedParams.Year = &year
}

if queryParams.Has("semester") {
parsedParams.Semester = queryParams.Get("semester")
}

if queryParams.Has("exam") {
parsedParams.Exam = queryParams.Get("exam")
}
fmt.Print(parsedParams)
questionPapers, err := db.GetQuestionPaperWithExactMatch(&parsedParams)
if err != nil {
sendErrorResponse(w, http.StatusInternalServerError, "could not fetch question papers, try again later", nil)
config.Get().Logger.Errorf("HandleFetchSimilarPapers: Error fetching papers from db: %+v", err.Error())
return
}

sendResponse(w, http.StatusOK, questionPapers)
}

func HandleDeletePaper(w http.ResponseWriter, r *http.Request) {
approverUsername := r.Context().Value(CLAIMS_KEY).(*Claims).Username
db := db.GetDB()
Expand Down
1 change: 1 addition & 0 deletions backend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func main() {
http.Handle("POST /approve", JWTMiddleware(http.HandlerFunc(HandleApprovePaper)))
http.Handle("POST /delete", JWTMiddleware(http.HandlerFunc(HandleDeletePaper)))
http.Handle("GET /profile", JWTMiddleware(http.HandlerFunc(HandleProfile)))
http.Handle("GET /similar", JWTMiddleware(http.HandlerFunc(HandleFetchSimilarPapers)))

logger := config.Get().Logger
c := cors.New(cors.Options{
Expand Down
42 changes: 42 additions & 0 deletions backend/pkg/db/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,45 @@ func (db *db) MarkPaperAsSoftDeletedAndUnApprove(qpID int, approvedBy string) er
}
return nil
}

func (db *db) GetQuestionPaperWithExactMatch(paper *models.QuestionPaper) ([]models.QuestionPaper, error) {
query := "SELECT course_code,course_name,year,exam,filelink,id,from_library,semester from iqps where course_code = @course_code"
params := pgx.NamedArgs{
"course_code": paper.CourseCode,
}

if paper.Exam != "" {
query += " and exam = @exam"
params["exam"] = paper.Exam
}

if paper.Year != nil {
query += " and year = @year"
params["year"] = paper.Year
}

if paper.Semester != "" {
query += " and semester = @semester"
params["semester"] = paper.Semester
}
rows, err := db.Db.Query(context.Background(), query, params)
if err != nil {
config.Get().Logger.Errorf("GetQuestionPaperWithExactMatch: Could not fetch all question papers, error: %+v", err.Error())
return nil, errors.New("unable to fetch question paper, try again later")
}
defer rows.Close()

var qps []models.QuestionPaper = make([]models.QuestionPaper, 0)
for rows.Next() {
qp := models.QuestionPaper{}
err := rows.Scan(&qp.CourseCode, &qp.CourseName, &qp.Year, &qp.Exam, &qp.FileLink, &qp.ID, &qp.FromLibrary, &qp.Semester)
if err != nil {
config.Get().Logger.Errorf("FetchAllQuestionpapers: Error Scanning Paper in Struct, error: %+v", err.Error())
return nil, errors.New("unable to fetch question paper, try again later")
}
qp.FileLink = fmt.Sprintf("%s/%s", config.Get().StaticFilesUrl, qp.FileLink)
qps = append(qps, qp)
}

return qps, nil
}
2 changes: 1 addition & 1 deletion backend/pkg/models/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ type QuestionPaper struct {
ID int `json:"id"`
CourseCode string `json:"course_code"`
CourseName string `json:"course_name"`
Year int `json:"year"`
Year *int `json:"year"`
Exam string `json:"exam"`
FileLink string `json:"filelink"`
FromLibrary bool `json:"from_library"`
Expand Down

0 comments on commit 4177635

Please sign in to comment.