Github user mitchell852 commented on a diff in the pull request:

    
https://github.com/apache/incubator-trafficcontrol/pull/544#discussion_r115861924
  
    --- Diff: traffic_ops/app/lib/API/ApiCapability.pm ---
    @@ -0,0 +1,256 @@
    +package API::ApiCapability;
    +#
    +#
    +# Licensed under the Apache License, Version 2.0 (the "License");
    +# you may not use this file except in compliance with the License.
    +# You may obtain a copy of the License at
    +#
    +#     http://www.apache.org/licenses/LICENSE-2.0
    +#
    +# Unless required by applicable law or agreed to in writing, software
    +# distributed under the License is distributed on an "AS IS" BASIS,
    +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    +# See the License for the specific language governing permissions and
    +# limitations under the License.
    +#
    +#
    +#
    +
    +use UI::Utils;
    +
    +use Mojo::Base 'Mojolicious::Controller';
    +use Data::Dumper;
    +
    +
    +
    +my $finfo = __FILE__ . ":";
    +
    +my %valid_http_methods = map { $_ => 1 } ('GET', 'POST', 'PUT', 'PATCH', 
'DELETE');
    +
    +sub index {
    +   my $self       = shift;
    +   my $capability = $self->param('capability');
    +
    +   my %criteria;
    +   if ( defined $capability ) {
    +           $criteria{'me.capability'} = $capability;
    +   }
    +   my @data;
    +   my $orderby = "capability";
    +   $orderby = $self->param('orderby') if ( defined $self->param('orderby') 
);
    +
    +   my $rs_data = $self->db->resultset("ApiCapability")->search( 
\%criteria, { prefetch => ['capability'], order_by => $orderby } );
    +   while ( my $row = $rs_data->next ) {
    +           push(
    +                   @data, {
    +                           "id"            => $row->id,
    +                           "httpMethod"    => $row->http_method,
    +                           "route"                 => $row->route,
    +                           "capName"       => $row->capability->name,
    +                           "lastUpdated"   => $row->last_updated
    +                   }
    +           );
    +   }
    +   $self->success( \@data );
    +}
    +
    +
    +sub renderResults {
    +   my $self = shift;
    +   my $rs_data = shift;
    +
    +   my @data = ();
    +   while ( my $row = $rs_data->next ) {
    +           push(
    +                   @data, {
    +                           "id"            => $row->id,
    +                           "httpMethod"    => $row->http_method,
    +                           "route"                 => $row->route,
    +                           "capName"       => $row->capability->name,
    +                           "lastUpdated"   => $row->last_updated
    +                   }
    +           );
    +   }
    +   $self->success( \@data );
    +}
    +
    +sub show {
    +   my $self = shift;
    +   my $id = $self->param('id');
    +
    +   my $rs_data = $self->db->resultset("ApiCapability")->search( 'me.id' => 
$id );
    +   $self->renderResults( $rs_data ) ;
    +}
    +
    +sub is_mapping_valid {
    +   my $self = shift;
    +   my $id = shift;
    +   my $http_method = shift;
    +   my $route = shift;
    +   my $capability = shift;
    +
    +   if ( !defined($http_method) ) {
    +           return ( undef, "HTTP method is required." );
    +   }
    +
    +   if ( !exists( $valid_http_methods{ $http_method } ) ) {
    +           return ( undef, "HTTP method \'$http_method\' is invalid. Valid 
values are: " . join(", ", sort keys %valid_http_methods ) );
    +   }
    +
    +   if ( !defined($route) or $route eq "" ) {
    +           return ( undef, "Route is required." );
    +   }
    +
    +   if ( !defined($capability) or $capability eq "" ) {
    +           return (undef, "Capability name is required." );
    +   }
    +   # check if capability exists
    +   my $rs_data = $self->db->resultset("Capability")->search( { 'name' => { 
'like', $capability } } )->single();
    +   if (!defined($rs_data)) {
    +           return (undef, "Capability '$capability' does not exist." );
    +   }
    +
    +   # search a mapping for the same http_method & route
    +   $rs_data = $self->db->resultset("ApiCapability")->search( { 'route' => 
{ 'like', $route } } )->search( {
    +           'http_method' => { '=', $http_method } } )->single();
    +   # if adding a new entry, make sure it is unique
    +   if ( !defined( $id ) ) {
    +           if (defined($rs_data)) {
    +                   my $allocated_capability = $rs_data->capability->name;
    +                   return (undef, "HTTP method '$http_method', route 
'$route' are already mapped to capability: $allocated_capability" );
    +           }
    +   }
    +   else {
    +           if (defined($rs_data)) {
    +                   my $lid = $rs_data->id;
    +                   if ($lid ne $id) {
    +                           my $allocated_capability = 
$rs_data->capability->name;
    +                           return (undef, "HTTP method '$http_method', 
route '$route' are already mapped to capability: $allocated_capability" );
    +                   }
    +           }
    +   }
    +
    +   return ( 1, undef );
    +}
    +
    +sub create {
    +   my $self = shift;
    +   my $params = $self->req->json;
    +
    +   if ( !&is_oper($self) ) {
    +           return $self->forbidden();
    +   }
    +
    +   if ( !defined($params) ) {
    +           return $self->alert("Parameters must be in JSON format.");
    +   }
    +
    +   my $http_method = $params->{httpMethod} if 
defined($params->{httpMethod});
    +   my $route = $params->{route} if defined($params->{route});
    +   my $capability = $params->{capName} if defined($params->{capName});
    +   my $id = undef;
    +
    +   my ( $is_valid, $errStr ) = $self->is_mapping_valid( $id, $http_method, 
$route, $capability );
    +   if ( !$is_valid ) {
    +           return $self->alert( $errStr );
    +   }
    +
    +   my $values = {
    +           id                      => 
$self->db->resultset('ApiCapability')->get_column('id')->max() + 1,
    --- End diff --
    
    Is there a reason you are specifying the id and not letting the database do 
it? I'm not sure I've seen this done before in our codebase...


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

Reply via email to