- my $id = sprintf "%X", time; # Not good enough!
-
- $db->begin_work;
- $db->insert(orders => {id => $id, %{$req->body_parameters}});
- my $products = decode_json $req->body_parameters->{products};
- for my $prod (@$products) {
- my $stock = $db->select(products => 'stock', {product => $prod->{product}})->list;
- die "Not enough of " .$prod->{title}."\n" if $prod->{quantity} > $stock;
- $db->update(products => {stock => $stock - $prod->{quantity}}, {product => $prod->{product}});
+ my ($id) = $env->{PATH_INFO} =~ m,^/([0-9A-F]+),;
+ if ($id) {
+ my $total = $db{$$}->select(orders => 'total', {id => $id})->list;
+ $tree->fid('orderid')->replace_content($id);
+ $tree->look_down(name => 'order')->attr(value => $id);
+ $tree->fid('total')->replace_content(stringify_money $total);
+ $tree->find('script')->attr('data-amount', $total);
+ return [200, ['Content-type' => 'text/html; charset=utf-8'], [$tree->as_HTML]]
+ } else {
+ my %parms = %{$req->body_parameters};
+ my $id = sprintf "%X%04X", time, $$;
+ my $err;
+ try {
+ $db{$$}->begin_work;
+ my $products = decode_json $req->body_parameters->{products};
+ for my $prod (@$products) {
+ my $stock = $db{$$}->select(products => 'stock', {product => $prod->{product}})->list;
+ die "Not enough of " .$prod->{title}."\n" if $prod->{quantity} > $stock;
+ $db{$$}->update(products => {stock => $stock - $prod->{quantity}}, {product => $prod->{product}});
+ }
+ $db{$$}->insert(orders => {id => $id, %parms});
+ $db{$$}->commit;
+ } catch {
+ $db{$$}->rollback;
+ $err = [500, ['Content-type', 'text/plain'], ["Error: $_"]]
+ };
+ return $err if $err;
+ return [303, [Location => "/order/$id"], []]