Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/add get request #2

Merged
merged 4 commits into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion backend/src/main/java/org/example/backend/Product.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
public record Product(
String id,
String name,
int ammount
int amount
) {
}
11 changes: 8 additions & 3 deletions backend/src/main/java/org/example/backend/ProductController.java
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();
}
}
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 backend/src/main/java/org/example/backend/ProductService.java
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();
}


}
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 backend/src/test/java/org/example/backend/ProductServiceTest.java
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);
}
}
8 changes: 5 additions & 3 deletions frontend/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@

import './App.css'
import ProductList from "./componets/ProductList.tsx";

function App() {

return (
<>
<>
<h1>Groceries app</h1>
<ProductList/>


</>
</>
)
}

Expand Down
30 changes: 30 additions & 0 deletions frontend/src/componets/ProductList.tsx
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>
);
}


Loading