Skip to content

Commit

Permalink
feat: ✨ change synonyms logic to default to no synonyms (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
HannesOberreiter committed Mar 27, 2024
1 parent 72322fd commit cc0dea8
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 19 deletions.
8 changes: 4 additions & 4 deletions components/components.templ
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,9 @@ templ Filter(){
<!-- Checkbox if Synonym Taxa should be shown -->
<div class="flex items-center w-full md:w-1/2 lg:w-1/4 px-3 mb-3 md:mb-0" >
<label class="uppercase tracking-wide text-gray-500 text-xs font-bold mb-2 mr-2" for="synonym">
Hide Synonyms
Show Synonyms
</label>
<input class="block py-1 mb-3 pl-1" id="synonym" type="checkbox" name="hide_synonyms" value="true" onclick="document.getElementById('filterBtn').click();" />
<input class="block py-1 mb-3 pl-1" id="synonym" type="checkbox" name="show_synonyms" value="true" onclick="document.getElementById('filterBtn').click();" />
</div>

<!-- Hidden fields for sorting -->
Expand Down Expand Up @@ -231,7 +231,7 @@ templ Table(rows TableRows, q queries.Query, counts queries.Counts, pages PagesS
@TableTh("Latest Observation", "date", q)
<th class="text-left">~Years</th>
@TableTh("Last Fetched", "fetch", q)
if !q.HIDE_SYNONYMS {
if q.SHOW_SYNONYMS {
<th class="text-left">Synonym</th>
}
<th class="text-left">Taxa</th>
Expand Down Expand Up @@ -271,7 +271,7 @@ templ Table(rows TableRows, q queries.Query, counts queries.Counts, pages PagesS
</span>

</td>
if !q.HIDE_SYNONYMS {
if q.SHOW_SYNONYMS {
<td>
if row.IsSynonym && row.SynonymID.Valid && row.SynonymName.Valid {
<a class="italic" href={ templ.URL("https://www.gbif.org/species/" + row.SynonymID.String)} target="_blank"> { nbsp(row.SynonymName.String) } </a>
Expand Down
17 changes: 9 additions & 8 deletions pkg/queries/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type Query struct {
RANK string
TAXA string
PAGE string
HIDE_SYNONYMS bool
SHOW_SYNONYMS bool
}

type Counts struct {
Expand Down Expand Up @@ -67,7 +67,7 @@ func NewQuery(payload any) Query {
RANK: "",
TAXA: "",
PAGE: "1",
HIDE_SYNONYMS: false,
SHOW_SYNONYMS: false,
}

if payload != nil {
Expand Down Expand Up @@ -97,9 +97,9 @@ func NewQuery(payload any) Query {
q.TAXA = val.(string)
case "PAGE":
q.PAGE = val.(string)
case "HIDE_SYNONYMS":
case "SHOW_SYNONYMS":
if reflect.TypeOf(val).Kind() == reflect.Bool {
q.HIDE_SYNONYMS = val.(bool)
q.SHOW_SYNONYMS = val.(bool)
}
}
}
Expand Down Expand Up @@ -127,6 +127,7 @@ func GetCounts(db *sql.DB, q Query) Counts {
taxaCount = observationCount // There should be only one taxa per observation per country
} else {
taxaQuery := sq.Select("COUNT(DISTINCT taxa.SynonymID)").From("taxa")

createFilterQuery(&taxaQuery, q)
err = taxaQuery.RunWith(db).QueryRow().Scan(&taxaCount)
if err != nil {
Expand Down Expand Up @@ -167,10 +168,6 @@ func GetTableData(db *sql.DB, q Query, isCSV ...bool) []TableRow {
}
}

if q.HIDE_SYNONYMS {
query = query.Where(sq.Eq{"isSynonym": false})
}

createFilterQuery(&query, q)

rows, err := query.RunWith(db).Query()
Expand Down Expand Up @@ -241,6 +238,10 @@ func countryCodeToFlag(x string) (country, flag string) {
}

func createFilterQuery(query *sq.SelectBuilder, q Query) {
if !q.SHOW_SYNONYMS {
*query = query.Where(sq.Or{sq.Eq{"isSynonym": false}, sq.Eq{"SynonymID": nil}})
}

if q.SEARCH != "" {
*query = query.Where(sq.ILike{"ScientificName": "%" + q.SEARCH + "%"})
}
Expand Down
12 changes: 6 additions & 6 deletions pkg/queries/queries_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func TestQuery(t *testing.T) {
q := Query{
ORDER_BY: "Date",
ORDER_DIR: "ASC",
HIDE_SYNONYMS: false,
SHOW_SYNONYMS: false,
}

counts := GetCounts(internal.DB, q)
Expand All @@ -39,14 +39,14 @@ func TestQuery(t *testing.T) {
}

table := GetTableData(internal.DB, q)
if len(table) != 2 {
t.Errorf("got %d, wanted %d", len(table), 2)
if len(table) != 1 {
t.Errorf("got %d, wanted %d", len(table), 1)
}

q.HIDE_SYNONYMS = true
q.SHOW_SYNONYMS = true
table = GetTableData(internal.DB, q)
if len(table) != 1 {
t.Errorf("got %d, wanted %d", len(table), 1)
if len(table) != 2 {
t.Errorf("got %d, wanted %d", len(table), 2)
}

if table[0].TaxonID != DemoTaxa[0] {
Expand Down
2 changes: 1 addition & 1 deletion server.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ type Payload struct {
RANK *string `query:"rank"`
TAXA *string `query:"taxa"`
PAGE *string `query:"page"`
HIDE_SYNONYMS *bool `query:"hide_synonyms"`
SHOW_SYNONYMS *bool `query:"show_synonyms"`
}

/* Pages */
Expand Down

0 comments on commit cc0dea8

Please sign in to comment.