partkeepr

fork of partkeepr
git clone https://git.e1e0.net/partkeepr.git
Log | Files | Refs | Submodules | README | LICENSE

DefaultController.php (2332B)


      1 <?php
      2 
      3 namespace PartKeepr\OctoPartBundle\Controller;
      4 
      5 use FOS\RestBundle\Controller\Annotations\View;
      6 use FOS\RestBundle\Controller\FOSRestController;
      7 use Sensio\Bundle\FrameworkExtraBundle\Configuration as Routing;
      8 use Symfony\Component\HttpFoundation\Request;
      9 
     10 class DefaultController extends FOSRestController
     11 {
     12     /**
     13      * @Routing\Route("/api/octopart/get/{id}", defaults={"method" = "GET","_format" = "json"})
     14      * @Routing\Method({"GET"})
     15      *
     16      * @param $id string the Part UID
     17      *
     18      * @View()
     19      *
     20      * @return \stdClass
     21      */
     22     public function indexAction($id)
     23     {
     24         $data = $this->get("partkeepr.octopart_service")->getPartByUID($id);
     25 
     26         return $data;
     27     }
     28 
     29     /**
     30      * @Routing\Route("/api/octopart/query/", defaults={"method" = "GET","_format" = "json"})
     31      * @Routing\Method({"GET"})
     32      *
     33      * @param Request $request
     34      *
     35      * @View()
     36      *
     37      * @return array
     38      */
     39     public function getPartsByQueryAction(Request $request)
     40     {
     41         $start = 1;
     42 
     43         $responseData = [];
     44 
     45         $query = $request->query->get("q");
     46 
     47         if ($request->query->has("page")) {
     48             $start = $request->query->get("page");
     49         }
     50 
     51         $data = $this->get("partkeepr.octopart_service")->getPartyByQuery($query, $start);
     52 
     53         $errors = $data["errors"];
     54         $data = $data["data"]["search"];
     55 
     56         $responseData["hits"] = $data["hits"];
     57         $responseData["results"] = [];
     58         $responseData["errors"] = $errors;
     59 
     60         if ($data) {
     61             foreach ($data["results"] as $result) {
     62                 $part = $result["part"];
     63                 $responseItem = [];
     64                 $responseItem["mpn"] = $part["mpn"];
     65                 $responseItem["title"] = $part["short_description"];
     66                 $responseItem["manufacturer"] = $part["manufacturer"]["name"];
     67                 $responseItem["numOffers"] = count($part["sellers"]);
     68                 $responseItem["numSpecs"] = count($part["specs"]);
     69                 $responseItem["numDatasheets"] = count($part["document_collections"]);
     70                 $responseItem["url"] = "https://octopart.com".$part["slug"];
     71                 $responseItem["uid"] = $part["id"];
     72                 $responseData["results"][] = $responseItem;
     73             }
     74         }
     75 
     76         return $responseData;
     77     }
     78 }