Created
March 30, 2016 09:20
-
-
Save NikitaMelnikov/bc0d03f0e7340399de027333b9a73107 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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