summaryrefslogtreecommitdiff
path: root/LedgerSMB/RESTXML/Document/Part_Search.pm
blob: 9339dcc2d5aa458d3af3c0367a82ade56719e9ee (plain)
  1. package LedgerSMB::RESTXML::Document::Part_Search;
  2. use strict;
  3. use warnings;
  4. use base qw(LedgerSMB::RESTXML::Document::Base);
  5. use LedgerSMB::Log;
  6. sub handle_get {
  7. my ( $self, $args ) = @_;
  8. my $user = $args->{user};
  9. my $dbh = $args->{dbh};
  10. my $handler = $args->{handler};
  11. my $query = $handler->read_query();
  12. my %terms;
  13. for my $field ( $query->param() ) {
  14. # TODO: BIG GAPING HOLE HERE.
  15. $terms{$field} = $query->param($field);
  16. }
  17. if ( $terms{_keyword} ) {
  18. %terms = (
  19. description => $terms{_keyword},
  20. partnumber => $terms{_keyword},
  21. );
  22. }
  23. my $sql =
  24. 'SELECT id,description,partnumber FROM parts WHERE '
  25. . join( ' OR ', map { "$_ like ?" } sort keys %terms );
  26. my $res = $dbh->prepare($sql);
  27. $res->execute( map { "$terms{$_}\%" } sort keys %terms )
  28. or return $handler->error( $dbh->errstr );
  29. my @rows;
  30. my $row;
  31. push @rows, $row while $row = $res->fetchrow_hashref();
  32. $res->finish();
  33. $handler->respond(
  34. XML::Twig::Elt->new(
  35. 'Part_Search_Response',
  36. { 'xmlns:xlink' => "http://www.w3.org/1999/xlink" },
  37. map {
  38. $self->hash_to_twig(
  39. {
  40. name => 'Part',
  41. root_attr => { 'xlink:href' => "Part/$_->{id}" },
  42. hash => $_
  43. }
  44. );
  45. } @rows
  46. )
  47. );
  48. }
  49. 1;