Skip to content

Commit

Permalink
Implemented faculty filter #1703
Browse files Browse the repository at this point in the history
  • Loading branch information
verheyenkoen committed Sep 26, 2024
1 parent 81d5a15 commit d973e96
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
11 changes: 9 additions & 2 deletions handlers/candidaterecords/candidate_records.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,22 @@ func CandidateRecords(w http.ResponseWriter, r *http.Request) {
return
}

facultyFacet, err := c.Repo.GetCandidateRecordsFacultyFacet(r.Context(), searchArgs)
if err != nil {
c.HandleError(w, r, err)
return
}

publicationYearFacet, err := c.Repo.GetCandidateRecordsPublicationYearFacet(r.Context(), searchArgs)
if err != nil {
c.HandleError(w, r, err)
return
}

facets := map[string]models.FacetValues{
"status": statusFacet,
"year": publicationYearFacet,
"status": statusFacet,
"faculty_id": facultyFacet,
"year": publicationYearFacet,
}

searchHits := &models.SearchHits{
Expand Down
27 changes: 27 additions & 0 deletions repositories/candidate_records.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,21 @@ func (r *Repo) GetCandidateRecordsStatusFacet(ctx context.Context, searchArgs *m
return result, nil
}

func (r *Repo) GetCandidateRecordsFacultyFacet(ctx context.Context, searchArgs *models.SearchArgs) (models.FacetValues, error) {
query := getBaseQuery("jsonb_path_query(metadata, '$.related_organizations.organization_id')->>0 AS Value", "COUNT(*) AS Count").
OrderBy("Value").
GroupBy("Value")

query = addQueryFilters(query, searchArgs, "faculty_id")

result, err := queryRows[models.Facet](r, ctx, query)
if err != nil {
return nil, err
}

return result, nil
}

func (r *Repo) GetCandidateRecordsPublicationYearFacet(ctx context.Context, searchArgs *models.SearchArgs) (models.FacetValues, error) {
query := getBaseQuery("metadata->>'year' AS Value", "COUNT(*) AS Count").
OrderBy("Value DESC").
Expand Down Expand Up @@ -310,6 +325,12 @@ func addQueryFilters(query sq.SelectBuilder, searchArgs *models.SearchArgs, omit
case "status":
query = query.Where(sq.Eq{"status": filterValue})

case "faculty_id":
conditions := lo.Map(filterValue, func(facultyID string, _ int) sq.Sqlizer {
return sq.Expr("metadata->'related_organizations' @> ?::jsonb", getFacultyFilter(facultyID))
})
query = query.Where(sq.Or(conditions))

case "year":
query = query.Where(sq.Eq{"metadata->>'year'": filterValue})

Expand All @@ -330,6 +351,12 @@ func getPersonFilter(personID string) []byte {
return personFilter
}

func getFacultyFilter(facultyID string) []byte {
facultyFilter, _ := json.Marshal([]models.RelatedOrganization{{OrganizationID: facultyID}})

return facultyFilter
}

func (r *Repo) mapRows(rows []candidateRecordRow, iteratee func(candidateRecordRow) *models.CandidateRecord) (int, []*models.CandidateRecord, error) {
if len(rows) == 0 {
return 0, make([]*models.CandidateRecord, 0), nil
Expand Down

0 comments on commit d973e96

Please sign in to comment.