Make fertilizer product brand name obligatory.

Commit
09e2b04a7b469daa4b8b6fd89b88ae800d612fd6
Author
Marius Peter <dev@marius-peter.com>
Author date
Committer
Marius Peter <dev@marius-peter.com>
Committer date
Changed files
db/migrations.rkt
index 4007db44..0147210e 100644..100644
@@ -154,9 +154,9 @@
154 154 (list (create-table #:if-not-exists fertilizer_products
155 155 #:columns [id integer #:not-null]
156 156 [canonical_name text #:not-null]
157 Removed: [brand_name text]
157 Added: [brand_name text #:not-null]
158 158 #:constraints (primary-key id)
159 Removed: (unique canonical_name))))
159 Added: (unique canonical_name brand_name))))
160 160
161 161 (module+ test
162 162 (connect!)
db/seed.rkt
index 0284becf..2d9f00bd 100644..100644
@@ -99,10 +99,7 @@
99 99 (define n (get-nutrient #:formula (car fertilizer-component)))
100 100 (define v (string->number (cdr fertilizer-component)))
101 101 (values n v)))
102 Removed: (cond
103 Removed: [(non-empty-string? brand-name)
104 Removed: (create-fertilizer-product! canonical-name nutrient-values brand-name)]
105 Removed: [else (create-fertilizer-product! canonical-name nutrient-values)]))
102 Added: (create-fertilizer-product! canonical-name brand-name nutrient-values))
106 103 (with-tx (csv-for-each row->seed! next-row)))
107 104
108 105 (define seed-sequence
formlets.rkt
index 6ae4551e..cc89fc0f 100644..100644
@@ -87,14 +87,12 @@
87 87 requirements*))])
88 88 (values effective-on nutrient-values))))
89 89
90 Removed: (define (name-formlet)
91 Removed: (define string-input (input #:type "string" #:attributes `((class "form-control"))))
92 Removed: (formlet (#%# (div ((class "mb-3")) ,{=> string-input string-value-b}))
93 Removed: (bytes->string/utf-8 (binding:form-value string-value-b))))
90 Added: (define string-input
91 Added: (to-string (required (text-input #:attributes '((class "form-control") [required "required"])))))
94 92
95 93 (define (fertilizer-formlet)
96 Removed: (formlet* (#%# `(div ((class "mb-3")) (h5 "Nom de référence") ,{=>* (name-formlet) canonical-name*})
97 Removed: `(div ((class "mb-3")) (h5 "Nom de marque") ,{=>* (name-formlet) brand-name*})
94 Added: (formlet* (#%# `(div ((class "mb-3")) (h5 "Nom de référence") ,{=>* string-input canonical-name*})
95 Added: `(div ((class "mb-3")) (h5 "Nom de marque") ,{=>* string-input brand-name*})
98 96 `(div ((class "mb-3"))
99 97 (h5 "Valeurs de l'intrant")
100 98 ,@(for/list ([nutrient (get-nutrients)])
@@ -104,4 +102,4 @@
104 102 [nutrient-values (for/hash ([nv (filter pair? nutrient-values*)])
105 103 (values (car nv) (cdr nv)))]
106 104 [brand-name (first brand-name*)])
107 Removed: (values canonical-name nutrient-values brand-name))))
105 Added: (values canonical-name brand-name nutrient-values))))
handlers.rkt
index 7304d18b..a6b6a13b 100644..100644
@@ -99,14 +99,12 @@
99 99 (response/xexpr #:preamble #"<!DOCTYPE html>" (new-fertilizer-page)))
100 100
101 101 (define (create-fertilizer req)
102 Removed: (define-values (canonical-name nutrient-values brand-name)
102 Added: (define-values (canonical-name brand-name nutrient-values)
103 103 (formlet-process (fertilizer-formlet) req))
104 Removed: (create-fertilizer-product! canonical-name nutrient-values brand-name)
104 Added: (create-fertilizer-product! canonical-name brand-name nutrient-values)
105 105 (redirect-to "/ferti"))
106 106
107 107 ;; Fallback
108 108
109 109 (define (fallback _)
110 110 (response/xexpr #:preamble #"<!DOCTYPE html>" (fallback-page 404)))
111 Removed:
112 Removed: (define secured-dispatch (wrap-basic-auth app-dispatch))
models/fertilizer-product.rkt
index f9965c29..347d141b 100644..100644
@@ -6,17 +6,16 @@
6 6 (rename-out [fertilizer-product-canonical-name fertilizer-name]
7 7 [fertilizer-product-nutrient-values fertilizer-product-values]
8 8 [fertilizer-product-brand-name fertilizer-brand-name])
9 Removed: (contract-out [create-fertilizer-product!
10 Removed: (->* (string? nutrient-value-hash/c) (string?) fertilizer-product?)]
11 Removed: [get-fertilizer-products (-> (listof fertilizer-product?))]
12 Removed: [get-fertilizer-product
13 Removed: (->* ()
14 Removed: (#:id (or/c #f exact-nonnegative-integer?)
15 Removed: #:canonical-name (or/c #f string?))
16 Removed: (or/c fertilizer-product? #f))]
17 Removed: [get-fertilizer-product-values (-> fertilizer-product? nutrient-value-hash/c)]
18 Removed: [get-fertilizer-product-value (-> fertilizer-product? nutrient? number?)]
19 Removed: [delete-fertilizer-product! (-> fertilizer-product? void?)]))
9 Added: (contract-out
10 Added: [create-fertilizer-product! (-> string? string? nutrient-value-hash/c fertilizer-product?)]
11 Added: [get-fertilizer-products (-> (listof fertilizer-product?))]
12 Added: [get-fertilizer-product
13 Added: (->* ()
14 Added: (#:id (or/c #f exact-nonnegative-integer?) #:canonical-name (or/c #f string?))
15 Added: (or/c fertilizer-product? #f))]
16 Added: [get-fertilizer-product-values (-> fertilizer-product? nutrient-value-hash/c)]
17 Added: [get-fertilizer-product-value (-> fertilizer-product? nutrient? (or/c #f number?))]
18 Added: [delete-fertilizer-product! (-> fertilizer-product? void?)]))
20 19
21 20 (require racket/contract
22 21 db
@@ -45,17 +44,14 @@
45 44
46 45 ;; CREATE
47 46
48 Removed: (define (create-fertilizer-product! canonical-name nutrient-values [brand-name #f])
47 Added: (define (create-fertilizer-product! canonical-name brand-name nutrient-values)
49 48 (or
50 49 (get-fertilizer-product #:canonical-name canonical-name)
51 50 (with-tx
52 51 (query-exec (current-conn)
53 Removed: (cond
54 Removed: [brand-name
55 Removed: (insert #:into fertilizer_products
56 Removed: #:set [canonical_name ,canonical-name]
57 Removed: [brand_name ,brand-name])]
58 Removed: [else (insert #:into fertilizer_products #:set [canonical_name ,canonical-name])]))
52 Added: (insert #:into fertilizer_products
53 Added: #:set [canonical_name ,canonical-name]
54 Added: [brand_name ,brand-name]))
59 55 (define fp-id
60 56 (query-value (current-conn)
61 57 (select id #:from fertilizer_products #:where (= canonical_name ,canonical-name))))