[Racket] Ferti hydroponic nutrient solver, redux.
Update model contracts.
Great use of my new modules to factor out common model contract logic!
Changed files
models/crop-requirement.rkt
@@ -9,8 +9,7 @@
9
9
(contract-out
10
10
[create-crop-requirement! (->* (string? nutrient-value-hash/c) (crop?) crop-requirement?)]
11
11
[get-crop-requirements (-> (listof crop-requirement?))]
12
Removed:
[get-crop-requirement
13
Removed:
(->* () (#:id exact-nonnegative-integer? #:profile string?) (or/c crop-requirement? #f))]
12
Added:
[get-crop-requirement (->* () (#:id db-id? #:profile string?) (or/c crop-requirement? #f))]
14
13
[get-crop-requirement-values (-> crop-requirement-or-id/c nutrient-value-hash/c)]
15
14
[get-crop-requirement-value (-> crop-requirement-or-id/c nutrient? maybe-nutrient-value?)]
16
15
[delete-crop-requirement! (-> crop-requirement-or-id/c void?)]
@@ -22,19 +21,20 @@
22
21
sql
23
22
"../db/conn.rkt"
24
23
"nutrient.rkt"
25
Removed:
"crop.rkt")
24
Added:
"nutrient-value.rkt"
25
Added:
"crop.rkt"
26
Added:
"utils.rkt")
26
27
27
28
(struct crop-requirement (id profile crop-id nutrient-values)
28
29
#:transparent
29
30
#:guard (λ (id profile crop-id nutrient-values _)
30
31
(values id profile (if (sql-null? crop-id) #f crop-id) nutrient-values)))
31
32
32
Removed:
(define crop-requirement-id? exact-nonnegative-integer?)
33
Removed:
(define crop-requirement-or-id/c (or/c crop-requirement? crop-requirement-id?))
33
Added:
(define crop-requirement-or-id/c (or/c crop-requirement? db-id?))
34
34
35
35
(define (->cr-id cr-or-id)
36
36
(match cr-or-id
37
Removed:
[(? crop-requirement-id? cr-or-id) cr-or-id]
37
Added:
[(? db-id? cr-or-id) cr-or-id]
38
38
[(crop-requirement id _ _ _) id]))
39
39
40
40
;; CREATE
models/crop-rotation.rkt
@@ -5,30 +5,28 @@
5
5
crop-rotation-id
6
6
(rename-out [crop-rotation-rotation-date crop-rotation-date]
7
7
[crop-rotation-requirement-proportions crop-rotation-requirements])
8
Removed:
(contract-out [create-crop-rotation!
9
Removed:
(-> string? requirement-proportion-hash/c crop-rotation?)]
10
Removed:
[get-crop-rotations (-> (listof crop-rotation?))]
11
Removed:
[get-crop-rotation
12
Removed:
(->* () (#:id crop-rotation-id? #:date string?) (or/c crop-rotation? #f))]
13
Removed:
[get-latest-crop-rotation (-> (or/c crop-rotation? #f))]
14
Removed:
[delete-crop-rotation! (-> crop-rotation-or-id/c void?)]))
8
Added:
(contract-out
9
Added:
[create-crop-rotation! (-> string? requirement-proportion-hash/c crop-rotation?)]
10
Added:
[get-crop-rotations (-> (listof crop-rotation?))]
11
Added:
[get-crop-rotation (->* () (#:id db-id? #:date string?) (or/c crop-rotation? #f))]
12
Added:
[get-latest-crop-rotation (-> (or/c crop-rotation? #f))]
13
Added:
[delete-crop-rotation! (-> crop-rotation-or-id/c void?)]))
15
14
16
15
(require racket/contract
17
16
db
18
17
sql
19
18
"../db/conn.rkt"
20
Removed:
"nutrient.rkt"
21
Removed:
"crop-requirement.rkt")
19
Added:
"crop-requirement.rkt"
20
Added:
"utils.rkt")
22
21
23
22
(struct crop-rotation (id rotation-date requirement-proportions) #:transparent)
24
23
25
Removed:
(define crop-rotation-id? exact-nonnegative-integer?)
26
Removed:
(define crop-rotation-or-id/c (or/c crop-rotation? crop-rotation-id?))
24
Added:
(define crop-rotation-or-id/c (or/c crop-rotation? db-id?))
27
25
(define requirement-proportion-hash/c (hash/c crop-requirement? (between/c 0 100) #:immutable #t))
28
26
29
27
(define (->cr-id cr-or-id)
30
28
(match cr-or-id
31
Removed:
[(? crop-rotation-id? id) id]
29
Added:
[(? db-id? id) id]
32
30
[(crop-rotation id _ _) id]
33
31
[#f (error '->nt-id "#f can not be converted to an id")]))
34
32
models/crop.rkt
@@ -6,18 +6,15 @@
6
6
crop-name
7
7
(contract-out [create-crop! (-> string? crop?)]
8
8
[get-crops (-> (listof crop?))]
9
Removed:
[get-crop
10
Removed:
(->* ()
11
Removed:
(#:id (or/c #f exact-nonnegative-integer?) #:name (or/c #f string?))
12
Removed:
(or/c crop? #f))]
13
Removed:
[update-crop!
14
Removed:
(->* (exact-nonnegative-integer?) (#:name (or/c #f string?)) (or/c crop? #f))]
15
Removed:
[delete-crop! (-> exact-nonnegative-integer? void?)]))
9
Added:
[get-crop (->* () (#:id db-id? #:name string?) (or/c crop? #f))]
10
Added:
[update-crop! (->* (db-id?) (#:name string?) (or/c crop? #f))]
11
Added:
[delete-crop! (-> db-id? void?)]))
16
12
17
13
(require racket/contract
18
14
db
19
15
sql
20
Removed:
"../db/conn.rkt")
16
Added:
"../db/conn.rkt"
17
Added:
"utils.rkt")
21
18
22
19
(struct crop (id name) #:transparent)
23
20
models/fertilizer-product.rkt
@@ -10,9 +10,7 @@
10
10
[create-fertilizer-product! (-> string? string? nutrient-value-hash/c fertilizer-product?)]
11
11
[get-fertilizer-products (-> (listof fertilizer-product?))]
12
12
[get-fertilizer-product
13
Removed:
(->* ()
14
Removed:
(#:id exact-nonnegative-integer? #:canonical-name string?)
15
Removed:
(or/c fertilizer-product? #f))]
13
Added:
(->* () (#:id db-id? #:canonical-name string?) (or/c fertilizer-product? #f))]
16
14
[get-fertilizer-product-values (-> fertilizer-product-or-id/c nutrient-value-hash/c)]
17
15
[get-fertilizer-product-value
18
16
(-> fertilizer-product-or-id/c nutrient? maybe-nutrient-value?)]
@@ -22,7 +20,9 @@
22
20
db
23
21
sql
24
22
"../db/conn.rkt"
25
Removed:
"nutrient.rkt")
23
Added:
"nutrient.rkt"
24
Added:
"nutrient-value.rkt"
25
Added:
"utils.rkt")
26
26
27
27
(struct fertilizer-product (id canonical-name nutrient-values brand-name)
28
28
#:transparent
@@ -46,12 +46,11 @@
46
46
(~a (nutrient-canonical-name n) #:min-width 14)
47
47
(~a v #:max-width 6 #:align 'right)))))
48
48
49
Removed:
(define fertilizer-product-id? exact-nonnegative-integer?)
50
Removed:
(define fertilizer-product-or-id/c (or/c fertilizer-product? fertilizer-product-id?))
49
Added:
(define fertilizer-product-or-id/c (or/c fertilizer-product? db-id?))
51
50
52
51
(define (->fp-id fp-or-id)
53
52
(match fp-or-id
54
Removed:
[(? fertilizer-product-id? id) id]
53
Added:
[(? db-id? id) id]
55
54
[(fertilizer-product id _ _ _) id]))
56
55
57
56
;; CREATE
models/nutrient-measurement.rkt
@@ -9,7 +9,7 @@
9
9
[create-nutrient-measurement! (-> string? nutrient-value-hash/c nutrient-measurement?)]
10
10
[get-nutrient-measurements (-> (listof nutrient-measurement?))]
11
11
[get-nutrient-measurement
12
Removed:
(->* () (#:id exact-nonnegative-integer? #:date string?) (or/c nutrient-measurement? #f))]
12
Added:
(->* () (#:id db-id? #:date string?) (or/c nutrient-measurement? #f))]
13
13
[get-nutrient-measurement-values (-> nutrient-measurement-or-id/c nutrient-value-hash/c)]
14
14
[get-nutrient-measurement-value
15
15
(-> nutrient-measurement-or-id/c nutrient? maybe-nutrient-value?)]
@@ -22,7 +22,9 @@
22
22
db
23
23
sql
24
24
"../db/conn.rkt"
25
Removed:
"nutrient.rkt")
25
Added:
"nutrient.rkt"
26
Added:
"nutrient-value.rkt"
27
Added:
"utils.rkt")
26
28
27
29
(struct nutrient-measurement (id measurement-date nutrient-values)
28
30
#:transparent
@@ -38,12 +40,11 @@
38
40
(~a (nutrient-canonical-name n) #:min-width 14)
39
41
(~a v #:max-width 6 #:align 'right)))))
40
42
41
Removed:
(define nutrient-measurement-id? exact-nonnegative-integer?)
42
Removed:
(define nutrient-measurement-or-id/c (or/c nutrient-measurement? nutrient-measurement-id?))
43
Added:
(define nutrient-measurement-or-id/c (or/c nutrient-measurement? db-id?))
43
44
44
45
(define (->nm-id nm-or-id)
45
46
(match nm-or-id
46
Removed:
[(? nutrient-measurement-id? id) id]
47
Added:
[(? db-id? id) id]
47
48
[(nutrient-measurement id _ _) id]))
48
49
49
50
;; CREATE
models/nutrient.rkt
@@ -9,11 +9,7 @@
9
9
(contract-out [create-nutrient! (-> string? string? string? nutrient?)]
10
10
[get-nutrients (-> (listof nutrient?))]
11
11
[get-nutrient
12
Removed:
(->* ()
13
Removed:
(#:id (or/c #f exact-nonnegative-integer?)
14
Removed:
#:name (or/c #f string?)
15
Removed:
#:formula (or/c #f string?))
16
Removed:
(or/c nutrient? #f))]
12
Added:
(->* () (#:id db-id? #:name string? #:formula string?) (or/c nutrient? #f))]
17
13
[update-nutrient!
18
14
(->* (nutrient?)
19
15
(#:name (or/c #f string?) #:formula (or/c #f string?))