summaryrefslogtreecommitdiff
path: root/models/crop-requirement.rkt
diff options
context:
space:
mode:
Diffstat (limited to 'models/crop-requirement.rkt')
-rw-r--r--models/crop-requirement.rkt54
1 files changed, 25 insertions, 29 deletions
diff --git a/models/crop-requirement.rkt b/models/crop-requirement.rkt
index e668c40..4faa6bb 100644
--- a/models/crop-requirement.rkt
+++ b/models/crop-requirement.rkt
@@ -35,35 +35,31 @@
;; CREATE
(define (create-crop-requirement! profile nutrient-values [crop #f])
- (define existing-crop-requirement (get-crop-requirement #:profile profile))
- (define (new-crop-requirement)
- (with-tx
- (query-exec (current-conn)
- (if crop
- (insert #:into crop_requirements
- #:set [crop_id ,(crop-id crop)] [profile ,profile])
- (insert #:into crop_requirements
- #:set [profile ,profile])))
- (define cr-id (crop-requirement-id (get-crop-requirement #:profile profile)))
- (query-exec (current-conn)
- (insert #:into nutrient_value_sets
- #:set [crop_requirement_id ,cr-id]))
- (define nvs-id (query-value (current-conn)
- (select id
- #:from nutrient_value_sets
- #:where (= crop_requirement_id ,cr-id))))
- (for ([nv nutrient-values])
- (match nv
- [(cons n v)
- (query-exec (current-conn)
- (insert #:into nutrient_values
- #:set
- [value_set_id ,nvs-id]
- [nutrient_id ,(nutrient-id n)]
- [value_ppm ,v]))])))
- (get-crop-requirement #:profile profile))
- (or existing-crop-requirement
- (new-crop-requirement)))
+ (or (get-crop-requirement #:profile profile)
+ (with-tx
+ (query-exec (current-conn)
+ (if crop
+ (insert #:into crop_requirements
+ #:set [crop_id ,(crop-id crop)] [profile ,profile])
+ (insert #:into crop_requirements
+ #:set [profile ,profile])))
+ (define cr-id (crop-requirement-id (get-crop-requirement #:profile profile)))
+ (query-exec (current-conn)
+ (insert #:into nutrient_value_sets
+ #:set [crop_requirement_id ,cr-id]))
+ (define nvs-id (query-value (current-conn)
+ (select id
+ #:from nutrient_value_sets
+ #:where (= crop_requirement_id ,cr-id))))
+ (for ([nv nutrient-values])
+ (match-define (cons n v) nv)
+ (query-exec (current-conn)
+ (insert #:into nutrient_values
+ #:set
+ [value_set_id ,nvs-id]
+ [nutrient_id ,(nutrient-id n)]
+ [value_ppm ,v])))
+ (get-crop-requirement #:profile profile))))
;; READ
Copyright 2019--2025 Marius PETER