Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
18 changes: 17 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,18 @@
<scope>provided</scope>
</dependency>

<!-- https://mvnrepository.com/artifact/org.mapstruct/mapstruct -->
<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 @@ -80,6 +92,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 @@ -88,7 +105,6 @@
</annotationProcessorPaths>
</configuration>
</plugin>

</plugins>
</build>

Expand Down
14 changes: 14 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,14 @@
package com.box.library.author;

import com.box.library.request.CreateAuthorRequest;
import com.box.library.request.UpdateAuthorRequest;
import org.mapstruct.Mapper;

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

Author toAuthor(CreateAuthorRequest request);
Author toAuthor(UpdateAuthorRequest request);
CreateAuthorRequest toCreateAuthorRequest(Author author);
UpdateAuthorRequest toUpdateAuthorRequest(Author author);
}
21 changes: 21 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,21 @@
package com.box.library.book;

import com.box.library.request.CreateBookRequest;
import com.box.library.request.UpdateBookRequest;
import org.mapstruct.Mapper;



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



Book toBook(CreateBookRequest request);
Book toBook(UpdateBookRequest request);

CreateBookRequest toCreateBookRequest(Book book);
UpdateBookRequest toUpdateBookRequest(Book book);


}
17 changes: 9 additions & 8 deletions src/main/java/com/box/library/book/BookService.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,18 @@ public class BookService {

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

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

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);
book.setAuthors(authors);
return repository.save(book);
}

Expand All @@ -50,13 +53,11 @@ public Book update(Long id, UpdateBookRequest request) {
var existingBook = findById(id);
var authors = authorService.findAllByIds(request.authorsIds());

existingBook.setTitle(request.title());
existingBook.setAuthors(authors);
existingBook.setISBN(request.ISBN());
existingBook.setPublisher(request.publisher());
existingBook.setStatus(request.status());
var updatedBook = bookMapper.toBook(request);
updatedBook.setId(existingBook.getId());
updatedBook.setAuthors(authors);

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

public List<Book> findAllByFilter(String author, String title, String isbn, String publisher) {
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/com/box/library/customer/CustomerMapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.box.library.customer;


import com.box.library.request.CustomerRequest;
import org.mapstruct.Mapper;

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

Customer toCustomer(CustomerRequest request);
CustomerRequest toCreateCustomerRequest(Customer customer);
}
4 changes: 2 additions & 2 deletions src/main/java/com/box/library/loan/LoanController.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.box.library.loan;

import com.box.library.request.CreateLoan;
import com.box.library.request.CreateLoanRequest;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
Expand Down Expand Up @@ -35,7 +35,7 @@ public ResponseEntity<List<Loan>> findByUserId(@PathVariable Long userId) {
// TODO[2]: Criação de empréstimo
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remover comentário


@PostMapping
public ResponseEntity<Loan> create(@RequestBody CreateLoan request) {
public ResponseEntity<Loan> create(@RequestBody CreateLoanRequest request) {
var savedLoan = service.create(request);
return ResponseEntity.ok(savedLoan);
}
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/com/box/library/loan/LoanMapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.box.library.loan;

import com.box.library.request.CreateLoanRequest;
import org.mapstruct.Mapper;

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

Loan toLoan(CreateLoanRequest request);
CreateLoanRequest toCreateLoanRequest(Loan loan);
}
5 changes: 2 additions & 3 deletions src/main/java/com/box/library/loan/LoanService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

import com.box.library.exception.LoanNotFoundException;
import com.box.library.report.Exporter;
import com.box.library.request.CreateLoan;
import com.box.library.request.CreateLoanRequest;
import com.box.library.response.ReportResponse;
import com.box.library.user.LibraryUserService;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
Expand All @@ -29,7 +28,7 @@ public List<Loan> findByUserId(Long userId) {
return repository.findByUserId(userId);
}

public Loan create(CreateLoan request) {
public Loan create(CreateLoanRequest request) {
var entity = new Loan(request.userId(), request.booksIds());
return repository.save(entity);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
import java.util.List;

// TODO[10]: Usando record
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remover esse comentário

public record CreateLoan(Long userId, List<Long> booksIds) {
public record CreateLoanRequest(Long userId, List<Long> booksIds) {
}
11 changes: 11 additions & 0 deletions src/main/java/com/box/library/user/LibraryUserMapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.box.library.user;

import com.box.library.request.UpdateLibraryUser;
import org.mapstruct.Mapper;

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

LibraryUser toLibraryUser(UpdateLibraryUser request);
UpdateLibraryUser toUpdateLibraryUser(LibraryUser libraryUser);
}