summaryrefslogtreecommitdiff
path: root/models/nutrient.rkt
diff options
context:
space:
mode:
Diffstat (limited to 'models/nutrient.rkt')
-rw-r--r--models/nutrient.rkt109
1 files changed, 41 insertions, 68 deletions
diff --git a/models/nutrient.rkt b/models/nutrient.rkt
index 944583e..49921d7 100644
--- a/models/nutrient.rkt
+++ b/models/nutrient.rkt
@@ -1,26 +1,27 @@
#lang racket
-(provide
- ;; Model struct
- nutrient
- nutrient?
- nutrient-id nutrient-name nutrient-formula
- ;; Contracts
- nutrient-value-pair/c
- (contract-out
- ;; SQL CRUD
- [create-nutrient! (-> string? string? nutrient?)]
- [get-nutrients (-> (listof nutrient?))]
- [get-nutrient (->* ()
- (#:id (or/c #f exact-nonnegative-integer?)
- #:name (or/c #f string?)
- #:formula (or/c #f string?))
- (or/c nutrient? #f))]
- [update-nutrient! (->* (nutrient?)
- (#:name (or/c #f string?)
- #:formula (or/c #f string?))
- (or/c nutrient? #f))]
- [delete-nutrient! (-> nutrient? void?)]))
+;; Model struct
+(provide nutrient
+ nutrient?
+ nutrient-id
+ nutrient-name
+ nutrient-formula
+ ;; Contracts
+ nutrient-value-pair/c
+ ;; SQL CRUD
+ (contract-out [create-nutrient! (-> string? string? nutrient?)]
+ [get-nutrients (-> (listof nutrient?))]
+ [get-nutrient
+ (->* ()
+ (#:id (or/c #f exact-nonnegative-integer?)
+ #:name (or/c #f string?)
+ #:formula (or/c #f string?))
+ (or/c nutrient? #f))]
+ [update-nutrient!
+ (->* (nutrient?)
+ (#:name (or/c #f string?) #:formula (or/c #f string?))
+ (or/c nutrient? #f))]
+ [delete-nutrient! (-> nutrient? void?)]))
(require racket/contract
db
@@ -30,14 +31,9 @@
(struct nutrient (id name formula)
#:transparent
#:property prop:custom-write
- (λ (v out _)
- (fprintf out "#<~a ~a>"
- (nutrient-id v)
- (nutrient-name v))))
-
-(define nutrient-value-pair/c
- (cons/c nutrient? (and/c real? (>=/c 0))))
+ (λ (v out _) (fprintf out "#<~a ~a>" (nutrient-id v) (nutrient-name v))))
+(define nutrient-value-pair/c (cons/c nutrient? (and/c real? (>=/c 0))))
;; CREATE
@@ -45,24 +41,18 @@
(or (get-nutrient #:name name #:formula formula)
(begin
(query-exec (current-conn)
- (insert #:into nutrients
- #:set [canonical_name ,name] [formula ,formula]))
+ (insert #:into nutrients #:set [canonical_name ,name] [formula ,formula]))
(get-nutrient #:name name))))
-
;; READ
(define (get-nutrients)
(for/list ([(id* name* formula*)
(in-query (current-conn)
- (select id canonical_name formula
- #:from nutrients
- #: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]
- #:name [name #f]
- #:formula [formula #f])
+(define (get-nutrient #:id [id #f] #:name [name #f] #:formula [formula #f])
(define (where-expr)
(define clauses
(filter values
@@ -73,47 +63,30 @@
[(null? clauses) ""]
[else (format "WHERE ~a" (string-join clauses " AND "))]))
(match (query-maybe-row (current-conn)
- (string-join
- `("SELECT id, canonical_name, formula"
- "FROM nutrients"
- ,(where-expr)
- "ORDER BY id ASC"
- "LIMIT 1")))
- [(vector id* name* formula*)
- (nutrient id* name* formula*)]
+ (string-join `("SELECT id, canonical_name, formula" "FROM nutrients"
+ ,(where-expr)
+ "ORDER BY id ASC"
+ "LIMIT 1")))
+ [(vector id* name* formula*) (nutrient id* name* formula*)]
[#f #f]))
-
;; UPDATE
-(define (update-nutrient! nutrient
- #:name [name #f]
- #:formula [formula #f])
- (define id(nutrient-id nutrient))
+(define (update-nutrient! nutrient #:name [name #f] #:formula [formula #f])
+ (define id (nutrient-id nutrient))
(cond
[(and name formula)
- (query-exec (current-conn)
- (update nutrients
- #:set [canonical_name ,name] [formula ,formula]
- #:where (= id ,id)))]
+ (query-exec
+ (current-conn)
+ (update nutrients #:set [canonical_name ,name] [formula ,formula] #:where (= id ,id)))]
[name
- (query-exec (current-conn)
- (update nutrients
- #:set [canonical_name ,name]
- #:where (= id ,id)))]
+ (query-exec (current-conn) (update nutrients #:set [canonical_name ,name] #:where (= id ,id)))]
[formula
- (query-exec (current-conn)
- (update nutrients
- #:set [formula ,formula]
- #:where (= id ,id)))]
+ (query-exec (current-conn) (update nutrients #:set [formula ,formula] #:where (= id ,id)))]
[else (void)])
- (or (get-nutrient #:id id)
- (error 'update-nutrient! "No nutrient with id ~a" id)))
-
+ (or (get-nutrient #:id id) (error 'update-nutrient! "No nutrient with id ~a" id)))
;; DELETE
(define (delete-nutrient! nutrient)
- (query-exec (current-conn)
- (delete #:from nutrients
- #:where (= id ,(nutrient-id nutrient)))))
+ (query-exec (current-conn) (delete #:from nutrients #:where (= id ,(nutrient-id nutrient)))))
Copyright 2019--2026 Marius PETER