Skip to content

Commit

Permalink
Add access to type categories
Browse files Browse the repository at this point in the history
See #10
  • Loading branch information
ezwelty committed Jul 11, 2023
1 parent 9ed7467 commit d776ab4
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 7 deletions.
1 change: 1 addition & 0 deletions db/repos/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class Types {
taxonomic_rank: null,
notes: null,
pending: true,
categories: [],
...obj
}
values = {
Expand Down
9 changes: 6 additions & 3 deletions db/sql/types/add.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ INSERT INTO types (
scientific_name,
scientific_synonyms,
taxonomic_rank,
notes
notes,
category_mask
)
VALUES (
${parent_id},
Expand All @@ -16,7 +17,8 @@ VALUES (
${scientific_name},
${scientific_synonyms},
${taxonomic_rank},
${notes}
${notes},
${category_mask}
)
RETURNING
id,
Expand All @@ -32,4 +34,5 @@ RETURNING
ar_name, de_name, el_name, es_name, fr_name, he_name, it_name, nl_name,
pl_name, pt_br_name, sk_name, sv_name, tr_name, zh_tw_name,
eat_the_weeds_url, foraging_texas_url, fruitipedia_url, urban_mushrooms_url, wikipedia_url,
usda_symbol
usda_symbol,
category_mask
5 changes: 3 additions & 2 deletions db/sql/types/list.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ SELECT
en_name, en_synonyms,
ar_name, de_name, el_name, es_name, fr_name, he_name, it_name, nl_name,
pl_name, pt_br_name, sk_name, sv_name, tr_name, zh_tw_name,
eat_the_weeds_url, foraging_texas_url, fruitipedia_url, urban_mushrooms_url, wikipedia_url,
usda_symbol
eat_the_weeds_url, foraging_texas_url, fruitipedia_url, urban_mushrooms_url, wikipedia_url,
usda_symbol,
category_mask
FROM types
5 changes: 3 additions & 2 deletions db/sql/types/show.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ SELECT
en_name, en_synonyms,
ar_name, de_name, el_name, es_name, fr_name, he_name, it_name, nl_name,
pl_name, pt_br_name, sk_name, sv_name, tr_name, zh_tw_name,
eat_the_weeds_url, foraging_texas_url, fruitipedia_url, urban_mushrooms_url, wikipedia_url,
usda_symbol
eat_the_weeds_url, foraging_texas_url, fruitipedia_url, urban_mushrooms_url, wikipedia_url,
usda_symbol,
category_mask
FROM types
WHERE id = ${id}
7 changes: 7 additions & 0 deletions docs/components/schemas.yml
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,13 @@ BaseType:
sv: ['Jabolko']
tr: ['Elma']
zh_tw: ['蘋果']
categories:
description: Categories.
type: array
items:
type: string
enum: [forager, freegan, honeybee, grafter]
example: [forager]
SubmitType:
title: Type (submit)
description: Type properties that can be submitted. At least one name (in `common_names.en` or `scientific_names`) is required.
Expand Down
12 changes: 12 additions & 0 deletions docs/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1243,6 +1243,18 @@ components:
- Elma
zh_tw:
- 蘋果
categories:
description: Categories.
type: array
items:
type: string
enum:
- forager
- freegan
- honeybee
- grafter
example:
- forager
SubmitType:
title: Type (submit)
description: Type properties that can be submitted. At least one name (in `common_names.en` or `scientific_names`) is required.
Expand Down
17 changes: 17 additions & 0 deletions helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,16 @@ _.types_array_to_sql = function(value) {
}
}

TYPE_CATEGORIES = ['forager', 'freegan', 'honeybee', 'grafter']

_.mask_bits = function(selected, all) {
return all.reduce((acc, curr, i) => acc | (selected.includes(curr) ? 1 << i : 0), 0)
}

_.unmask_bits = function(mask, all) {
return all.filter((_, i) => mask & (1 << i))
}

_.format_type = function(type) {
const replaced = []
// Format scientific names: [name, synonyms...]
Expand Down Expand Up @@ -206,6 +216,9 @@ _.format_type = function(type) {
type.urls.usda = `https://plants.usda.gov/home/plantProfile?symbol=${type.usda_symbol}`
}
replaced.push('usda_symbol')
// Format categories: {categories: [category, ...]}
type.categories = _.unmask_bits(type.category_mask, TYPE_CATEGORIES)
replaced.push('category_mask')
// Drop replaced properties
for (const key of replaced) {
delete type[key]
Expand All @@ -227,6 +240,10 @@ _.deconstruct_type = function(type) {
type.scientific_synonyms = type.scientific_names.slice(1).join(', ')
delete type.scientific_names
}
if (type.categories) {
type.category_mask = _.mask_bits(type.categories, TYPE_CATEGORIES)
delete type.categories
}
return type
}

Expand Down

0 comments on commit d776ab4

Please sign in to comment.