Skip to content

Commit

Permalink
fix: change question paper table to iqps
Browse files Browse the repository at this point in the history
Signed-off-by: Rajiv Harlalka <[email protected]>
  • Loading branch information
rajivharlalka committed Sep 9, 2024
1 parent 1ce6332 commit b3147ae
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 20 deletions.
18 changes: 8 additions & 10 deletions backend/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func HandleHealthCheck(w http.ResponseWriter, r *http.Request) {

func HandleQPYear(w http.ResponseWriter, r *http.Request) {
db := db.GetDB()
result := db.QueryRow(context.Background(), "SELECT MIN(year), MAX(year) FROM iqps_test")
result := db.QueryRow(context.Background(), "SELECT MIN(year), MAX(year) FROM iqps")
var minYear, maxYear int
err := result.Scan(&minYear, &maxYear)
if err != nil {
Expand All @@ -59,7 +59,7 @@ func HandleQPYear(w http.ResponseWriter, r *http.Request) {

func HandleLibraryPapers(w http.ResponseWriter, r *http.Request) {
db := db.GetDB()
rows, err := db.Query(context.Background(), "SELECT * FROM iqps_test WHERE from_library = 'true'")
rows, err := db.Query(context.Background(), "SELECT * FROM iqps WHERE from_library = 'true'")
if err != nil {
sendErrorResponse(w, http.StatusInternalServerError, "Could not Query Question Paper, Try Later!", nil)
return
Expand Down Expand Up @@ -93,16 +93,14 @@ func HandleQPSearch(w http.ResponseWriter, r *http.Request) {
// var params []interface{}
// params = append(params, course)
params := pgx.NamedArgs{
"query_text": course,
"match_count": 50,
"query_text": course,
}
exam := r.URL.Query().Get("exam")
if exam != "" {
query = fmt.Sprintf(`%s WHERE (exam = @exam OR exam = '')`, query)
params = pgx.NamedArgs{
"query_text": course,
"match_count": 50,
"exam": exam,
"query_text": course,
"exam": exam,
}
}

Expand Down Expand Up @@ -135,7 +133,7 @@ func HandleQPSearch(w http.ResponseWriter, r *http.Request) {

func ListUnapprovedPapers(w http.ResponseWriter, r *http.Request) {
db := db.GetDB()
rows, err := db.Query(context.Background(), "SELECT course_code, course_name, year, exam,filelink,id, from_library FROM iqps_test WHERE approve_status = false ORDER BY upload_timestamp ASC")
rows, err := db.Query(context.Background(), "SELECT course_code, course_name, year, exam,filelink,id, from_library FROM iqps WHERE approve_status = false ORDER BY upload_timestamp ASC")
if err != nil {
sendErrorResponse(w, http.StatusInternalServerError, err.Error(), nil)
return
Expand Down Expand Up @@ -205,8 +203,8 @@ func HandleFileUpload(w http.ResponseWriter, r *http.Request) {
defer file.Close()

fileType := fileHeader.Header.Get("Content-Type")
config.Get().Logger.Error("HandleFileUpload:", slog.String("Invalid file type", fileType))
if fileType != "application/pdf" {
config.Get().Logger.Error("HandleFileUpload:", slog.String("Invalid file type", fileType))
resp.Status = "failed"
resp.Description = "invalid file type. Only PDFs are supported"
response = append(response, resp)
Expand Down Expand Up @@ -298,7 +296,7 @@ func populateDB(filename string) error {
exam := qpData[3]
fromLibrary := false
fileLink := filepath.Join(config.Get().UploadedQPsPath, filename+".pdf")
query := "INSERT INTO qp (course_code, course_name, year, exam, filelink, from_library) VALUES ($1, $2, $3, $4, $5, $6);"
query := "INSERT INTO iqps (course_code, course_name, year, exam, filelink, from_library) VALUES ($1, $2, $3, $4, $5, $6);"

_, err := db.Exec(context.Background(), query, courseCode, courseName, year, exam, fileLink, fromLibrary)
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions backend/pkg/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ var (
mu sync.Mutex
)

const init_db = `CREATE TABLE IF NOT EXISTS iqps_test (
const init_db = `CREATE TABLE IF NOT EXISTS iqps (
id SERIAL PRIMARY KEY,
course_code TEXT NOT NULL DEFAULT '',
course_name TEXT NOT NULL,
Expand All @@ -28,9 +28,9 @@ const init_db = `CREATE TABLE IF NOT EXISTS iqps_test (
fts_course_details tsvector GENERATED ALWAYS AS (to_tsvector('english', course_code || ' ' || course_name)) stored
);
CREATE INDEX IF NOT EXISTS iqps_test_fts ON iqps_test USING gin (fts_course_details);
CREATE INDEX IF NOT EXISTS iqps_fts ON iqps USING gin (fts_course_details);
create index IF NOT EXISTS idx_course_name_trgm on iqps_test using gin (course_name gin_trgm_ops);
create index IF NOT EXISTS idx_course_name_trgm on iqps using gin (course_name gin_trgm_ops);
`

Expand Down
11 changes: 4 additions & 7 deletions backend/query/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ with fuzzy as (
from iqps_test
where (course_code || ' ' || course_name) %>> @query_text
order by rank_ix
limit least(@match_count, 30)
),
full_text as (
select
Expand All @@ -78,7 +77,6 @@ full_text as (
fts_course_details @@ websearch_to_tsquery(@query_text)
AND approve_status = true
order by rank_ix
limit least(@match_count, 30) * 2
),
partial_search as (
select id,
Expand All @@ -90,9 +88,8 @@ partial_search as (
websearch_to_tsquery('simple', @query_text)::text || ':*'
)
AND approve_status = true
limit least(@match_count, 30) * 2
)
select
), result as (
select
iqps_test.id,iqps_test.course_code, iqps_test.course_name, iqps_test.year, iqps_test.exam, iqps_test.filelink, iqps_test.from_library, iqps_test.upload_timestamp, iqps_test.approve_status
from
fuzzy
Expand All @@ -104,6 +101,6 @@ order by
coalesce(1.0 / (50 + full_text.rank_ix), 0.0) * 1 +
coalesce(1.0 / (50 + partial_search.rank_ix), 0.0) * 1
desc
limit
least(@match_count, 30)
)
select * from result
`

0 comments on commit b3147ae

Please sign in to comment.