Skip to content

Commit ca65f85

Browse files
authored
Merge pull request #135 from nanotaboada/feature/lombok
refactor(models): add Lombok annotations
2 parents 335ba78 + 6794b7b commit ca65f85

6 files changed

Lines changed: 75 additions & 226 deletions

File tree

pom.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,21 @@
181181
<artifactId>spring-boot-starter-cache</artifactId>
182182
<version>3.3.1</version>
183183
</dependency>
184+
<!--
185+
Project Lombok
186+
Lombok is a Java library that provides annotations to simplify Java development by automating the generation of
187+
boilerplate code. Key features include automatic generation of getters, setters, equals, hashCode, and toString
188+
methods, as well as a facility for automatic resource management. It aims to reduce the amount of manual coding,
189+
thereby streamlining the codebase and reducing potential for errors.
190+
191+
https://mvnrepository.com/artifact/org.projectlombok/lombok
192+
-->
193+
<dependency>
194+
<groupId>org.projectlombok</groupId>
195+
<artifactId>lombok</artifactId>
196+
<version>1.18.34</version>
197+
<scope>provided</scope>
198+
</dependency>
184199
</dependencies>
185200
<build>
186201
<!--

src/main/java/ar/com/nanotaboada/java/samples/spring/boot/controllers/BooksController.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ public ResponseEntity<String> post(@RequestBody BookDTO bookDTO) {
8989
@GetMapping("/books/{isbn}")
9090
@Operation(summary = "Retrieves a book by its ID")
9191
@ApiResponses(value = {
92-
@ApiResponse(responseCode = "200", description = "OK", content = @Content(mediaType = "application/json",
93-
schema = @Schema(implementation = BookDTO.class))),
92+
@ApiResponse(responseCode = "200", description = "OK", content = @Content(mediaType = "application/json", schema =
93+
@Schema(implementation = BookDTO.class))),
9494
@ApiResponse(responseCode = "404", description = "Not Found", content = @Content)
9595
})
9696
public ResponseEntity<BookDTO> getByIsbn(@PathVariable String isbn) {
@@ -105,8 +105,8 @@ public ResponseEntity<BookDTO> getByIsbn(@PathVariable String isbn) {
105105
@GetMapping("/books")
106106
@Operation(summary = "Retrieves all books")
107107
@ApiResponses(value = {
108-
@ApiResponse(responseCode = "200", description = "OK", content = @Content(mediaType = "application/json",
109-
schema = @Schema(implementation = BookDTO[].class)))
108+
@ApiResponse(responseCode = "200", description = "OK", content = @Content(mediaType = "application/json", schema =
109+
@Schema(implementation = BookDTO[].class)))
110110
})
111111
public ResponseEntity<List<BookDTO>> getAll() {
112112
List<BookDTO> books = service.retrieveAll();
@@ -125,7 +125,7 @@ public ResponseEntity<List<BookDTO>> getAll() {
125125
@ApiResponse(responseCode = "404", description = "Not Found", content = @Content)
126126
})
127127
public ResponseEntity<String> put(@RequestBody BookDTO bookDTO) {
128-
if (service.retrieveByIsbn(bookDTO.getIsbn()) != null) {
128+
if (service.retrieveByIsbn(bookDTO.getIsbn()) != null) {
129129
if (service.update(bookDTO)) {
130130
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
131131
} else {

src/main/java/ar/com/nanotaboada/java/samples/spring/boot/models/Book.java

Lines changed: 8 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -7,116 +7,32 @@
77
import jakarta.persistence.Lob;
88
import jakarta.persistence.Table;
99

10-
import jakarta.validation.constraints.NotBlank;
11-
import jakarta.validation.constraints.Past;
12-
13-
import org.hibernate.validator.constraints.ISBN;
14-
import org.hibernate.validator.constraints.URL;
15-
1610
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
1711
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
1812
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
1913
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
2014

15+
import lombok.Data;
16+
import lombok.NoArgsConstructor;
17+
import lombok.AllArgsConstructor;
18+
2119
@Entity
2220
@Table(name = "books")
21+
@Data
22+
@NoArgsConstructor
23+
@AllArgsConstructor
2324
public class Book {
2425
@Id
25-
@ISBN
2626
private String isbn;
27-
@NotBlank(message ="Title is required.")
2827
private String title;
2928
private String subtitle;
30-
@NotBlank(message ="Author is required.")
3129
private String author;
3230
private String publisher;
33-
@Past
3431
@JsonDeserialize(using = LocalDateDeserializer.class)
3532
@JsonSerialize(using = LocalDateSerializer.class)
3633
private LocalDate published;
3734
private int pages;
3835
@Lob
39-
@NotBlank(message ="Description is required.")
4036
private String description;
41-
@URL
42-
@NotBlank(message ="Website is required.")
4337
private String website;
44-
45-
public Book() {
46-
// The no-argument constructor, which is also a JavaBean convention,
47-
// is a requirement for all persistent classes.
48-
// https://docs.jboss.org/hibernate/orm/5.5/quickstart/html_single/
49-
}
50-
51-
public String getIsbn() {
52-
return isbn;
53-
}
54-
55-
public void setIsbn(String isbn) {
56-
this.isbn = isbn;
57-
}
58-
59-
public String getTitle() {
60-
return title;
61-
}
62-
63-
public void setTitle(String title) {
64-
this.title = title;
65-
}
66-
67-
public String getSubtitle() {
68-
return subtitle;
69-
}
70-
71-
public void setSubtitle(String subtitle) {
72-
this.subtitle = subtitle;
73-
}
74-
75-
public String getAuthor() {
76-
return author;
77-
}
78-
79-
public void setAuthor(String author) {
80-
this.author = author;
81-
}
82-
83-
public String getPublisher() {
84-
return publisher;
85-
}
86-
87-
public void setPublisher(String publisher) {
88-
this.publisher = publisher;
89-
}
90-
91-
public LocalDate getPublished() {
92-
return published;
93-
}
94-
95-
public void setPublished(LocalDate published) {
96-
this.published = published;
97-
}
98-
99-
public int getPages() {
100-
return pages;
101-
}
102-
103-
public void setPages(int pages) {
104-
this.pages = pages;
105-
}
106-
107-
public String getDescription() {
108-
return description;
109-
}
110-
111-
public void setDescription(String description) {
112-
this.description = description;
113-
}
114-
115-
public String getWebsite() {
116-
return website;
117-
}
118-
119-
public void setWebsite(String website) {
120-
this.website = website;
121-
}
122-
}
38+
}

src/main/java/ar/com/nanotaboada/java/samples/spring/boot/models/BookDTO.java

Lines changed: 16 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -2,93 +2,37 @@
22

33
import java.time.LocalDate;
44

5+
import jakarta.validation.constraints.NotBlank;
6+
import jakarta.validation.constraints.Past;
7+
8+
import org.hibernate.validator.constraints.ISBN;
9+
import org.hibernate.validator.constraints.URL;
10+
511
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
612
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
713
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
814
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
915

16+
import lombok.Data;
17+
18+
@Data
1019
public class BookDTO {
20+
@ISBN
21+
@NotBlank
1122
private String isbn;
23+
@NotBlank
1224
private String title;
1325
private String subtitle;
26+
@NotBlank
1427
private String author;
1528
private String publisher;
29+
@Past
1630
@JsonDeserialize(using = LocalDateDeserializer.class)
1731
@JsonSerialize(using = LocalDateSerializer.class)
1832
private LocalDate published;
1933
private int pages;
34+
@NotBlank
2035
private String description;
36+
@URL
2137
private String website;
22-
23-
public String getIsbn() {
24-
return isbn;
25-
}
26-
27-
public void setIsbn(String isbn) {
28-
this.isbn = isbn;
29-
}
30-
31-
public String getTitle() {
32-
return title;
33-
}
34-
35-
public void setTitle(String title) {
36-
this.title = title;
37-
}
38-
39-
public String getSubtitle() {
40-
return subtitle;
41-
}
42-
43-
public void setSubtitle(String subtitle) {
44-
this.subtitle = subtitle;
45-
}
46-
47-
public String getAuthor() {
48-
return author;
49-
}
50-
51-
public void setAuthor(String author) {
52-
this.author = author;
53-
}
54-
55-
public String getPublisher() {
56-
return publisher;
57-
}
58-
59-
public void setPublisher(String publisher) {
60-
this.publisher = publisher;
61-
}
62-
63-
public LocalDate getPublished() {
64-
return published;
65-
}
66-
67-
public void setPublished(LocalDate published) {
68-
this.published = published;
69-
}
70-
71-
public int getPages() {
72-
return pages;
73-
}
74-
75-
public void setPages(int pages) {
76-
this.pages = pages;
77-
}
78-
79-
public String getDescription() {
80-
return description;
81-
}
82-
83-
public void setDescription(String description) {
84-
this.description = description;
85-
}
86-
87-
public String getWebsite() {
88-
return website;
89-
}
90-
91-
public void setWebsite(String website) {
92-
this.website = website;
93-
}
9438
}

src/main/java/ar/com/nanotaboada/java/samples/spring/boot/services/BooksService.java

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@ public BooksService(BooksRepository repository, Validator validator, ModelMapper
3535

3636
@CachePut(value = "books", key = "#bookDTO.isbn")
3737
public boolean create(BookDTO bookDTO) {
38-
boolean created = false;
39-
Book book = mapper.map(bookDTO, Book.class);
40-
if (validator.validate(book).isEmpty() && !repository.existsById(book.getIsbn())) {
38+
if (validator.validate(bookDTO).isEmpty()
39+
&& !repository.existsById(bookDTO.getIsbn())) {
40+
Book book = mapper.map(bookDTO, Book.class);
4141
repository.save(book);
42-
created = true;
42+
return true;
4343
}
44-
return created;
44+
return false;
4545
}
4646

4747
/* --------------------------------------------------------------------------------------------
@@ -50,21 +50,16 @@ public boolean create(BookDTO bookDTO) {
5050

5151
@Cacheable(value = "books", key = "#isbn")
5252
public BookDTO retrieveByIsbn(String isbn) {
53-
BookDTO bookDTO = null;
54-
Book book = repository.findByIsbn(isbn).orElse(null);
55-
if (book != null) {
56-
bookDTO = mapper.map(book, BookDTO.class);
57-
}
58-
return bookDTO;
53+
return repository.findByIsbn(isbn)
54+
.map(book -> mapper.map(book, BookDTO.class))
55+
.orElse(null);
5956
}
6057

6158
@Cacheable(value = "books")
6259
public List<BookDTO> retrieveAll() {
63-
List<Book> books = StreamSupport.stream(repository.findAll().spliterator(), false)
64-
.collect(Collectors.toList());
65-
return books.stream()
66-
.map(book -> mapper.map(book, BookDTO.class))
67-
.collect(Collectors.toList());
60+
return StreamSupport.stream(repository.findAll().spliterator(), false)
61+
.map(book -> mapper.map(book, BookDTO.class))
62+
.collect(Collectors.toList());
6863
}
6964

7065
/* --------------------------------------------------------------------------------------------
@@ -73,13 +68,13 @@ public List<BookDTO> retrieveAll() {
7368

7469
@CachePut(value = "books", key = "#bookDTO.isbn")
7570
public boolean update(BookDTO bookDTO) {
76-
boolean updated = false;
77-
Book book = mapper.map(bookDTO, Book.class);
78-
if (validator.validate(book).isEmpty() && repository.existsById(book.getIsbn())) {
71+
if (validator.validate(bookDTO).isEmpty()
72+
&& repository.existsById(bookDTO.getIsbn())) {
73+
Book book = mapper.map(bookDTO, Book.class);
7974
repository.save(book);
80-
updated = true;
75+
return true;
8176
}
82-
return updated;
77+
return false;
8378
}
8479

8580
/* --------------------------------------------------------------------------------------------
@@ -88,11 +83,10 @@ public boolean update(BookDTO bookDTO) {
8883

8984
@CacheEvict(value = "books", key = "#isbn")
9085
public boolean delete(String isbn) {
91-
boolean deleted = false;
92-
if (!isbn.isBlank() && repository.existsById(isbn)) {
86+
if (repository.existsById(isbn)) {
9387
repository.deleteById(isbn);
94-
deleted = true;
88+
return true;
9589
}
96-
return deleted;
90+
return false;
9791
}
9892
}

0 commit comments

Comments
 (0)