summaryrefslogtreecommitdiff
path: root/models/crop-requirement.rkt
diff options
context:
space:
mode:
authorMarius Peter <dev@marius-peter.com>2025-12-06 18:11:22 +0100
committerMarius Peter <dev@marius-peter.com>2025-12-06 18:11:22 +0100
commita23660b2edb802bdf0fcc65a40d3294fc235cb4e (patch)
treebeba9fa822c904caf358b08b9981f1c00ebe5f8e /models/crop-requirement.rkt
parentc3557a7d5f54d7a9cb7fb2ece9487332c264236e (diff)
Update model entity persistency (creation) logic.
This greatly reduces the amount of SQL INSERT queries.
Diffstat (limited to 'models/crop-requirement.rkt')
-rw-r--r--models/crop-requirement.rkt38
1 files changed, 17 insertions, 21 deletions
diff --git a/models/crop-requirement.rkt b/models/crop-requirement.rkt
index 256305e..96d94cf 100644
--- a/models/crop-requirement.rkt
+++ b/models/crop-requirement.rkt
@@ -40,27 +40,23 @@
;; CREATE
(define (create-crop-requirement! profile nutrient-values [crop #f])
- (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
- (query-value (current-conn) (select id #:from crop_requirements #:where (= 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 ([(n v) (in-hash nutrient-values)])
- (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))))
+ (with-tx
+ (define cr-id
+ (insert-id
+ (query (current-conn)
+ (if crop
+ (insert #:into crop_requirements #:set [crop_id ,(crop-id crop)] [profile ,profile])
+ (insert #:into crop_requirements #:set [profile ,profile])))))
+ (define nvs-id
+ (insert-id (query (current-conn)
+ (insert #:into nutrient_value_sets #:set [crop_requirement_id ,cr-id]))))
+ (insert-nutrient-values (current-conn) nvs-id nutrient-values)
+ (crop-requirement cr-id
+ profile
+ (if crop
+ (crop-id crop)
+ #f)
+ nutrient-values)))
;; READ
Copyright 2019--2026 Marius PETER