Skip to content

Commit a9c651f

Browse files
author
simon
committed
using genreIds and authorIds on Book Post,put
1 parent 9d3ff0c commit a9c651f

File tree

5 files changed

+60
-35
lines changed

5 files changed

+60
-35
lines changed

API_DOCUMENTATION.md

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -120,19 +120,11 @@ Base Path: `/books`
120120

121121
```json
122122
{
123-
"name": "The Lord of the Rings",
123+
"name": "The Lord he Rings",
124124
"isbn": "978-3-16-148410-0",
125125
"publisher": "Klett-Cotta",
126-
"genres": [
127-
{
128-
"id": 1
129-
}
130-
],
131-
"authors": [
132-
{
133-
"id": 1
134-
}
135-
],
126+
"genreIds": [1, 6],
127+
"authorIds": [1, 2],
136128
"availableCopies": 10,
137129
"totalCopies": 10
138130
}
@@ -142,20 +134,12 @@ Base Path: `/books`
142134

143135
```json
144136
{
145-
"name": "The Hobbit",
137+
"name": "The Lord he Rings",
146138
"isbn": "978-3-16-148410-0",
147139
"publisher": "Klett-Cotta",
148-
"genres": [
149-
{
150-
"id": 1
151-
}
152-
],
153-
"authors": [
154-
{
155-
"id": 1
156-
}
157-
],
158-
"availableCopies": 5,
140+
"genreIds": [1, 6],
141+
"authorIds": [1, 2],
142+
"availableCopies": 10,
159143
"totalCopies": 10
160144
}
161145
```

src/main/java/com/themetalstorm/bibliothekssystem/controller/BookController.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.themetalstorm.bibliothekssystem.controller;
22

3+
import com.themetalstorm.bibliothekssystem.dto.BookPostDTO;
34
import com.themetalstorm.bibliothekssystem.service.BookService;
45
import com.themetalstorm.bibliothekssystem.dto.BookDTO;
56
import org.springframework.beans.factory.annotation.Autowired;
@@ -41,13 +42,13 @@ ResponseEntity<BookDTO> findById(@PathVariable int id) {
4142
@PreAuthorize("hasRole('ROLE_ADMIN')")
4243
@PostMapping("")
4344
@ResponseBody
44-
ResponseEntity<BookDTO> addBook(@RequestBody BookDTO book) {
45-
return new ResponseEntity<>(bookService.addBook(book), HttpStatus.CREATED);
45+
ResponseEntity<BookDTO> addBook(@RequestBody BookPostDTO book) {
46+
return new ResponseEntity<>(bookService.addBookPostDTO(book), HttpStatus.CREATED);
4647
}
4748

4849
@PreAuthorize("hasRole('ROLE_ADMIN')")
4950
@PutMapping("/{id}")
50-
public ResponseEntity<BookDTO> updateBook(@PathVariable int id, @RequestBody BookDTO bookDTO) {
51+
public ResponseEntity<BookDTO> updateBook(@PathVariable int id, @RequestBody BookPostDTO bookDTO) {
5152
return ResponseEntity.ok(bookService.updateBook(id, bookDTO));
5253
}
5354

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.themetalstorm.bibliothekssystem.dto;
2+
3+
import org.springframework.boot.context.properties.bind.DefaultValue;
4+
5+
import java.util.Set;
6+
7+
public record BookPostDTO (String name, String isbn, String publisher, Set<Integer> genreIds, Set<Integer> authorIds, @DefaultValue(value = "1") int availableCopies, @DefaultValue(value = "1") int totalCopies){
8+
9+
}

src/main/java/com/themetalstorm/bibliothekssystem/model/Book.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.themetalstorm.bibliothekssystem.model;
22

33
import com.themetalstorm.bibliothekssystem.dto.BookDTO;
4+
import com.themetalstorm.bibliothekssystem.dto.BookPostDTO;
45
import jakarta.persistence.*;
56
import lombok.AllArgsConstructor;
67
import lombok.Getter;
@@ -84,6 +85,16 @@ public Book(BookDTO bookDTO) {
8485
}
8586

8687

88+
public Book(BookPostDTO bookDTO) {
89+
this.name = bookDTO.name();
90+
this.isbn = bookDTO.isbn();
91+
this.publisher = bookDTO.publisher();
92+
this.genres = new HashSet<>();
93+
this.authors = new HashSet<>();
94+
this.availableCopies = bookDTO.availableCopies();
95+
this.totalCopies = bookDTO.totalCopies();
96+
}
97+
8798
@Override
8899
public boolean equals(Object o) {
89100
if (o == null || getClass() != o.getClass()) return false;

src/main/java/com/themetalstorm/bibliothekssystem/service/BookService.java

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.themetalstorm.bibliothekssystem.service;
22

33
import com.themetalstorm.bibliothekssystem.dto.AuthorDTO;
4+
import com.themetalstorm.bibliothekssystem.dto.BookPostDTO;
45
import com.themetalstorm.bibliothekssystem.dto.GenreDTO;
56
import com.themetalstorm.bibliothekssystem.exceptions.ResourceAlreadyExistsException;
67
import com.themetalstorm.bibliothekssystem.model.Author;
@@ -79,25 +80,44 @@ public BookDTO addBook(BookDTO bookDTO) {
7980
return new BookDTO(bookRepository.save(book));
8081
}
8182

82-
public BookDTO updateBook(int id, BookDTO bookDTO) {
83+
public BookDTO addBookPostDTO(BookPostDTO bookDTO) {
84+
85+
boolean isbnExists = bookRepository.existsByIsbn(bookDTO.isbn());
86+
if(isbnExists){
87+
throw new ResourceAlreadyExistsException("Books with ISBN " + bookDTO.isbn() + " already exists");
88+
}
89+
90+
Book book = new Book(bookDTO);
91+
for (Integer authorId : bookDTO.authorIds()) {
92+
Author existingAuthor = authorRepository.findById(authorId).orElseThrow(() -> new ResourceNotFoundException("Genre not found with id: " + authorId));
93+
book.getAuthors().add(existingAuthor);
94+
}
95+
96+
for (Integer genreId : bookDTO.genreIds()) {
97+
Genre existingGenre = genreRepository.findById(genreId).orElseThrow(() -> new ResourceNotFoundException("Genre not found with id: " + genreId));
98+
book.getGenres().add(existingGenre);
99+
}
100+
101+
return new BookDTO(bookRepository.save(book));
102+
}
103+
104+
public BookDTO updateBook(int id, BookPostDTO bookDTO) {
83105
Book book = bookRepository.findById(id).orElseThrow(() -> new ResourceNotFoundException("Book not found with id: " + id));
84106
book.setName(bookDTO.name());
85107
book.setIsbn(bookDTO.isbn());
86108
book.setAvailableCopies(bookDTO.availableCopies());
87109
book.setTotalCopies(bookDTO.totalCopies());
88110

89111
book.getAuthors().clear();
90-
for (AuthorDTO authorDTO : bookDTO.authors()) {
91-
Author author = authorRepository.findByFirstNameAndLastName(authorDTO.firstName(), authorDTO.lastName())
92-
.orElseGet(() -> authorRepository.save(new Author(authorDTO)));
93-
book.getAuthors().add(author);
112+
for (Integer authorId : bookDTO.authorIds()) {
113+
Author existingAuthor = authorRepository.findById(authorId).orElseThrow(() -> new ResourceNotFoundException("Genre not found with id: " + authorId));
114+
book.getAuthors().add(existingAuthor);
94115
}
95116

96117
book.getGenres().clear();
97-
for (GenreDTO genreDTO : bookDTO.genres()) {
98-
Genre genre = genreRepository.findByName(genreDTO.name())
99-
.orElseGet(() -> genreRepository.save(new Genre(genreDTO)));
100-
book.getGenres().add(genre);
118+
for (Integer genreId : bookDTO.genreIds()) {
119+
Genre existingGenre = genreRepository.findById(genreId).orElseThrow(() -> new ResourceNotFoundException("Genre not found with id: " + genreId));
120+
book.getGenres().add(existingGenre);
101121
}
102122

103123
return new BookDTO(bookRepository.save(book));

0 commit comments

Comments
 (0)