Skip to content

Commit 6027da8

Browse files
authored
feat: adds lesson_26 homework and lesson_27 pre-work (#593)
* feat: adds lesson_26 homework and lesson_27 pre-work Signed-off-by: Anthony D. Mays <[email protected]> * tests: adds unit tests for media items controller Signed-off-by: Anthony D. Mays <[email protected]> * chore: updating slides Signed-off-by: Anthony D. Mays <[email protected]> * chore: adds build checks Signed-off-by: Anthony D. Mays <[email protected]> --------- Signed-off-by: Anthony D. Mays <[email protected]>
1 parent b3e337f commit 6027da8

File tree

128 files changed

+14961
-3
lines changed

Some content is hidden

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

128 files changed

+14961
-3
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
name: Check Lesson 26 Java Pull Request
2+
3+
on:
4+
pull_request:
5+
branches: [ "main" ]
6+
paths:
7+
- "lesson_26/api/java/**"
8+
9+
jobs:
10+
build:
11+
12+
runs-on: ubuntu-latest
13+
permissions:
14+
contents: read
15+
pull-requests: write
16+
17+
steps:
18+
- uses: actions/checkout@v4
19+
20+
- name: Set up JDK
21+
uses: actions/setup-java@v4
22+
with:
23+
java-version: '21'
24+
distribution: 'temurin'
25+
26+
- name: Build Lesson 26 with Java
27+
working-directory: ./lesson_26/api/java
28+
run: ./gradlew check
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
name: Check Lesson 26 TS Pull Request
2+
3+
on:
4+
pull_request:
5+
branches: [ "main" ]
6+
paths:
7+
- "lesson_26/api/javascript/api_app/**"
8+
9+
jobs:
10+
build:
11+
12+
runs-on: ubuntu-latest
13+
permissions:
14+
contents: read
15+
16+
steps:
17+
- uses: actions/checkout@v4
18+
19+
- name: Use Node.js
20+
uses: actions/setup-node@v4
21+
with:
22+
node-version: '20.x'
23+
24+
- name: Build Lesson 26 with Node.js
25+
working-directory: ./lesson_26/api/javascript/api_app
26+
run: |
27+
npm ci
28+
npm run check

.github/workflows/check_push.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,3 +164,13 @@ jobs:
164164
working-directory: ./lesson_17/bank
165165
run: ./gradlew check
166166

167+
- name: Build Lesson 26 with Java
168+
working-directory: ./lesson_26/api/java
169+
run: ./gradlew assemble
170+
171+
- name: Build Lesson 26 with Node.js
172+
working-directory: ./lesson_26/api/javascript/api_app
173+
run: |
174+
npm ci
175+
npm run build
176+

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+
}

0 commit comments

Comments
 (0)