From 824b10855ce452d561c321110965727684b64d9c Mon Sep 17 00:00:00 2001 From: Marcel Herr Date: Wed, 14 Aug 2024 15:31:17 +0200 Subject: [PATCH 01/12] add put endpoint to backend --- .../backend/controllers/BookController.java | 15 ++++++++++++--- .../com/github/esgoet/backend/models/Book.java | 7 ++++++- .../backend/repositories/BookRepository.java | 11 +++++++++++ .../esgoet/backend/services/BookService.java | 4 ++++ 4 files changed, 33 insertions(+), 4 deletions(-) diff --git a/backend/src/main/java/com/github/esgoet/backend/controllers/BookController.java b/backend/src/main/java/com/github/esgoet/backend/controllers/BookController.java index f55eded..93fd62a 100644 --- a/backend/src/main/java/com/github/esgoet/backend/controllers/BookController.java +++ b/backend/src/main/java/com/github/esgoet/backend/controllers/BookController.java @@ -3,9 +3,9 @@ import com.github.esgoet.backend.models.Book; import com.github.esgoet.backend.services.BookService; 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 org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.*; +import org.springframework.web.server.ResponseStatusException; import java.util.List; @@ -21,4 +21,13 @@ public class BookController { public List getBooks() { return bookService.getAllBooks(); } + + @PutMapping(path = {"{id}/update", "{id}"}) + Book update(@PathVariable String id, @RequestBody Book book) { + if (!book.id().equals(id)) { + throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "The id in the url does not match the request body's id"); + } + return bookService.update(book); + } + } diff --git a/backend/src/main/java/com/github/esgoet/backend/models/Book.java b/backend/src/main/java/com/github/esgoet/backend/models/Book.java index ff5d770..553f117 100644 --- a/backend/src/main/java/com/github/esgoet/backend/models/Book.java +++ b/backend/src/main/java/com/github/esgoet/backend/models/Book.java @@ -8,5 +8,10 @@ public record Book( String id, String author, - String title) { + String title, + String genre, + String isbn, + String cover, + String description +) { } diff --git a/backend/src/main/java/com/github/esgoet/backend/repositories/BookRepository.java b/backend/src/main/java/com/github/esgoet/backend/repositories/BookRepository.java index f8eb941..401495c 100644 --- a/backend/src/main/java/com/github/esgoet/backend/repositories/BookRepository.java +++ b/backend/src/main/java/com/github/esgoet/backend/repositories/BookRepository.java @@ -4,6 +4,17 @@ import org.springframework.data.mongodb.repository.MongoRepository; import org.springframework.stereotype.Repository; +import java.util.HashMap; +import java.util.Map; + + @Repository public interface BookRepository extends MongoRepository { + + final Map books = new HashMap<>(); + + public default Book update(Book book) { + books.put(book.id(), book); + return book; + } } diff --git a/backend/src/main/java/com/github/esgoet/backend/services/BookService.java b/backend/src/main/java/com/github/esgoet/backend/services/BookService.java index c42aaa4..6fe7fd5 100644 --- a/backend/src/main/java/com/github/esgoet/backend/services/BookService.java +++ b/backend/src/main/java/com/github/esgoet/backend/services/BookService.java @@ -16,4 +16,8 @@ public class BookService { public List getAllBooks() { return bookRepository.findAll(); } + + public Book update(Book book) { + return bookRepository.update(book); + } } From 13a3fbf5cbcd7e05d8416e2e0d68b9e478864b94 Mon Sep 17 00:00:00 2001 From: Marcel Herr Date: Wed, 14 Aug 2024 16:45:35 +0200 Subject: [PATCH 02/12] add editForm component --- frontend/src/App.tsx | 16 ++--- frontend/src/components/editForm/EditForm.css | 5 ++ frontend/src/components/editForm/EditForm.tsx | 58 +++++++++++++++++++ 3 files changed, 71 insertions(+), 8 deletions(-) create mode 100644 frontend/src/components/editForm/EditForm.css create mode 100644 frontend/src/components/editForm/EditForm.tsx diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx index 4c161ec..b6ab4b0 100644 --- a/frontend/src/App.tsx +++ b/frontend/src/App.tsx @@ -4,11 +4,12 @@ import axios from "axios" import {Book} from "./types/types.ts"; import {useEffect, useState} from "react"; import BookGallery from "./components/bookGallery/BookGallery.tsx"; +import EditForm from "./components/editForm/EditForm.tsx"; function App() { - const[data, setData] = useState([]) + const [data, setData] = useState([]) const fetchBooks = () => { axios.get("/api/books") @@ -24,14 +25,13 @@ function App() { fetchBooks() }, []); - console.log(data) - return ( - <> -

LibraryApp

-

Cool motto ( ͡° ͜ʖ ͡°)

- - + <> +

LibraryApp

+

Cool motto ( ͡° ͜ʖ ͡°)

+ + + ) } diff --git a/frontend/src/components/editForm/EditForm.css b/frontend/src/components/editForm/EditForm.css new file mode 100644 index 0000000..6328b39 --- /dev/null +++ b/frontend/src/components/editForm/EditForm.css @@ -0,0 +1,5 @@ +form { + display: flex; + flex-direction: column; +} + diff --git a/frontend/src/components/editForm/EditForm.tsx b/frontend/src/components/editForm/EditForm.tsx new file mode 100644 index 0000000..13273ff --- /dev/null +++ b/frontend/src/components/editForm/EditForm.tsx @@ -0,0 +1,58 @@ +import './EditForm.css' +import {ChangeEvent, FormEvent, useState} from "react"; +import axios from "axios"; + +export default function EditForm({data}) { + + type BookData = { + title?: string, + author?: string, + description?: string, + genre?: string, + isbn?: string, + cover?: string + + } + + const [formData, setFormData] = useState() + + function handleChange(event: ChangeEvent) { + const {name, value} = event.target; + setFormData({ + ...formData, [name]: value + }); + } + + async function onSubmit(event: FormEvent) { + event.preventDefault(); + try { + const response = await axios.put(`/api/books/${data.id}/update`, formData); + console.log('Update successful:', response.data); + } catch (error) { + console.error('Update failed:', error); + } + console.log(formData) + } + + function onCancel() { + console.log("cancel") + } + + return ( + +
+ + + +