summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--db/migrations.rkt4
-rw-r--r--db/seed.rkt5
-rw-r--r--formlets.rkt12
-rw-r--r--handlers.rkt6
-rw-r--r--models/fertilizer-product.rkt32
5 files changed, 24 insertions, 35 deletions
diff --git a/db/migrations.rkt b/db/migrations.rkt
index 4007db4..0147210 100644
--- a/db/migrations.rkt
+++ b/db/migrations.rkt
@@ -154,9 +154,9 @@
(list (create-table #:if-not-exists fertilizer_products
#:columns [id integer #:not-null]
[canonical_name text #:not-null]
- [brand_name text]
+ [brand_name text #:not-null]
#:constraints (primary-key id)
- (unique canonical_name))))
+ (unique canonical_name brand_name))))
(module+ test
(connect!)
diff --git a/db/seed.rkt b/db/seed.rkt
index 0284bec..2d9f00b 100644
--- a/db/seed.rkt
+++ b/db/seed.rkt
@@ -99,10 +99,7 @@
(define n (get-nutrient #:formula (car fertilizer-component)))
(define v (string->number (cdr fertilizer-component)))
(values n v)))
- (cond
- [(non-empty-string? brand-name)
- (create-fertilizer-product! canonical-name nutrient-values brand-name)]
- [else (create-fertilizer-product! canonical-name nutrient-values)]))
+ (create-fertilizer-product! canonical-name brand-name nutrient-values))
(with-tx (csv-for-each row->seed! next-row)))
(define seed-sequence
diff --git a/formlets.rkt b/formlets.rkt
index 6ae4551..cc89fc0 100644
--- a/formlets.rkt
+++ b/formlets.rkt
@@ -87,14 +87,12 @@
requirements*))])
(values effective-on nutrient-values))))
-(define (name-formlet)
- (define string-input (input #:type "string" #:attributes `((class "form-control"))))
- (formlet (#%# (div ((class "mb-3")) ,{=> string-input string-value-b}))
- (bytes->string/utf-8 (binding:form-value string-value-b))))
+(define string-input
+ (to-string (required (text-input #:attributes '((class "form-control") [required "required"])))))
(define (fertilizer-formlet)
- (formlet* (#%# `(div ((class "mb-3")) (h5 "Nom de référence") ,{=>* (name-formlet) canonical-name*})
- `(div ((class "mb-3")) (h5 "Nom de marque") ,{=>* (name-formlet) brand-name*})
+ (formlet* (#%# `(div ((class "mb-3")) (h5 "Nom de référence") ,{=>* string-input canonical-name*})
+ `(div ((class "mb-3")) (h5 "Nom de marque") ,{=>* string-input brand-name*})
`(div ((class "mb-3"))
(h5 "Valeurs de l'intrant")
,@(for/list ([nutrient (get-nutrients)])
@@ -104,4 +102,4 @@
[nutrient-values (for/hash ([nv (filter pair? nutrient-values*)])
(values (car nv) (cdr nv)))]
[brand-name (first brand-name*)])
- (values canonical-name nutrient-values brand-name))))
+ (values canonical-name brand-name nutrient-values))))
diff --git a/handlers.rkt b/handlers.rkt
index 7304d18..a6b6a13 100644
--- a/handlers.rkt
+++ b/handlers.rkt
@@ -99,14 +99,12 @@
(response/xexpr #:preamble #"<!DOCTYPE html>" (new-fertilizer-page)))
(define (create-fertilizer req)
- (define-values (canonical-name nutrient-values brand-name)
+ (define-values (canonical-name brand-name nutrient-values)
(formlet-process (fertilizer-formlet) req))
- (create-fertilizer-product! canonical-name nutrient-values brand-name)
+ (create-fertilizer-product! canonical-name brand-name nutrient-values)
(redirect-to "/ferti"))
;; Fallback
(define (fallback _)
(response/xexpr #:preamble #"<!DOCTYPE html>" (fallback-page 404)))
-
-(define secured-dispatch (wrap-basic-auth app-dispatch))
diff --git a/models/fertilizer-product.rkt b/models/fertilizer-product.rkt
index f9965c2..347d141 100644
--- a/models/fertilizer-product.rkt
+++ b/models/fertilizer-product.rkt
@@ -6,17 +6,16 @@
(rename-out [fertilizer-product-canonical-name fertilizer-name]
[fertilizer-product-nutrient-values fertilizer-product-values]
[fertilizer-product-brand-name fertilizer-brand-name])
- (contract-out [create-fertilizer-product!
- (->* (string? nutrient-value-hash/c) (string?) fertilizer-product?)]
- [get-fertilizer-products (-> (listof fertilizer-product?))]
- [get-fertilizer-product
- (->* ()
- (#:id (or/c #f exact-nonnegative-integer?)
- #:canonical-name (or/c #f string?))
- (or/c fertilizer-product? #f))]
- [get-fertilizer-product-values (-> fertilizer-product? nutrient-value-hash/c)]
- [get-fertilizer-product-value (-> fertilizer-product? nutrient? number?)]
- [delete-fertilizer-product! (-> fertilizer-product? void?)]))
+ (contract-out
+ [create-fertilizer-product! (-> string? string? nutrient-value-hash/c fertilizer-product?)]
+ [get-fertilizer-products (-> (listof fertilizer-product?))]
+ [get-fertilizer-product
+ (->* ()
+ (#:id (or/c #f exact-nonnegative-integer?) #:canonical-name (or/c #f string?))
+ (or/c fertilizer-product? #f))]
+ [get-fertilizer-product-values (-> fertilizer-product? nutrient-value-hash/c)]
+ [get-fertilizer-product-value (-> fertilizer-product? nutrient? (or/c #f number?))]
+ [delete-fertilizer-product! (-> fertilizer-product? void?)]))
(require racket/contract
db
@@ -45,17 +44,14 @@
;; CREATE
-(define (create-fertilizer-product! canonical-name nutrient-values [brand-name #f])
+(define (create-fertilizer-product! canonical-name brand-name nutrient-values)
(or
(get-fertilizer-product #:canonical-name canonical-name)
(with-tx
(query-exec (current-conn)
- (cond
- [brand-name
- (insert #:into fertilizer_products
- #:set [canonical_name ,canonical-name]
- [brand_name ,brand-name])]
- [else (insert #:into fertilizer_products #:set [canonical_name ,canonical-name])]))
+ (insert #:into fertilizer_products
+ #:set [canonical_name ,canonical-name]
+ [brand_name ,brand-name]))
(define fp-id
(query-value (current-conn)
(select id #:from fertilizer_products #:where (= canonical_name ,canonical-name))))
Copyright 2019--2026 Marius PETER