dev-master
9999999-devUser Interests, follow & unfollow
The Requires
by Fernando Hidalgo Becerra
User Interests, follow & unfollow
composer require opositatest/interest-user-bundle, (*1)
# app/AppKernel.php new Opositatest\InterestUserBundle\OpositatestInterestUserBundle(),
# src/AppBundle/Entity/User ... // Add trait use \Opositatest\InterestUserBundle\Model\UserTrait { __construct as _traitconstructor; } ... // Add construct public function __construct() { $this->_traitconstructor(); parent::__construct(); }
You need add followInterests and unfollowInterests to UserAdmin Class, so:, (*2)
# src/AppBundle/Admin/UserAdmin protected function configureFormFields(FormMapper $formMapper) { ... ->add('followInterests', 'sonata_type_model', array('multiple' => true, 'by_reference' => false)) ->add('unfollowInterests', 'sonata_type_model', array('multiple' => true, 'by_reference' => false)) ... }
Add validate function, so:, (*3)
public function validate(ErrorElement $errorElement, $object) { /** @var SyliusUser $user */ $user = $object; foreach($user->getFollowInterests() as $followInterest) { if ($user->existUnfollowInterest($followInterest)) { $custom_error = "Interest ".$followInterest." used in follow and unfollow"; $errorElement->with( 'enabled' )->addViolation( $custom_error )->end(); } } }
# app/config/config.yml orm: resolve_target_entities: Opositatest\InterestUserBundle\Model\UserInterface: AppBundle\Entity\User
Add routes to your project. It should be compatible with nelmio api doc, (*4)
# app/config/routing.yml opositatest_interestuser_api: resource: "@OpositatestInterestUserBundle/Resources/config/routing.yml" prefix: /api/ # Prefix is customizable
Enable annotations for group feature, (*5)
# app/config/config.yml framework: # ... serializer: enable_annotations: true
Add OpositatestInterestUserBundle group:, (*6)
# app/config/config.yml sonata_admin: ... dashboard: ... groups: OpositatestInterestUserBundle: label: "Intereses de Usuario" blocks: - { position: right, type: sonata.admin.block.admin_list, settings: { groups: [OpositatestInterestUserBundle] } }
We have three functions, "/api/" is customizable prefix url:, (*7)
It add interest to logged user, (*8)
POST /api/interest/{interest}
It remove interest to logged user, (*9)
DELETE /api/interest/{interest}
It return interests global and for logged user, (*10)
GET /api//interests
You can try unit test with:, (*11)
./vendor/bin/phpunit vendor/opositatest/interest-user-bundle/Opositatest/InterestUserBundle
El bundle gestiona los intereses de un usuario, permite añadir y eliminar followInterest y unfollowInterest., (*12)
Tiene dos lógicas importantes implementadas: 1. Cuando añades un nuevo interés en followInterest o unfollowInterest el sistema añade los intereses hijo automáticamente. Este proceso se hace a través de la Entidad Interest en el caso de que se use SonataAdmin y en el servicio InterestService en el caso de que se use la API. Esta diferenciación es así porque a través de la API necesitamos comprobar el punto 2, mientras que a través de Sonata el punto 2 ya se comprueba con una función especial en el propio Sonata., (*13)
User Interests, follow & unfollow