Add timestamp to item (back-end)



use DateTime::Tiny;


date => DateTime::Tiny->now,


snippets/10/lib/D2/Ajax.pm
post '/api/v2/item' => sub {
        my $text = param('text') // '';
        $text =~ s/^\s+|\s+$//g;
        if ($text eq '') {
            return to_json { error => 'No text provided' };
        }
     
        my $items = _mongodb('items');
        $items->insert({
            text => $text,
            date => DateTime::Tiny->now,
        });
        return to_json { ok => 1, text => $text };
};

mongo client


$ mongo
test> use d2-ajax
d2-ajax> db.items.find()
{
  "_id": ObjectId("557593b5a114607aa9188b91"),
  "date": ISODate("2015-06-08T16:08:05Z"),
  "text": "new item"
}
Fetched 1 record(s) in 3ms