diff options
Diffstat (limited to 'models/fertilizer-product.rkt')
| -rw-r--r-- | models/fertilizer-product.rkt | 43 |
1 files changed, 16 insertions, 27 deletions
diff --git a/models/fertilizer-product.rkt b/models/fertilizer-product.rkt index f7a5e9c..7c0a267 100644 --- a/models/fertilizer-product.rkt +++ b/models/fertilizer-product.rkt @@ -24,13 +24,13 @@ "nutrient-value.rkt" "utils.rkt") -(struct fertilizer-product (id canonical-name nutrient-values brand-name) +(struct fertilizer-product (id canonical-name brand-name nutrient-values) #:transparent - #:guard (λ (id canonical-name nutrient-values brand-name _) + #:guard (λ (id canonical-name brand-name nutrient-values _) (values id canonical-name - nutrient-values - (if (or (sql-null? brand-name) (= (string-length brand-name) 0)) #f brand-name))) + (if (or (sql-null? brand-name) (= (string-length brand-name) 0)) #f brand-name) + nutrient-values)) #:property prop:custom-write (λ (v out _mode) (fprintf out "Fertilizer #~a\n" (fertilizer-product-id v)) @@ -56,28 +56,17 @@ ;; CREATE (define (create-fertilizer-product! canonical-name brand-name nutrient-values) - (or - (get-fertilizer-product #:canonical-name canonical-name) - (with-tx - (query-exec (current-conn) - (insert #:into fertilizer_products - #:set [canonical_name ,canonical-name] - [brand_name ,brand-name])) - (define fp-id - (query-value (current-conn) - (select id #:from fertilizer_products #:where (= canonical_name ,canonical-name)))) - (query-exec (current-conn) - (insert #:into nutrient_value_sets #:set [fertilizer_product_id ,fp-id])) - (define nvs-id - (query-value (current-conn) - (select id #:from nutrient_value_sets #:where (= fertilizer_product_id ,fp-id)))) - (for ([(n v) (in-hash nutrient-values)]) - (query-exec (current-conn) - (insert #:into nutrient_values - #:set [value_set_id ,nvs-id] - [nutrient_id ,(nutrient-id n)] - [value_ppm ,v]))) - (get-fertilizer-product #:canonical-name canonical-name)))) + (with-tx (define fp-id + (insert-id (query (current-conn) + (insert #:into fertilizer_products + #:set [canonical_name ,canonical-name] + [brand_name ,brand-name])))) + (define nvs-id + (insert-id (query (current-conn) + (insert #:into nutrient_value_sets + #:set [fertilizer_product_id ,fp-id])))) + (insert-nutrient-values (current-conn) nvs-id nutrient-values) + (fertilizer-product nvs-id canonical-name brand-name nutrient-values))) ;; READ @@ -92,7 +81,7 @@ (define (grouped-row->fertilizer-product grouped-row) (match-define (vector fp-id canonical-name brand-name residuals) grouped-row) - (fertilizer-product fp-id canonical-name (residuals->nutrient-value-hash residuals) brand-name)) + (fertilizer-product fp-id canonical-name brand-name (residuals->nutrient-value-hash residuals))) (define (get-fertilizer-products) (define grouped-rows |