Skip to content
Open
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
17 changes: 16 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,17 @@
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>1.5.5.Final</version>
</dependency>

<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>1.5.5.Final</version>
</dependency>
</dependencies>

<build>
Expand All @@ -85,6 +96,11 @@
<version>3.8.1</version>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>1.5.5.Final</version>
</path>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
Expand All @@ -93,7 +109,6 @@
</annotationProcessorPaths>
</configuration>
</plugin>

</plugins>
</build>

Expand Down
15 changes: 15 additions & 0 deletions src/main/java/com/box/library/author/AuthorMapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.box.library.author;

import com.box.library.book.Book;
import com.box.library.request.CreateAuthorRequest;
import com.box.library.request.UpdateAuthorRequest;
import org.mapstruct.Mapper;
import java.util.List;

@Mapper(componentModel = "spring")
public interface AuthorMapper {

Author toAuthor(CreateAuthorRequest request, List<Book> books);

Author toAuthor(UpdateAuthorRequest request, List<Book> books, Long id);
}
13 changes: 6 additions & 7 deletions src/main/java/com/box/library/author/AuthorService.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import jakarta.transaction.Transactional;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
Expand All @@ -21,10 +20,12 @@ public class AuthorService {

private final AuthorRepository repository;
private final BookService bookService;
private final AuthorMapper authorMapper;

public AuthorService(AuthorRepository repository, @Lazy BookService bookService) {
public AuthorService(AuthorRepository repository, @Lazy BookService bookService, AuthorMapper authorMapper) {
this.repository = repository;
this.bookService = bookService;
this.authorMapper = authorMapper;
}

public List<Author> findAllByIds(List<Long> ids) {
Expand All @@ -41,7 +42,7 @@ public Author findById(Long id) {

public Author create(CreateAuthorRequest request) {
var books = bookService.findAllByIds(request.booksIds());
var author = new Author(request.name(), books);
var author = authorMapper.toAuthor(request, books);
books.forEach(book -> book.getAuthors().add(author));
return repository.save(author);
}
Expand All @@ -53,10 +54,8 @@ public Author update(Long id, UpdateAuthorRequest request) {
removeOldBooksAssociations(author, books);
addNewBooksAssociations(author, books);

author.setName(request.name());
author.setBooks(new ArrayList<>(books));

return repository.save(author);
var updatedAuthor = authorMapper.toAuthor(request, new ArrayList<>(books), id);
return repository.save(updatedAuthor);
}

@Transactional
Expand Down
1 change: 0 additions & 1 deletion src/main/java/com/box/library/book/Book.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,4 @@ public Book(String title, List<Author> authors, String publisher, String ISBN) {
this.publisher = publisher;
this.ISBN = ISBN;
}

}
29 changes: 29 additions & 0 deletions src/main/java/com/box/library/book/BookMapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.box.library.book;

import com.box.library.author.Author;
import com.box.library.loan.Loan;
import com.box.library.request.CreateBookRequest;
import com.box.library.request.UpdateBookRequest;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Named;
import java.util.List;

@Mapper(componentModel = "spring")
public interface BookMapper {

@Mapping(target="authors", source="authors")
Book toBook(CreateBookRequest request, List<Author> authors);

@Mapping(target="authors", source="authors")
@Mapping(target="id", source="id")
@Mapping(target="status", source="request.status", qualifiedByName = "stringToStatus")
@Mapping(target = "loans", source = "loans")
@Mapping(target="ISBN", source="request.isbn")
Book toBook(UpdateBookRequest request, Long id, List<Author> authors, List<Loan> loans);

@Named("stringToStatus")
default BookStatus stringToStatus(String status) {
return BookStatus.valueOf(status);
}
}
21 changes: 11 additions & 10 deletions src/main/java/com/box/library/book/BookService.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import com.box.library.author.AuthorService;
import com.box.library.exception.BookNotFoundException;
import com.box.library.exception.NoFilterProvidedException;
import com.box.library.loan.LoanService;
import com.box.library.request.CreateBookRequest;
import com.box.library.request.UpdateBookRequest;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

Expand All @@ -15,15 +17,19 @@ public class BookService {

private final BookRepository repository;
private final AuthorService authorService;
private final BookMapper bookMapper;
private final LoanService loanService;

public BookService(BookRepository repository, AuthorService authorService) {
public BookService(BookRepository repository, AuthorService authorService, BookMapper bookMaper, @Lazy LoanService loanService) {
this.repository = repository;
this.authorService = authorService;
this.bookMapper = bookMaper;
this.loanService = loanService;
}

public Book create(CreateBookRequest request) {
var authors = authorService.findAllByIds(request.authorsIds());
var book = new Book(request.title(), authors, request.publisher(), request.isbn());
var book = bookMapper.toBook(request, authors);
return repository.save(book);
}

Expand Down Expand Up @@ -51,16 +57,11 @@ public void deleteById(Long id) {
}

public Book update(Long id, UpdateBookRequest request) {
var existingBook = findById(id);
var authors = authorService.findAllByIds(request.authorsIds());
var loans = loanService.findAllByIds(request.loansIds());
var updatedBook = bookMapper.toBook(request, id, authors, loans);

existingBook.setTitle(request.title());
existingBook.setAuthors(authors);
existingBook.setISBN(request.isbn());
existingBook.setPublisher(request.publisher());
existingBook.setStatus(request.status());

return repository.save(existingBook);
return repository.save(updatedBook);
}

public List<Book> findAllByFilter(String author, String title, String isbn, String publisher) {
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/box/library/loan/LoanService.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public List<Loan> findAll() {
return repository.findAll();
}

public List<Loan> findAllByIds(List<Long> ids){return repository.findAllById(ids);}

public List<Loan> findByCustomerId(Long customerId) {
return repository.findByCustomerId(customerId);
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/box/library/request/UpdateBookRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@

public record UpdateBookRequest(@NotBlank String title,
@NotEmpty List<Long> authorsIds,
List<Long> loansIds,
@NotBlank String publisher,
@NotBlank String isbn,
@NotNull BookStatus status) {
@NotNull String status) {
}