forked from code-differently/code-differently-24-q1
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adds Lesson 14 content and homework assignment (code-differentl…
…y#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
- Loading branch information
1 parent
cfa0266
commit 971d16f
Showing
74 changed files
with
3,694 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
name: Check Lesson 14 Pull Request | ||
|
||
on: | ||
pull_request: | ||
branches: [ "main" ] | ||
paths: | ||
- "lesson_14/db/**" | ||
|
||
jobs: | ||
build: | ||
|
||
runs-on: ubuntu-latest | ||
permissions: | ||
contents: read | ||
|
||
steps: | ||
- uses: actions/checkout@v4 | ||
- name: Set up JDK 17 | ||
uses: actions/setup-java@v4 | ||
with: | ||
java-version: '17' | ||
distribution: 'temurin' | ||
|
||
# Configure Gradle for optimal use in GiHub Actions, including caching of downloaded dependencies. | ||
# See: https://github.com/gradle/actions/blob/main/setup-gradle/README.md | ||
- name: Setup Gradle | ||
uses: gradle/actions/setup-gradle@417ae3ccd767c252f5661f1ace9f835f9654f2b5 # v3.1.0 | ||
|
||
- name: Build Lesson 14 with Gradle Wrapper | ||
working-directory: ./lesson_14/db | ||
run: ./gradlew check | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -140,3 +140,11 @@ dist | |
.pnp.* | ||
|
||
.DS_Store | ||
|
||
# Python | ||
*.pyc | ||
*.pyo | ||
*.pyd | ||
__pycache__/ | ||
venv/ | ||
pip-log.txt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# Lesson 14 | ||
|
||
## Homework | ||
|
||
* Memorize the `printPermutations` and `reverseString` methods in [algos_app][algos-app]. | ||
* [Extra credit] Complete [Loading the Library, Part II](#loading-the-library-part-ii) assignment. | ||
|
||
## Loading The Library, Part II | ||
|
||
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. | ||
|
||
To build familiarity in working with databases, you are charged with the following tasks: | ||
|
||
* 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. | ||
* A `SELECT` query that returns the counts of media items by type. | ||
* A `SELECT` query that returns the sum of total pages checked out by guests. | ||
* A `SELECT` query that shows all 5 guests and any corresponding records in the `checked_out_items` table. | ||
* 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. | ||
|
||
As before, you can run the app from the console using the following command: | ||
|
||
```bash | ||
./gradlew run --console=plain | ||
``` | ||
|
||
[algos-app]: ./algos/algos_app/src/main/java/com/codedifferently/lesson14/Lesson14.java | ||
[queries-dir]: ./db/db_app/src/main/resources/queries/ | ||
[library-app]: ./db/db_app/src/main/java/com/codedifferently/lesson14/cli/LibraryApp.java#L26 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# | ||
# https://help.github.com/articles/dealing-with-line-endings/ | ||
# | ||
# Linux start script should use lf | ||
/gradlew text eol=lf | ||
|
||
# These are Windows script files and should use crlf | ||
*.bat text eol=crlf | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Ignore Gradle project-specific cache directory | ||
.gradle | ||
|
||
# Ignore Gradle build output directory | ||
build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
plugins { | ||
// Apply the application plugin to add support for building a CLI application in Java. | ||
application | ||
eclipse | ||
id("com.diffplug.spotless") version "6.25.0" | ||
id("org.springframework.boot") version "3.2.2" | ||
id("com.adarshr.test-logger") version "4.0.0" | ||
} | ||
|
||
apply(plugin = "io.spring.dependency-management") | ||
|
||
repositories { | ||
// Use Maven Central for resolving dependencies. | ||
mavenCentral() | ||
} | ||
|
||
dependencies { | ||
// Use JUnit Jupiter for testing. | ||
testImplementation("com.codedifferently.instructional:instructional-lib") | ||
testImplementation("org.junit.jupiter:junit-jupiter:5.9.1") | ||
testImplementation("org.springframework.boot:spring-boot-starter-test") | ||
testImplementation("org.assertj:assertj-core:3.25.1") | ||
testImplementation("at.favre.lib:bcrypt:0.10.2") | ||
|
||
// This dependency is used by the application. | ||
implementation("com.codedifferently.instructional:instructional-lib") | ||
implementation("com.google.guava:guava:31.1-jre") | ||
implementation("com.google.code.gson:gson:2.10.1") | ||
implementation("org.projectlombok:lombok:1.18.30") | ||
implementation("org.springframework.boot:spring-boot-starter") | ||
} | ||
|
||
application { | ||
// Define the main class for the application. | ||
mainClass.set("com.codedifferently.lesson14.Lesson14") | ||
} | ||
|
||
tasks.named<Test>("test") { | ||
// Use JUnit Platform for unit tests. | ||
useJUnitPlatform() | ||
} | ||
|
||
|
||
configure<com.diffplug.gradle.spotless.SpotlessExtension> { | ||
|
||
format("misc", { | ||
// define the files to apply `misc` to | ||
target("*.gradle", ".gitattributes", ".gitignore") | ||
|
||
// define the steps to apply to those files | ||
trimTrailingWhitespace() | ||
indentWithTabs() // or spaces. Takes an integer argument if you don't like 4 | ||
endWithNewline() | ||
}) | ||
|
||
java { | ||
// don't need to set target, it is inferred from java | ||
|
||
// apply a specific flavor of google-java-format | ||
googleJavaFormat() | ||
// fix formatting of type annotations | ||
formatAnnotations() | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
lesson_14/algos/algos_app/src/main/java/com/codedifferently/lesson14/Lesson14.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package com.codedifferently.lesson14; | ||
|
||
public class Lesson14 { | ||
|
||
public static void main(String[] args) { | ||
System.out.println("The permutations of 'abc' are:"); | ||
printPermutations("abc", ""); | ||
|
||
System.out.println(); | ||
System.out.print("The reverse of 'abcd' is: "); | ||
System.out.println(reverseString("abc")); | ||
} | ||
|
||
/** | ||
* Prints all the permutations of a string. | ||
* | ||
* @param value The string to permute. | ||
* @param answer The current permutation. | ||
*/ | ||
public static void printPermutations(String value, String answer) { | ||
if (value.length() == 0) { | ||
System.out.println(answer); | ||
return; | ||
} | ||
|
||
for (int i = 0; i < value.length(); i++) { | ||
char ch = value.charAt(i); | ||
String left = value.substring(0, i); | ||
String right = value.substring(i + 1); | ||
String rest = left + right; | ||
printPermutations(rest, answer + ch); | ||
} | ||
} | ||
|
||
/** | ||
* Reverses a string by swapping the front half of the characters with the back half. | ||
* | ||
* @param input The string to reverse. | ||
* @return The reversed string. | ||
*/ | ||
public static String reverseString(String input) { | ||
if (input.length() == 0) { | ||
return input; | ||
} | ||
|
||
char[] charArray = input.toCharArray(); | ||
|
||
for (int i = 0; i < charArray.length / 2; i++) { | ||
// Compute the corresponding index from the back of the string. | ||
var j = charArray.length - i - 1; | ||
swapCharacters(charArray, i, j); | ||
} | ||
|
||
return new String(charArray); | ||
} | ||
|
||
/** | ||
* Swaps the characters in the provided character array. | ||
* | ||
* @param charArray | ||
* @param i | ||
* @param j | ||
*/ | ||
private static void swapCharacters(char[] charArray, int i, int j) { | ||
Character temp = charArray[i]; | ||
charArray[i] = charArray[j]; | ||
charArray[j] = temp; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
lesson_14/algos/algos_app/src/test/java/com/codedifferently/lesson14/Lesson14Test.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.codedifferently.lesson14; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
class Lesson14Test { | ||
|
||
@Test | ||
void testCanInstantiate() { | ||
assertThat(new Lesson14()).isNotNull(); | ||
} | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
distributionBase=GRADLE_USER_HOME | ||
distributionPath=wrapper/dists | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-8.0.2-bin.zip | ||
networkTimeout=10000 | ||
zipStoreBase=GRADLE_USER_HOME | ||
zipStorePath=wrapper/dists |
Oops, something went wrong.