diff options
| author | Marius Peter <dev@marius-peter.com> | 2025-10-21 21:33:31 +0200 |
|---|---|---|
| committer | Marius Peter <dev@marius-peter.com> | 2025-10-21 21:33:31 +0200 |
| commit | 8efe7a50f84f1ec5e79cf328c0161337db16410f (patch) | |
| tree | bf14e0648aae790b7a844de6b83ed8c2f77cc3f1 | |
| parent | 7986b70b022f54b5ca5ae2d9f5eaff29bdbdae88 (diff) | |
Added crop accessors for the crop requirement entity.
| -rw-r--r-- | models/crop-requirement.rkt | 44 |
1 files changed, 18 insertions, 26 deletions
diff --git a/models/crop-requirement.rkt b/models/crop-requirement.rkt index d4eb78b..e668c40 100644 --- a/models/crop-requirement.rkt +++ b/models/crop-requirement.rkt @@ -4,32 +4,22 @@ ;; Struct definitions crop-requirement crop-requirement? - crop-requirement-id crop-requirement-profile + crop-requirement-id crop-requirement-profile crop-requirement-crop-id ;; SQL CRUD (contract-out [create-crop-requirement! (->* (string? - (listof (cons/c - nutrient? - number?))) + (listof (cons/c nutrient? number?))) ((or/c #f crop?)) crop-requirement?)] [get-crop-requirements (-> (listof crop-requirement?))] [get-crop-requirement (->* () - (#:id - (or/c #f exact-nonnegative-integer?) - #:profile - (or/c #f string?)) + (#:id (or/c #f exact-nonnegative-integer?) + #:profile (or/c #f string?)) (or/c crop-requirement? #f))] - [get-crop-requirement-values (-> crop-requirement? - (listof (cons/c - nutrient? - number?)))] - [get-crop-requirement-value (-> crop-requirement? - nutrient? - number?)] + [get-crop-requirement-values (-> crop-requirement? (listof (cons/c nutrient? number?)))] + [get-crop-requirement-value (-> crop-requirement? nutrient? number?)] [get-latest-crop-requirement-value (-> nutrient? number?)] - [delete-crop-requirement! (-> crop-requirement? - void?)])) + [delete-crop-requirement! (-> crop-requirement? void?)])) (require racket/contract db @@ -39,7 +29,7 @@ "crop.rkt") ;; Instances of this struct are persisted in the crop_requirements table. -(struct crop-requirement (id profile) #:transparent) +(struct crop-requirement (id profile crop-id) #:transparent) ;; CREATE @@ -79,33 +69,35 @@ ;; READ (define (get-crop-requirements) - (for/list ([(id* profile*) + (for/list ([(id* profile* crop-id*) (in-query (current-conn) - (select id profile + (select id profile crop_id #:from crop_requirements #:order-by id #:asc))]) - (crop-requirement id* profile*))) + (crop-requirement id* profile* (if (sql-null? crop-id*) #f crop-id*)))) (define (get-crop-requirement #:id [id #f] - #:profile [profile #f]) + #:profile [profile #f] + #:crop [crop #f]) (define (where-expr) (define clauses (filter values (list (and id (format "id = ~e" id)) - (and profile (format "profile = ~e" profile))))) + (and profile (format "profile = ~e" profile)) + (and crop (format "crop_id = ~e" (crop-id crop)))))) (cond [(null? clauses) ""] [else (format "WHERE ~a" (string-join clauses " AND "))])) (define query (string-join - `("SELECT id, profile" + `("SELECT id, profile, crop_id" "FROM crop_requirements" ,(where-expr) "ORDER BY id ASC" "LIMIT 1"))) (match (query-maybe-row (current-conn) query) - [(vector id* profile*) - (crop-requirement id* profile*)] + [(vector id* profile* crop-id*) + (crop-requirement id* profile* crop-id*)] [#f #f])) (define (get-crop-requirement-values crop-requirement) |