Skip to content

Commit

Permalink
feat: added addToFavorite feature
Browse files Browse the repository at this point in the history
  • Loading branch information
benborla committed Nov 18, 2023
1 parent 9fa0006 commit cb1fbcf
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 2 deletions.
37 changes: 37 additions & 0 deletions api/migrations/Version20231118070207.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace DoctrineMigrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20231118070207 extends AbstractMigration
{
public function getDescription(): string
{
return '';
}

public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('CREATE TABLE favorites (id INT AUTO_INCREMENT NOT NULL, fruit_id INT NOT NULL, date_added DATETIME DEFAULT NULL, UNIQUE INDEX UNIQ_E46960F5BAC115F0 (fruit_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
$this->addSql('ALTER TABLE favorites ADD CONSTRAINT FK_E46960F5BAC115F0 FOREIGN KEY (fruit_id) REFERENCES fruits (id)');
$this->addSql('ALTER TABLE favorite DROP FOREIGN KEY FK_68C58ED9BAC115F0');
$this->addSql('DROP TABLE favorite');
}

public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('CREATE TABLE favorite (id INT AUTO_INCREMENT NOT NULL, fruit_id INT NOT NULL, date_added DATETIME DEFAULT NULL, UNIQUE INDEX UNIQ_68C58ED9BAC115F0 (fruit_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB COMMENT = \'\' ');
$this->addSql('ALTER TABLE favorite ADD CONSTRAINT FK_68C58ED9BAC115F0 FOREIGN KEY (fruit_id) REFERENCES fruits (id) ON UPDATE NO ACTION ON DELETE NO ACTION');
$this->addSql('ALTER TABLE favorites DROP FOREIGN KEY FK_E46960F5BAC115F0');
$this->addSql('DROP TABLE favorites');
}
}
56 changes: 54 additions & 2 deletions api/src/Controller/IndexController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use App\Repository\FruitRepository;
use App\Repository\FavoriteRepository;
use Symfony\Component\Routing\Annotation\Route;
use App\Entity\Fruit;
use App\Entity\Favorite;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Doctrine\ORM\EntityManagerInterface;

Expand All @@ -22,6 +24,9 @@ public function __construct(private EntityManagerInterface $em)
/**
* Retrieve all fruits based on request parameter
*
* @param \Symfony\Component\HttpFoundation\Request $request
* @param \App\Repository\FruitRepository $fruits
*
* @return \Symfony\Component\HttpFoundation\JsonResponse
*/
#[Route('/', name: 'fruits', methods: ['GET'])]
Expand All @@ -45,6 +50,9 @@ public function all(Request $request, FruitRepository $fruits): JsonResponse
/**
* Creates an instance of Fruit
*
* @param \Symfony\Component\HttpFoundation\Request $request
* @param \Symfony\Component\Validator\Validator\ValidatorInterface $validator
*
* @return \Symfony\Component\HttpFoundation\JsonResponse
*/
#[Route('/fruit', name: 'fruit_add', methods: ['POST', 'PUT'])]
Expand Down Expand Up @@ -76,6 +84,9 @@ public function fruitNew(Request $request, ValidatorInterface $validator): JsonR
/**
* Get or patch a single Fruit instance based on Fruit ID provided
*
* @param \Symfony\Component\HttpFoundation\Request $request
* @param \App\Repository\FruitRepository $fruits
*
* @return \Symfony\Component\HttpFoundation\JsonResponse
*/
#[Route('/fruit/{id}', name: 'fruit', methods: ['GET', 'POST', 'PATCH', 'DELETE'])]
Expand Down Expand Up @@ -119,9 +130,50 @@ public function fruit(Request $request, FruitRepository $fruits)

return $this->json($fruit->toArray());
}
}

/**
* Add fruit to favorites
*
* @param \Symfony\Component\HttpFoundation\Request $request
* @param \App\Repository\FruitRepository $fruits
*
* @return \Symfony\Component\HttpFoundation\JsonResponse
*/
#[Route('/favorite/{id}', name: 'favorite.add', methods: ['POST'])]
public function addToFavorite(
Request $request,
FruitRepository $fruits,
FavoriteRepository $favorites,
) {
$id = (int) $request->attributes->get('id');
$fruit = $fruits->find($id);

// @INFO: Throw a 404 if a resource is not found
// @INFO: Ensure that request method is DELETE
if (
!$fruit instanceof Fruit ||
$request->getMethod() !== Request::METHOD_POST
) {
return $this->json([], Response::HTTP_NOT_FOUND);
}

$favorite = $favorites->findOneBy(['fruit' => $id]);


if (!$favorite instanceof Favorite) {
$favorite = (new Favorite())
->setFruit($fruit)
->setDateAdded(new \DateTime('now'));

$this->em->persist($favorite);
$this->em->flush();
}

// @INFO: Default 404 return
return $this->json([], Response::HTTP_NOT_FOUND);
return $this->json([
'fruit' => $favorite->getFruit()->toArray(),
'date_added' => $favorite->getId(),
]);
}

/**
Expand Down
10 changes: 10 additions & 0 deletions api/src/Entity/Favorite.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity(repositoryClass: FavoriteRepository::class)]
#[ORM\Table(name: 'favorites')]
class Favorite
{
/**
* INFO: Set a maximum number of items in the favorites table
*/
public const MAX_FAVORITE = 10;
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
Expand Down Expand Up @@ -49,4 +54,9 @@ public function setDateAdded(?\DateTimeInterface $dateAdded): static

return $this;
}

public function toArray(): array
{
return get_object_vars($this);
}
}
17 changes: 17 additions & 0 deletions api/src/Entity/Fruit.php
Original file line number Diff line number Diff line change
Expand Up @@ -258,4 +258,21 @@ public function toArray(): array
{
return get_object_vars($this);
}

public function getFavorite(): ?Favorite
{
return $this->favorite;
}

public function setFavorite(Favorite $favorite): static
{
// set the owning side of the relation if necessary
if ($favorite->getFruit() !== $this) {
$favorite->setFruit($this);
}

$this->favorite = $favorite;

return $this;
}
}

0 comments on commit cb1fbcf

Please sign in to comment.