Skip to content
Snippets Groups Projects
Commit f458ec71 authored by PEREZ-RAMIREZ Julian's avatar PEREZ-RAMIREZ Julian
Browse files

adding missing methods to items and wishlist controllers

parent f740cb87
Branches
No related tags found
No related merge requests found
......@@ -3,6 +3,7 @@
namespace App\Controller;
use App\Repository\ItemRepository;
use App\Repository\WishListRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\JsonResponse;
......@@ -17,15 +18,18 @@ final class ItemController extends AbstractController
{
private EntityManagerInterface $entityManager;
private ItemRepository $itemRepository;
private WishListRepository $wishListRepository;
private ValidatorInterface $validator;
public function __construct(
EntityManagerInterface $entityManager,
ItemRepository $itemRepository,
WishListRepository $wishListRepository,
ValidatorInterface $validator
) {
$this->entityManager = $entityManager;
$this->itemRepository = $itemRepository;
$this->wishListRepository = $wishListRepository;
$this->validator = $validator;
}
......@@ -112,47 +116,242 @@ final class ItemController extends AbstractController
}
#[Route('/{itemId}', methods: ['GET'])]
public function getItemsById($itemId): JsonResponse
{
$item = $this->itemRepository->findOneById($itemId);
public function getItemsById($itemId): JsonResponse
{
$item = $this->itemRepository->findOneById($itemId);
if (!$item) {
return new JsonResponse(['error' => 'Item not found'], Response::HTTP_NOT_FOUND);
}
if (!$item) {
return new JsonResponse(['error' => 'Item not found'], Response::HTTP_NOT_FOUND);
}
$wishList = $item->getWishList();
$user = $wishList->getUser(); // Assuming a wishlist has an associated user
$wishList = $item->getWishList();
$user = $wishList->getUser(); // Assuming a wishlist has an associated user
$itemDTO = [
'id' => $item->getId(),
'wishList' => [
'id' => $wishList->getId(),
'user' => [
'id' => $user->getId(),
'username' => $user->getUserName(),
'mail' => $user->getEmail(),
'role' => $user->getRoles(),
'isBlocked' => $user->isBlocked(),
'createdAt' => $user->getCreatedAt()->format('Y-m-d H:i:s'),
$itemDTO = [
'id' => $item->getId(),
'wishList' => [
'id' => $wishList->getId(),
'user' => [
'id' => $user->getId(),
'username' => $user->getUserName(),
'mail' => $user->getEmail(),
'role' => $user->getRoles(),
'isBlocked' => $user->isBlocked(),
'createdAt' => $user->getCreatedAt()->format('Y-m-d H:i:s'),
],
'name' => $wishList->getName(),
'description' => $wishList->getDescription(),
'expirationDate' => $wishList->getExpirationDate()->format('Y-m-d'),
'isActive' => $wishList->isActive(),
'createdAt' => $wishList->getCreatedAt()->format('Y-m-d H:i:s'),
],
'name' => $wishList->getName(),
'description' => $wishList->getDescription(),
'expirationDate' => $wishList->getExpirationDate()->format('Y-m-d'),
'isActive' => $wishList->isActive(),
'createdAt' => $wishList->getCreatedAt()->format('Y-m-d H:i:s'),
],
'title' => $item->getTitle(),
'description' => $item->getDescription(),
'price' => $item->getPrice(),
'purchaseUrl' => $item->getPurchaseUrl(),
'createdAt' => $item->getCreatedAt()->format('Y-m-d H:i:s'),
];
return $this->json([
'item' => $itemDTO,
'path' => 'src/Controller/ItemController.php',
]);
}
'title' => $item->getTitle(),
'description' => $item->getDescription(),
'price' => $item->getPrice(),
'purchaseUrl' => $item->getPurchaseUrl(),
'createdAt' => $item->getCreatedAt()->format('Y-m-d H:i:s'),
];
return $this->json([
'item' => $itemDTO,
'path' => 'src/Controller/ItemController.php',
]);
}
// NEW METHODS
#[Route('', methods: ['POST'])]
public function createItem(Request $request): JsonResponse
{
$data = json_decode($request->getContent(), true);
if (json_last_error() !== JSON_ERROR_NONE) {
return new JsonResponse(
['error' => 'Invalid JSON payload'],
Response::HTTP_BAD_REQUEST
);
}
// Check required fields
if (!isset($data['title']) || !isset($data['price']) || !isset($data['wishList'])) {
return new JsonResponse(
['error' => 'Missing required fields: title, price and wishList are required'],
Response::HTTP_BAD_REQUEST
);
}
$item = new Item();
try {
$wishList = $this->wishListRepository->findOneById($data['wishList']);
if (!$wishList) {
return new JsonResponse(
['error' => 'WishList not found'],
Response::HTTP_BAD_REQUEST
);
}
$item->setWishList($wishList);
} catch (\Exception $e) {
return new JsonResponse(
['error' => 'Invalid wishList: ' . $e->getMessage()],
Response::HTTP_BAD_REQUEST
);
}
$item->setTitle($data['title']);
$item->setDescription($data['description'] ?? null);
$item->setPrice((float)$data['price']);
$item->setPurchaseUrl($data['purchaseUrl'] ?? null);
$item->setCreatedAt(new \DateTimeImmutable());
// Validate using ValidatorInterface
$errors = $this->validator->validate($item);
if (count($errors) > 0) {
$errorMessages = [];
foreach ($errors as $error) {
$errorMessages[$error->getPropertyPath()] = $error->getMessage();
}
return new JsonResponse(
['validation_errors' => $errorMessages],
Response::HTTP_UNPROCESSABLE_ENTITY
);
}
try {
$this->entityManager->persist($item);
$this->entityManager->flush();
return new JsonResponse([
'id' => $item->getId(),
'title' => $item->getTitle(),
'price' => $item->getPrice(),
'wishListId' => $item->getWishList()->getId(),
'createdAt' => $item->getCreatedAt()->format('Y-m-d H:i:s'),
'path' => 'src/Controller/ItemController.php'
], Response::HTTP_CREATED);
} catch (\Exception $e) {
return new JsonResponse(
['error' => 'Failed to create item: ' . $e->getMessage()],
Response::HTTP_INTERNAL_SERVER_ERROR
);
}
}
#[Route('/{id}', methods: ['PATCH'])]
public function updateItem(int $id, Request $request): JsonResponse
{
$item = $this->itemRepository->findByOne($id);
if (!$item) {
return new JsonResponse(
['error' => 'Item not found'],
Response::HTTP_NOT_FOUND
);
}
$data = json_decode($request->getContent(), true);
if (json_last_error() !== JSON_ERROR_NONE) {
return new JsonResponse(
['error' => 'Invalid JSON payload'],
Response::HTTP_BAD_REQUEST
);
}
if (isset($data['title'])) {
$item->setTitle($data['title']);
}
if (isset($data['description'])) {
$item->setDescription($data['description']);
}
if (isset($data['price'])) {
$item->setPrice((float)$data['price']);
}
if (isset($data['purchaseUrl'])) {
$item->setPurchaseUrl($data['purchaseUrl']);
}
if (isset($data['wishList'])) {
try {
$wishList = $this->wishListRepository->findByOne($data['wishList']);
if (!$wishList) {
return new JsonResponse(
['error' => 'WishList not found'],
Response::HTTP_BAD_REQUEST
);
}
$item->setWishList($wishList);
} catch (\Exception $e) {
return new JsonResponse(
['error' => 'Invalid wishList: ' . $e->getMessage()],
Response::HTTP_BAD_REQUEST
);
}
}
// Validate using ValidatorInterface
$errors = $this->validator->validate($item);
if (count($errors) > 0) {
$errorMessages = [];
foreach ($errors as $error) {
$errorMessages[$error->getPropertyPath()] = $error->getMessage();
}
return new JsonResponse(
['validation_errors' => $errorMessages],
Response::HTTP_UNPROCESSABLE_ENTITY
);
}
try {
$this->entityManager->flush();
return new JsonResponse([
'id' => $item->getId(),
'title' => $item->getTitle(),
'price' => $item->getPrice(),
'wishListId' => $item->getWishList()->getId(),
'updatedAt' => (new \DateTimeImmutable())->format('Y-m-d H:i:s'),
'path' => 'src/Controller/ItemController.php'
]);
} catch (\Exception $e) {
return new JsonResponse(
['error' => 'Failed to update item: ' . $e->getMessage()],
Response::HTTP_INTERNAL_SERVER_ERROR
);
}
}
#[Route('/{id}', methods: ['DELETE'])]
public function deleteItem(int $id): JsonResponse
{
$item = $this->itemRepository->findByOne($id);
if (!$item) {
return new JsonResponse(
['error' => 'Item not found'],
Response::HTTP_NOT_FOUND
);
}
try {
$this->entityManager->remove($item);
$this->entityManager->flush();
return new JsonResponse(
null,
Response::HTTP_NO_CONTENT
);
} catch (\Exception $e) {
return new JsonResponse(
['error' => 'Failed to delete item: ' . $e->getMessage()],
Response::HTTP_INTERNAL_SERVER_ERROR
);
}
}
......
......@@ -7,8 +7,10 @@ use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use App\Entity\WishList;
use App\Repository\WishListRepository;
use App\Repository\ItemRepository;
use App\Repository\UserRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Symfony\Component\HttpFoundation\Request;
......@@ -19,17 +21,20 @@ final class WishListController extends AbstractController
private EntityManagerInterface $entityManager;
private WishListRepository $wishListRepository;
private ItemRepository $itemRepository;
private UserRepository $userRepository;
private ValidatorInterface $validator;
public function __construct(
EntityManagerInterface $entityManager,
WishListRepository $wishListRepository,
ItemRepository $itemRepository,
UserRepository $userRepository,
ValidatorInterface $validator
) {
$this->entityManager = $entityManager;
$this->wishListRepository = $wishListRepository;
$this->itemRepository = $itemRepository;
$this->userRepository = $userRepository;
$this->validator = $validator;
}
......@@ -165,6 +170,201 @@ final class WishListController extends AbstractController
]);
}
// NEW METHODS
#[Route('', methods: ['POST'])]
public function createWishList(Request $request): JsonResponse
{
$data = json_decode($request->getContent(), true);
if (json_last_error() !== JSON_ERROR_NONE) {
return new JsonResponse(
['error' => 'Invalid JSON payload'],
Response::HTTP_BAD_REQUEST
);
}
$wishList = new WishList();
if (isset($data['user'])) {
try {
$user = $this->userRepository->findOneById($data['user']);
$wishList->setUser($user);
} catch (\Exception $e) {
return new JsonResponse(
['error' => 'Invalid user id' . $e ],
Response::HTTP_BAD_REQUEST
);
}
}
$wishList->setName($data['name'] ?? '');
$wishList->setDescription($data['description'] ?? null);
if (isset($data['expirationDate'])) {
try {
$wishList->setExpirationDate(new \DateTime($data['expirationDate']));
} catch (\Exception $e) {
return new JsonResponse(
['error' => 'Invalid expiration date format'],
Response::HTTP_BAD_REQUEST
);
}
}
$wishList->setIsActive($data['isActive'] ?? true);
$wishList->setUrlViewMode($data['urlViewMode'] ?? 'http://urlViewMode.com');
$wishList->setUrlEditMode($data['urlEditMode'] ?? 'http://urlEditMode.com');
$wishList->setCreatedAt(new \DateTimeImmutable());
// Validate using ValidatorInterface
$errors = $this->validator->validate($wishList);
if (count($errors) > 0) {
$errorMessages = [];
foreach ($errors as $error) {
$errorMessages[$error->getPropertyPath()] = $error->getMessage();
}
return new JsonResponse(
['validation_errors' => $errorMessages],
Response::HTTP_UNPROCESSABLE_ENTITY
);
}
try {
$this->entityManager->persist($wishList);
$this->entityManager->flush();
return new JsonResponse([
'id' => $wishList->getId(),
'name' => $wishList->getName(),
'description' => $wishList->getDescription(),
'isActive' => $wishList->isActive(),
'createdAt' => $wishList->getCreatedAt()->format('Y-m-d H:i:s'),
'path' => 'src/Controller/WishListController.php'
], Response::HTTP_CREATED);
} catch (\Exception $e) {
return new JsonResponse(
['error' => 'Failed to create wishlist: ' . $e->getMessage()],
Response::HTTP_INTERNAL_SERVER_ERROR
);
}
}
#[Route('/{id}', methods: ['PATCH'])]
public function updateWishList(int $id, Request $request): JsonResponse
{
$wishList = $this->wishListRepository->find($id);
if (!$wishList) {
return new JsonResponse(
['error' => 'Wishlist not found'],
Response::HTTP_NOT_FOUND
);
}
$data = json_decode($request->getContent(), true);
if (json_last_error() !== JSON_ERROR_NONE) {
return new JsonResponse(
['error' => 'Invalid JSON payload'],
Response::HTTP_BAD_REQUEST
);
}
if (isset($data['name'])) {
$wishList->setName($data['name']);
}
if (isset($data['description'])) {
$wishList->setDescription($data['description']);
}
if (isset($data['expirationDate'])) {
try {
$wishList->setExpirationDate(new \DateTime($data['expirationDate']));
} catch (\Exception $e) {
return new JsonResponse(
['error' => 'Invalid expiration date format'],
Response::HTTP_BAD_REQUEST
);
}
}
if (isset($data['isActive'])) {
$wishList->setIsActive($data['isActive']);
}
if (isset($data['urlViewMode'])) {
$wishList->setUrlViewMode($data['urlViewMode']);
}
if (isset($data['urlEditMode'])) {
$wishList->setUrlEditMode($data['urlEditMode']);
}
// Validate using ValidatorInterface
$errors = $this->validator->validate($wishList);
if (count($errors) > 0) {
$errorMessages = [];
foreach ($errors as $error) {
$errorMessages[$error->getPropertyPath()] = $error->getMessage();
}
return new JsonResponse(
['validation_errors' => $errorMessages],
Response::HTTP_UNPROCESSABLE_ENTITY
);
}
try {
$this->entityManager->flush();
return new JsonResponse([
'message' => 'WishList updated successfully',
'wishList' => [
'id' => $wishList->getId(),
'name' => $wishList->getName(),
'description' => $wishList->getDescription(),
'isActive' => $wishList->isActive(),
'updatedAt' => (new \DateTimeImmutable())->format('Y-m-d H:i:s')
]
]);
} catch (\Exception $e) {
return new JsonResponse(
['error' => 'Failed to update wishlist: ' . $e->getMessage()],
Response::HTTP_INTERNAL_SERVER_ERROR
);
}
}
#[Route('/{id}', methods: ['DELETE'])]
public function deleteWishList(int $id): JsonResponse
{
$wishList = $this->wishListRepository->find($id);
if (!$wishList) {
return new JsonResponse(
['error' => 'Wishlist not found'],
Response::HTTP_NOT_FOUND
);
}
try {
$this->entityManager->remove($wishList);
$this->entityManager->flush();
return new JsonResponse(
[
'message' => 'WishList deleted successfully',
'path' => 'src/Controller/WishListController.php',
],
Response::HTTP_OK
);
} catch (\Exception $e) {
return new JsonResponse(
['error' => 'Failed to delete wishlist: ' . $e->getMessage()],
Response::HTTP_INTERNAL_SERVER_ERROR
);
}
}
}
......@@ -64,25 +64,7 @@ class PurchaseFixtures extends Fixture implements DependentFixtureInterface
'item' => $items[6], // Mechanical Keyboard
'url_proof' => 'https://example.com/proof7.jpg',
'message' => 'Enjoy your gift!',
],
[
'user' => $users[9],
'item' => $items[7], // E-Reader
'url_proof' => 'https://example.com/proof8.jpg',
'message' => 'Something I thought you’d love!',
],
[
'user' => $users[5],
'item' => $items[8], // Noise Cancelling Earbuds
'url_proof' => 'https://example.com/proof9.jpg',
'message' => 'A special gift for a special person!',
],
[
'user' => $users[2],
'item' => $items[9], // Camera Drone
'url_proof' => 'https://example.com/proof10.jpg',
'message' => 'Just a small token of appreciation!',
],
]
];
foreach ($purchasesData as $data) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment