Skip to content

Commit 41528e9

Browse files
authored
feat: adds lesson 16 homework and infrastructure (#360)
* chore fixed typo Signed-off-by: Anthony D. Mays <[email protected]> * feat: adds lesson_16 homework framework code * chore: add Spring Web dep * feat: implements initial api controller for media items * chore: rejiggers deps to use Spring config * chore: lint * tests: adds controller tests Signed-off-by: Anthony D. Mays <[email protected]> * chore: adds gh actions Signed-off-by: Anthony D. Mays <[email protected]> * chore: resets controller for assignment Signed-off-by: Anthony D. Mays <[email protected]> * docs: adds assignment details Signed-off-by: Anthony D. Mays <[email protected]> * chore: introduces MediaType enum Signed-off-by: Anthony D. Mays <[email protected]> * fix: adds missing import Signed-off-by: Anthony D. Mays <[email protected]> * chore: adds message validation constraints Signed-off-by: Anthony D. Mays <[email protected]> * chore: adds request validation reporting Signed-off-by: Anthony D. Mays <[email protected]> * docs: renamed references section and moved Signed-off-by: Anthony D. Mays <[email protected]> * chore: adds proper id for library guests separate from email. Signed-off-by: Anthony D. Mays <[email protected]> * chore: adds ability to find library guest by email. Signed-off-by: Anthony D. Mays <[email protected]> --------- Signed-off-by: Anthony D. Mays <[email protected]>
1 parent 317f1da commit 41528e9

File tree

71 files changed

+3598
-0
lines changed

Some content is hidden

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

71 files changed

+3598
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
name: Check Lesson 16 Pull Request
2+
3+
on:
4+
pull_request:
5+
branches: [ "main" ]
6+
paths:
7+
- "lesson_16/api/**"
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+
- name: Set up JDK 17
19+
uses: actions/setup-java@v4
20+
with:
21+
java-version: '17'
22+
distribution: 'temurin'
23+
24+
# Configure Gradle for optimal use in GiHub Actions, including caching of downloaded dependencies.
25+
# See: https://github.com/gradle/actions/blob/main/setup-gradle/README.md
26+
- name: Setup Gradle
27+
uses: gradle/actions/setup-gradle@417ae3ccd767c252f5661f1ace9f835f9654f2b5 # v3.1.0
28+
29+
- name: Build Lesson 16 with Gradle Wrapper
30+
working-directory: ./lesson_16/api
31+
run: ./gradlew check
32+
33+

.github/workflows/check_push.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ on:
2020
- "lesson_13/bank/**"
2121
- "lesson_14/algos/**"
2222
- "lesson_14/db/**"
23+
- "lesson_16/api/**"
2324

2425
jobs:
2526
build:
@@ -101,6 +102,10 @@ jobs:
101102
working-directory: ./lesson_14/db
102103
run: ./gradlew check
103104

105+
- name: Build Lesson 16 with Gradle Wrapper
106+
working-directory: ./lesson_16/api
107+
run: ./gradlew assemble && ./gradlew spotlessCheck
108+
104109
- name: Build Shared Lib with Gradle Wrapper
105110
working-directory: ./lib/java/codedifferently-instructional
106111
run: ./gradlew check

lesson_16/README.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Lesson 16
2+
3+
## Homework
4+
5+
* Complete the [Creating the Library API](#create-the-books-api) exercise.
6+
7+
## Creating the Library API
8+
9+
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.
10+
11+
* You will implement the [MediaItemsController][controller-file] to enable the following API:
12+
* `GET /items` - Retrieves a list of media items
13+
* `POST /items` - Creates a new media item
14+
* `GET /items/:id` - Retrieves a single media item with the given ID.
15+
* `DELETE /items/:id` - Deletes a single media item with the given ID.
16+
* You will also implement a new `PatronsController` that will allow the following interactions:
17+
* `GET /patrons` - Retrieves a list of patrons.
18+
* `POST /patrons` - Creates a new patron.
19+
* `GET /patrons/:id` - Retrieves a single patron with the given ID.
20+
* `DELETE /patrons/:id` - Deletes a single patron with the given ID.
21+
* Study the tests in [MediaItemsControllerTest][controller-test-file] to understand what you should accept and return in the API.
22+
* You should not need to make any code changes outside of the `com.codedifferently.lesson16.web` package.
23+
24+
## Running the API
25+
26+
You can run the server using the usual `./gradlew run` command. If you want to test that the server is running correctly, you can use `curl` like so:
27+
28+
```bash
29+
curl http://localhost:5000/items | json_pp
30+
```
31+
32+
Alternatively, you can test the API using the tool [Postman][postman-link]. I recommend installing this tool to make it easier to test things.
33+
34+
## Additional Resources
35+
36+
* [What are HTTP requests?](https://youtu.be/-Zea7GB2OwA)
37+
* [Build a REST API with Spring and Java Config](https://www.baeldung.com/building-a-restful-web-service-with-spring-and-java-based-configuration)
38+
* [Exploring REST APIs with Spring MVC](https://www.developer.com/java/exploring-rest-apis-with-spring-mvc/)
39+
* [Using Lombok’s @Builder Annotation](https://www.baeldung.com/lombok-builder)
40+
* [Validation in Spring Boot](https://www.baeldung.com/spring-boot-bean-validation)
41+
42+
[controller-file]: ./api//api_app/src/main/java/com/codedifferently/lesson16/web/MediaItemsController.java
43+
[controller-test-file]: ./api/api_app/src/test/java/com/codedifferently/lesson16/web/MediaItemsControllerTest.java
44+
[postman-link]: https://postman.com

lesson_16/api/.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_16/api/.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: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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.2.2"
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.9.1")
22+
testImplementation("org.springframework.boot:spring-boot-starter-test")
23+
testImplementation("org.assertj:assertj-core:3.25.1")
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:31.1-jre")
30+
implementation("com.google.code.gson:gson:2.10.1")
31+
implementation("commons-cli:commons-cli:1.6.0")
32+
implementation("org.springframework.boot:spring-boot-starter")
33+
implementation("org.springframework.boot:spring-boot-starter-data-jpa")
34+
implementation("org.springframework.boot:spring-boot-starter-validation")
35+
implementation("org.springframework.boot:spring-boot-starter-web")
36+
compileOnly("org.springframework.boot:spring-boot-devtools")
37+
implementation("com.opencsv:opencsv:5.9")
38+
implementation("org.apache.commons:commons-csv:1.10.0")
39+
implementation("org.xerial:sqlite-jdbc:3.36.0")
40+
implementation("org.hibernate.orm:hibernate-community-dialects:6.2.7.Final")
41+
}
42+
43+
application {
44+
// Define the main class for the application.
45+
mainClass.set("com.codedifferently.lesson16.Lesson16")
46+
}
47+
48+
tasks.named<JavaExec>("run") {
49+
standardInput = System.`in`
50+
}
51+
52+
tasks.named<Test>("test") {
53+
// Use JUnit Platform for unit tests.
54+
useJUnitPlatform()
55+
}
56+
57+
58+
configure<com.diffplug.gradle.spotless.SpotlessExtension> {
59+
60+
format("misc", {
61+
// define the files to apply `misc` to
62+
target("*.gradle", ".gitattributes", ".gitignore")
63+
64+
// define the steps to apply to those files
65+
trimTrailingWhitespace()
66+
indentWithTabs() // or spaces. Takes an integer argument if you don't like 4
67+
endWithNewline()
68+
})
69+
70+
java {
71+
// don't need to set target, it is inferred from java
72+
73+
// apply a specific flavor of google-java-format
74+
googleJavaFormat()
75+
// fix formatting of type annotations
76+
formatAnnotations()
77+
}
78+
}

lesson_16/api/api_app/lombok.config

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.lesson16;
2+
3+
import com.codedifferently.lesson16.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 Lesson16 implements CommandLineRunner {
13+
@Autowired private LibraryApp libraryApp;
14+
15+
public static void main(String[] args) {
16+
var application = new SpringApplication(Lesson16.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)