|
|
@@ -14,26 +14,31 @@ class FavoriteController extends AbstractController |
|
|
|
*/ |
|
|
|
public function toggle(Request $request) |
|
|
|
{ |
|
|
|
$user = $this->_getUser(); |
|
|
|
$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'; |
|
|
|
$user = $this->getUserCurrent(); |
|
|
|
if($user) { |
|
|
|
$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'; |
|
|
|
} |
|
|
|
|
|
|
|
$this->_saveUser($user); |
|
|
|
|
|
|
|
return new JsonResponse([ |
|
|
|
'return' => 'success', |
|
|
|
'is_favorite' => $isFavorite, |
|
|
|
'message' => $message |
|
|
|
]); |
|
|
|
} |
|
|
|
else { |
|
|
|
return $this->_errorUserNotConnected(); |
|
|
|
} |
|
|
|
|
|
|
|
$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(); |
|
|
|
$productFamily = $this->_getProductFamily($request); |
|
|
|
$user = $this->getUserCurrent(); |
|
|
|
if($user) { |
|
|
|
$productFamily = $this->_getProductFamily($request); |
|
|
|
|
|
|
|
$user->addFavoriteProductFamily($productFamily); |
|
|
|
$this->_saveUser($user); |
|
|
|
$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->_errorUserNotConnected(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
@@ -58,32 +68,27 @@ class FavoriteController extends AbstractController |
|
|
|
*/ |
|
|
|
public function delete(Request $request) |
|
|
|
{ |
|
|
|
$user = $this->_getUser(); |
|
|
|
$productFamily = $this->_getProductFamily($request); |
|
|
|
|
|
|
|
$user->removeFavoriteProductFamily($productFamily); |
|
|
|
$this->_saveUser($user); |
|
|
|
$user = $this->getUserCurrent(); |
|
|
|
if($user) { |
|
|
|
$productFamily = $this->_getProductFamily($request); |
|
|
|
|
|
|
|
return new JsonResponse([ |
|
|
|
'return' => 'success', |
|
|
|
'message' => 'Le produit a bien été supprimé de vos favoris' |
|
|
|
]); |
|
|
|
} |
|
|
|
$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->_errorUserNotConnected(); |
|
|
|
} |
|
|
|
|
|
|
|
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 _errorUserNotConnected() |
|
|
|
{ |
|
|
|
return new JsonResponse([ |
|
|
|
'return' => 'error', |
|
|
|
'message' => 'Vous devez être connecté pour gérer vos favoris.' |
|
|
|
]); |
|
|
|
} |
|
|
|
} |