diff options
Diffstat (limited to 'models')
| -rw-r--r-- | models/crop-rotation.rkt | 44 |
1 files changed, 11 insertions, 33 deletions
diff --git a/models/crop-rotation.rkt b/models/crop-rotation.rkt index abff080..1033b8e 100644 --- a/models/crop-rotation.rkt +++ b/models/crop-rotation.rkt @@ -4,12 +4,9 @@ crop-rotation? crop-rotation-id (rename-out [crop-rotation-rotation-date crop-rotation-date] - [crop-rotation-requirement-proportions crop-rotation-requirements] - [crop-rotation-nutrient-measurement-id crop-rotation-measurement-id]) + [crop-rotation-requirement-proportions crop-rotation-requirements]) (contract-out [create-crop-rotation! - (->* (string? requirement-proportion-hash/c) - (#:nutrient-measurement exact-nonnegative-integer?) - 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))] @@ -23,13 +20,7 @@ "nutrient.rkt" "crop-requirement.rkt") -(struct crop-rotation (id rotation-date requirement-proportions nutrient-measurement-id) - #:transparent - #:guard (λ (id rotation-date requirement-proportions nutrient-measurement-id _) - (values id - rotation-date - requirement-proportions - (if (sql-null? nutrient-measurement-id) #f nutrient-measurement-id)))) +(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?)) @@ -38,23 +29,15 @@ (define (->cr-id cr-or-id) (match cr-or-id [(? crop-rotation-id? id) id] - [(crop-rotation id _ _ _) id] + [(crop-rotation id _ _) id] [#f (error '->nt-id "#f can not be converted to an id")])) ;; CREATE -(define (create-crop-rotation! rotation-date - requirement-proportions - #:nutrient-measurement [nutrient-measurement-id #f]) +(define (create-crop-rotation! rotation-date requirement-proportions) (or (get-crop-rotation #:date rotation-date) (with-tx - (if nutrient-measurement-id - (query-exec (current-conn) - (insert #:into crop_rotations - #:set [rotation_date ,rotation-date] - [nutrient_measurement_id ,nutrient-measurement-id])) - (query-exec (current-conn) - (insert #:into crop_rotations #:set [rotation_date ,rotation-date]))) + (query-exec (current-conn) (insert #:into crop_rotations #:set [rotation_date ,rotation-date])) (define cr-id (query-value (current-conn) (select id #:from crop_rotations #:where (= rotation_date ,rotation-date)))) @@ -76,27 +59,23 @@ (define (residuals->requirement-proportion-hash residuals) (for/hash ([r (in-list residuals)]) (match-define (vector requirement-id proportion) r) - (values requirement-id proportion))) + (values (get-crop-requirement #:id requirement-id) proportion))) (define (grouped-row->crop-rotation grouped-row) - (match-define (vector cr-id cr-rotation-date cr-nutrient-measurement-id residuals) grouped-row) - (crop-rotation cr-id - cr-rotation-date - (residuals->requirement-proportion-hash residuals) - cr-nutrient-measurement-id)) + (match-define (vector cr-id cr-rotation-date residuals) grouped-row) + (crop-rotation cr-id cr-rotation-date (residuals->requirement-proportion-hash residuals))) (define (get-crop-rotations) (define grouped-rows (query-rows (current-conn) (select cr.id cr.rotation_date - cr.nutrient_measurement_id crr.crop_requirement_id crr.proportion_percent #:from (TableExpr:AST ,joined) #:order-by cr.rotation_date #:desc) - #:group '#(0 1 2))) + #:group '#(0 1))) (map grouped-row->crop-rotation grouped-rows)) (define (get-crop-rotation #:id [cr-id #f] #:date [rotation-date #f]) @@ -111,14 +90,13 @@ (query-rows (current-conn) (select cr.id cr.rotation_date - cr.nutrient_measurement_id crr.crop_requirement_id crr.proportion_percent #:from (TableExpr:AST ,joined) #:where (ScalarExpr:AST ,where) #:order-by cr.rotation_date #:desc) - #:group '#(0 1 2))) + #:group '#(0 1))) (match grouped-rows ['() #f] [(list grouped-row) (grouped-row->crop-rotation grouped-row)] |