-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from neuefische/feat/add-get-request
Feat/add get request
- Loading branch information
Showing
9 changed files
with
125 additions
and
10 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,6 @@ | |
public record Product( | ||
String id, | ||
String name, | ||
int ammount | ||
int amount | ||
) { | ||
} |
11 changes: 8 additions & 3 deletions
11
backend/src/main/java/org/example/backend/ProductController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,19 @@ | ||
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 | ||
@RequestMapping("/api/products") | ||
public class ProductController { | ||
private final ProductService productService; | ||
|
||
public ProductController(ProductService productService) { | ||
this.productService = productService; | ||
@GetMapping | ||
public List<Product> getAllGroceries() { | ||
return productService.findAllGroceries(); | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
backend/src/main/java/org/example/backend/ProductRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,9 @@ | ||
package org.example.backend; | ||
|
||
import org.springframework.data.mongodb.repository.MongoRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
@Repository | ||
public interface ProductRepository extends MongoRepository<Product, String> { | ||
|
||
} |
14 changes: 11 additions & 3 deletions
14
backend/src/main/java/org/example/backend/ProductService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,20 @@ | ||
package org.example.backend; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.List; | ||
|
||
|
||
@RequiredArgsConstructor | ||
@Service | ||
public class ProductService { | ||
private final ProductService productService; | ||
private final ProductRepository productRepository; | ||
|
||
|
||
public ProductService(ProductService productService) { | ||
this.productService = productService; | ||
public List<Product> findAllGroceries(){ | ||
return productRepository.findAll(); | ||
} | ||
|
||
|
||
} |
37 changes: 37 additions & 0 deletions
37
backend/src/test/java/org/example/backend/ProductControllerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +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() throws Exception { | ||
//GIVEN | ||
//WHEN | ||
mockMvc.perform(MockMvcRequestBuilders.get("/api/products")) | ||
|
||
//THEN | ||
.andExpect(status().isOk()) | ||
.andExpect(content().json(""" | ||
[] | ||
""")); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
backend/src/test/java/org/example/backend/ProductServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Product> products = List.of(p1, p2, p3); | ||
|
||
when(productRepository.findAll()).thenReturn(products); | ||
//WHEN | ||
List<Product> actual = productService.findAllGroceries(); | ||
//THEN | ||
verify(productRepository).findAll(); | ||
assertEquals(products, actual); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { useEffect, useState } from 'react'; | ||
import axios from 'axios'; | ||
import { Product } from '../models/product.tsx'; | ||
|
||
export default function ProductList() { | ||
const [products, setProducts] = useState<Product[]>([]); // Verwende den Typ für die State-Variable | ||
|
||
useEffect(() => { | ||
axios.get<Product[]>('http://localhost:8080/api/products') | ||
.then(response => { | ||
setProducts(response.data); | ||
}) | ||
.catch(error => { | ||
console.error("Es gab einen Fehler beim Abrufen der Daten!", error); | ||
}); | ||
}, []); | ||
|
||
return ( | ||
<div> | ||
<h1>Einkaufsliste</h1> | ||
<ul> | ||
{products.map(product => ( | ||
<li key={product.id}>{product.name} - Menge: {product.ammount}</li> | ||
))} | ||
</ul> | ||
</div> | ||
); | ||
} | ||
|
||
|