Skip to content

Commit 744540d

Browse files
committed
feat: adds lesson_26 homework and lesson_27 pre-work
Signed-off-by: Anthony D. Mays <[email protected]>
1 parent b3e337f commit 744540d

File tree

123 files changed

+14635
-1
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

123 files changed

+14635
-1
lines changed

lesson_26/README.md

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,49 @@ Please review the following resources before lecture:
1919

2020
## Homework
2121

22-
- TODO(anthonydmays): Add details here.
22+
- [ ] Complete the [Creating a Library API](#creating-a-library-api) assignment.
23+
- [ ] Do pre-work for [lesson 27](/lesson_27/).
24+
25+
### Creating a Library API
26+
27+
We are continuing to build atop the foundation of our library app. For this assignment, you will help implement the API that will be used by a yet-to-come front-end app.
28+
29+
* You will implement the [MediaItemsController][controller-file] to enable the following API:
30+
* `GET /items` - Retrieves a list of media items
31+
* `POST /items` - Creates a new media item
32+
* `GET /items/:id` - Retrieves a single media item with the given ID.
33+
* `DELETE /items/:id` - Deletes a single media item with the given ID.
34+
* Study the tests in [MediaItemsControllerTest][controller-test-file] to understand what you should accept and return in the API.
35+
* You should not need to make any code changes outside of the `com.codedifferently.lesson26.web` package.
36+
37+
#### Running the API
38+
39+
You can run the server using the usual `./gradlew run` command from the `api/java` directory. If you want to test that the server is running correctly, you can use `curl` like so:
40+
41+
```bash
42+
curl http://localhost:3001/items | json_pp
43+
```
44+
45+
The project also includes an OpenAPI user interface (Swagger) for navigating the API. Just visit http://localhost:3001/swagger-ui.html to access it.
46+
47+
Alternatively, you can also test the API using the tool [Postman][postman-link]. I recommend installing this tool to make it easier to test things.
48+
49+
#### Debugging the API
50+
51+
Remember that you can debug the API by visiting the main function in [Lesson26.java][main-file] and clicking `Debug main`. You'll be able to set breakpoints in your code to see what's happening and fix issues.
52+
53+
![Debugging the API](./debug.png)
54+
55+
56+
#### TypeScript API
57+
58+
This project also includes a fully functioning TypeScript version of the Java project. You can visit `api/javascript/api_app` to execute it using `npm start` and view the OpenAPI documentation at http://localhost:3000/api (note that it runs on port 3000).
59+
60+
## Additional resources
61+
62+
* [gRPC vs REST: Comparing API Styles in Practice (Article)](https://dev.to/anthonydmays/grpc-vs-rest-comparing-api-styles-in-practice-4bl): This article explains why the stuff most people call REST isn't actually.
63+
64+
[controller-file]: ./api/java/api_app/src/main/java/com/codedifferently/lesson26/web/MediaItemsController.java
65+
[controller-test-file]: ./api/java/api_app/src/test/java/com/codedifferently/lesson26/web/MediaItemsControllerTest.java
66+
[postman-link]: https://www.postman.com/downloads/
67+
[main-file]: ./api/java/api_app/src/main/java/com/codedifferently/lesson26/Lesson26.java

lesson_26/api/java/.gitattributes

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#
2+
# https://help.github.com/articles/dealing-with-line-endings/
3+
#
4+
# Linux start script should use lf
5+
/gradlew text eol=lf
6+
7+
# These are Windows script files and should use crlf
8+
*.bat text eol=crlf
9+

lesson_26/api/java/.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Ignore Gradle project-specific cache directory
2+
.gradle
3+
4+
# Ignore Gradle build output directory
5+
build
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
plugins {
2+
// Apply the application plugin to add support for building a CLI application in Java.
3+
application
4+
eclipse
5+
id("com.diffplug.spotless") version "6.25.0"
6+
id("org.springframework.boot") version "3.4.0"
7+
id("com.adarshr.test-logger") version "4.0.0"
8+
id("io.freefair.lombok") version "8.6"
9+
}
10+
11+
apply(plugin = "io.spring.dependency-management")
12+
13+
repositories {
14+
// Use Maven Central for resolving dependencies.
15+
mavenCentral()
16+
}
17+
18+
dependencies {
19+
// Use JUnit Jupiter for testing.
20+
testImplementation("com.codedifferently.instructional:instructional-lib")
21+
testImplementation("org.junit.jupiter:junit-jupiter:5.11.3")
22+
testImplementation("org.springframework.boot:spring-boot-starter-test")
23+
testImplementation("org.assertj:assertj-core:3.26.3")
24+
testImplementation("at.favre.lib:bcrypt:0.10.2")
25+
testImplementation("org.springframework.boot:spring-boot-starter-test")
26+
27+
// This dependency is used by the application.
28+
implementation("com.codedifferently.instructional:instructional-lib")
29+
implementation("com.google.guava:guava:33.3.1-jre")
30+
implementation("com.google.code.gson:gson:2.11.0")
31+
implementation("commons-cli:commons-cli:1.6.0")
32+
implementation("org.springdoc:springdoc-openapi-starter-webmvc-ui:2.6.0")
33+
implementation("org.springframework.boot:spring-boot-starter")
34+
implementation("org.springframework.boot:spring-boot-starter-data-jpa")
35+
implementation("org.springframework.boot:spring-boot-starter-validation")
36+
implementation("org.springframework.boot:spring-boot-starter-web")
37+
compileOnly("org.springframework.boot:spring-boot-devtools")
38+
implementation("com.opencsv:opencsv:5.9")
39+
implementation("org.apache.commons:commons-csv:1.10.0")
40+
implementation("org.xerial:sqlite-jdbc:3.36.0")
41+
implementation("org.hibernate.orm:hibernate-community-dialects:6.2.7.Final")
42+
}
43+
44+
application {
45+
// Define the main class for the application.
46+
mainClass.set("com.codedifferently.lesson26.Lesson26")
47+
}
48+
49+
tasks.named<JavaExec>("run") {
50+
standardInput = System.`in`
51+
}
52+
53+
tasks.named<Test>("test") {
54+
// Use JUnit Platform for unit tests.
55+
useJUnitPlatform()
56+
}
57+
58+
59+
configure<com.diffplug.gradle.spotless.SpotlessExtension> {
60+
61+
format("misc", {
62+
// define the files to apply `misc` to
63+
target("*.gradle", ".gitattributes", ".gitignore")
64+
65+
// define the steps to apply to those files
66+
trimTrailingWhitespace()
67+
indentWithTabs() // or spaces. Takes an integer argument if you don't like 4
68+
endWithNewline()
69+
})
70+
71+
java {
72+
// don't need to set target, it is inferred from java
73+
74+
// apply a specific flavor of google-java-format
75+
googleJavaFormat()
76+
// fix formatting of type annotations
77+
formatAnnotations()
78+
}
79+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# This file is generated by the 'io.freefair.lombok' Gradle plugin
2+
config.stopBubbling = true
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.codedifferently.lesson26;
2+
3+
import com.codedifferently.lesson26.cli.LibraryApp;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.boot.CommandLineRunner;
6+
import org.springframework.boot.SpringApplication;
7+
import org.springframework.boot.autoconfigure.SpringBootApplication;
8+
import org.springframework.context.annotation.Configuration;
9+
10+
@Configuration
11+
@SpringBootApplication(scanBasePackages = "com.codedifferently")
12+
public class Lesson26 implements CommandLineRunner {
13+
@Autowired private LibraryApp libraryApp;
14+
15+
public static void main(String[] args) {
16+
var application = new SpringApplication(Lesson26.class);
17+
application.run(args);
18+
}
19+
20+
@Override
21+
public void run(String... args) throws Exception {
22+
// Don't run as an app if we're running as a JUnit test.
23+
if (isJUnitTest()) {
24+
return;
25+
}
26+
27+
libraryApp.run(args);
28+
}
29+
30+
private static boolean isJUnitTest() {
31+
for (StackTraceElement element : Thread.currentThread().getStackTrace()) {
32+
if (element.getClassName().startsWith("org.junit.")) {
33+
return true;
34+
}
35+
}
36+
return false;
37+
}
38+
}
Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
package com.codedifferently.lesson26.cli;
2+
3+
import com.codedifferently.lesson26.factory.LibraryDataLoader;
4+
import com.codedifferently.lesson26.library.Book;
5+
import com.codedifferently.lesson26.library.Library;
6+
import com.codedifferently.lesson26.library.LibraryInfo;
7+
import com.codedifferently.lesson26.library.MediaItem;
8+
import com.codedifferently.lesson26.library.search.SearchCriteria;
9+
import java.util.Map;
10+
import java.util.Scanner;
11+
import java.util.Set;
12+
import java.util.UUID;
13+
import org.apache.commons.cli.CommandLine;
14+
import org.apache.commons.cli.CommandLineParser;
15+
import org.apache.commons.cli.DefaultParser;
16+
import org.apache.commons.cli.HelpFormatter;
17+
import org.apache.commons.cli.Option;
18+
import org.apache.commons.cli.Options;
19+
import org.apache.commons.cli.ParseException;
20+
import org.springframework.beans.factory.annotation.Autowired;
21+
import org.springframework.stereotype.Service;
22+
23+
@Service
24+
public final class LibraryApp {
25+
@Autowired private Library library;
26+
27+
public void run(String[] args) throws Exception {
28+
// Show stats about the loaded library to the user.
29+
printLibraryInfo(library);
30+
31+
try (var scanner = new Scanner(System.in)) {
32+
LibraryCommand command;
33+
// Main application loop.
34+
while ((command = promptForCommand(scanner)) != LibraryCommand.EXIT) {
35+
switch (command) {
36+
case SEARCH -> doSearch(scanner, library);
37+
default -> System.out.println("\nNot ready yet, coming soon!");
38+
}
39+
}
40+
}
41+
}
42+
43+
private void printLibraryInfo(Library library) {
44+
LibraryInfo info = library.getInfo();
45+
Map<UUID, Set<MediaItem>> checkedOutItemsByGuest = info.getCheckedOutItemsByGuest();
46+
int numCheckedOutItems = checkedOutItemsByGuest.values().stream().mapToInt(Set::size).sum();
47+
System.out.println();
48+
System.out.println("========================================");
49+
System.out.println("Library id: " + library.getId());
50+
System.out.println("Number of items: " + info.getItems().size());
51+
System.out.println("Number of guests: " + info.getGuests().size());
52+
System.out.println("Number of checked out items: " + numCheckedOutItems);
53+
System.out.println("========================================");
54+
System.out.println();
55+
}
56+
57+
private static LibraryDataLoader getLoaderOrDefault(
58+
String[] args, LibraryDataLoader defaultLoader) throws Exception {
59+
String loaderType = getLoaderFromCommandLine(args);
60+
return loaderType == null
61+
? defaultLoader
62+
: Class.forName(loaderType)
63+
.asSubclass(LibraryDataLoader.class)
64+
.getDeclaredConstructor()
65+
.newInstance();
66+
}
67+
68+
private static String getLoaderFromCommandLine(String[] args) throws IllegalArgumentException {
69+
Options options = new Options();
70+
Option input = new Option("l", "loader", true, "data loader type");
71+
input.setRequired(false);
72+
options.addOption(input);
73+
CommandLineParser parser = new DefaultParser();
74+
HelpFormatter formatter = new HelpFormatter();
75+
try {
76+
CommandLine cmd = parser.parse(options, args);
77+
return cmd.getOptionValue("loader");
78+
} catch (ParseException e) {
79+
System.out.println();
80+
System.out.println(e.getMessage());
81+
formatter.printHelp("utility-name", options);
82+
83+
System.exit(1);
84+
}
85+
return null;
86+
}
87+
88+
private static LibraryCommand promptForCommand(Scanner scanner) {
89+
var command = LibraryCommand.UNKNOWN;
90+
while (command == LibraryCommand.UNKNOWN) {
91+
printMenu();
92+
var input = scanner.nextLine();
93+
try {
94+
command = LibraryCommand.fromValue(Integer.parseInt(input.trim()));
95+
} catch (IllegalArgumentException e) {
96+
System.out.println("Invalid command: " + input);
97+
}
98+
}
99+
return command;
100+
}
101+
102+
private static void printMenu() {
103+
System.out.println("\nEnter the number of the desired command:");
104+
System.out.println("1) << EXIT");
105+
System.out.println("2) SEARCH");
106+
System.out.println("3) CHECKOUT");
107+
System.out.println("4) RETURN");
108+
System.out.print("command> ");
109+
}
110+
111+
private void doSearch(Scanner scanner, Library library) {
112+
LibrarySearchCommand command = promptForSearchCommand(scanner);
113+
if (command == LibrarySearchCommand.RETURN) {
114+
return;
115+
}
116+
SearchCriteria criteria = getSearchCriteria(scanner, command);
117+
Set<MediaItem> results = library.search(criteria);
118+
printSearchResults(results);
119+
}
120+
121+
private LibrarySearchCommand promptForSearchCommand(Scanner scanner) {
122+
var command = LibrarySearchCommand.UNKNOWN;
123+
while (command == LibrarySearchCommand.UNKNOWN) {
124+
printSearchMenu();
125+
var input = scanner.nextLine();
126+
try {
127+
command = LibrarySearchCommand.fromValue(Integer.parseInt(input.trim()));
128+
} catch (IllegalArgumentException e) {
129+
System.out.println("Invalid command: " + input);
130+
}
131+
}
132+
return command;
133+
}
134+
135+
private void printSearchMenu() {
136+
System.out.println("\nEnter the number of the desired search criteria:");
137+
System.out.println("1) << RETURN");
138+
System.out.println("2) TITLE");
139+
System.out.println("3) AUTHOR");
140+
System.out.println("4) TYPE");
141+
System.out.print("search> ");
142+
}
143+
144+
private SearchCriteria getSearchCriteria(Scanner scanner, LibrarySearchCommand command) {
145+
System.out.println();
146+
switch (command) {
147+
case TITLE -> {
148+
System.out.println("Enter the title to search for: ");
149+
System.out.print("title> ");
150+
var title = scanner.nextLine();
151+
return SearchCriteria.builder().title(title).build();
152+
}
153+
case AUTHOR -> {
154+
System.out.println("Enter the author to search for: ");
155+
System.out.print("author> ");
156+
var author = scanner.nextLine();
157+
return SearchCriteria.builder().author(author).build();
158+
}
159+
case TYPE -> {
160+
System.out.println("Enter the type to search for: ");
161+
System.out.print("type> ");
162+
var type = scanner.nextLine();
163+
return SearchCriteria.builder().type(type).build();
164+
}
165+
default -> System.out.println("Invalid search command: " + command);
166+
}
167+
return null;
168+
}
169+
170+
private void printSearchResults(Set<MediaItem> results) {
171+
System.out.println();
172+
173+
if (results.isEmpty()) {
174+
System.out.println("No results found.");
175+
return;
176+
}
177+
178+
System.out.println("Search results:\n");
179+
for (MediaItem item : results) {
180+
System.out.println("ID: " + item.getId());
181+
System.out.println("TITLE: " + item.getTitle());
182+
if (item instanceof Book book) {
183+
System.out.println("AUTHOR(S): " + String.join(", ", book.getAuthors()));
184+
}
185+
System.out.println("TYPE: " + item.getType().toString().toUpperCase());
186+
System.out.println();
187+
}
188+
System.out.println("Found " + results.size() + " result(s).\n");
189+
}
190+
}

0 commit comments

Comments
 (0)