diff options
| author | Marius Peter <dev@marius-peter.com> | 2025-11-29 18:10:26 +0100 |
|---|---|---|
| committer | Marius Peter <dev@marius-peter.com> | 2025-11-29 18:10:26 +0100 |
| commit | a3e00cb41614056b898d74bafe0f86afb2590c56 (patch) | |
| tree | 9920bcf87797ac7e7fe1dc51316ca4103af7511c /models/fertilizer-product.rkt | |
| parent | dafe1aaa54d41999b4c81f4904ae1f0e7cc9de11 (diff) | |
Main model structs can now be manipulated by id or entire struct.
This paves the way to passing ids in URLs, and acting upon them in
handlers.
Diffstat (limited to 'models/fertilizer-product.rkt')
| -rw-r--r-- | models/fertilizer-product.rkt | 28 |
1 files changed, 18 insertions, 10 deletions
diff --git a/models/fertilizer-product.rkt b/models/fertilizer-product.rkt index b0ac7de..4481010 100644 --- a/models/fertilizer-product.rkt +++ b/models/fertilizer-product.rkt @@ -13,9 +13,9 @@ (->* () (#:id (or/c #f exact-nonnegative-integer?) #:canonical-name (or/c #f string?)) (or/c fertilizer-product? #f))] - [get-fertilizer-product-values (-> fertilizer-product? nutrient-value-hash/c)] - [get-fertilizer-product-value (-> fertilizer-product? nutrient? (or/c #f number?))] - [delete-fertilizer-product! (-> fertilizer-product? void?)])) + [get-fertilizer-product-values (-> fertilizer-product-or-id/c nutrient-value-hash/c)] + [get-fertilizer-product-value (-> fertilizer-product-or-id/c nutrient? (or/c #f number?))] + [delete-fertilizer-product! (-> fertilizer-product-or-id/c void?)])) (require racket/contract db @@ -42,6 +42,15 @@ (~a (nutrient-canonical-name n) #:min-width 14) (~a v #:max-width 6 #:align 'right))))) +(define fertilizer-product-id? exact-nonnegative-integer?) + +(define fertilizer-product-or-id/c (or/c fertilizer-product? fertilizer-product-id?)) + +(define (->fp-id fp-or-id) + (match fp-or-id + [(? fertilizer-product-id? id) id] + [(fertilizer-product id _ _ _) id])) + ;; CREATE (define (create-fertilizer-product! canonical-name brand-name nutrient-values) @@ -129,7 +138,7 @@ [(list row) (grouped-row->fertilizer-product row)] [many (error 'get-fertilizer-product "expected 1 fertilizer product, got ~a" (length many))])) -(define (get-fertilizer-product-values fertilizer-product) +(define (get-fertilizer-product-values fp-or-id) (for/hash ([(nutrient-id canonical-name french-name formula value_ppm) (in-query (current-conn) (select n.id @@ -138,20 +147,19 @@ n.formula nv.value_ppm #:from (TableExpr:AST ,joined) - #:where (= fp.id ,(fertilizer-product-id fertilizer-product))))]) + #:where (= fp.id ,(->fp-id fp-or-id))))]) (values (nutrient nutrient-id canonical-name french-name formula) value_ppm))) -(define (get-fertilizer-product-value fertilizer-product nutrient) +(define (get-fertilizer-product-value fp-or-id nutrient) (query-maybe-value (current-conn) (select value_ppm #:from (TableExpr:AST ,joined) - #:where (and (= fp.id ,(fertilizer-product-id fertilizer-product)) + #:where (and (= fp.id ,(->fp-id fp-or-id)) (= nv.nutrient_id ,(nutrient-id nutrient)))))) ;; UPDATE ;; DELETE -(define (delete-fertilizer-product! fertilizer-product) - (define id (fertilizer-product-id fertilizer-product)) - (query-exec (current-conn) (delete #:from fertilizer_products #:where (= id ,id)))) +(define (delete-fertilizer-product! fp-or-id) + (query-exec (current-conn) (delete #:from fertilizer_products #:where (= id ,(->fp-id fp-or-id))))) |