Skip to content

Instantly share code, notes, and snippets.

@tony-o
Last active July 19, 2017 16:49
Show Gist options
  • Save tony-o/458a31973a3a1e55e3549e42ef943c9d to your computer and use it in GitHub Desktop.
Save tony-o/458a31973a3a1e55e3549e42ef943c9d to your computer and use it in GitHub Desktop.

Zef::API::Validator

unit module Zef::API::Validated;

multi sub trait_mod:<is>(Sub $s, :$validated!) is export {
  &$s.wrap(&validate-params);
}

sub validate-params($req, $res) {
  'stash'.say;
  dd $req.params<stash>; #is dying here
  '/stash'.say;
  callsame;
}

Zef::Controller::Search

unit module Zef::Controller::Search;
use Zef::API::Validated;
use Zef::API::Middleware;
use Zef::API::DB;

sub search($req, $res) 
#  is json-check 
  is validated
  is export
{
  my %query = (
    %(
      module => ( '-like' => "\%{$req.params<module>}%" ),
    ), 
    %(
      qw<author>.map({
        $req.params<body>{$_}.defined 
          ?? ($_ => ( '-like' => "\%{$req.params<body>{$_}}%" ) )
          !! ();
      })
    )
  );
  my $search = db.search('version', %query);
  my @results;
  while $search.next -> $module {
    @results.push(%(
      qw<version version author author commit_id commit module module>.map(-> $db, $key {
        $key => $module.get($db)
      });
    ));
  }
  $res.close(to-json({ status => 0, results => @results }));
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment