From 0548487eea5787ae7c55b496497997dc5cc7abac Mon Sep 17 00:00:00 2001 From: vokarl Date: Thu, 15 Aug 2024 21:05:24 +0200 Subject: [PATCH 1/4] added get request in backend --- .../java/org/example/backend/ProductController.java | 7 +++++++ .../java/org/example/backend/ProductRepository.java | 1 + .../java/org/example/backend/ProductService.java | 13 ++++++++++--- .../org/example/backend/ProductControllerTest.java | 11 +++++++++++ 4 files changed, 29 insertions(+), 3 deletions(-) create mode 100644 backend/src/test/java/org/example/backend/ProductControllerTest.java diff --git a/backend/src/main/java/org/example/backend/ProductController.java b/backend/src/main/java/org/example/backend/ProductController.java index fe534b1..35fec8d 100644 --- a/backend/src/main/java/org/example/backend/ProductController.java +++ b/backend/src/main/java/org/example/backend/ProductController.java @@ -1,8 +1,11 @@ package org.example.backend; +import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; +import java.util.List; + @RestController @RequestMapping public class ProductController { @@ -11,4 +14,8 @@ public class ProductController { public ProductController(ProductService productService) { this.productService = productService; } + @GetMapping + public List getAllGroceries() { + return productService.findAllGroceries(); + } } diff --git a/backend/src/main/java/org/example/backend/ProductRepository.java b/backend/src/main/java/org/example/backend/ProductRepository.java index c719dfa..15aa29b 100644 --- a/backend/src/main/java/org/example/backend/ProductRepository.java +++ b/backend/src/main/java/org/example/backend/ProductRepository.java @@ -3,4 +3,5 @@ import org.springframework.data.mongodb.repository.MongoRepository; public interface ProductRepository extends MongoRepository { + } diff --git a/backend/src/main/java/org/example/backend/ProductService.java b/backend/src/main/java/org/example/backend/ProductService.java index 4c9acd9..118842a 100644 --- a/backend/src/main/java/org/example/backend/ProductService.java +++ b/backend/src/main/java/org/example/backend/ProductService.java @@ -2,11 +2,18 @@ import org.springframework.stereotype.Service; +import java.util.List; + @Service public class ProductService { - private final ProductService productService; + private final ProductRepository productRepository; - public ProductService(ProductService productService) { - this.productService = productService; + public ProductService(ProductRepository productRepository) { + this.productRepository = productRepository; + } + public List findAllGroceries(){ + return productRepository.findAll(); } + + } diff --git a/backend/src/test/java/org/example/backend/ProductControllerTest.java b/backend/src/test/java/org/example/backend/ProductControllerTest.java new file mode 100644 index 0000000..70259ab --- /dev/null +++ b/backend/src/test/java/org/example/backend/ProductControllerTest.java @@ -0,0 +1,11 @@ +package org.example.backend; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; +class ProductControllerTest { + + @Test + void getAllGroceries() { + } +} \ No newline at end of file From e794b020304c16c643845da004d5b5f6673c132a Mon Sep 17 00:00:00 2001 From: vokarl Date: Thu, 15 Aug 2024 22:29:32 +0200 Subject: [PATCH 2/4] added testing for get request in backend --- .../example/backend/ProductController.java | 2 +- .../org/example/backend/ProductService.java | 2 ++ .../backend/BackendApplicationTest.java | 13 +++++++++ .../backend/ProductControllerTest.java | 28 +++++++++++++++++- .../example/backend/ProductServiceTest.java | 29 +++++++++++++++++++ 5 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 backend/src/test/java/org/example/backend/BackendApplicationTest.java create mode 100644 backend/src/test/java/org/example/backend/ProductServiceTest.java diff --git a/backend/src/main/java/org/example/backend/ProductController.java b/backend/src/main/java/org/example/backend/ProductController.java index 35fec8d..7cc978b 100644 --- a/backend/src/main/java/org/example/backend/ProductController.java +++ b/backend/src/main/java/org/example/backend/ProductController.java @@ -7,7 +7,7 @@ import java.util.List; @RestController -@RequestMapping +@RequestMapping("/api/products") public class ProductController { private final ProductService productService; diff --git a/backend/src/main/java/org/example/backend/ProductService.java b/backend/src/main/java/org/example/backend/ProductService.java index 118842a..83de4f2 100644 --- a/backend/src/main/java/org/example/backend/ProductService.java +++ b/backend/src/main/java/org/example/backend/ProductService.java @@ -4,6 +4,8 @@ import java.util.List; + + @Service public class ProductService { private final ProductRepository productRepository; diff --git a/backend/src/test/java/org/example/backend/BackendApplicationTest.java b/backend/src/test/java/org/example/backend/BackendApplicationTest.java new file mode 100644 index 0000000..8c1aac8 --- /dev/null +++ b/backend/src/test/java/org/example/backend/BackendApplicationTest.java @@ -0,0 +1,13 @@ +package org.example.backend; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class TodoBackendApplicationTests { + + @Test + void contextLoads() { + } + +} diff --git a/backend/src/test/java/org/example/backend/ProductControllerTest.java b/backend/src/test/java/org/example/backend/ProductControllerTest.java index 70259ab..ec7e655 100644 --- a/backend/src/test/java/org/example/backend/ProductControllerTest.java +++ b/backend/src/test/java/org/example/backend/ProductControllerTest.java @@ -1,11 +1,37 @@ package org.example.backend; import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import static org.junit.jupiter.api.Assertions.*; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + + +@SpringBootTest +@AutoConfigureMockMvc class ProductControllerTest { + @Autowired + MockMvc mockMvc; + + @Autowired + ProductRepository productRepository; + @Test - void getAllGroceries() { + void getAllGroceries() throws Exception { + //GIVEN + //WHEN + mockMvc.perform(MockMvcRequestBuilders.get("/api/products")) + + //THEN + .andExpect(status().isOk()) + .andExpect(content().json(""" + [] + """)); } } \ No newline at end of file diff --git a/backend/src/test/java/org/example/backend/ProductServiceTest.java b/backend/src/test/java/org/example/backend/ProductServiceTest.java new file mode 100644 index 0000000..7299b76 --- /dev/null +++ b/backend/src/test/java/org/example/backend/ProductServiceTest.java @@ -0,0 +1,29 @@ +package org.example.backend; + +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.*; + +class ProductServiceTest { + ProductRepository productRepository = mock(ProductRepository.class); + ProductService productService = new ProductService(productRepository); + + @Test + void findAllGroceries() { + //GIVEN + Product p1 = new Product("1", "apple", 2); + Product p2 = new Product("2", "peach",2); + Product p3 = new Product("3", "banana",3); + List products = List.of(p1, p2, p3); + + when(productRepository.findAll()).thenReturn(products); + //WHEN + List actual = productService.findAllGroceries(); + //THEN + verify(productRepository).findAll(); + assertEquals(products, actual); + } +} \ No newline at end of file From 9a069a1d988e229f141afe2e4937407ece1eb961 Mon Sep 17 00:00:00 2001 From: vokarl Date: Thu, 15 Aug 2024 23:40:59 +0200 Subject: [PATCH 3/4] added frontend for get request --- frontend/src/App.tsx | 8 ++++--- frontend/src/componets/ProductList.tsx | 30 ++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 3 deletions(-) create mode 100644 frontend/src/componets/ProductList.tsx diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx index b808013..694c2f5 100644 --- a/frontend/src/App.tsx +++ b/frontend/src/App.tsx @@ -1,13 +1,15 @@ import './App.css' +import ProductList from "./componets/ProductList.tsx"; function App() { return ( - <> + <> +

Groceries app

+ - - + ) } diff --git a/frontend/src/componets/ProductList.tsx b/frontend/src/componets/ProductList.tsx new file mode 100644 index 0000000..5ae6063 --- /dev/null +++ b/frontend/src/componets/ProductList.tsx @@ -0,0 +1,30 @@ +import React, { useEffect, useState } from 'react'; +import axios from 'axios'; +import { Product } from '../models/product.tsx'; + +export default function ProductList() { + const [products, setProducts] = useState([]); // Verwende den Typ für die State-Variable + + useEffect(() => { + axios.get('http://localhost:8080/api/products') + .then(response => { + setProducts(response.data); + }) + .catch(error => { + console.error("Es gab einen Fehler beim Abrufen der Daten!", error); + }); + }, []); + + return ( +
+

Einkaufsliste

+
    + {products.map(product => ( +
  • {product.name} - Menge: {product.ammount}
  • + ))} +
+
+ ); +} + + From f2ed69f6f8578ca429b486826d1f954d4c252b24 Mon Sep 17 00:00:00 2001 From: vokarl Date: Fri, 16 Aug 2024 14:43:28 +0200 Subject: [PATCH 4/4] refactoring --- .idea/compiler.xml | 1 + .../src/main/java/org/example/backend/Product.java | 2 +- .../java/org/example/backend/ProductController.java | 6 ++---- .../java/org/example/backend/ProductRepository.java | 2 ++ .../java/org/example/backend/ProductService.java | 7 +++---- .../org/example/backend/BackendApplicationTest.java | 13 ------------- frontend/src/componets/ProductList.tsx | 2 +- 7 files changed, 10 insertions(+), 23 deletions(-) delete mode 100644 backend/src/test/java/org/example/backend/BackendApplicationTest.java diff --git a/.idea/compiler.xml b/.idea/compiler.xml index c86ed3f..66bc19f 100644 --- a/.idea/compiler.xml +++ b/.idea/compiler.xml @@ -2,6 +2,7 @@ + diff --git a/backend/src/main/java/org/example/backend/Product.java b/backend/src/main/java/org/example/backend/Product.java index b290558..bab9ee3 100644 --- a/backend/src/main/java/org/example/backend/Product.java +++ b/backend/src/main/java/org/example/backend/Product.java @@ -3,6 +3,6 @@ public record Product( String id, String name, - int ammount + int amount ) { } diff --git a/backend/src/main/java/org/example/backend/ProductController.java b/backend/src/main/java/org/example/backend/ProductController.java index 7cc978b..880073e 100644 --- a/backend/src/main/java/org/example/backend/ProductController.java +++ b/backend/src/main/java/org/example/backend/ProductController.java @@ -1,19 +1,17 @@ package org.example.backend; +import lombok.RequiredArgsConstructor; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; - +@RequiredArgsConstructor @RestController @RequestMapping("/api/products") public class ProductController { private final ProductService productService; - public ProductController(ProductService productService) { - this.productService = productService; - } @GetMapping public List getAllGroceries() { return productService.findAllGroceries(); diff --git a/backend/src/main/java/org/example/backend/ProductRepository.java b/backend/src/main/java/org/example/backend/ProductRepository.java index 15aa29b..6c1b23c 100644 --- a/backend/src/main/java/org/example/backend/ProductRepository.java +++ b/backend/src/main/java/org/example/backend/ProductRepository.java @@ -1,7 +1,9 @@ package org.example.backend; import org.springframework.data.mongodb.repository.MongoRepository; +import org.springframework.stereotype.Repository; +@Repository public interface ProductRepository extends MongoRepository { } diff --git a/backend/src/main/java/org/example/backend/ProductService.java b/backend/src/main/java/org/example/backend/ProductService.java index 83de4f2..7434024 100644 --- a/backend/src/main/java/org/example/backend/ProductService.java +++ b/backend/src/main/java/org/example/backend/ProductService.java @@ -1,18 +1,17 @@ package org.example.backend; +import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import java.util.List; - +@RequiredArgsConstructor @Service public class ProductService { private final ProductRepository productRepository; - public ProductService(ProductRepository productRepository) { - this.productRepository = productRepository; - } + public List findAllGroceries(){ return productRepository.findAll(); } diff --git a/backend/src/test/java/org/example/backend/BackendApplicationTest.java b/backend/src/test/java/org/example/backend/BackendApplicationTest.java deleted file mode 100644 index 8c1aac8..0000000 --- a/backend/src/test/java/org/example/backend/BackendApplicationTest.java +++ /dev/null @@ -1,13 +0,0 @@ -package org.example.backend; - -import org.junit.jupiter.api.Test; -import org.springframework.boot.test.context.SpringBootTest; - -@SpringBootTest -class TodoBackendApplicationTests { - - @Test - void contextLoads() { - } - -} diff --git a/frontend/src/componets/ProductList.tsx b/frontend/src/componets/ProductList.tsx index 5ae6063..a4d6a01 100644 --- a/frontend/src/componets/ProductList.tsx +++ b/frontend/src/componets/ProductList.tsx @@ -1,4 +1,4 @@ -import React, { useEffect, useState } from 'react'; +import { useEffect, useState } from 'react'; import axios from 'axios'; import { Product } from '../models/product.tsx';