MT#7119 API order_by for collections

most collections support ordering by the column names of
their primary table now
ipeshinskaya/InvoiceTemplate5
Gerhard Jungwirth 11 years ago
parent a0c598ea45
commit 83d37c5d23

@ -73,11 +73,7 @@ sub GET :Allow {
my $rows = $c->request->params->{rows} // 10;
{
my $fees = $self->item_rs($c);
my $total_count = int($fees->count);
$fees = $fees->search(undef, {
page => $page,
rows => $rows,
});
(my $total_count, $fees) = $self->paginate_order_collection($c, $fees);
my (@embedded, @links);
my $form = $self->get_form($c);
for my $fee ($fees->all) {

@ -73,11 +73,7 @@ sub GET :Allow {
my $rows = $c->request->params->{rows} // 10;
{
my $profiles = $self->item_rs($c);
my $total_count = int($profiles->count);
$profiles = $profiles->search(undef, {
page => $page,
rows => $rows,
});
(my $total_count, $profiles) = $self->paginate_order_collection($c, $profiles);
my (@embedded, @links);
my $form = $self->get_form($c);
for my $profile ($profiles->all) {

@ -73,11 +73,7 @@ sub GET :Allow {
my $rows = $c->request->params->{rows} // 10;
{
my $zones = $self->item_rs($c);
my $total_count = int($zones->count);
$zones = $zones->search(undef, {
page => $page,
rows => $rows,
});
(my $total_count, $zones) = $self->paginate_order_collection($c, $zones);
my (@embedded, @links);
my $form = $self->get_form($c);
for my $zone ($zones->all) {

@ -89,11 +89,7 @@ sub GET :Allow {
{
my $dsets = $self->item_rs($c);
my $total_count = int($dsets->count);
$dsets = $dsets->search(undef, {
page => $page,
rows => $rows,
});
(my $total_count, $dsets) = $self->paginate_order_collection($c, $dsets);
my (@embedded, @links);
for my $dset ($dsets->search({}, {order_by => {-asc => 'me.id'}})->all) {
push @embedded, $self->hal_from_item($c, $dset, "cfdestinationsets");

@ -66,11 +66,7 @@ sub GET :Allow {
{
my $items = $self->item_rs($c); # items is actually a voip_subscribers
my $total_count = int($items->count);
$items = $items->search(undef, {
page => $page,
rows => $rows,
});
(my $total_count, $items) = $self->paginate_order_collection($c, $items);
my (@embedded, @links);
for my $subs ($items->search({}, {order_by => {-asc => 'me.id'}})->all) {
push @embedded, $self->hal_from_item($c, $subs, "cfmappings");

@ -89,11 +89,7 @@ sub GET :Allow {
{
my $timesets = $self->item_rs($c);
my $total_count = int($timesets->count);
$timesets = $timesets->search(undef, {
page => $page,
rows => $rows,
});
(my $total_count, $timesets) = $self->paginate_order_collection($c, $timesets);
my (@embedded, @links);
for my $tset ($timesets->search({}, {order_by => {-asc => 'me.id'}})->all) {
push @embedded, $self->hal_from_item($c, $tset, "cftimesets");

@ -64,11 +64,7 @@ sub GET :Allow {
my $rows = $c->request->params->{rows} // 10;
{
my $cfs = $self->item_rs($c, "callforwards");
my $total_count = int($cfs->count);
$cfs = $cfs->search(undef, {
page => $page,
rows => $rows,
});
(my $total_count, $cfs) = $self->paginate_order_collection($c, $cfs);
my (@embedded, @links);
for my $cf ($cfs->all) {
try {

@ -99,11 +99,7 @@ sub GET :Allow {
my $rows = $c->request->params->{rows} // 10;
{
my $items = $self->item_rs($c);
my $total_count = int($items->count);
$items = $items->search(undef, {
page => $page,
rows => $rows,
});
(my $total_count, $items) = $self->paginate_order_collection($c, $items);
my (@embedded, @links);
my $form = $self->get_form($c);
for my $item ($items->search({}, {order_by => {-asc => 'me.id'}})->all) {

@ -85,11 +85,7 @@ sub GET :Allow {
my $rows = $c->request->params->{rows} // 10;
{
my $contracts = $self->item_rs($c);
my $total_count = int($contracts->count);
$contracts = $contracts->search(undef, {
page => $page,
rows => $rows,
});
(my $total_count, $contracts) = $self->paginate_order_collection($c, $contracts);
my (@embedded, @links);
my $form = $self->get_form($c);
for my $contract ($contracts->all) {

@ -85,11 +85,7 @@ sub GET :Allow {
my $rows = $c->request->params->{rows} // 10;
{
my $contacts = $self->item_rs($c);
my $total_count = int($contacts->count);
$contacts = $contacts->search(undef, {
page => $page,
rows => $rows,
});
(my $total_count, $contacts) = $self->paginate_order_collection($c, $contacts);
my (@embedded, @links);
my $form = $self->get_form($c);
for my $contact ($contacts->search({}, {order_by => {-asc => 'me.id'}, prefetch => ['reseller']})->all) {

@ -77,11 +77,7 @@ sub GET :Allow {
{
my $field_devs = $self->item_rs($c);
my $total_count = int($field_devs->count);
$field_devs = $field_devs->search(undef, {
page => $page,
rows => $rows,
});
(my $total_count, $field_devs) = $self->paginate_order_collection($c, $field_devs);
my $query_string = $self->query_param_string($c);
return unless $query_string;
my (@embedded, @links);

@ -87,11 +87,7 @@ sub GET :Allow {
my $rows = $c->request->params->{rows} // 10;
{
my $customers = $self->item_rs($c);
my $total_count = int($customers->count);
$customers = $customers->search(undef, {
page => $page,
rows => $rows,
});
(my $total_count, $customers) = $self->paginate_order_collection($c, $customers);
my (@embedded, @links);
my $form = $self->get_form($c);
for my $customer($customers->all) {

@ -56,11 +56,7 @@ sub GET :Allow {
my $rows = $c->request->params->{rows} // 10;
{
my $domains = $self->item_rs($c, "domains");
my $total_count = int($domains->count);
$domains = $domains->search(undef, {
page => $page,
rows => $rows,
});
(my $total_count, $domains) = $self->paginate_order_collection($c, $domains);
my (@embedded, @links);
for my $domain ($domains->search({}, {order_by => {-asc => 'me.id'}})->all) {
push @embedded, $self->hal_from_item($c, $domain, "domains");

@ -87,11 +87,7 @@ sub GET :Allow {
my $rows = $c->request->params->{rows} // 10;
{
my $domains = $self->item_rs($c);
my $total_count = int($domains->count);
$domains = $domains->search(undef, {
page => $page,
rows => $rows,
});
(my $total_count, $domains) = $self->paginate_order_collection($c, $domains);
my (@embedded, @links);
my $form = $self->get_form($c);
for my $domain ($domains->search({}, {order_by => {-asc => 'me.id'}})->all) {

@ -74,11 +74,7 @@ sub GET :Allow {
my $rows = $c->request->params->{rows} // 10;
{
my $items = $self->item_rs($c);
my $total_count = int($items->count);
$items = $items->search(undef, {
page => $page,
rows => $rows,
});
(my $total_count, $items) = $self->paginate_order_collection($c, $items);
my (@embedded, @links);
my $form = $self->get_form($c);
for my $item ($items->search({}, {order_by => {-asc => 'me.id'}, prefetch => ['reseller']})->all) {

@ -86,11 +86,7 @@ sub GET :Allow {
{
my $field_devs = $self->item_rs($c);
my $total_count = int($field_devs->count);
$field_devs = $field_devs->search(undef, {
page => $page,
rows => $rows,
});
(my $total_count, $field_devs) = $self->paginate_order_collection($c, $field_devs);
my (@embedded, @links);
for my $dev ($field_devs->search({}, {order_by => {-asc => 'me.id'}})->all) {
push @embedded, $self->hal_from_item($c, $dev);

@ -86,11 +86,7 @@ sub GET :Allow {
{
my $field_devs = $self->item_rs($c);
my $total_count = int($field_devs->count);
$field_devs = $field_devs->search(undef, {
page => $page,
rows => $rows,
});
(my $total_count, $field_devs) = $self->paginate_order_collection($c, $field_devs);
my (@embedded, @links);
for my $dev ($field_devs->search({}, {order_by => {-asc => 'me.id'}})->all) {
push @embedded, $self->hal_from_item($c, $dev);

@ -75,11 +75,7 @@ sub GET :Allow {
{
my $field_devs = $self->item_rs($c);
my $total_count = int($field_devs->count);
$field_devs = $field_devs->search(undef, {
page => $page,
rows => $rows,
});
(my $total_count, $field_devs) = $self->paginate_order_collection($c, $field_devs);
my (@embedded, @links);
for my $dev ($field_devs->search({}, {order_by => {-asc => 'me.id'}})->all) {
push @embedded, $self->hal_from_item($c, $dev);

@ -97,11 +97,7 @@ sub GET :Allow {
{
my $field_devs = $self->item_rs($c);
my $total_count = int($field_devs->count);
$field_devs = $field_devs->search(undef, {
page => $page,
rows => $rows,
});
(my $total_count, $field_devs) = $self->paginate_order_collection($c, $field_devs);
my (@embedded, @links);
for my $dev ($field_devs->search({}, {order_by => {-asc => 'me.id'}})->all) {
push @embedded, $self->hal_from_item($c, $dev);

@ -78,11 +78,7 @@ sub GET :Allow {
my $rows = $c->request->params->{rows} // 10;
{
my $items = $self->item_rs($c);
my $total_count = int($items->count);
$items = $items->search(undef, {
page => $page,
rows => $rows,
});
(my $total_count, $items) = $self->paginate_order_collection($c, $items);
my (@embedded, @links);
my $form = $self->get_form($c);
for my $item ($items->search({}, {order_by => {-asc => 'me.id'}})->all) {

@ -73,11 +73,7 @@ sub GET :Allow {
my $rows = $c->request->params->{rows} // 10;
{
my $resellers = $self->item_rs($c);
my $total_count = int($resellers->count);
$resellers = $resellers->search(undef, {
page => $page,
rows => $rows,
});
(my $total_count, $resellers) = $self->paginate_order_collection($c, $resellers);
my (@embedded, @links);
my $form = $self->get_form($c);
for my $reseller ($resellers->all) {

@ -97,11 +97,7 @@ sub GET :Allow {
my $rows = $c->request->params->{rows} // 10;
{
my $rwr_set = $self->item_rs($c, "rulesets");
my $total_count = int($rwr_set->count);
$rwr_set = $rwr_set->search(undef, {
page => $page,
rows => $rows,
});
(my $total_count, $rwr_set) = $self->paginate_order_collection($c, $rwr_set);
my (@embedded, @links);
for my $set ($rwr_set->search({}, {order_by => {-asc => 'me.id'}})->all) {
push @embedded, $self->hal_from_item($c, $set, "rewriterulesets");

@ -86,11 +86,7 @@ sub GET :Allow {
{
my $rules = $self->item_rs($c, "rules");
my $total_count = int($rules->count);
$rules = $rules->search(undef, {
page => $page,
rows => $rows,
});
(my $total_count, $rules) = $self->paginate_order_collection($c, $rules);
my (@embedded, @links);
for my $rule ($rules->search({}, {order_by => {-asc => 'me.id'}})->all) {
push @embedded, $self->hal_from_item($c, $rule, "rewriterules");

@ -74,11 +74,7 @@ sub GET :Allow {
my $rows = $c->request->params->{rows} // 10;
{
my $items = $self->item_rs($c);
my $total_count = int($items->count);
$items = $items->search(undef, {
page => $page,
rows => $rows,
});
(my $total_count, $items) = $self->paginate_order_collection($c, $items);
my (@embedded, @links);
my $form = $self->get_form($c);
for my $item ($items->search({}, {order_by => {-asc => 'me.id'}})->all) {

@ -85,11 +85,7 @@ sub GET :Allow {
my $rows = $c->request->params->{rows} // 10;
{
my $items = $self->item_rs($c);
my $total_count = int($items->count);
$items = $items->search(undef, {
page => $page,
rows => $rows,
});
(my $total_count, $items) = $self->paginate_order_collection($c, $items);
my (@embedded, @links);
my $form = $self->get_form($c);
for my $item ($items->search({}, {order_by => {-asc => 'me.id'}})->all) {

@ -81,11 +81,7 @@ sub GET :Allow {
my $rows = $c->request->params->{rows} // 10;
{
my $subscribers = $self->item_rs($c);
my $total_count = int($subscribers->count);
$subscribers = $subscribers->search(undef, {
page => $page,
rows => $rows,
});
(my $total_count, $subscribers) = $self->paginate_order_collection($c, $subscribers);
my (@embedded, @links);
for my $subscriber ($subscribers->search({}, {order_by => {-asc => 'me.id'}})->all) {
push @embedded, $self->hal_from_item($c, $subscriber);

@ -56,11 +56,7 @@ sub GET :Allow {
my $rows = $c->request->params->{rows} // 10;
{
my $subscribers = $self->item_rs($c, "subscribers");
my $total_count = int($subscribers->count);
$subscribers = $subscribers->search(undef, {
page => $page,
rows => $rows,
});
(my $total_count, $subscribers) = $self->paginate_order_collection($c, $subscribers);
my (@embedded, @links);
for my $subscriber ($subscribers->search({}, {order_by => {-asc => 'me.id'}})->all) {
next unless($subscriber->provisioning_voip_subscriber);

@ -74,11 +74,7 @@ sub GET :Allow {
my $rows = $c->request->params->{rows} // 10;
{
my $items = $self->item_rs($c);
my $total_count = int($items->count);
$items = $items->search(undef, {
page => $page,
rows => $rows,
});
(my $total_count, $items) = $self->paginate_order_collection($c, $items);
my (@embedded, @links);
my $form = $self->get_form($c);
for my $item ($items->search({}, {order_by => {-asc => 'me.id'}, prefetch => ['reseller']})->all) {

@ -74,11 +74,7 @@ sub GET :Allow {
my $rows = $c->request->params->{rows} // 10;
{
my $items = $self->item_rs($c);
my $total_count = int($items->count);
$items = $items->search(undef, {
page => $page,
rows => $rows,
});
(my $total_count, $items) = $self->paginate_order_collection($c, $items);
my (@embedded, @links);
my $form = $self->get_form($c);
for my $item ($items->search({}, {order_by => {-asc => 'me.id'}})->all) {

@ -82,11 +82,7 @@ sub GET :Allow {
my $rows = $c->request->params->{rows} // 10;
{
my $items = $self->item_rs($c);
my $total_count = int($items->count);
$items = $items->search(undef, {
page => $page,
rows => $rows,
});
(my $total_count, $items) = $self->paginate_order_collection($c, $items);
my (@embedded, @links);
my $form = $self->get_form($c);
for my $item ($items->search({}, {order_by => {-asc => 'me.id'}})->all) {

@ -141,11 +141,7 @@ sub GET :Allow {
my $rows = $c->request->params->{rows} // 10;
{
my $subscribers = $self->item_rs($c);
my $total_count = int($subscribers->count);
$subscribers = $subscribers->search(undef, {
page => $page,
rows => $rows,
});
(my $total_count, $subscribers) = $self->paginate_order_collection($c, $subscribers);
my (@embedded, @links);
my $form = $self->get_form($c);
for my $subscriber ($subscribers->search({}, {order_by => {-asc => 'me.id'}})->all) {

@ -74,11 +74,7 @@ sub GET :Allow {
my $rows = $c->request->params->{rows} // 10;
{
my $contacts = $self->item_rs($c);
my $total_count = int($contacts->count);
$contacts = $contacts->search(undef, {
page => $page,
rows => $rows,
});
(my $total_count, $contacts) = $self->paginate_order_collection($c, $contacts);
my (@embedded, @links);
my $form = $self->get_form($c);
for my $contact ($contacts->search({}, {order_by => {-asc => 'me.id'}, prefetch => ['reseller']})->all) {

@ -78,11 +78,7 @@ sub GET :Allow {
my $rows = $c->request->params->{rows} // 10;
{
my $items = $self->item_rs($c);
my $total_count = int($items->count);
$items = $items->search(undef, {
page => $page,
rows => $rows,
});
(my $total_count, $items) = $self->paginate_order_collection($c, $items);
my (@embedded, @links);
my $form = $self->get_form($c);
for my $item ($items->search({}, {order_by => {-asc => 'me.id'}})->all) {

@ -78,11 +78,7 @@ sub GET :Allow {
my $rows = $c->request->params->{rows} // 10;
{
my $items = $self->item_rs($c);
my $total_count = int($items->count);
$items = $items->search(undef, {
page => $page,
rows => $rows,
});
(my $total_count, $items) = $self->paginate_order_collection($c, $items);
my (@embedded, @links);
my $form = $self->get_form($c);
for my $item ($items->all) {

@ -75,11 +75,7 @@ sub GET :Allow {
my $rows = $c->request->params->{rows} // 10;
{
my $items = $self->item_rs($c);
my $total_count = int($items->count);
$items = $items->search(undef, {
page => $page,
rows => $rows,
});
(my $total_count, $items) = $self->paginate_order_collection($c, $items);
my (@embedded, @links);
for my $item ($items->search({}, {order_by => {-asc => 'me.id'}})->all) {
push @embedded, $self->hal_from_item($c, $item);

@ -351,6 +351,35 @@ sub resource_exists {
return;
}
sub paginate_order_collection {
my ($self, $c, $item_rs) = @_;
my $page = $c->request->params->{page} // 1;
my $rows = $c->request->params->{rows} // 10;
my $order_by = $c->request->params->{order_by};
my $direction = $c->request->params->{order_by_direction} // "asc";
my $total_count = int($item_rs->count);
$item_rs = $item_rs->search(undef, {
page => $page,
rows => $rows,
});
if ($order_by && $item_rs->result_source->has_column($order_by)) {
my $me = $item_rs->current_source_alias;
if (lc($direction) eq 'desc') {
$item_rs = $item_rs->search(undef, {
order_by => {-desc => "$me.$order_by"},
});
$c->log->debug("ordering by $me.$order_by DESC");
} else {
$item_rs = $item_rs->search(undef, {
order_by => "$me.$order_by",
});
$c->log->debug("ordering by $me.$order_by");
}
}
return ($total_count, $item_rs);
}
sub apply_patch {
my ($self, $c, $entity, $json) = @_;
my $patch = JSON::decode_json($json);

Loading…
Cancel
Save