|
|
@@ -14,26 +14,31 @@ class FavoriteController extends AbstractController |
|
|
|
*/ |
|
|
|
public function toggle(Request $request) |
|
|
|
{ |
|
|
|
$user = $this->_getUser(); |
|
|
|
$user = $this->getUserCurrent(); |
|
|
|
$productFamily = $this->_getProductFamily($request); |
|
|
|
|
|
|
|
if ($user->getFavoriteProductFamilies()->contains($productFamily)) { |
|
|
|
$user->removeFavoriteProductFamily($productFamily); |
|
|
|
$isFavorite = false; |
|
|
|
$message = 'Le produit a bien été supprimé de vos favoris'; |
|
|
|
} else { |
|
|
|
$user->addFavoriteProductFamily($productFamily); |
|
|
|
$isFavorite = true; |
|
|
|
$message = 'Le produit a bien été ajouté à vos favoris'; |
|
|
|
if($user && $productFamily) { |
|
|
|
if ($user->getFavoriteProductFamilies()->contains($productFamily)) { |
|
|
|
$user->removeFavoriteProductFamily($productFamily); |
|
|
|
$isFavorite = false; |
|
|
|
$message = 'Le produit a bien été supprimé de vos favoris'; |
|
|
|
} else { |
|
|
|
$user->addFavoriteProductFamily($productFamily); |
|
|
|
$isFavorite = true; |
|
|
|
$message = 'Le produit a bien été ajouté à vos favoris'; |
|
|
|
} |
|
|
|
|
|
|
|
$this->_saveUser($user); |
|
|
|
|
|
|
|
return new JsonResponse([ |
|
|
|
'return' => 'success', |
|
|
|
'is_favorite' => $isFavorite, |
|
|
|
'message' => $message |
|
|
|
]); |
|
|
|
} |
|
|
|
else { |
|
|
|
return $this->_errorResponse(); |
|
|
|
} |
|
|
|
|
|
|
|
$this->_saveUser($user); |
|
|
|
|
|
|
|
return new JsonResponse([ |
|
|
|
'return' => 'success', |
|
|
|
'is_favorite' => $isFavorite, |
|
|
|
'message' => $message |
|
|
|
]); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
@@ -41,16 +46,21 @@ class FavoriteController extends AbstractController |
|
|
|
*/ |
|
|
|
public function add(Request $request) |
|
|
|
{ |
|
|
|
$user = $this->_getUser(); |
|
|
|
$user = $this->getUserCurrent(); |
|
|
|
$productFamily = $this->_getProductFamily($request); |
|
|
|
|
|
|
|
$user->addFavoriteProductFamily($productFamily); |
|
|
|
$this->_saveUser($user); |
|
|
|
if($user && $productFamily) { |
|
|
|
$user->addFavoriteProductFamily($productFamily); |
|
|
|
$this->_saveUser($user); |
|
|
|
|
|
|
|
return new JsonResponse([ |
|
|
|
'return' => 'success', |
|
|
|
'message' => 'Le produit a bien été ajouté à vos favoris' |
|
|
|
]); |
|
|
|
return new JsonResponse([ |
|
|
|
'return' => 'success', |
|
|
|
'message' => 'Le produit a bien été ajouté à vos favoris' |
|
|
|
]); |
|
|
|
} |
|
|
|
else { |
|
|
|
return $this->_errorResponse(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
@@ -58,32 +68,27 @@ class FavoriteController extends AbstractController |
|
|
|
*/ |
|
|
|
public function delete(Request $request) |
|
|
|
{ |
|
|
|
$user = $this->_getUser(); |
|
|
|
$user = $this->getUserCurrent(); |
|
|
|
$productFamily = $this->_getProductFamily($request); |
|
|
|
|
|
|
|
$user->removeFavoriteProductFamily($productFamily); |
|
|
|
$this->_saveUser($user); |
|
|
|
|
|
|
|
return new JsonResponse([ |
|
|
|
'return' => 'success', |
|
|
|
'message' => 'Le produit a bien été supprimé de vos favoris' |
|
|
|
]); |
|
|
|
} |
|
|
|
if($user && $productFamily) { |
|
|
|
$user->removeFavoriteProductFamily($productFamily); |
|
|
|
$this->_saveUser($user); |
|
|
|
|
|
|
|
private function _getUser() |
|
|
|
{ |
|
|
|
$user = $this->getUserCurrent(); |
|
|
|
if (!$user) { |
|
|
|
throw new \ErrorException('Vous devez être connecté pour gérer vos favoris'); |
|
|
|
return new JsonResponse([ |
|
|
|
'return' => 'success', |
|
|
|
'message' => 'Le produit a bien été supprimé de vos favoris' |
|
|
|
]); |
|
|
|
} |
|
|
|
else { |
|
|
|
return $this->_errorResponse(); |
|
|
|
} |
|
|
|
|
|
|
|
return $user; |
|
|
|
} |
|
|
|
|
|
|
|
private function _saveUser($user) |
|
|
|
{ |
|
|
|
$entityManager = $this->getEntityManager(); |
|
|
|
$entityManager->persist($user); |
|
|
|
$entityManager->update($user); |
|
|
|
$entityManager->flush(); |
|
|
|
} |
|
|
|
|
|
|
@@ -97,8 +102,14 @@ class FavoriteController extends AbstractController |
|
|
|
|
|
|
|
if (isset($productFamily) && $productFamily) { |
|
|
|
return $productFamily; |
|
|
|
} else { |
|
|
|
throw new \ErrorException('Ce produit est introuvable'); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private function _errorResponse() |
|
|
|
{ |
|
|
|
return new JsonResponse([ |
|
|
|
'return' => 'error', |
|
|
|
'message' => 'Une erreur est survenue.' |
|
|
|
]); |
|
|
|
} |
|
|
|
} |