Skip to content

Commit 971d16f

Browse files
authored
feat: adds Lesson 14 content and homework assignment (code-differently#330)
* chore: adds lesson_14 boilerplate. * chore: adds initial sqlite db for library data. * feat: implements db data loader * chore: adds @service annotation to core loaders. * docs: adds homework instructions * chore: add lesson 14 build actions * docs: fix misspelling * docs: updating for clarity. * chore: nit updates
1 parent cfa0266 commit 971d16f

File tree

74 files changed

+3694
-2
lines changed

Some content is hidden

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

74 files changed

+3694
-2
lines changed

.devcontainer/devcontainer.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
"ghcr.io/devcontainers/features/node:1": {},
1515
"ghcr.io/devcontainers/features/python:1": {},
1616
"ghcr.io/devcontainers-contrib/features/ts-node:1": {},
17-
"ghcr.io/devcontainers/features/sshd:1": {}
17+
"ghcr.io/devcontainers/features/sshd:1": {},
18+
"ghcr.io/warrenbuckley/codespace-features/sqlite:1": {}
1819
},
1920
"portsAttributes": {
2021
"80": {
@@ -32,7 +33,8 @@
3233
"ms-vscode.vscode-typescript-next",
3334
"ms-dotnettools.csdevkit",
3435
"ritwickdey.LiveServer",
35-
"mechatroner.rainbow-csv"
36+
"mechatroner.rainbow-csv",
37+
"alexcvzz.vscode-sqlite"
3638
]
3739
}
3840
}
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
name: Check Lesson 14 Pull Request
2+
3+
on:
4+
pull_request:
5+
branches: [ "main" ]
6+
paths:
7+
- "lesson_14/db/**"
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 14 with Gradle Wrapper
30+
working-directory: ./lesson_14/db
31+
run: ./gradlew check
32+
33+

.github/workflows/check_push.yml

+16
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ on:
1616
- "lesson_10/examples/**"
1717
- "lesson_10/quiz/**"
1818
- "lesson_10/solid/**"
19+
- "lesson_12/io/**"
20+
- "lesson_13/bank/**"
21+
- "lesson_14/algos/**"
22+
- "lesson_14/db/**"
1923

2024
jobs:
2125
build:
@@ -85,6 +89,18 @@ jobs:
8589
working-directory: ./lesson_12/io
8690
run: ./gradlew assemble && ./gradlew spotlessCheck
8791

92+
- name: Build Lesson 13 with Gradle Wrapper
93+
working-directory: ./lesson_13/bank
94+
run: ./gradlew check
95+
96+
- name: Build Lesson 14 Algos with Gradle Wrapper
97+
working-directory: ./lesson_14/algos
98+
run: ./gradlew check
99+
100+
- name: Build Lesson 14 DB with Gradle Wrapper
101+
working-directory: ./lesson_14/db
102+
run: ./gradlew check
103+
88104
- name: Build Shared Lib with Gradle Wrapper
89105
working-directory: ./lib/java/codedifferently-instructional
90106
run: ./gradlew check

.gitignore

+8
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,11 @@ dist
140140
.pnp.*
141141

142142
.DS_Store
143+
144+
# Python
145+
*.pyc
146+
*.pyo
147+
*.pyd
148+
__pycache__/
149+
venv/
150+
pip-log.txt

lesson_14/README.md

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Lesson 14
2+
3+
## Homework
4+
5+
* Memorize the `printPermutations` and `reverseString` methods in [algos_app][algos-app].
6+
* [Extra credit] Complete [Loading the Library, Part II](#loading-the-library-part-ii) assignment.
7+
8+
## Loading The Library, Part II
9+
10+
Instead of loading our library data from JSON or CSV files, we now want to load data from a proper database. A new implementation of the `LibraryDbDataLoader` data loader has been provided to accomplish this task and is now the [default data loader][library-app] for the app.
11+
12+
To build familiarity in working with databases, you are charged with the following tasks:
13+
14+
* Write a `.sql` script file that queries the following data. Use a unique name for your file and store it in the [queries][queries-dir] directory of the resources folder.
15+
* A `SELECT` query that returns the counts of media items by type.
16+
* A `SELECT` query that returns the sum of total pages checked out by guests.
17+
* A `SELECT` query that shows all 5 guests and any corresponding records in the `checked_out_items` table.
18+
* Add a new table called `library_users` to the SQLite database that stores a user's id (UUID formatted string), email, first name, last name, and a password (bcrypt encoded string). Add a model and repository that loads the users into the LibraryDataModel. Populate the database with a few users.
19+
20+
As before, you can run the app from the console using the following command:
21+
22+
```bash
23+
./gradlew run --console=plain
24+
```
25+
26+
[algos-app]: ./algos/algos_app/src/main/java/com/codedifferently/lesson14/Lesson14.java
27+
[queries-dir]: ./db/db_app/src/main/resources/queries/
28+
[library-app]: ./db/db_app/src/main/java/com/codedifferently/lesson14/cli/LibraryApp.java#L26

lesson_14/algos/.gitattributes

+9
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_14/algos/.gitignore

+5
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
+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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+
}
9+
10+
apply(plugin = "io.spring.dependency-management")
11+
12+
repositories {
13+
// Use Maven Central for resolving dependencies.
14+
mavenCentral()
15+
}
16+
17+
dependencies {
18+
// Use JUnit Jupiter for testing.
19+
testImplementation("com.codedifferently.instructional:instructional-lib")
20+
testImplementation("org.junit.jupiter:junit-jupiter:5.9.1")
21+
testImplementation("org.springframework.boot:spring-boot-starter-test")
22+
testImplementation("org.assertj:assertj-core:3.25.1")
23+
testImplementation("at.favre.lib:bcrypt:0.10.2")
24+
25+
// This dependency is used by the application.
26+
implementation("com.codedifferently.instructional:instructional-lib")
27+
implementation("com.google.guava:guava:31.1-jre")
28+
implementation("com.google.code.gson:gson:2.10.1")
29+
implementation("org.projectlombok:lombok:1.18.30")
30+
implementation("org.springframework.boot:spring-boot-starter")
31+
}
32+
33+
application {
34+
// Define the main class for the application.
35+
mainClass.set("com.codedifferently.lesson14.Lesson14")
36+
}
37+
38+
tasks.named<Test>("test") {
39+
// Use JUnit Platform for unit tests.
40+
useJUnitPlatform()
41+
}
42+
43+
44+
configure<com.diffplug.gradle.spotless.SpotlessExtension> {
45+
46+
format("misc", {
47+
// define the files to apply `misc` to
48+
target("*.gradle", ".gitattributes", ".gitignore")
49+
50+
// define the steps to apply to those files
51+
trimTrailingWhitespace()
52+
indentWithTabs() // or spaces. Takes an integer argument if you don't like 4
53+
endWithNewline()
54+
})
55+
56+
java {
57+
// don't need to set target, it is inferred from java
58+
59+
// apply a specific flavor of google-java-format
60+
googleJavaFormat()
61+
// fix formatting of type annotations
62+
formatAnnotations()
63+
}
64+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.codedifferently.lesson14;
2+
3+
public class Lesson14 {
4+
5+
public static void main(String[] args) {
6+
System.out.println("The permutations of 'abc' are:");
7+
printPermutations("abc", "");
8+
9+
System.out.println();
10+
System.out.print("The reverse of 'abcd' is: ");
11+
System.out.println(reverseString("abc"));
12+
}
13+
14+
/**
15+
* Prints all the permutations of a string.
16+
*
17+
* @param value The string to permute.
18+
* @param answer The current permutation.
19+
*/
20+
public static void printPermutations(String value, String answer) {
21+
if (value.length() == 0) {
22+
System.out.println(answer);
23+
return;
24+
}
25+
26+
for (int i = 0; i < value.length(); i++) {
27+
char ch = value.charAt(i);
28+
String left = value.substring(0, i);
29+
String right = value.substring(i + 1);
30+
String rest = left + right;
31+
printPermutations(rest, answer + ch);
32+
}
33+
}
34+
35+
/**
36+
* Reverses a string by swapping the front half of the characters with the back half.
37+
*
38+
* @param input The string to reverse.
39+
* @return The reversed string.
40+
*/
41+
public static String reverseString(String input) {
42+
if (input.length() == 0) {
43+
return input;
44+
}
45+
46+
char[] charArray = input.toCharArray();
47+
48+
for (int i = 0; i < charArray.length / 2; i++) {
49+
// Compute the corresponding index from the back of the string.
50+
var j = charArray.length - i - 1;
51+
swapCharacters(charArray, i, j);
52+
}
53+
54+
return new String(charArray);
55+
}
56+
57+
/**
58+
* Swaps the characters in the provided character array.
59+
*
60+
* @param charArray
61+
* @param i
62+
* @param j
63+
*/
64+
private static void swapCharacters(char[] charArray, int i, int j) {
65+
Character temp = charArray[i];
66+
charArray[i] = charArray[j];
67+
charArray[j] = temp;
68+
}
69+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.codedifferently.lesson14;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
class Lesson14Test {
8+
9+
@Test
10+
void testCanInstantiate() {
11+
assertThat(new Lesson14()).isNotNull();
12+
}
13+
}
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
distributionBase=GRADLE_USER_HOME
2+
distributionPath=wrapper/dists
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-8.0.2-bin.zip
4+
networkTimeout=10000
5+
zipStoreBase=GRADLE_USER_HOME
6+
zipStorePath=wrapper/dists

0 commit comments

Comments
 (0)