Skip to content

Instantly share code, notes, and snippets.

@NikitaMelnikov
Created March 30, 2016 09:20
Show Gist options
  • Save NikitaMelnikov/bc0d03f0e7340399de027333b9a73107 to your computer and use it in GitHub Desktop.
Save NikitaMelnikov/bc0d03f0e7340399de027333b9a73107 to your computer and use it in GitHub Desktop.
<?php
// CRUD Controller
class UserController {
protected $mapping = [
User::class => new JsonMapping(new UserReads(), new UserWrites())
];
public function postIndex(User $user) {
if ($user->save()) {
return new Ok($user);
} else {
return new BadRequest($user->getErrors());
}
}
public function patchUser($id, User $user) {
$origin = User::find()->one($id);
if ($origin !== null) {
$origin->setAttributes($user->getAttributes());
return $origin->save() ? new Ok($origin) : new BadRequest($origin->getErrors())
} else {
return new NotFound($id);
}
}
}
// Custom controller
class CommissionController {
/**
* @Inject
*/
protected $commissionService;
/**
* @RequestBody(mapping={"reads": CommissionListReads::class})
* @param Commission[] $commissions
* @return Response
*/
public function postIndex(array $commissions) {
try {
$this->commissionService->persist($commissions);
return new Created();
} catch (CommissionServiceException $e) {
return new BadRequest($e->getMessage());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment