diff --git a/api/src/Controller/FruitController.php b/api/src/Controller/FruitController.php index a6a8c7b..1da6de0 100644 --- a/api/src/Controller/FruitController.php +++ b/api/src/Controller/FruitController.php @@ -133,6 +133,19 @@ public function fruit(Request $request, FruitRepository $fruits) } } + /** + * Get all favorites + * + * @param \App\Repository\FavoriteRepository $favorites + * + * @return \Symfony\Component\HttpFoundation\JsonResponse + */ + #[Route('/favorites', name: 'favorite.all', methods: ['GET'])] + public function favorites(FavoriteRepository $favorites) + { + return $this->json($favorites->all()); + } + /** * Add fruit to favorites * diff --git a/api/src/Repository/FavoriteRepository.php b/api/src/Repository/FavoriteRepository.php index 71dee5c..948ebc1 100644 --- a/api/src/Repository/FavoriteRepository.php +++ b/api/src/Repository/FavoriteRepository.php @@ -21,28 +21,37 @@ public function __construct(ManagerRegistry $registry) parent::__construct($registry, Favorite::class); } -// /** -// * @return Favorite[] Returns an array of Favorite objects -// */ -// public function findByExampleField($value): array -// { -// return $this->createQueryBuilder('f') -// ->andWhere('f.exampleField = :val') -// ->setParameter('val', $value) -// ->orderBy('f.id', 'ASC') -// ->setMaxResults(10) -// ->getQuery() -// ->getResult() -// ; -// } + public function all() + { + return $this->createQueryBuilder('f') + ->select('f.id', 'f.dateAdded', 'fruit.id as fruit_id', 'fruit.name as fruit_name') + ->leftJoin('f.fruit', 'fruit') + ->getQuery() + ->getResult(\Doctrine\ORM\Query::HYDRATE_ARRAY); + } + + // /** + // * @return Favorite[] Returns an array of Favorite objects + // */ + // public function findByExampleField($value): array + // { + // return $this->createQueryBuilder('f') + // ->andWhere('f.exampleField = :val') + // ->setParameter('val', $value) + // ->orderBy('f.id', 'ASC') + // ->setMaxResults(10) + // ->getQuery() + // ->getResult() + // ; + // } -// public function findOneBySomeField($value): ?Favorite -// { -// return $this->createQueryBuilder('f') -// ->andWhere('f.exampleField = :val') -// ->setParameter('val', $value) -// ->getQuery() -// ->getOneOrNullResult() -// ; -// } + // public function findOneBySomeField($value): ?Favorite + // { + // return $this->createQueryBuilder('f') + // ->andWhere('f.exampleField = :val') + // ->setParameter('val', $value) + // ->getQuery() + // ->getOneOrNullResult() + // ; + // } } diff --git a/app/src/views/Form.vue b/app/src/views/Form.vue index a3d67ec..111443f 100644 --- a/app/src/views/Form.vue +++ b/app/src/views/Form.vue @@ -2,6 +2,10 @@

Fruit Details

+
@@ -9,8 +13,8 @@
+
-
@@ -42,10 +46,15 @@ v-model="fruit.calories">
- + +
+
@@ -55,11 +64,24 @@ import FruitsApi from '@/api/fruits' import type Fruit from '@/types/Fruit' export default defineComponent({ - name: 'form', + name: 'fruit-form', data() { return { - fruit: {} as Fruit, + fruit: { + id: null, + name: '', + genus: '', + family: '', + fruitOrder: '', + carbohydrates: 0, + protein: 0, + fat: 0, + sugar: 0, + calories: 0 + } as Fruit, message: '' as string, + isSuccessful: true, + submitted: false, } }, methods: { @@ -71,18 +93,40 @@ export default defineComponent({ }) }, + async createData() { + await FruitsApi.new(this.fruit) + .then((response: any) => { + this.message = 'Fruit data has been created' + this.isSuccessful = true + this.submitted = true + }) + .catch((e: Error) => { + console.error(e); + this.message = 'Something went wrong, please try again' + this.isSuccessful = false + }) + }, + async updateData() { await FruitsApi.update(this.fruit.id, this.fruit) - .then((response: any) => { - this.message = 'Fruit data has been updated' - }) - .error((e: Error) => { - console.error(e); - }) + .then((response: any) => { + this.message = 'Fruit data has been updated' + this.isSuccessful = true + this.submitted = true + }) + .catch((e: Error) => { + console.error(e); + this.message = 'Something went wrong, please try again' + this.isSuccessful = false + }) } }, mounted() { - this.retrieveData(this.$route.params.id) + // @INFO: Only retrieve, if the id is provided in the /fruit route + if (this.$route.params.id) { + this.retrieveData(this.$route.params.id) + } + this.message = '' }, onUnmounted() {