Skip to content

Commit e6e5c2b

Browse files
author
Ivan Franchin
committed
Remove MapStruct dependency
1 parent 8012baf commit e6e5c2b

File tree

18 files changed

+131
-225
lines changed

18 files changed

+131
-225
lines changed

author-book-api/src/main/java/com/ivanfranchin/authorbookapi/graphql/AuthorController.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.ivanfranchin.authorbookapi.graphql;
22

33
import com.ivanfranchin.authorbookapi.graphql.input.AuthorInput;
4-
import com.ivanfranchin.authorbookapi.graphql.mapper.AuthorMapper;
54
import com.ivanfranchin.authorbookapi.model.Author;
65
import com.ivanfranchin.authorbookapi.service.AuthorService;
76
import lombok.RequiredArgsConstructor;
@@ -17,7 +16,6 @@
1716
public class AuthorController {
1817

1918
private final AuthorService authorService;
20-
private final AuthorMapper authorMapper;
2119

2220
@QueryMapping
2321
public List<Author> getAuthors() {
@@ -36,14 +34,14 @@ public List<Author> getAuthorByName(@Argument String authorName) {
3634

3735
@MutationMapping
3836
public Author createAuthor(@Argument AuthorInput authorInput) {
39-
Author author = authorMapper.toAuthor(authorInput);
37+
Author author = Author.from(authorInput);
4038
return authorService.saveAuthor(author);
4139
}
4240

4341
@MutationMapping
4442
public Author updateAuthor(@Argument Long authorId, @Argument AuthorInput authorInput) {
4543
Author author = authorService.validateAndGetAuthorById(authorId);
46-
authorMapper.updateAuthorFromRequest(authorInput, author);
44+
Author.updateFrom(authorInput, author);
4745
return authorService.saveAuthor(author);
4846
}
4947

author-book-api/src/main/java/com/ivanfranchin/authorbookapi/graphql/BookController.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import com.ivanfranchin.authorbookapi.client.BookReviewApiClient;
44
import com.ivanfranchin.authorbookapi.client.BookReviewApiQueryBuilder;
55
import com.ivanfranchin.authorbookapi.graphql.input.BookInput;
6-
import com.ivanfranchin.authorbookapi.graphql.mapper.BookMapper;
76
import com.ivanfranchin.authorbookapi.model.Author;
87
import com.ivanfranchin.authorbookapi.model.Book;
98
import com.ivanfranchin.authorbookapi.model.BookReview;
@@ -24,7 +23,6 @@ public class BookController {
2423

2524
private final AuthorService authorService;
2625
private final BookService bookService;
27-
private final BookMapper bookMapper;
2826
private final BookReviewApiQueryBuilder bookReviewApiQueryBuilder;
2927
private final BookReviewApiClient bookReviewApiClient;
3028

@@ -41,15 +39,15 @@ public Book getBookById(@Argument Long bookId) {
4139
@MutationMapping
4240
public Book createBook(@Argument BookInput bookInput) {
4341
Author author = authorService.validateAndGetAuthorById(bookInput.authorId());
44-
Book book = bookMapper.toBook(bookInput);
42+
Book book = Book.from(bookInput);
4543
book.setAuthor(author);
4644
return bookService.saveBook(book);
4745
}
4846

4947
@MutationMapping
5048
public Book updateBook(@Argument Long bookId, @Argument BookInput bookInput) {
5149
Book book = bookService.validateAndGetBookById(bookId);
52-
bookMapper.updateBookFromRequest(bookInput, book);
50+
Book.updateFrom(bookInput, book);
5351

5452
Long authorId = bookInput.authorId();
5553
if (authorId != null) {

author-book-api/src/main/java/com/ivanfranchin/authorbookapi/graphql/mapper/AuthorMapper.java

Lines changed: 0 additions & 28 deletions
This file was deleted.

author-book-api/src/main/java/com/ivanfranchin/authorbookapi/graphql/mapper/BookMapper.java

Lines changed: 0 additions & 30 deletions
This file was deleted.

author-book-api/src/main/java/com/ivanfranchin/authorbookapi/model/Author.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.ivanfranchin.authorbookapi.model;
22

3+
import com.ivanfranchin.authorbookapi.graphql.input.AuthorInput;
4+
import com.ivanfranchin.authorbookapi.restapi.dto.CreateAuthorRequest;
5+
import com.ivanfranchin.authorbookapi.restapi.dto.UpdateAuthorRequest;
36
import jakarta.persistence.CascadeType;
47
import jakarta.persistence.Column;
58
import jakarta.persistence.Entity;
@@ -47,4 +50,28 @@ public void onPrePersist() {
4750
public void onPreUpdate() {
4851
updatedAt = Instant.now();
4952
}
53+
54+
public static Author from(AuthorInput authorInput) {
55+
Author author = new Author();
56+
author.setName(authorInput.name());
57+
return author;
58+
}
59+
60+
public static void updateFrom(AuthorInput authorInput, Author author) {
61+
if (authorInput.name() != null) {
62+
author.setName(authorInput.name());
63+
}
64+
}
65+
66+
public static Author from(CreateAuthorRequest createAuthorRequest) {
67+
Author author = new Author();
68+
author.setName(createAuthorRequest.name());
69+
return author;
70+
}
71+
72+
public static void updateFrom(UpdateAuthorRequest updateAuthorRequest, Author author) {
73+
if (updateAuthorRequest.name() != null) {
74+
author.setName(updateAuthorRequest.name());
75+
}
76+
}
5077
}

author-book-api/src/main/java/com/ivanfranchin/authorbookapi/model/Book.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.ivanfranchin.authorbookapi.model;
22

3+
import com.ivanfranchin.authorbookapi.graphql.input.BookInput;
4+
import com.ivanfranchin.authorbookapi.restapi.dto.CreateBookRequest;
5+
import com.ivanfranchin.authorbookapi.restapi.dto.UpdateBookRequest;
36
import jakarta.persistence.Column;
47
import jakarta.persistence.Entity;
58
import jakarta.persistence.FetchType;
@@ -58,4 +61,44 @@ public void onPrePersist() {
5861
public void onPreUpdate() {
5962
updatedAt = Instant.now();
6063
}
64+
65+
public static Book from(BookInput bookInput) {
66+
Book book = new Book();
67+
book.setIsbn(bookInput.isbn());
68+
book.setTitle(bookInput.title());
69+
book.setYear(bookInput.year());
70+
return book;
71+
}
72+
73+
public static void updateFrom(BookInput bookInput, Book book) {
74+
if (bookInput.isbn() != null) {
75+
book.setIsbn(bookInput.isbn());
76+
}
77+
if (bookInput.title() != null) {
78+
book.setTitle(bookInput.title());
79+
}
80+
if (bookInput.year() != null) {
81+
book.setYear(bookInput.year());
82+
}
83+
}
84+
85+
public static Book from(CreateBookRequest createBookRequest) {
86+
Book book = new Book();
87+
book.setIsbn(createBookRequest.isbn());
88+
book.setTitle(createBookRequest.title());
89+
book.setYear(createBookRequest.year());
90+
return book;
91+
}
92+
93+
public static void updateFrom(UpdateBookRequest updateBookRequest, Book book) {
94+
if (updateBookRequest.isbn() != null) {
95+
book.setIsbn(updateBookRequest.isbn());
96+
}
97+
if (updateBookRequest.title() != null) {
98+
book.setTitle(updateBookRequest.title());
99+
}
100+
if (updateBookRequest.year() != null) {
101+
book.setYear(updateBookRequest.year());
102+
}
103+
}
61104
}

author-book-api/src/main/java/com/ivanfranchin/authorbookapi/restapi/AuthorController.java

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
import com.ivanfranchin.authorbookapi.restapi.dto.BookResponse;
66
import com.ivanfranchin.authorbookapi.restapi.dto.CreateAuthorRequest;
77
import com.ivanfranchin.authorbookapi.restapi.dto.UpdateAuthorRequest;
8-
import com.ivanfranchin.authorbookapi.restapi.mapper.AuthorMapper;
9-
import com.ivanfranchin.authorbookapi.restapi.mapper.BookMapper;
108
import com.ivanfranchin.authorbookapi.service.AuthorService;
119
import jakarta.validation.Valid;
1210
import lombok.RequiredArgsConstructor;
@@ -31,60 +29,58 @@
3129
public class AuthorController {
3230

3331
private final AuthorService authorService;
34-
private final AuthorMapper authorMapper;
35-
private final BookMapper bookMapper;
3632

3733
@GetMapping
3834
public List<AuthorResponse> getAuthors() {
3935
return authorService.getAuthors()
4036
.stream()
41-
.map(authorMapper::toAuthorResponse)
37+
.map(AuthorResponse::from)
4238
.collect(Collectors.toList());
4339
}
4440

4541
@GetMapping("/name/{authorName}")
4642
public List<AuthorResponse> getAuthorByName(@PathVariable String authorName) {
4743
return authorService.validateAndGetAuthorByName(authorName)
4844
.stream()
49-
.map(authorMapper::toAuthorResponse)
45+
.map(AuthorResponse::from)
5046
.collect(Collectors.toList());
5147
}
5248

5349
@GetMapping("/{authorId}")
5450
public AuthorResponse getAuthorById(@PathVariable Long authorId) {
5551
Author author = authorService.validateAndGetAuthorById(authorId);
56-
return authorMapper.toAuthorResponse(author);
52+
return AuthorResponse.from(author);
5753
}
5854

5955
@ResponseStatus(HttpStatus.CREATED)
6056
@PostMapping
6157
public AuthorResponse createAuthor(@Valid @RequestBody CreateAuthorRequest createAuthorRequest) {
62-
Author author = authorMapper.toAuthor(createAuthorRequest);
58+
Author author = Author.from(createAuthorRequest);
6359
author = authorService.saveAuthor(author);
64-
return authorMapper.toAuthorResponse(author);
60+
return AuthorResponse.from(author);
6561
}
6662

6763
@PutMapping("/{authorId}")
6864
public AuthorResponse updateAuthor(@PathVariable Long authorId, @Valid @RequestBody UpdateAuthorRequest updateAuthorRequest) {
6965
Author author = authorService.validateAndGetAuthorById(authorId);
70-
authorMapper.updateAuthorFromRequest(updateAuthorRequest, author);
66+
Author.updateFrom(updateAuthorRequest, author);
7167
author = authorService.saveAuthor(author);
72-
return authorMapper.toAuthorResponse(author);
68+
return AuthorResponse.from(author);
7369
}
7470

7571
@DeleteMapping("/{authorId}")
7672
public AuthorResponse deleteAuthor(@PathVariable Long authorId) {
7773
Author author = authorService.validateAndGetAuthorById(authorId);
7874
authorService.deleteAuthor(author);
79-
return authorMapper.toAuthorResponse(author);
75+
return AuthorResponse.from(author);
8076
}
8177

8278
@GetMapping("/{authorId}/books")
8379
public Set<BookResponse> getAuthorBooks(@PathVariable Long authorId) {
8480
Author author = authorService.validateAndGetAuthorById(authorId);
8581
return author.getBooks()
8682
.stream()
87-
.map(bookMapper::toBookResponse)
83+
.map(BookResponse::from)
8884
.collect(Collectors.toSet());
8985
}
9086
}

author-book-api/src/main/java/com/ivanfranchin/authorbookapi/restapi/BookController.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import com.ivanfranchin.authorbookapi.restapi.dto.BookResponse;
99
import com.ivanfranchin.authorbookapi.restapi.dto.CreateBookRequest;
1010
import com.ivanfranchin.authorbookapi.restapi.dto.UpdateBookRequest;
11-
import com.ivanfranchin.authorbookapi.restapi.mapper.BookMapper;
1211
import com.ivanfranchin.authorbookapi.service.AuthorService;
1312
import com.ivanfranchin.authorbookapi.service.BookService;
1413
import jakarta.validation.Valid;
@@ -36,50 +35,49 @@ public class BookController {
3635
private final AuthorService authorService;
3736
private final BookReviewApiClient bookReviewApiClient;
3837
private final BookReviewApiQueryBuilder bookReviewApiQueryBuilder;
39-
private final BookMapper bookMapper;
4038

4139
@GetMapping
4240
public List<BookResponse> getBooks() {
4341
return bookService.getBooks()
4442
.stream()
45-
.map(bookMapper::toBookResponse)
43+
.map(BookResponse::from)
4644
.collect(Collectors.toList());
4745
}
4846

4947
@GetMapping("/{bookId}")
5048
public BookResponse getBook(@PathVariable Long bookId) {
5149
Book book = bookService.validateAndGetBookById(bookId);
52-
return bookMapper.toBookResponse(book);
50+
return BookResponse.from(book);
5351
}
5452

5553
@ResponseStatus(HttpStatus.CREATED)
5654
@PostMapping
5755
public BookResponse createBook(@Valid @RequestBody CreateBookRequest createBookRequest) {
5856
Author author = authorService.validateAndGetAuthorById(createBookRequest.authorId());
59-
Book book = bookMapper.toBook(createBookRequest);
57+
Book book = Book.from(createBookRequest);
6058
book.setAuthor(author);
6159
book = bookService.saveBook(book);
62-
return bookMapper.toBookResponse(book);
60+
return BookResponse.from(book);
6361
}
6462

6563
@PutMapping("/{bookId}")
6664
public BookResponse updateBook(@PathVariable Long bookId, @Valid @RequestBody UpdateBookRequest updateBookRequest) {
6765
Book book = bookService.validateAndGetBookById(bookId);
68-
bookMapper.updateBookFromRequest(updateBookRequest, book);
66+
Book.updateFrom(updateBookRequest, book);
6967
Long authorId = updateBookRequest.authorId();
7068
if (authorId != null) {
7169
Author author = authorService.validateAndGetAuthorById(authorId);
7270
book.setAuthor(author);
7371
}
7472
book = bookService.saveBook(book);
75-
return bookMapper.toBookResponse(book);
73+
return BookResponse.from(book);
7674
}
7775

7876
@DeleteMapping("/{bookId}")
7977
public BookResponse deleteBook(@PathVariable Long bookId) {
8078
Book book = bookService.validateAndGetBookById(bookId);
8179
bookService.deleteBook(book);
82-
return bookMapper.toBookResponse(book);
80+
return BookResponse.from(book);
8381
}
8482

8583
@GetMapping("/{bookId}/reviews")
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
package com.ivanfranchin.authorbookapi.restapi.dto;
22

3+
import com.ivanfranchin.authorbookapi.model.Author;
4+
35
public record AuthorResponse(Long id, String name) {
6+
7+
public static AuthorResponse from(Author author) {
8+
return new AuthorResponse(author.getId(), author.getName());
9+
}
410
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
package com.ivanfranchin.authorbookapi.restapi.dto;
22

3+
import com.ivanfranchin.authorbookapi.model.Book;
4+
35
public record BookResponse(Long id, String isbn, String title, Integer year) {
6+
7+
public static BookResponse from(Book book) {
8+
return new BookResponse(book.getId(), book.getIsbn(), book.getTitle(), book.getYear());
9+
}
410
}

0 commit comments

Comments
 (0)