add support for scaling recipes to web-interface

This commit is contained in:
Christoffer Müller Madsen 2017-04-08 21:12:14 +02:00
parent 92c70970f5
commit cb2225715f
2 changed files with 32 additions and 21 deletions

View File

@ -36,17 +36,17 @@
;; Predicates ;; Predicates
(define contains-ingredient? (define contains-ingredient?
(lambda (recipe search-ingredient) (lambda (search-ingredient recipe)
(contains-ingredients? recipe (cons search-ingredient '())))) (contains-ingredients? (list search-ingredient) recipe)))
(define contains-ingredients? (define contains-ingredients?
(lambda (recipe search-ingredients) (lambda (search-ingredients recipe)
(let ((ingredients (recipe-ingredients recipe))) (let ((ingredients (recipe-ingredients recipe)))
(cond [(null? search-ingredients) #t] (cond [(null? search-ingredients) #t]
[(ingredient-by-name ingredients [(ingredient-by-name (car search-ingredients)
(car search-ingredients)) ingredients)
(contains-ingredients? recipe (contains-ingredients? (cdr search-ingredients)
(cdr search-ingredients))] recipe)]
[else #f]) [else #f])
))) )))
@ -63,18 +63,18 @@
(ingredient-ref (cdr ingredients) (- n 1))))) (ingredient-ref (cdr ingredients) (- n 1)))))
(define ingredient-by-name (define ingredient-by-name
(lambda (ingredients search-name) (lambda (search-name ingredient-list)
(let ((ingredient (car ingredients))) (let ((ingredient (car ingredient-list)))
(cond [(equal? (ingredient-name ingredient) (cond [(equal? (ingredient-name ingredient)
search-name) search-name)
ingredient] ingredient]
[(null? (cdr ingredients)) #f] [(null? (cdr ingredient-list)) #f]
[else (ingredient-by-name (cdr ingredients) [else (ingredient-by-name search-name
search-name)])))) (cdr ingredient-list))]))))
(define recipes-by-ingredients (define recipes-by-ingredients
(lambda (recipes search-ingredients) (lambda (search-ingredients recipes)
(filter (lambda (x) (contains-ingredients? x search-ingredients)) (filter (lambda (x) (contains-ingredients? search-ingredients x))
recipes))) recipes)))
(define recipe-by-name (define recipe-by-name
@ -89,15 +89,15 @@
;; Manipulation ;; Manipulation
(define scale-recipe (define scale-recipe-by-servings
(lambda (recipe wanted-servings) (lambda (wanted-servings recipe)
(let ([name (recipe-name recipe)] (let ([name (recipe-name recipe)]
[ingredients (recipe-ingredients recipe)] [ingredients (recipe-ingredients recipe)]
[steps (recipe-steps recipe)] [steps (recipe-steps recipe)]
[servings (recipe-servings recipe)]) [servings (recipe-servings recipe)])
(let ([new-name name] (let ([new-name name]
[new-ingredients (map (lambda (ingr) [new-ingredients (map (lambda (ingr)
(list (* (/ (car ingr) (cons (* (/ (car ingr)
servings) servings)
wanted-servings) wanted-servings)
(cdr ingr))) (cdr ingr)))
@ -108,9 +108,9 @@
)))) ))))
(define scale-recipe-by-factor (define scale-recipe-by-factor
(lambda (recipe scale-factor) (lambda (scale-factor recipe)
(let ([new-servings (* (recipe-servings recipe) scale-factor)]) (let ([new-servings (* (recipe-servings recipe) scale-factor)])
(scale-recipe recipe new-servings)))) (scale-recipe-by-servings new-servings recipe))))
;; Constructors ;; Constructors

View File

@ -3,15 +3,26 @@ require 'sinatra'
require 'htmlentities' require 'htmlentities'
get '/' do get '/' do
servings = params['servings']
scaling_factor = params['scale']
`cd ..; echo "(display (html-wrap (cookbook-as-html cookbook)))" | petite -q "batch.scm"` `cd ..; echo "(display (html-wrap (cookbook-as-html cookbook)))" | petite -q "batch.scm"`
end end
get '/ingredient/:ingredient' do get '/ingredient/:ingredient' do
ingredient = HTMLEntities.new.decode params['ingredient'] ingredient = HTMLEntities.new.decode params['ingredient']
`cd ..; echo "(display (html-wrap (cookbook-as-html (recipes-by-ingredients cookbook '(\\"#{ingredient}\\")))))" | petite -q "batch.scm"` `cd ..; echo "(display (html-wrap (cookbook-as-html (recipes-by-ingredients '(\\"#{ingredient}\\" cookbook)))))" | petite -q "batch.scm"`
end end
get '/recipe/:title' do get '/recipe/:title' do
title = HTMLEntities.new.decode params['title'] title = HTMLEntities.new.decode params['title']
`cd ..; echo "(display (html-wrap (recipe-as-html (recipe-by-name cookbook \\"#{title}\\"))))" | petite -q "batch.scm"` servings = params['servings']
scaling_factor = params['scale']
if servings
puts "servings"
`cd ..; echo "(display (html-wrap (recipe-as-html (scale-recipe-by-servings #{servings} (recipe-by-name \\"#{title}\\" cookbook)))))" | petite -q "batch.scm"`
elsif scaling_factor
`cd ..; echo "(display (html-wrap (recipe-as-html (scale-recipe-by-factor #{scaling_factor} (recipe-by-name \\"#{title}\\" cookbook)))))" | petite -q "batch.scm"`
else
`cd ..; echo "(display (html-wrap (recipe-as-html (recipe-by-name \\"#{title}\\" cookbook))))" | petite -q "batch.scm"`
end
end end