Skip to content

Commit

Permalink
Remove MapStruct dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan Franchin committed Jan 15, 2025
1 parent 09a0979 commit cd5a16e
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 46 deletions.
9 changes: 0 additions & 9 deletions book-service/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ repositories {
}

ext {
set('mapstructVersion', '1.6.3')
set('lombokMapstructBindingVersion', '0.2.0')
set('springdocOpenApiVersion', '2.7.0')
set('keycloakVersion', '26.0.3')
set('httpClient5Version', '5.4.1')
Expand Down Expand Up @@ -80,13 +78,6 @@ dependencies {
// See: /~https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-3.0-Migration-Guide#apache-httpclient-in-resttemplate
implementation "org.apache.httpcomponents.client5:httpclient5:${httpClient5Version}"

implementation "org.mapstruct:mapstruct:${mapstructVersion}"
annotationProcessor "org.mapstruct:mapstruct-processor:${mapstructVersion}"
testAnnotationProcessor "org.mapstruct:mapstruct-processor:${mapstructVersion}"

// This dependency is needed so that Mapstruct can work with Lombok 1.18.16
annotationProcessor "org.projectlombok:lombok-mapstruct-binding:${lombokMapstructBindingVersion}"

annotationProcessor 'org.projectlombok:lombok'
compileOnly 'org.projectlombok:lombok'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.ivanfranchin.bookservice.dto.BookResponse;
import com.ivanfranchin.bookservice.dto.CreateBookRequest;
import com.ivanfranchin.bookservice.dto.UpdateBookRequest;
import com.ivanfranchin.bookservice.mapper.BookMapper;
import com.ivanfranchin.bookservice.model.Book;
import com.ivanfranchin.bookservice.service.BookService;
import io.swagger.v3.oas.annotations.Operation;
Expand Down Expand Up @@ -37,7 +36,6 @@
public class BookController {

private final BookService bookService;
private final BookMapper bookMapper;

@Operation(summary = "Get list of book. It can be filtered by author name")
@GetMapping
Expand All @@ -49,15 +47,15 @@ public List<BookResponse> getBooks(@RequestParam(required = false) String author
log.info("Get books");
}
List<Book> books = filterByAuthorName ? bookService.getBooksByAuthorName(authorName) : bookService.getBooks();
return books.stream().map(bookMapper::toBookResponse).collect(Collectors.toList());
return books.stream().map(BookResponse::from).collect(Collectors.toList());
}

@Operation(summary = "Get book by id")
@GetMapping("/{id}")
public BookResponse getBookById(@PathVariable String id) {
log.info("Get books with id equals to {}", id);
Book book = bookService.validateAndGetBookById(id);
return bookMapper.toBookResponse(book);
return BookResponse.from(book);
}

@Operation(
Expand All @@ -67,9 +65,8 @@ public BookResponse getBookById(@PathVariable String id) {
@PostMapping
public BookResponse createBook(@Valid @RequestBody CreateBookRequest createBookRequest, Principal principal) {
log.info("Post request made by {} to create a book {}", principal.getName(), createBookRequest);
Book book = bookMapper.toBook(createBookRequest);
book = bookService.saveBook(book);
return bookMapper.toBookResponse(book);
Book book = bookService.saveBook(Book.from(createBookRequest));
return BookResponse.from(book);
}

@Operation(
Expand All @@ -81,9 +78,9 @@ public BookResponse updateBook(@PathVariable String id,
Principal principal) {
log.info("Patch request made by {} to update book with id {}. New values {}", principal.getName(), id, updateBookRequest);
Book book = bookService.validateAndGetBookById(id);
bookMapper.updateUserFromRequest(updateBookRequest, book);
Book.updateFrom(updateBookRequest, book);
book = bookService.saveBook(book);
return bookMapper.toBookResponse(book);
return BookResponse.from(book);
}

@Operation(
Expand All @@ -94,6 +91,6 @@ public BookResponse deleteBook(@PathVariable String id, Principal principal) {
log.info("Delete request made by {} to remove book with id {}", principal.getName(), id);
Book book = bookService.validateAndGetBookById(id);
bookService.deleteBook(book);
return bookMapper.toBookResponse(book);
return BookResponse.from(book);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
package com.ivanfranchin.bookservice.dto;

import com.ivanfranchin.bookservice.model.Book;

import java.math.BigDecimal;

public record BookResponse(String id, String authorName, String title, BigDecimal price) {

public static BookResponse from(Book book) {
return new BookResponse(book.getId(), book.getAuthorName(), book.getTitle(), book.getPrice());
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.ivanfranchin.bookservice.model;

import com.ivanfranchin.bookservice.dto.CreateBookRequest;
import com.ivanfranchin.bookservice.dto.UpdateBookRequest;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
Expand All @@ -25,4 +27,24 @@ public Book(String authorName, String title, BigDecimal price) {
this.title = title;
this.price = price;
}

public static Book from(CreateBookRequest createBookRequest) {
return new Book(
createBookRequest.getAuthorName(),
createBookRequest.getTitle(),
createBookRequest.getPrice()
);
}

public static void updateFrom(UpdateBookRequest updateBookRequest, Book book) {
if (updateBookRequest.getAuthorName() != null) {
book.setAuthorName(updateBookRequest.getAuthorName());
}
if (updateBookRequest.getTitle() != null) {
book.setTitle(updateBookRequest.getTitle());
}
if (updateBookRequest.getPrice() != null) {
book.setPrice(updateBookRequest.getPrice());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.ivanfranchin.bookservice.dto.CreateBookRequest;
import com.ivanfranchin.bookservice.dto.UpdateBookRequest;
import com.ivanfranchin.bookservice.exception.BookNotFoundException;
import com.ivanfranchin.bookservice.mapper.BookMapperImpl;
import com.ivanfranchin.bookservice.model.Book;
import com.ivanfranchin.bookservice.security.JwtAuthConverterProperties;
import com.ivanfranchin.bookservice.security.SecurityConfig;
Expand Down Expand Up @@ -39,7 +38,7 @@
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@WebMvcTest(BookController.class)
@Import({BookMapperImpl.class, JwtAuthConverterProperties.class, SecurityConfig.class})
@Import({JwtAuthConverterProperties.class, SecurityConfig.class})
class BookControllerTest {

@Autowired
Expand Down

0 comments on commit cd5a16e

Please sign in to comment.