Only add footer to successful requests
[app-web-oof.git] / lib / App / Web / Oof.pm
CommitLineData
6e33dd68
MG
1package App::Web::Oof;
2
3use 5.014000;
4use strict;
5use warnings;
6use utf8;
7use parent qw/Plack::Component/;
8
39be4169 9our $VERSION = '0.000_002';
6e33dd68
MG
10
11use DBIx::Simple;
1576fc41 12use File::Slurp;
6e33dd68
MG
13use HTML::TreeBuilder;
14use HTML::Element::Library;
15use JSON::MaybeXS qw/encode_json decode_json/;
16use Plack::Builder;
17use Plack::Request;
1576fc41 18use Try::Tiny;
6e33dd68
MG
19
20sub HTML::Element::iter3 {
21 my ($self, $data, $code) = @_;
22 my $orig = $self;
23 my $prev = $orig;
24 for my $el (@$data) {
25 my $current = $orig->clone;
26 $code->($el, $current);
27 $prev->postinsert($current);
28 $prev = $current;
29 }
30 $orig->detach;
31}
32
33sub HTML::Element::fid { shift->look_down(id => shift) }
34sub HTML::Element::fclass { shift->look_down(class => qr/\b$_[0]\b/) }
35
36##################################################
37
1576fc41
MG
38my %db;
39my ($form, $continue, $order, $details, $pay);
6e33dd68
MG
40
41{
42 sub parse_html {
43 my $builder = HTML::TreeBuilder->new;
44 $builder->ignore_unknown(0);
45 $builder->parse_file("tmpl/$_[0].html");
46 $builder
47 }
48
49 $form = parse_html 'form';
50 $continue = parse_html 'continue';
51 $order = parse_html 'order';
1576fc41
MG
52 $details = parse_html 'details';
53 $pay = parse_html 'pay';
6e33dd68
MG
54}
55
56sub stringify_money { sprintf "£%.2f", $_[0] / 100 }
57
1576fc41
MG
58sub make_slug {
59 my $slug = $_[0];
60 $slug =~ y/ /-/;
61 $slug =~ y/a-zA-Z0-9-//cd;
62 $slug
63}
64
6e33dd68
MG
65sub form_table_row {
66 my ($data, $tr) = @_;
67 $tr->fclass($_)->replace_content($data->{$_}) for qw/title subtitle stock/;
68 $tr->fclass('price')->replace_content(stringify_money $data->{price});
69 $tr->fclass('title')->attr('data-product', $data->{product});
1576fc41
MG
70 $tr->fclass('title')->attr('href', '/details/'.$data->{product}.'/'.make_slug $data->{title});
71# $tr->fclass('title')->attr('data-summary', $data->{summary});
6e33dd68
MG
72 $tr->look_down(_tag => 'input')->attr(max => $data->{stock});
73 $tr->look_down(_tag => 'input')->attr(name => 'quant'.$data->{product});
74}
75
76sub form_app {
77 my ($env) = @_;
1576fc41 78 $db{$$} //= DBIx::Simple->connect($ENV{OOF_DSN} // 'dbi:Pg:');
6e33dd68 79
1576fc41 80 my $data = $db{$$}->select(products => '*', {}, 'product')->hashes;
6e33dd68
MG
81 my $tree = $form->clone;
82 $tree->find('tbody')->find('tr')->iter3($data, \&form_table_row);
83
84 [200, ['Content-type' => 'text/html; charset=utf-8'], [$tree->as_HTML]]
85}
86
87sub continue_table_row {
88 my ($data, $tr) = @_;
89 $tr->fclass($_)->replace_content($data->{$_}) for qw/title subtitle quantity/;
90 $tr->fclass('price')->replace_content(stringify_money $data->{subtotal});
91 $tr->fclass('title')->attr('data-product', $data->{product});
92}
93
94sub continue_app {
95 my ($env) = @_;
1576fc41 96 $db{$$} //= DBIx::Simple->connect($ENV{OOF_DSN} // 'dbi:Pg:');
6e33dd68
MG
97 my $tree = $continue->clone;
98 my $req = Plack::Request->new($env);
99 my $params = $req->body_parameters;
100
101 my ($quant, $total, @data, @notes);
102 for (sort keys %$params) {
103 next unless /^quant/;
104 next unless $params->{$_};
1576fc41 105 my $data = $db{$$}->select(products => '*', {product => substr $_, 5})->hash;
6e33dd68
MG
106 $data->{quantity} = $params->{$_};
107 if ($data->{stock} == 0) {
108 push @notes, 'Item is out of stock and was removed from order: '.$data->{title};
109 next
110 }
111 if ($data->{quantity} > $data->{stock}) {
112 $data->{quantity} = $data->{stock};
113 push @notes, 'Not enough units of "'.$data->{title}.'" available. Quantity reduced to '.$data->{quantity}
114 }
115 $data->{subtotal} = $data->{price} * $data->{quantity};
116 $quant += $data->{quantity};
117 $total += $data->{subtotal};
118 push @data, $data
119 }
120
1576fc41
MG
121 return [500, ['Content-type' => 'text/plain'], ['Error: no items in order.']] unless $quant;
122
6e33dd68
MG
123 $tree->fid('subtotal')->replace_content(stringify_money $total);
124 my $dvalue;
125 if ($params->{discount}) {
1576fc41 126 my $discount = $db{$$}->select(discounts => '*', {discount => $params->{discount}})->hash;
6e33dd68
MG
127 if (!defined $discount) {
128 push @notes, 'Discount code incorrect. No discount applied.'
1576fc41 129 } elsif ($db{$$}->select(orders => 'COUNT(*)', {discount => $params->{discount}})->list) {
6e33dd68
MG
130 push @notes, 'Discount code already used once. No discount applied.'
131 } else {
132 $dvalue = int (0.5 + $discount->{fraction} * $total) if $discount->{fraction};
133 $dvalue = $discount->{flat} if $discount->{flat};
134 $tree->fid('discount')->replace_content('-'.stringify_money $dvalue);
135 $total -= $dvalue;
136 $tree->look_down(name => 'discount')->attr(value => $params->{discount});
137 push @notes, 'Discount applied.'
138 }
139 }
140 $tree->look_down(name => 'discount')->detach unless $dvalue;
141 $tree->fid('discount_tr')->detach unless $dvalue;
142 my $postage = 220 + 50 * $quant;
143 $tree->fid('postage')->replace_content(stringify_money $postage);
144 $total += $postage;
145 $tree->fid('total')->replace_content(stringify_money $total);
146
147 $tree->fid('order')->find('tbody')->find('tr')->iter3(\@data, \&continue_table_row);
148 $tree->iter($tree->fid('notes')->find('li') => @notes);
149
150 $tree->look_down(name => 'products')->attr(value => encode_json \@data);
151 $tree->look_down(name => 'total')->attr(value => $total);
152
153 [200, ['Content-type' => 'text/html; charset=utf-8'], [$tree->as_HTML]]
154}
155
156sub order_app {
157 my ($env) = @_;
1576fc41 158 $db{$$} //= DBIx::Simple->connect($ENV{OOF_DSN} // 'dbi:Pg:');
6e33dd68
MG
159 my $tree = $order->clone;
160 my $req = Plack::Request->new($env);
1576fc41
MG
161 my ($id) = $env->{PATH_INFO} =~ m,^/([0-9A-F]+),;
162 if ($id) {
163 my $total = $db{$$}->select(orders => 'total', {id => $id})->list;
164 $tree->fid('orderid')->replace_content($id);
165 $tree->look_down(name => 'order')->attr(value => $id);
166 $tree->fid('total')->replace_content(stringify_money $total);
167 $tree->find('script')->attr('data-amount', $total);
168 return [200, ['Content-type' => 'text/html; charset=utf-8'], [$tree->as_HTML]]
169 } else {
170 my %parms = %{$req->body_parameters};
171 my $id = sprintf "%X%04X", time, $$;
172 my $err;
173 try {
174 $db{$$}->begin_work;
175 my $products = decode_json $req->body_parameters->{products};
176 for my $prod (@$products) {
177 my $stock = $db{$$}->select(products => 'stock', {product => $prod->{product}})->list;
178 die "Not enough of " .$prod->{title}."\n" if $prod->{quantity} > $stock;
179 $db{$$}->update(products => {stock => $stock - $prod->{quantity}}, {product => $prod->{product}});
180 }
181 $db{$$}->insert(orders => {id => $id, %parms});
182 $db{$$}->commit;
183 } catch {
184 $db{$$}->rollback;
185 $err = [500, ['Content-type', 'text/plain'], ["Error: $_"]]
186 };
187 return $err if $err;
188 return [303, [Location => "/order/$id"], []]
6e33dd68 189 }
1576fc41
MG
190}
191
192sub details_list_element {
193 my ($data, $li) = @_;
194 $li->find('a')->attr(href => "/$data");
195 my $thumb = $data =~ s/fullpics/thumbs/r;
196 $thumb = $data unless -f $thumb;
197 $li->find('img')->attr(src => "/$thumb");
198}
199
200sub details_app {
201 my ($env) = @_;
202 $db{$$} //= DBIx::Simple->connect($ENV{OOF_DSN} // 'dbi:Pg:');
203 my $tree = $details->clone;
204 my ($id) = $env->{PATH_INFO} =~ m,^/(\d+),;
205 my $title = $db{$$}->select(products => 'title', {product => $id})->list;
206 my @pics = <static/fullpics/$id-*>;
207 my $slug = make_slug $title;
208 $tree->find('title')->replace_content("Pictures of $title");
209 $tree->find('h2')->replace_content($title);
210 $tree->look_down(rel => 'canonical')->attr(href => "/details/$id/$slug");
211 $tree->fid('pictures')->iter3(\@pics, \&details_list_element);
6e33dd68 212
6e33dd68
MG
213 [200, ['Content-type' => 'text/html; charset=utf-8'], [$tree->as_HTML]]
214}
215
1576fc41
MG
216sub pay_app {
217 my ($env) = @_;
218 my $req = Plack::Request->new($env);
219 $db{$$} //= DBIx::Simple->connect($ENV{OOF_DSN} // 'dbi:Pg:');
220 my $order = $req->body_parameters->{order};
221 my $token = $req->body_parameters->{stripeToken};
222 return [500, ['Content-type' => 'text/html; charset=utf-8'], ['No token received, payment did not succeed.']] unless $token;
223 $db{$$}->update(orders => {stripe_token => $token}, {id => $order});
224 [200, ['Content-type' => 'text/html; charset=utf-8'], [$pay->as_HTML]];
225}
226
6e33dd68 227sub app {
1576fc41 228 my $footer = read_file 'tmpl/footer.html';
6e33dd68 229 builder {
1576fc41
MG
230 enable sub {
231 my $app = shift;
232 sub {
233 my $res = $app->(@_);
fb23ef22 234 push @{$res->[2]}, $footer if $res->[0] == 200;
1576fc41
MG
235 $res;
236 }
237 };
6e33dd68
MG
238 mount '/' => sub { [301, [Location => '/form'], []] };
239 mount '/form' => \&form_app;
240 mount '/continue' => \&continue_app;
241 mount '/order' => \&order_app;
1576fc41
MG
242 mount '/details' => \&details_app;
243 mount '/pay' => \&pay_app;
6e33dd68
MG
244 }
245}
246
2471;
248__END__
249
250=head1 NAME
251
252App::Web::Oof - Oversimplified order form / ecommerce website
253
254=head1 SYNOPSIS
255
256 use App::Web::Oof;
257
258=head1 DESCRIPTION
259
260Oof (Oversimplified order form) is a very simple ecommerce website.
39be4169
MG
261It is the code behind L<https://ledparts4you.uk.to>.
262
263This version is reasonably functional, yet not very reusable, hence
264the version number.
6e33dd68
MG
265
266=head1 AUTHOR
267
268Marius Gavrilescu, E<lt>marius@ieval.roE<gt>
269
270=head1 COPYRIGHT AND LICENSE
271
272Copyright (C) 2016 by Marius Gavrilescu
273
274This library is free software; you can redistribute it and/or modify
275it under the same terms as Perl itself, either Perl version 5.22.1 or,
276at your option, any later version of Perl 5 you may have available.
277
278
279=cut
This page took 0.027671 seconds and 4 git commands to generate.