From fddbcb3923d3dd019d07b3f37c033fab36c2cadc Mon Sep 17 00:00:00 2001 From: Marius Peter Date: Mon, 20 Oct 2025 20:41:07 +0200 Subject: Simplify get-* model accessors. --- models/nutrient.rkt | 28 +++++----------------------- 1 file changed, 5 insertions(+), 23 deletions(-) (limited to 'models/nutrient.rkt') diff --git a/models/nutrient.rkt b/models/nutrient.rkt index 5a32e70..f2ef4fd 100644 --- a/models/nutrient.rkt +++ b/models/nutrient.rkt @@ -8,11 +8,7 @@ ;; SQL CRUD (contract-out [create-nutrient! (-> string? string? void?)] - [get-nutrients (->* () - (#:id (or/c #f exact-nonnegative-integer?) - #:name (or/c #f string?) - #:formula (or/c #f string?)) - (listof nutrient?))] + [get-nutrients (-> (listof nutrient?))] [get-nutrient (->* () (#:id (or/c #f exact-nonnegative-integer?) #:name (or/c #f string?) @@ -42,26 +38,12 @@ ;; READ -(define (get-nutrients #:id [id #f] - #:name [name #f] - #:formula [formula #f]) - (define (where-expr) - (define clauses - (filter values - (list - (and id (format "id = ~e" id)) - (and name (format "canonical_name = ~e" name)) - (and formula (format "formula = ~e" formula))))) - (cond - [(null? clauses) ""] - [else (format "WHERE ~a" (string-join clauses " AND "))])) +(define (get-nutrients) (for/list ([(id* name* formula*) (in-query (current-conn) - (string-join - `("SELECT id, canonical_name, formula" - "FROM nutrients" - ,(where-expr) - "ORDER BY id ASC")))]) + (select id canonical_name formula + #:from nutrients + #:order-by id #:asc))]) (nutrient id* name* formula*))) (define (get-nutrient #:id [id #f] -- cgit v1.2.3