-
Notifications
You must be signed in to change notification settings - Fork 3
Application Entities (Models)
sunrise-php edited this page Jan 7, 2019
·
1 revision
Do not forget to update the database after creating the model
declare(strict_types=1);
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity
* @ORM\Table(name="note")
*/
class Note extends AbstractEntity
{
/**
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Column(type="integer")
*
* @var null|int
*/
protected $id;
/**
* @ORM\Column(
* type="string",
* length=128,
* nullable=false
* )
*
* @Assert\NotBlank
* @Assert\Type("string")
* @Assert\Length(max=128)
*
* @var null|string
*/
protected $title;
/**
* @ORM\Column(
* type="text",
* nullable=false
* )
*
* @Assert\NotBlank
* @Assert\Type("string")
*
* @var null|string
*/
protected $content;
/**
* Gets the note ID
*
* @return null|int
*/
public function getId()
{
return $this->id;
}
/**
* Gets the note title
*
* @return null|string
*/
public function getTitle()
{
return $this->title;
}
/**
* Gets the note content
*
* @return null|string
*/
public function getContent()
{
return $this->content;
}
/**
* Sets the note title
*
* @param string $title
*
* @return void
*/
public function setTitle($title)
{
$this->title = $title;
}
/**
* Sets the note content
*
* @param string $content
*
* @return void
*/
public function setContent($content)
{
$this->content = $content;
}
}
declare(strict_types=1);
namespace App\Http\Controller;
/**
* Import classes
*/
use App\Entity\Note;
use Doctrine\ORM\EntityManager;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
/**
* @Route(
* id="note.create",
* path="/note",
* methods={"POST"}
* )
*/
class NoteCreateController implements MiddlewareInterface
{
/**
* @Inject
*
* @var EntityManager
*/
protected $entityManager;
/**
* @param ServerRequestInterface $request
* @param RequestHandlerInterface $handler
*
* @return ResponseInterface
*/
public function process(
ServerRequestInterface $request,
RequestHandlerInterface $handler) : ResponseInterface
{
$data = (array) $request->getParsedBody();
$response = $handler->handle($request);
$note = new Note();
$note->setTitle($data['title'] ?? null);
$note->setContent($data['content'] ?? null);
$violations = $note->validate();
if ($violations->count() > 0) {
$response->getBody()->write((string) $violations);
return $response->withStatus(400);
}
$this->entityManager->persist($note);
$this->entityManager->flush();
$response->getBody()->write(
\sprintf("successful; note id: %d\n", $note->getId())
);
return $response->withStatus(201);
}
}
composer db:update
Request
curl -d "title=&content=" -X POST http://0.0.0.0:8080/note
Response
Object(App\Entity\Note).title:
This value should not be blank. (code c1051bb4-d103-4f74-8988-acbcafc7fdc3)
Object(App\Entity\Note).content:
This value should not be blank. (code c1051bb4-d103-4f74-8988-acbcafc7fdc3)
Request
curl -d "title=foo&content=bar" -X POST http://0.0.0.0:8080/note
Response
successful; note id: 1
Request
curl -d '{"title": "baz", "content": "qux"}' -H "Content-Type: application/json" -X POST http://0.0.0.0:8080/note
Response
successful; note id: 2
Have questions?
Ask your questions in our chat:
Get more features for your application using Awesome PSR-15 Middleware