Skip to content

Commit 3141e5e

Browse files
author
Rafal
committed
Added 2nd part of the test. Cars query.
1 parent 783f657 commit 3141e5e

File tree

3 files changed

+193
-0
lines changed

3 files changed

+193
-0
lines changed

src/APP/AnswersBundle/Controller/APIController.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,53 @@ public function searchAnswerAction(Request $request)
368368
);
369369
}
370370

371+
/**
372+
* @Route("/get-cars", name="api_get_cars")
373+
* @Method({"GET", "POST"})
374+
*/
375+
public function getCarsAction()
376+
{
377+
$em = $this->getDoctrine()->getManager();
378+
379+
$conn = $em->getConnection();
380+
381+
$sql = "SELECT DISTINCT t2.fabrication_year,
382+
SUM(t2.BMW) as BMW,
383+
SUM(t2.Mercedes) as Mercedes,
384+
SUM(t2.Audi) as Audi,
385+
SUM(t2.Fiat) as Fiat
386+
FROM
387+
(SELECT
388+
CASE WHEN t1.fabrication_year BETWEEN 0 AND 1998 THEN '0-1998'
389+
WHEN t1.fabrication_year BETWEEN 1999 AND 2002 THEN '1999-2002'
390+
WHEN t1.fabrication_year BETWEEN 2003 AND 2006 THEN '2003-2006'
391+
WHEN t1.fabrication_year BETWEEN 2007 AND 2009 THEN '2007-2009'
392+
ELSE '2010-2016' END AS fabrication_year,
393+
SUM(t1.BMW) as BMW,
394+
SUM(t1.Mercedes) as Mercedes,
395+
SUM(t1.Audi) as Audi,
396+
SUM(t1.Fiat) as Fiat
397+
FROM
398+
(SELECT
399+
DISTINCT fabrication_year,
400+
COUNT(CASE WHEN producer='Fiat' THEN producer END) AS Fiat,
401+
COUNT(CASE WHEN producer='BMW' THEN producer END) AS BMW,
402+
COUNT(CASE WHEN producer='Mercedes' THEN producer END) AS Mercedes,
403+
COUNT(CASE WHEN producer='Audi' THEN producer END) AS Audi
404+
FROM cars
405+
GROUP BY fabrication_year) t1
406+
GROUP BY t1.fabrication_year) t2
407+
GROUP BY fabrication_year;";
408+
409+
$statement = $conn->prepare($sql);
410+
$statement->execute();
411+
$cars = $statement->fetchAll();
412+
413+
return new JsonResponse(
414+
$cars
415+
);
416+
}
417+
371418
private function getSlug($value)
372419
{
373420
$slugify = new Slugify();
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<?php
2+
3+
namespace APP\AnswersBundle\Entity;
4+
5+
use Doctrine\ORM\Mapping as ORM;
6+
7+
/**
8+
* Car
9+
*
10+
* @ORM\Entity(repositoryClass="APP\AnswersBundle\Entity\AnswerRepository")
11+
* @ORM\Table(name="cars",
12+
* indexes={
13+
* @ORM\Index(name="search_idx", columns={
14+
* "fabrication_year",
15+
* "producer"
16+
* })
17+
* }
18+
* )
19+
*/
20+
class Car
21+
{
22+
/**
23+
* @var integer
24+
*
25+
* @ORM\Column(name="id", type="integer")
26+
* @ORM\Id
27+
* @ORM\GeneratedValue(strategy="AUTO")
28+
*/
29+
private $id;
30+
31+
/**
32+
* @var string
33+
*
34+
* @ORM\Column(name="fabrication_year", type="integer")
35+
*/
36+
private $fabrication_year;
37+
38+
/**
39+
* @var string
40+
*
41+
* @ORM\Column(name="producer", type="string", length=255)
42+
*/
43+
private $producer;
44+
45+
/**
46+
* Get id
47+
*
48+
* @return integer
49+
*/
50+
public function getId()
51+
{
52+
return $this->id;
53+
}
54+
55+
/**
56+
* Set fabricationYear
57+
*
58+
* @param integer $fabricationYear
59+
*
60+
* @return Car
61+
*/
62+
public function setFabricationYear($fabricationYear)
63+
{
64+
$this->fabrication_year = $fabricationYear;
65+
66+
return $this;
67+
}
68+
69+
/**
70+
* Get fabricationYear
71+
*
72+
* @return integer
73+
*/
74+
public function getFabricationYear()
75+
{
76+
return $this->fabrication_year;
77+
}
78+
79+
/**
80+
* Set producer
81+
*
82+
* @param string $producer
83+
*
84+
* @return Car
85+
*/
86+
public function setProducer($producer)
87+
{
88+
$this->producer = $producer;
89+
90+
return $this;
91+
}
92+
93+
/**
94+
* Get producer
95+
*
96+
* @return string
97+
*/
98+
public function getProducer()
99+
{
100+
return $this->producer;
101+
}
102+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace APP\AnswersBundle\Entity;
4+
5+
use Doctrine\ORM\Mapping as ORM;
6+
7+
/**
8+
* Car
9+
*
10+
* @ORM\Entity(repositoryClass="APP\AnswersBundle\Entity\AnswerRepository")
11+
* @ORM\Table(name="cars",
12+
* indexes={
13+
* @ORM\Index(name="search_idx", columns={
14+
* "fabrication_year",
15+
* "producer"
16+
* })
17+
* }
18+
* )
19+
*/
20+
class Car
21+
{
22+
/**
23+
* @var integer
24+
*
25+
* @ORM\Column(name="id", type="integer")
26+
* @ORM\Id
27+
* @ORM\GeneratedValue(strategy="AUTO")
28+
*/
29+
private $id;
30+
31+
/**
32+
* @var string
33+
*
34+
* @ORM\Column(name="fabrication_year", type="integer")
35+
*/
36+
private $fabrication_year;
37+
38+
/**
39+
* @var string
40+
*
41+
* @ORM\Column(name="producer", type="string", length=255)
42+
*/
43+
private $producer;
44+
}

0 commit comments

Comments
 (0)