Skip to content

Commit 84704eb

Browse files
authored
Merge pull request #124 from nanotaboada/feature/crudrepository-findall
feat: add endpoint to retrieve entire catalog
2 parents e54c2e9 + 059a91c commit 84704eb

7 files changed

Lines changed: 340 additions & 46 deletions

File tree

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"java.configuration.updateBuildConfiguration": "automatic"
3+
}

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package ar.com.nanotaboada.java.samples.spring.boot.controllers;
22

33
import java.net.URI;
4+
import java.util.List;
45

56
import org.springframework.http.HttpHeaders;
67
import org.springframework.http.HttpStatus;
@@ -63,6 +64,12 @@ public ResponseEntity<BookDTO> getBook(@PathVariable String isbn) {
6364
}
6465
}
6566

67+
@GetMapping("/books")
68+
public ResponseEntity<List<BookDTO>> getAllBooks() {
69+
List<BookDTO> books = service.retrieveAll();
70+
return new ResponseEntity<>(books, HttpStatus.OK);
71+
}
72+
6673
/* --------------------------------------------------------------------------------------------
6774
* HTTP PUT
6875
* ----------------------------------------------------------------------------------------- */
@@ -96,4 +103,4 @@ public ResponseEntity<String> deleteBook(@PathVariable String isbn) {
96103
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
97104
}
98105
}
99-
}
106+
}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
import jakarta.validation.Validator;
44

5+
import java.util.List;
6+
import java.util.stream.Collectors;
7+
import java.util.stream.StreamSupport;
8+
59
import org.modelmapper.ModelMapper;
610
import org.springframework.cache.annotation.CacheEvict;
711
import org.springframework.cache.annotation.CachePut;
@@ -54,6 +58,15 @@ public BookDTO retrieveByIsbn(String isbn) {
5458
return bookDTO;
5559
}
5660

61+
@Cacheable(value = "books")
62+
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());
68+
}
69+
5770
/* --------------------------------------------------------------------------------------------
5871
* Update
5972
* ----------------------------------------------------------------------------------------- */
Lines changed: 131 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,149 @@
11
package ar.com.nanotaboada.java.samples.spring.boot.test;
22

33
import java.time.LocalDate;
4+
import java.util.ArrayList;
5+
import java.util.List;
46

57
import ar.com.nanotaboada.java.samples.spring.boot.models.BookDTO;
68

79
public class BookDTOsBuilder {
810

911
public static BookDTO buildOneValid() {
10-
BookDTO bookDto = new BookDTO();
11-
bookDto.setIsbn("978-1484200773");
12-
bookDto.setTitle("Pro Git");
13-
bookDto.setSubtitle("Everything you neeed to know about Git");
14-
bookDto.setAuthor("Scott Chacon and Ben Straub");
15-
bookDto.setPublisher("lulu.com; First Edition");
16-
bookDto.setPublished(LocalDate.of(2014, 11, 18));
17-
bookDto.setPages(458);
18-
bookDto.setDescription("""
12+
BookDTO bookDTO = new BookDTO();
13+
bookDTO.setIsbn("978-1484200773");
14+
bookDTO.setTitle("Pro Git");
15+
bookDTO.setSubtitle("Everything you neeed to know about Git");
16+
bookDTO.setAuthor("Scott Chacon and Ben Straub");
17+
bookDTO.setPublisher("lulu.com; First Edition");
18+
bookDTO.setPublished(LocalDate.of(2014, 11, 18));
19+
bookDTO.setPages(458);
20+
bookDTO.setDescription(
21+
"""
1922
Pro Git (Second Edition) is your fully-updated guide to Git and its \
2023
usage in the modern world. Git has come a long way since it was first developed by \
2124
Linus Torvalds for Linux kernel development. It has taken the open source world by \
2225
storm since its inception in 2005, and this book teaches you how to use it like a \
23-
pro.\
24-
""");
25-
bookDto.setWebsite("https://git-scm.com/book/en/v2");
26-
return bookDto;
26+
pro.""");
27+
bookDTO.setWebsite("https://git-scm.com/book/en/v2");
28+
return bookDTO;
2729
}
2830

2931
public static BookDTO buildOneInvalid() {
30-
BookDTO bookDto = new BookDTO();
31-
bookDto.setIsbn("978-1234567890"); // Invalid (invalid ISBN)
32-
bookDto.setTitle("Title");
33-
bookDto.setSubtitle("Sub Title");
34-
bookDto.setAuthor("Author");
35-
bookDto.setPublisher("Publisher");
36-
bookDto.setPublished(LocalDate.now()); // Invalid (must be a past date)
37-
bookDto.setPages(123);
38-
bookDto.setDescription("Description");
39-
bookDto.setWebsite("https://domain.com/");
40-
return bookDto;
32+
BookDTO bookDTO = new BookDTO();
33+
bookDTO.setIsbn("978-1234567890"); // Invalid (invalid ISBN)
34+
bookDTO.setTitle("Title");
35+
bookDTO.setSubtitle("Sub Title");
36+
bookDTO.setAuthor("Author");
37+
bookDTO.setPublisher("Publisher");
38+
bookDTO.setPublished(LocalDate.now()); // Invalid (must be a past date)
39+
bookDTO.setPages(123);
40+
bookDTO.setDescription("Description");
41+
bookDTO.setWebsite("https://domain.com/");
42+
return bookDTO;
43+
}
44+
45+
public static List<BookDTO> buildManyValid() {
46+
ArrayList<BookDTO> bookDTOs = new ArrayList<>();
47+
BookDTO bookDTO9781838986698 = new BookDTO();
48+
bookDTO9781838986698.setIsbn("9781838986698");
49+
bookDTO9781838986698.setTitle("The Java Workshop");
50+
bookDTO9781838986698.setSubtitle("Learn object-oriented programming and kickstart your career in software development");
51+
bookDTO9781838986698.setAuthor("David Cuartielles, Andreas Göransson, Eric Foster-Johnson");
52+
bookDTO9781838986698.setPublisher("Packt Publishing");
53+
bookDTO9781838986698.setPublished(LocalDate.of(2019, 10, 31));
54+
bookDTO9781838986698.setPages(606);
55+
bookDTO9781838986698.setDescription(
56+
"""
57+
Java is a versatile, popular programming language used across a wide range of \
58+
industries. Learning how to write effective Java code can take your career to \
59+
the next level, and The Java Workshop will help you do just that. This book is \
60+
designed to take the pain out of Java coding and teach you everything you need \
61+
to know to be productive in building real-world software. The Workshop starts by \
62+
showing you how to use classes, methods, and the built-in Collections API to \
63+
manipulate data structures effortlessly. You'll dive right into learning about \
64+
object-oriented programming by creating classes and interfaces and making use of \
65+
inheritance and polymorphism. After learning how to handle exceptions, you'll \
66+
study the modules, packages, and libraries that help you organize your code. As \
67+
you progress, you'll discover how to connect to external databases and web \
68+
servers, work with regular expressions, and write unit tests to validate your \
69+
code. You'll also be introduced to functional programming and see how to \
70+
implement it using lambda functions. By the end of this Workshop, you'll be \
71+
well-versed with key Java concepts and have the knowledge and confidence to \
72+
tackle your own ambitious projects with Java.""");
73+
bookDTO9781838986698.setWebsite("https://www.packtpub.com/free-ebook/the-java-workshop/9781838986698");
74+
bookDTOs.add(bookDTO9781838986698);
75+
BookDTO bookDTO9781789613476 = new BookDTO();
76+
bookDTO9781789613476.setIsbn("9781789613476");
77+
bookDTO9781789613476.setTitle("Hands-On Microservices with Spring Boot and Spring Cloud");
78+
bookDTO9781789613476.setSubtitle("Build and deploy Java microservices using Spring Cloud, Istio, and Kubernetes");
79+
bookDTO9781789613476.setAuthor("Magnus Larsson");
80+
bookDTO9781789613476.setPublisher("Packt Publishing");
81+
bookDTO9781789613476.setPublished(LocalDate.of(2019, 9, 20));
82+
bookDTO9781789613476.setPages(668);
83+
bookDTO9781789613476.setDescription(
84+
"""
85+
Microservices architecture allows developers to build and maintain applications \
86+
with ease, and enterprises are rapidly adopting it to build software using \
87+
Spring Boot as their default framework. With this book, you'll learn how to \
88+
efficiently build and deploy microservices using Spring Boot. This microservices \
89+
book will take you through tried and tested approaches to building distributed \
90+
systems and implementing microservices architecture in your organization. \
91+
Starting with a set of simple cooperating microservices developed using Spring \
92+
Boot, you'll learn how you can add functionalities such as persistence, make \
93+
your microservices reactive, and describe their APIs using Swagger/OpenAPI. As \
94+
you advance, you'll understand how to add different services from Spring Cloud \
95+
to your microservice system. The book also demonstrates how to deploy your \
96+
microservices using Kubernetes and manage them with Istio for improved security \
97+
and traffic management. Finally, you'll explore centralized log management using \
98+
the EFK stack and monitor microservices using Prometheus and Grafana. By the end \
99+
of this book, you'll be able to build microservices that are scalable and robust \
100+
using Spring Boot and Spring Cloud.""");
101+
bookDTO9781789613476.setWebsite("https://www.packtpub.com/free-ebook/hands-on-microservices-with-spring-boot-and-spring-cloud/9781789613476");
102+
bookDTOs.add(bookDTO9781789613476);
103+
BookDTO bookDTO9781838555726 = new BookDTO();
104+
bookDTO9781838555726.setIsbn("9781838555726");
105+
bookDTO9781838555726.setTitle("Mastering Kotlin");
106+
bookDTO9781838555726.setSubtitle("Learn advanced Kotlin programming techniques to build apps for Android, iOS, and the web");
107+
bookDTO9781838555726.setAuthor("Nate Ebel");
108+
bookDTO9781838555726.setPublisher("Packt Publishing");
109+
bookDTO9781838555726.setPublished(LocalDate.of(2019, 10, 11));
110+
bookDTO9781838555726.setPages(434);
111+
bookDTO9781838555726.setDescription(
112+
"""
113+
Using Kotlin without taking advantage of its power and interoperability is like \
114+
owning a sports car and never taking it out of the garage. While documentation \
115+
and introductory resources can help you learn the basics of Kotlin, the fact \
116+
that it's a new language means that there are limited learning resources and \
117+
code bases available in comparison to Java and other established languages. This \
118+
Kotlin book will show you how to leverage software designs and concepts that \
119+
have made Java the most dominant enterprise programming language. You'll \
120+
understand how Kotlin is a modern approach to object-oriented programming (OOP). \
121+
This book will take you through the vast array of features that Kotlin provides \
122+
over other languages. These features include seamless interoperability with \
123+
Java, efficient syntax, built-in functional programming constructs, and support \
124+
for creating your own DSL. Finally, you will gain an understanding of \
125+
implementing practical design patterns and best practices to help you master the \
126+
Kotlin language. By the end of the book, you'll have obtained an advanced \
127+
understanding of Kotlin in order to be able to build production-grade \
128+
applications.""");
129+
bookDTO9781838555726.setWebsite("https://www.packtpub.com/free-ebook/mastering-kotlin/9781838555726");
130+
bookDTOs.add(bookDTO9781838555726);
131+
BookDTO bookDTO9781484242216 = new BookDTO();
132+
bookDTO9781484242216.setIsbn("9781484242216");
133+
bookDTO9781484242216.setTitle("Rethinking Productivity in Software Engineering");
134+
bookDTO9781484242216.setAuthor("Caitlin Sadowski, Thomas Zimmermann");
135+
bookDTO9781484242216.setPublisher("Apress");
136+
bookDTO9781484242216.setPublished(LocalDate.of(2019, 5, 7));
137+
bookDTO9781484242216.setPages(301);
138+
bookDTO9781484242216.setDescription(
139+
"""
140+
Get the most out of this foundational reference and improve the productivity of \
141+
your software teams. This open access book collects the wisdom of the 2017 \
142+
"Dagstuhl" seminar on productivity in software engineering, a meeting of \
143+
community leaders, who came together with the goal of rethinking traditional \
144+
definitions and measures of productivity.""");
145+
bookDTO9781484242216.setWebsite("https://link.springer.com/book/10.1007/978-1-4842-4221-6");
146+
bookDTOs.add(bookDTO9781484242216);
147+
return bookDTOs;
41148
}
42149
}

0 commit comments

Comments
 (0)