diff options
| author | Marius Peter <dev@marius-peter.com> | 2025-11-17 17:47:17 +0100 |
|---|---|---|
| committer | Marius Peter <dev@marius-peter.com> | 2025-11-17 17:47:17 +0100 |
| commit | c0f93e8d41188fc4138a350430ee349b61ea0535 (patch) | |
| tree | 5d88fd1195d65521c5e1a787cd773047605b7e72 /models/crop.rkt | |
| parent | 02ef60dd46676b5069aeae666b544b62f270ffd1 (diff) | |
raco fmt.
Diffstat (limited to 'models/crop.rkt')
| -rw-r--r-- | models/crop.rkt | 91 |
1 files changed, 35 insertions, 56 deletions
diff --git a/models/crop.rkt b/models/crop.rkt index 51b332d..edbb7a3 100644 --- a/models/crop.rkt +++ b/models/crop.rkt @@ -1,22 +1,20 @@ #lang racket -(provide - ;; Model struct - crop - crop? - crop-id crop-name - (contract-out - ;; SQL CRUD - [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?)])) +;; Model struct +(provide crop + crop? + crop-id + crop-name + ;; SQL CRUD + (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?)])) (require racket/contract db @@ -25,67 +23,48 @@ (struct crop (id name) #:transparent) - ;; CREATE (define (create-crop! name) (or (get-crop #:name name) (begin - (query-exec (current-conn) - (insert #:into crops - #:set [canonical_name ,name])) + (query-exec (current-conn) (insert #:into crops #:set [canonical_name ,name])) (get-crop #:name name)))) - ;; READ (define (get-crops) - (for/list ([(id* name*) - (in-query (current-conn) - (select id canonical_name - #:from crops - #:order-by id #:asc))]) + (for/list ([(id* name*) (in-query (current-conn) + (select id canonical_name #:from crops #:order-by id #:asc))]) (crop id* name*))) -(define (get-crop #:id [id #f] - #:name [name #f]) +(define (get-crop #:id [id #f] #:name [name #f]) (define where (cond - [(and id name) - (scalar-expr-qq (and (= id ,id) - (= canonical_name ,name)))] - [id - (scalar-expr-qq (= id ,id))] - [name - (scalar-expr-qq (= canonical_name ,name))])) - (define query (select id canonical_name - #:from crops - #:where (ScalarExpr:AST ,where) - #:order-by id #:asc - #:limit 1)) + [(and id name) (scalar-expr-qq (and (= id ,id) (= canonical_name ,name)))] + [id (scalar-expr-qq (= id ,id))] + [name (scalar-expr-qq (= canonical_name ,name))])) + (define query + (select id + canonical_name + #:from crops + #:where (ScalarExpr:AST ,where) + #:order-by id + #:asc + #:limit 1)) (match (query-maybe-row (current-conn) query) - [(vector id* name*) - (crop id* name*)] + [(vector id* name*) (crop id* name*)] [#f #f])) - ;; UPDATE -(define (update-crop! id - #:name [name #f]) +(define (update-crop! id #:name [name #f]) (cond - [name - (query-exec (current-conn) - (update crops - #:set [canonical_name ,name] - #:where (= id ,id)))] + [name (query-exec (current-conn) (update crops #:set [canonical_name ,name] #:where (= id ,id)))] [else (void)]) - (or (get-crop #:id id) - (error 'update-crop! "No crop with id ~a" id))) - + (or (get-crop #:id id) (error 'update-crop! "No crop with id ~a" id))) ;; DELETE (define (delete-crop! id) - (query-exec (current-conn) - (delete #:from crops #:where (= id ,id)))) + (query-exec (current-conn) (delete #:from crops #:where (= id ,id)))) |