diff options
| author | Marius Peter <dev@marius-peter.com> | 2025-12-06 17:55:58 +0100 |
|---|---|---|
| committer | Marius Peter <dev@marius-peter.com> | 2025-12-06 17:55:58 +0100 |
| commit | c3557a7d5f54d7a9cb7fb2ece9487332c264236e (patch) | |
| tree | 1a87077374fba18f4ef38fb1ae084f6884e3cd5b | |
| parent | c7ec194f4a6c3dc879c24d8075bfe05a7687f976 (diff) | |
Update model contracts.
Great use of my new modules to factor out common model contract logic!
| -rw-r--r-- | models/crop-requirement.rkt | 12 | ||||
| -rw-r--r-- | models/crop-rotation.rkt | 22 | ||||
| -rw-r--r-- | models/crop.rkt | 13 | ||||
| -rw-r--r-- | models/fertilizer-product.rkt | 13 | ||||
| -rw-r--r-- | models/nutrient-measurement.rkt | 11 | ||||
| -rw-r--r-- | models/nutrient.rkt | 6 |
6 files changed, 34 insertions, 43 deletions
diff --git a/models/crop-requirement.rkt b/models/crop-requirement.rkt index bc02fb5..256305e 100644 --- a/models/crop-requirement.rkt +++ b/models/crop-requirement.rkt @@ -9,8 +9,7 @@ (contract-out [create-crop-requirement! (->* (string? nutrient-value-hash/c) (crop?) crop-requirement?)] [get-crop-requirements (-> (listof crop-requirement?))] - [get-crop-requirement - (->* () (#:id exact-nonnegative-integer? #:profile string?) (or/c crop-requirement? #f))] + [get-crop-requirement (->* () (#:id db-id? #:profile string?) (or/c crop-requirement? #f))] [get-crop-requirement-values (-> crop-requirement-or-id/c nutrient-value-hash/c)] [get-crop-requirement-value (-> crop-requirement-or-id/c nutrient? maybe-nutrient-value?)] [delete-crop-requirement! (-> crop-requirement-or-id/c void?)] @@ -22,19 +21,20 @@ sql "../db/conn.rkt" "nutrient.rkt" - "crop.rkt") + "nutrient-value.rkt" + "crop.rkt" + "utils.rkt") (struct crop-requirement (id profile crop-id nutrient-values) #:transparent #:guard (λ (id profile crop-id nutrient-values _) (values id profile (if (sql-null? crop-id) #f crop-id) nutrient-values))) -(define crop-requirement-id? exact-nonnegative-integer?) -(define crop-requirement-or-id/c (or/c crop-requirement? crop-requirement-id?)) +(define crop-requirement-or-id/c (or/c crop-requirement? db-id?)) (define (->cr-id cr-or-id) (match cr-or-id - [(? crop-requirement-id? cr-or-id) cr-or-id] + [(? db-id? cr-or-id) cr-or-id] [(crop-requirement id _ _ _) id])) ;; CREATE diff --git a/models/crop-rotation.rkt b/models/crop-rotation.rkt index 630e493..cb6832e 100644 --- a/models/crop-rotation.rkt +++ b/models/crop-rotation.rkt @@ -5,30 +5,28 @@ crop-rotation-id (rename-out [crop-rotation-rotation-date crop-rotation-date] [crop-rotation-requirement-proportions crop-rotation-requirements]) - (contract-out [create-crop-rotation! - (-> string? requirement-proportion-hash/c crop-rotation?)] - [get-crop-rotations (-> (listof crop-rotation?))] - [get-crop-rotation - (->* () (#:id crop-rotation-id? #:date string?) (or/c crop-rotation? #f))] - [get-latest-crop-rotation (-> (or/c crop-rotation? #f))] - [delete-crop-rotation! (-> crop-rotation-or-id/c void?)])) + (contract-out + [create-crop-rotation! (-> string? requirement-proportion-hash/c crop-rotation?)] + [get-crop-rotations (-> (listof crop-rotation?))] + [get-crop-rotation (->* () (#:id db-id? #:date string?) (or/c crop-rotation? #f))] + [get-latest-crop-rotation (-> (or/c crop-rotation? #f))] + [delete-crop-rotation! (-> crop-rotation-or-id/c void?)])) (require racket/contract db sql "../db/conn.rkt" - "nutrient.rkt" - "crop-requirement.rkt") + "crop-requirement.rkt" + "utils.rkt") (struct crop-rotation (id rotation-date requirement-proportions) #:transparent) -(define crop-rotation-id? exact-nonnegative-integer?) -(define crop-rotation-or-id/c (or/c crop-rotation? crop-rotation-id?)) +(define crop-rotation-or-id/c (or/c crop-rotation? db-id?)) (define requirement-proportion-hash/c (hash/c crop-requirement? (between/c 0 100) #:immutable #t)) (define (->cr-id cr-or-id) (match cr-or-id - [(? crop-rotation-id? id) id] + [(? db-id? id) id] [(crop-rotation id _ _) id] [#f (error '->nt-id "#f can not be converted to an id")])) diff --git a/models/crop.rkt b/models/crop.rkt index eff54a3..8c50c14 100644 --- a/models/crop.rkt +++ b/models/crop.rkt @@ -6,18 +6,15 @@ crop-name (contract-out [create-crop! (-> string? crop?)] [get-crops (-> (listof crop?))] - [get-crop - (->* () - (#:id (or/c #f exact-nonnegative-integer?) #:name (or/c #f string?)) - (or/c crop? #f))] - [update-crop! - (->* (exact-nonnegative-integer?) (#:name (or/c #f string?)) (or/c crop? #f))] - [delete-crop! (-> exact-nonnegative-integer? void?)])) + [get-crop (->* () (#:id db-id? #:name string?) (or/c crop? #f))] + [update-crop! (->* (db-id?) (#:name string?) (or/c crop? #f))] + [delete-crop! (-> db-id? void?)])) (require racket/contract db sql - "../db/conn.rkt") + "../db/conn.rkt" + "utils.rkt") (struct crop (id name) #:transparent) diff --git a/models/fertilizer-product.rkt b/models/fertilizer-product.rkt index 7c1ca54..f7a5e9c 100644 --- a/models/fertilizer-product.rkt +++ b/models/fertilizer-product.rkt @@ -10,9 +10,7 @@ [create-fertilizer-product! (-> string? string? nutrient-value-hash/c fertilizer-product?)] [get-fertilizer-products (-> (listof fertilizer-product?))] [get-fertilizer-product - (->* () - (#:id exact-nonnegative-integer? #:canonical-name string?) - (or/c fertilizer-product? #f))] + (->* () (#:id db-id? #:canonical-name string?) (or/c fertilizer-product? #f))] [get-fertilizer-product-values (-> fertilizer-product-or-id/c nutrient-value-hash/c)] [get-fertilizer-product-value (-> fertilizer-product-or-id/c nutrient? maybe-nutrient-value?)] @@ -22,7 +20,9 @@ db sql "../db/conn.rkt" - "nutrient.rkt") + "nutrient.rkt" + "nutrient-value.rkt" + "utils.rkt") (struct fertilizer-product (id canonical-name nutrient-values brand-name) #:transparent @@ -46,12 +46,11 @@ (~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 fertilizer-product-or-id/c (or/c fertilizer-product? db-id?)) (define (->fp-id fp-or-id) (match fp-or-id - [(? fertilizer-product-id? id) id] + [(? db-id? id) id] [(fertilizer-product id _ _ _) id])) ;; CREATE diff --git a/models/nutrient-measurement.rkt b/models/nutrient-measurement.rkt index bc7a463..88177da 100644 --- a/models/nutrient-measurement.rkt +++ b/models/nutrient-measurement.rkt @@ -9,7 +9,7 @@ [create-nutrient-measurement! (-> string? nutrient-value-hash/c nutrient-measurement?)] [get-nutrient-measurements (-> (listof nutrient-measurement?))] [get-nutrient-measurement - (->* () (#:id exact-nonnegative-integer? #:date string?) (or/c nutrient-measurement? #f))] + (->* () (#:id db-id? #:date string?) (or/c nutrient-measurement? #f))] [get-nutrient-measurement-values (-> nutrient-measurement-or-id/c nutrient-value-hash/c)] [get-nutrient-measurement-value (-> nutrient-measurement-or-id/c nutrient? maybe-nutrient-value?)] @@ -22,7 +22,9 @@ db sql "../db/conn.rkt" - "nutrient.rkt") + "nutrient.rkt" + "nutrient-value.rkt" + "utils.rkt") (struct nutrient-measurement (id measurement-date nutrient-values) #:transparent @@ -38,12 +40,11 @@ (~a (nutrient-canonical-name n) #:min-width 14) (~a v #:max-width 6 #:align 'right))))) -(define nutrient-measurement-id? exact-nonnegative-integer?) -(define nutrient-measurement-or-id/c (or/c nutrient-measurement? nutrient-measurement-id?)) +(define nutrient-measurement-or-id/c (or/c nutrient-measurement? db-id?)) (define (->nm-id nm-or-id) (match nm-or-id - [(? nutrient-measurement-id? id) id] + [(? db-id? id) id] [(nutrient-measurement id _ _) id])) ;; CREATE diff --git a/models/nutrient.rkt b/models/nutrient.rkt index cbf6bb1..397e1e1 100644 --- a/models/nutrient.rkt +++ b/models/nutrient.rkt @@ -9,11 +9,7 @@ (contract-out [create-nutrient! (-> string? 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))] + (->* () (#:id db-id? #:name string? #:formula string?) (or/c nutrient? #f))] [update-nutrient! (->* (nutrient?) (#:name (or/c #f string?) #:formula (or/c #f string?)) |