Feat : Add company and answer api base

This commit is contained in:
2018-09-17 10:15:12 +02:00
parent 04229b0781
commit 44d0711dc1
11 changed files with 408 additions and 5 deletions
@@ -0,0 +1,79 @@
<?php
namespace AppBundle\Controller\Api;
use AppBundle\Entity\Answer;
use AppBundle\Representation\Answers;
use AppBundle\Representation\SingleAnswer;
use FOS\RestBundle\Controller\Annotations as Rest;
use FOS\RestBundle\Controller\Annotations\Get;
use FOS\RestBundle\Controller\Annotations\Post;
use FOS\RestBundle\Controller\Annotations\View;
use FOS\RestBundle\Controller\FOSRestController;
use FOS\RestBundle\Request\ParamFetcherInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
class AnswerController extends FOSRestController
{
/**
* @Rest\Get(path="/api/answers/{id}", name="api_answer_show", requirements={"id"="\d+"})
* @Rest\View
*
*/
public function showAction(Answer $answer)
{
// return $answer;
return new SingleAnswer($answer);
}
/**
* @Rest\Get("/api/answers", name="api_answer_list")
* @Rest\QueryParam(
* name="order",
* requirements="asc|desc",
* default="asc",
* description="Sort order (asc or desc)"
* )
* @Rest\QueryParam(
* name="limit",
* requirements="\d+",
* default="15",
* description="Max number of answers per page."
* )
* @Rest\QueryParam(
* name="offset",
* requirements="\d+",
* default="1",
* description="The pagination offset"
* )
* @Rest\View(StatusCode = 200)
*/
public function listAction(ParamFetcherInterface $paramFetcher)
{
$pager = $this->getDoctrine()->getRepository('AppBundle:Answer')->search(
$paramFetcher->get('order'),
$paramFetcher->get('limit'),
$paramFetcher->get('offset')
);
return new Answers($pager);
}
/**
* @Rest\Post("/api/answers", name="api_answer_create")
* @Rest\View(StatusCode = 201)
* @ParamConverter("answer", converter="fos_rest.request_body")
*/
public function createAction(Answer $answer)
{
$em = $this->getDoctrine()->getManager();
$em->persist($answer);
$em->flush();
return $this->view(
new SingleAnswer($answer),
Response::HTTP_CREATED,
['Location' => $this->generateUrl('api_answer_show', ['id' => $answer->getId(), UrlGeneratorInterface::ABSOLUTE_URL])]);
}
}
@@ -0,0 +1,79 @@
<?php
namespace AppBundle\Controller\Api;
use AppBundle\Entity\Company;
use AppBundle\Representation\Companys;
use AppBundle\Representation\SingleCompany;
use FOS\RestBundle\Controller\Annotations as Rest;
use FOS\RestBundle\Controller\Annotations\Get;
use FOS\RestBundle\Controller\Annotations\Post;
use FOS\RestBundle\Controller\Annotations\View;
use FOS\RestBundle\Controller\FOSRestController;
use FOS\RestBundle\Request\ParamFetcherInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
class CompanyController extends FOSRestController
{
/**
* @Rest\Get(path="/api/companies/{id}", name="api_company_show", requirements={"id"="\d+"})
* @Rest\View
*
*/
public function showAction(Company $company)
{
// return $company;
return new SingleCompany($company);
}
/**
* @Rest\Get("/api/companies", name="api_company_list")
* @Rest\QueryParam(
* name="order",
* requirements="asc|desc",
* default="asc",
* description="Sort order (asc or desc)"
* )
* @Rest\QueryParam(
* name="limit",
* requirements="\d+",
* default="15",
* description="Max number of companys per page."
* )
* @Rest\QueryParam(
* name="offset",
* requirements="\d+",
* default="1",
* description="The pagination offset"
* )
* @Rest\View(StatusCode = 200)
*/
public function listAction(ParamFetcherInterface $paramFetcher)
{
$pager = $this->getDoctrine()->getRepository('AppBundle:Company')->search(
$paramFetcher->get('order'),
$paramFetcher->get('limit'),
$paramFetcher->get('offset')
);
return new Companys($pager);
}
/**
* @Rest\Post("/api/companies", name="api_company_create")
* @Rest\View(StatusCode = 201)
* @ParamConverter("company", converter="fos_rest.request_body")
*/
public function createAction(Company $company)
{
$em = $this->getDoctrine()->getManager();
$em->persist($company);
$em->flush();
return $this->view(
new SingleCompany($company),
Response::HTTP_CREATED,
['Location' => $this->generateUrl('api_company_show', ['id' => $company->getId(), UrlGeneratorInterface::ABSOLUTE_URL])]);
}
}
@@ -35,7 +35,10 @@ use Hateoas\Configuration\Annotation as Hateoas;
* embedded = @Hateoas\Embedded("expr(object.getCompany())")
* )
*
*
* @Hateoas\Relation(
* "creator",
* embedded = @Hateoas\Embedded("expr(object.getCreator())")
* )
*/
class Ad
{
@@ -91,6 +94,7 @@ class Ad
* @var string
*
* @ORM\Column(name="description", type="text", nullable=true)
* @Expose
*/
private $description;
@@ -99,6 +103,7 @@ class Ad
*
* @ORM\Column(name="published_at", type="datetime", nullable=true)
* @Assert\GreaterThanOrEqual("today", groups={"dates"})
* @Expose
*/
private $publishedAt;
@@ -106,6 +111,7 @@ class Ad
* @var \DateTime
*
* @ORM\Column(name="unpublished_at", type="datetime", nullable=true)
* @Expose
*/
private $unpublishedAt;
@@ -113,6 +119,7 @@ class Ad
* @var boolean
*
* @ORM\Column(name="is_verified", type="boolean", nullable=true, options={"comment":"Etat de vérification"})
* @Expose
*/
private $isVerified;
@@ -120,6 +127,7 @@ class Ad
* @var boolean
*
* @ORM\Column(name="is_promoted", type="boolean", nullable=true, options={"comment":"Peux t'on répondre"})
* @Expose
*/
private $isPromoted;
@@ -127,6 +135,7 @@ class Ad
* @var boolean
*
* @ORM\Column(name="is_private", type="boolean", nullable=true, options={"comment":"Annonce privé, non listée en front"})
* @Expose
*/
private $isPrivate;
@@ -134,6 +143,7 @@ class Ad
* @var boolean
*
* @ORM\Column(name="is_answerable", type="boolean", nullable=true, options={"comment":"Annonce promu ?"})
* @Expose
*/
private $isAnswerable;
@@ -149,6 +159,7 @@ class Ad
* @var array
*
* @ORM\Column(name="public_tag", type="json_document", nullable=true, options={"jsonb":true})
* @Expose
*/
private $publicTag;
@@ -156,6 +167,7 @@ class Ad
* @var array
*
* @ORM\Column(name="private_tag", type="json_document", nullable=true, options={"jsonb":true})
* @Expose
*/
private $privateTag;
@@ -163,6 +175,7 @@ class Ad
* @var array
*
* @ORM\Column(name="extra", type="json_document", nullable=true, options={"jsonb":true})
* @Expose
*/
private $extra;
@@ -175,6 +188,7 @@ class Ad
/**
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\Media", inversedBy="ads", cascade={"persist"})
* @Expose
*/
private $video;
@@ -193,6 +207,7 @@ class Ad
* @var string
*
* @ORM\Column(name="emphasis", type="boolean", options={"comment":"A la une"}, nullable=true)
* @Expose
*/
private $emphasis;
@@ -200,6 +215,7 @@ class Ad
* @var string
*
* @ORM\Column(name="emphasis_order", type="integer", options={"comment":"Ordre"}, nullable=true)
* @Expose
*/
private $emphasisOrder;
@@ -7,6 +7,9 @@ use AppBundle\Utils\Traits\StatusTrait;
use AppBundle\Utils\Traits\TimestampableTrait;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\Criteria as Criteria;
use JMS\Serializer\Annotation\ExclusionPolicy;
use JMS\Serializer\Annotation\Expose;
use Hateoas\Configuration\Annotation as Hateoas;
/**
* Answer
@@ -14,6 +17,32 @@ use Doctrine\Common\Collections\Criteria as Criteria;
* @ORM\Table(name="answer")
* @ORM\Entity(repositoryClass="AppBundle\Repository\AnswerRepository")
* @ORM\HasLifecycleCallbacks()
*
* @ExclusionPolicy("all")
*
* @Hateoas\Relation(
* "self",
* href = @Hateoas\Route(
* "api_answer_show",
* parameters = { "id" = "expr(object.getId())" },
* absolute=true
* )
* )
*
* @Hateoas\Relation(
* "ad",
* embedded = @Hateoas\Embedded("expr(object.getAd())")
* )
*
* @Hateoas\Relation(
* "user",
* embedded = @Hateoas\Embedded("expr(object.getUser())")
* )
*
* @Hateoas\Relation(
* "company",
* embedded = @Hateoas\Embedded("expr(object.getCompany())")
* )
*/
class Answer
{
@@ -28,6 +57,7 @@ class Answer
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @Expose
*/
private $id;
@@ -50,6 +80,7 @@ class Answer
/**
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\Media", inversedBy="answers", cascade={"persist"})
* @Expose
*/
private $video;
@@ -57,6 +88,7 @@ class Answer
* @var int
*
* @ORM\Column(name="vote", type="smallint", nullable=true)
* @Expose
*/
private $vote;
@@ -64,6 +96,7 @@ class Answer
* @var string
*
* @ORM\Column(name="comment", type="text", nullable=true)
* @Expose
*/
private $comment;
@@ -71,6 +104,7 @@ class Answer
* @var \DateTime $sendTime
*
* @ORM\Column(name="send_time", type="datetime", nullable=true)
* @Expose
*/
private $sendTime;
@@ -8,9 +8,10 @@ use AppBundle\Utils\Traits\IsVisibleTrait;
use AppBundle\Utils\Traits\TagsJsonTrait;
use AppBundle\Utils\Traits\TimestampableTrait;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Hateoas\Configuration\Annotation as Hateoas;
use JMS\Serializer\Annotation\ExclusionPolicy;
use JMS\Serializer\Annotation\Expose;
use Symfony\Component\HttpFoundation\File\UploadedFile;
/**
* Company
@@ -18,8 +19,18 @@ use JMS\Serializer\Annotation\Expose;
* @ORM\Table(name="company")
* @ORM\Entity(repositoryClass="AppBundle\Repository\CompanyRepository")
* @ORM\HasLifecycleCallbacks()
*
*
* @ExclusionPolicy("all")
*
* @Hateoas\Relation(
* "self",
* href = @Hateoas\Route(
* "api_company_show",
* parameters = { "id" = "expr(object.getId())" },
* absolute=true
* )
* )
*
*/
class Company
{
@@ -52,6 +63,7 @@ class Company
* @var string
*
* @ORM\Column(name="business_name", type="string", length=255, options={"comment":"Raison sociale"}, nullable=true)
* @Expose
*/
private $businessName;
@@ -59,6 +71,7 @@ class Company
* @var text
*
* @ORM\Column(name="description", type="text", length=255, nullable=true)
* @Expose
*/
private $description;
@@ -66,6 +79,7 @@ class Company
* @var text
*
* @ORM\Column(name="description_admin", type="text", length=255, nullable=true)
* @Expose
*/
private $descriptionAdmin;
@@ -81,6 +95,7 @@ class Company
* @var string
*
* @ORM\Column(name="website", type="string", nullable=true)
* @Expose
*/
private $website;
@@ -88,12 +103,14 @@ class Company
* @var string
*
* @ORM\Column(name="alias", type="string", length=255, unique=true)
* @Expose
*/
private $alias;
/**
* @ORM\OneToOne(targetEntity="AppBundle\Entity\Media", cascade={"persist", "remove"})
* @ORM\JoinColumn(name="presentation_video_id", nullable=true)
* @Expose
*/
private $presentationVideo;
@@ -101,6 +118,7 @@ class Company
* @var string
*
* @ORM\Column(name="type", type="string", nullable=true)
* @Expose
*/
private $type;
@@ -110,6 +128,7 @@ class Company
* @var string
*
* @ORM\Column(name="emphasis", type="boolean", options={"comment":"A la une"}, nullable=true)
* @Expose
*/
private $emphasis;
@@ -117,6 +136,7 @@ class Company
* @var string
*
* @ORM\Column(name="emphasis_order", type="integer", options={"comment":"Ordre"}, nullable=true)
* @Expose
*/
private $emphasisOrder;
@@ -124,6 +144,7 @@ class Company
* @var bool
*
* @ORM\Column(name="searchable", type="boolean", nullable=true)
* @Expose
*/
private $searchable;
@@ -131,6 +152,7 @@ class Company
* @var boolean
*
* @ORM\Column(name="is_private", type="boolean", nullable=true, options={"comment":"Company privé, non listée en front"})
* @Expose
*/
private $isPrivate;
@@ -8,9 +8,19 @@ namespace AppBundle\Repository;
* This class was generated by the Doctrine ORM. Add your own custom
* repository methods below.
*/
class AnswerRepository extends \Doctrine\ORM\EntityRepository
class AnswerRepository extends AbstractRepository
{
public function search($order = 'asc', $limit = 20, $offset = 0)
{
$qb = $this
->createQueryBuilder('a')
->select('a')
->orderBy('a.id', $order)
;
return $this->paginate($qb, $limit, $offset);
}
public function getAnswerByCompany($id)
{
$qb = $this
@@ -10,7 +10,7 @@ use Doctrine\ORM\EntityRepository;
* This class was generated by the Doctrine ORM. Add your own custom
* repository methods below.
*/
class CompanyRepository extends EntityRepository
class CompanyRepository extends AbstractRepository
{
public function getCompanySortByAds()
{
@@ -75,4 +75,21 @@ class CompanyRepository extends EntityRepository
return $qb;
}
public function search($term, $order = 'asc', $limit = 20, $offset = 0)
{
$qb = $this
->createQueryBuilder('c')
->select('c')
->orderBy('c.id', $order)
;
if ($term) {
$qb
->where('c.title LIKE ?1')
->setParameter(1, '%' . $term . '%')
;
}
return $this->paginate($qb, $limit, $offset);
}
}
@@ -0,0 +1,46 @@
<?php
namespace AppBundle\Representation;
use Hateoas\Configuration\Annotation as Hateoas;
use JMS\Serializer\Annotation\Type;
use Pagerfanta\Pagerfanta;
/**
* @Hateoas\Relation(
* "authenticated_user",
* embedded = @Hateoas\Embedded("expr(service('security.token_storage').getToken().getUser())")
* )
*/
class Answers
{
/**
* @Type("array<AppBundle\Entity\Answer>")
*/
public $data;
public $meta;
public function __construct(Pagerfanta $data)
{
$this->data = $data;
$this->addMeta('limit', $data->getMaxPerPage());
$this->addMeta('current_items', count($data->getCurrentPageResults()));
$this->addMeta('total_items', $data->getNbResults());
$this->addMeta('offset', $data->getCurrentPageOffsetStart());
}
public function addMeta($name, $value)
{
if (isset($this->meta[$name])) {
throw new \LogicException(sprintf('This meta already exists. You are trying to override this meta, use the setMeta method instead for the %s meta.', $name));
}
$this->setMeta($name, $value);
}
public function setMeta($name, $value)
{
$this->meta[$name] = $value;
}
}
@@ -0,0 +1,46 @@
<?php
namespace AppBundle\Representation;
use Hateoas\Configuration\Annotation as Hateoas;
use JMS\Serializer\Annotation\Type;
use Pagerfanta\Pagerfanta;
/**
* @Hateoas\Relation(
* "authenticated_user",
* embedded = @Hateoas\Embedded("expr(service('security.token_storage').getToken().getUser())")
* )
*/
class Companies
{
/**
* @Type("array<AppBundle\Entity\Company>")
*/
public $data;
public $meta;
public function __construct(Pagerfanta $data)
{
$this->data = $data;
$this->addMeta('limit', $data->getMaxPerPage());
$this->addMeta('current_items', count($data->getCurrentPageResults()));
$this->addMeta('total_items', $data->getNbResults());
$this->addMeta('offset', $data->getCurrentPageOffsetStart());
}
public function addMeta($name, $value)
{
if (isset($this->meta[$name])) {
throw new \LogicException(sprintf('This meta already exists. You are trying to override this meta, use the setMeta method instead for the %s meta.', $name));
}
$this->setMeta($name, $value);
}
public function setMeta($name, $value)
{
$this->meta[$name] = $value;
}
}
@@ -0,0 +1,27 @@
<?php
namespace AppBundle\Representation;
use AppBundle\Entity\Answer;
use Hateoas\Configuration\Annotation as Hateoas;
use JMS\Serializer\Annotation\Type;
/**
* @Hateoas\Relation(
* "authenticated_user",
* embedded = @Hateoas\Embedded("expr(service('security.token_storage').getToken().getUser())")
* )
*/
class SingleAnswer
{
/**
* @Type("AppBundle\Entity\Answer")
*/
public $data;
public function __construct(Answer $data)
{
$this->data = $data;
}
}
@@ -0,0 +1,27 @@
<?php
namespace AppBundle\Representation;
use AppBundle\Entity\Company;
use Hateoas\Configuration\Annotation as Hateoas;
use JMS\Serializer\Annotation\Type;
/**
* @Hateoas\Relation(
* "authenticated_user",
* embedded = @Hateoas\Embedded("expr(service('security.token_storage').getToken().getUser())")
* )
*/
class SingleCompany
{
/**
* @Type("AppBundle\Entity\Company")
*/
public $data;
public function __construct(Company $data)
{
$this->data = $data;
}
}