-
Notifications
You must be signed in to change notification settings - Fork 3
/
tacos.pl
277 lines (233 loc) · 9.34 KB
/
tacos.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
use Mojolicious::Lite;
use Mojo::SQLite;
use POSIX qw(strftime);
app->plugin('Config');
helper sizes => sub { 'M', 'L', 'L Mixte', 'XL', 'XXL', 'Giga' };
helper sizes_prices => sub { (M => 8, L => 9, 'L Mixte' => 10, XL => 16, XXL => 24, Giga => 32) };
helper sizes_max_meat => sub { my %max = (M => 1, L => 1, 'L Mixte' => 3, XL => 3, XXL => 4, Giga => 5); $max{ $_[1] } };
helper meats => sub { 'Viande hachée', 'Escalope de poulet', 'Cordon bleu', 'Merguez', 'Nuggets', 'Kebab', 'Soudjouk', 'Végétarien' };
helper garnishes => sub { 'Frites', 'Cheddar', 'Gruyère', 'Salade', 'Tomate', 'Oignons', 'Carottes', 'Cornichons' };
helper sauces => sub { 'Fromagère', 'Ketchup', 'Mayonnaise', 'Cocktail', 'Blanche', 'Barbecue', 'Américaine', 'Biggy burger', 'Tartare', 'Curry', 'Cheezy', 'Andalouse', 'Algérienne', 'Marocaine', 'Harissa', 'Samouraï', 'Poivre' };
helper price => sub { my %prices = shift->sizes_prices; $prices{ shift() } };
helper sqlite => sub { state $sqlite = Mojo::SQLite->new('sqlite:tacos.db') };
helper hashtag => sub { shift->sqlite->db->select('hashtags', undef, undef, { -desc => 'id' })->hash };
helper tacos_to_order => sub { $_[0]->sqlite->db->select('tacos', undef, { hashtag_id => $_[0]->hashtag->{id} })->hashes };
app->sqlite->migrations->name('tacos')->from_string(<<SQL)->migrate;
-- 1 up
create table hashtags (id integer primary key autoincrement, name text);
create table tacos (id integer primary key autoincrement, hashtag_id integer, name text, size text, meat text, garnish text, sauce text);
-- 1 down
drop table hashtags;
drop table tacos;
-- 2 up
alter table hashtags add column created_at datetime default 0;
alter table tacos add column created_at datetime default 0;
-- 2 down
SQL
get '/' => 'form';
post '/' => sub {
my $c = shift;
my $tacos = {
hashtag_id => $c->hashtag->{id},
name => $c->param('name'),
size => $c->param('size'),
meat => join(', ', @{ $c->every_param('meat') }),
garnish => join(', ', @{ $c->every_param('garnish') }),
sauce => join(', ', @{ $c->every_param('sauce') }),
created_at => strftime('%F %X', localtime),
};
my $max_meat_count = $c->sizes_max_meat($tacos->{size});
my $message = "Il faut prendre $max_meat_count viandes avec cette taille!";
if (@{ $c->every_param('meat') } <= $max_meat_count) {
$message = 'Tacos ajouté!';
$c->sqlite->db->insert('tacos', $tacos);
}
return $c->render('form', message => $message);
};
get '/hashtag' => 'hashtag';
post '/hashtag' => sub {
my $c = shift;
$c->sqlite->db->insert('hashtags', { name => $c->param('hashtag'), created_at => strftime('%F %X', localtime) });
return $c->render('hashtag', message => 'Nouvelle commande!');
};
get '/delete/:tacos_id' => sub {
my $c = shift;
$c->sqlite->db->delete('tacos', { id => $c->stash('tacos_id') });
$c->flash(message => 'Tacos supprimé!');
return $c->redirect_to('/');
};
get '/whatsapp' => 'whatsapp';
app->defaults('message' => undef);
app->start;
__DATA__
@@ whatsapp.html.ep
% layout 'main';
% my %prices = sizes_prices();
% my $all_tacos = tacos_to_order();
% my @all_tacos = @{ $all_tacos };
% my $sizes_count = $all_tacos->reduce(sub { $a->{$b->{size}}++; $a }, {});
% my $sizes_label = join ', ', map { "$sizes_count->{$_} $_" } grep { $sizes_count->{$_} } sizes();
% my $total = $all_tacos->reduce(sub { $a + price($b->{size}) }, 0);
<textarea cols="100" rows="10">Total: <%= $total %> CHF
% foreach my $tacos (@all_tacos) {
<%= $tacos->{name} %>: <%= price($tacos->{size}) %>.-
% }
</textarea><hr>
<textarea cols="100" rows="20">Bonjour, j'aimerais commander <%= scalar @all_tacos %> tacos :
<%= $sizes_label %>.
% foreach my $tacos (@all_tacos) {
=============================================
*<%= $tacos->{size} %>* :
*Viande* : <%= $tacos->{meat} %>
*Garniture* : <%= $tacos->{garnish} %>
*Sauce* : <%= $tacos->{sauce} %>
% }
=============================================
Livraison à l'arrêt M1 EPFL.
À 11h30.
Merci et bonne journée.</textarea>
@@ hashtag.html.ep
% layout 'main';
<h2 style="color:red">!!! en envoyant un hashtag, tu démarres une toute nouvelle commande !!!</h2>
<p>
<img class="tacospin" src="https://media.giphy.com/media/3xz2BxlT8yngXMEX4Y/giphy.gif">
<img class="tacospin" src="https://media.giphy.com/media/lDpFBflBkkxfq/giphy.gif">
<img class="tacospin" src="https://media.giphy.com/media/3xz2BxlT8yngXMEX4Y/giphy.gif">
</p>
<form method="post" action="/hashtag">
<label for="hashtag">Hashtag de la commande: </label>
<input type="text" id="hashtag" name="hashtag">
<input type="submit">
</form>
@@ form.html.ep
% layout 'main';
% my $hashtag = hashtag();
<p>
<img class="tacospin" src="https://media.giphy.com/media/7if9hGmIHjrTG/giphy.gif">
<img class="tacospin" src="https://media.giphy.com/media/7if9hGmIHjrTG/giphy.gif">
<img class="tacospin" src="https://media.giphy.com/media/7if9hGmIHjrTG/giphy.gif">
</p>
<h2><%= $hashtag->{name} %></h2>
<h3><%= $hashtag->{created_at} %></h3>
%= form_for '/' => (method => 'POST') => begin
<p>
%= label_for 'name' => 'Say your name:'
%= text_field 'name', id => 'name', required => 'required', autofocus => 'autofocus'
</p>
<p class="sizes">
Taille:
% foreach my $size (sizes()) {
<span id="span-<%=$size%>" class="size tacosButton">
%= radio_button size => $size, id => $size, required => 'required'
%= label_for $size => begin
<%= $size %> (<%= sizes_max_meat($size) %> viandes, <%= price($size) %>.-)
<span class="decoButton"></span>
%= end
</span>
% }
</p>
<p class="meats">
Viandes:
% foreach my $meat (meats()) {
<span id="span-<%=$meat%>" class="meat tacosButton">
%= check_box meat => $meat, id => $meat
%= label_for $meat => begin
<%= $meat %>
<span class="decoButton"></span>
%= end
</span>
% }
</p>
<p class="garnishes">
Garnitures:
% foreach my $garnish (garnishes()) {
<span id="span-<%=$garnish%>" class="garnish tacosButton">
%= check_box garnish => $garnish, id => $garnish
%= label_for $garnish => begin
<%= $garnish %>
<span class="decoButton <%= $garnish %>"></span>
%= end
</span>
% }
</p>
<p class="sauces">
Sauces:
% foreach my $sauce (sauces()) {
<span id="span-<%=$sauce%>" class="sauce tacosButton">
%= check_box sauce => $sauce, id => $sauce
%= label_for $sauce => begin
<%= $sauce %>
<span class="decoButton <%= $sauce %>" ></span>
%= end
</span>
% }
</p>
%= submit_button 'ORDER MY TACOS'
% end
% foreach my $tacos (@{ tacos_to_order() }) {
<hr/>
Pour <%= $tacos->{name} %><br>
Size <%= $tacos->{size} %><br>
[<%= $tacos->{meat} %>],<br>
[<%= $tacos->{garnish} %>],<br>
[<%= $tacos->{sauce} %>]<br>
<a href="/delete/<%= $tacos->{id} %>" style="color:red">Supprimer ce tacos</a>
% }
<script>
let name = document.querySelector('#name')
name.addEventListener('keyup', () => localStorage.name = name.value)
name.value = localStorage.name || ''
</script>
@@ layouts/main.html.ep
<!DOCTYPE html>
<html>
<head>
<title>Enforced modularity for TACOS</title>
<link rel="shortcut icon" href="/icons/icon-16.png" type="image/x-icon">
<link rel="manifest" href="tacos.webmanifest">
<link type="text/css" rel="stylesheet" href="tacos.css">
<meta name="color-scheme" content="dark light">
<style>
% foreach my $garnish (garnishes()) {
.tacosButton > input:checked + label .decoButton.<%= $garnish %> { background-image: url('garnishEnabled-<%= $garnish %>.svg'); }
% }
% foreach my $sauce (sauces()) {
.tacosButton > input:checked + label .decoButton.<%= $sauce =~ tr/ /./r %> { background-image: url('sauceEnabled-<%= $sauce %>.svg'); }
% }
</style>
<meta charset="utf-8">
</head>
<body>
<script>
let getInitialColorMode = () => {
const persistedColorPreference = window.localStorage.getItem('color-mode')
const hasPersistedPreference = typeof persistedColorPreference === 'string'
if (hasPersistedPreference)
return persistedColorPreference
const mql = window.matchMedia('(prefers-color-scheme: dark)')
const hasMediaQueryPreference = typeof mql.matches === 'boolean'
if (hasMediaQueryPreference)
return mql.matches ? 'dark' : 'light'
return 'light'
}
const colorMode = getInitialColorMode()
const body = document.querySelector('body')
body.classList.add(colorMode)
</script>
<h1>Mmmh TACOS <button id="btn-color-mode"></button></h1>
<h2><%== $message || " " %></h2>
<%= content %>
<p><a href="/">Home</a> | <a href="/hashtag">Nouvelle commande</a> | <a href="/whatsapp">Message WhatsApp</a> | <a href="https://github.com/ttreyer/Tacos-2.0/issues">Feetback/Contributions</a></p>
<script>
let toggleColorMode = () => {
const currentColorMode = getInitialColorMode()
const invertColorMode = currentColorMode === 'light' ? 'dark' : 'light'
window.localStorage.setItem('color-mode', invertColorMode)
body.classList.remove(currentColorMode)
body.classList.add(invertColorMode)
}
document.querySelector('#btn-color-mode')
.addEventListener('click', toggleColorMode)
</script>
</body>
</html>