コード例 #1
0
 /**
  * Instantiates a like entity and sets its properties using db data.
  *
  * @param array $likeData
  *   The array of db data.
  *
  * @return \MusicBox\Entity\Like
  */
 protected function buildLike($likeData)
 {
     // Load the related artist and user.
     $artist = $this->artistRepository->find($likeData['artist_id']);
     $user = $this->userRepository->find($likeData['user_id']);
     $like = new Like();
     $like->setId($likeData['like_id']);
     $like->setArtist($artist);
     $like->setUser($user);
     $createdAt = new \DateTime('@' . $likeData['created_at']);
     $like->setCreatedAt($createdAt);
     return $like;
 }
コード例 #2
0
 public function likeAction(Request $request, Application $app)
 {
     $artist = $request->attributes->get('artist');
     $token = $app['security']->getToken();
     $user = $token->getUser();
     if (!$artist) {
         $app->abort(404, 'The requested artist was not found.');
     }
     if ($user == 'anon.') {
         // Only logged-in users can comment.
         return;
     }
     // Don't allow the user to like the artist twice.
     $existingLike = $app['repository.like']->findByArtistAndUser($artist->getId(), $user->getId());
     if (!$existingLike) {
         // Save the individual like record.
         $like = new Like();
         $like->setArtist($artist);
         $like->setUser($user);
         $app['repository.like']->save($like);
         // Increase the counter on the artist.
         $numLikes = $artist->getLikes();
         $numLikes++;
         $artist->setLikes($numLikes);
         $app['repository.artist']->save($artist);
     }
     return '';
 }