Skip to content

Commit

Permalink
feat: adds Lesson 14 content and homework assignment (code-differentl…
Browse files Browse the repository at this point in the history
…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
anthonydmays committed Mar 29, 2024
1 parent cfa0266 commit 971d16f
Show file tree
Hide file tree
Showing 74 changed files with 3,694 additions and 2 deletions.
6 changes: 4 additions & 2 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"ghcr.io/devcontainers/features/node:1": {},
"ghcr.io/devcontainers/features/python:1": {},
"ghcr.io/devcontainers-contrib/features/ts-node:1": {},
"ghcr.io/devcontainers/features/sshd:1": {}
"ghcr.io/devcontainers/features/sshd:1": {},
"ghcr.io/warrenbuckley/codespace-features/sqlite:1": {}
},
"portsAttributes": {
"80": {
Expand All @@ -32,7 +33,8 @@
"ms-vscode.vscode-typescript-next",
"ms-dotnettools.csdevkit",
"ritwickdey.LiveServer",
"mechatroner.rainbow-csv"
"mechatroner.rainbow-csv",
"alexcvzz.vscode-sqlite"
]
}
}
Expand Down
33 changes: 33 additions & 0 deletions .github/workflows/check_lesson_14_pr.yml
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


16 changes: 16 additions & 0 deletions .github/workflows/check_push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ on:
- "lesson_10/examples/**"
- "lesson_10/quiz/**"
- "lesson_10/solid/**"
- "lesson_12/io/**"
- "lesson_13/bank/**"
- "lesson_14/algos/**"
- "lesson_14/db/**"

jobs:
build:
Expand Down Expand Up @@ -85,6 +89,18 @@ jobs:
working-directory: ./lesson_12/io
run: ./gradlew assemble && ./gradlew spotlessCheck

- name: Build Lesson 13 with Gradle Wrapper
working-directory: ./lesson_13/bank
run: ./gradlew check

- name: Build Lesson 14 Algos with Gradle Wrapper
working-directory: ./lesson_14/algos
run: ./gradlew check

- name: Build Lesson 14 DB with Gradle Wrapper
working-directory: ./lesson_14/db
run: ./gradlew check

- name: Build Shared Lib with Gradle Wrapper
working-directory: ./lib/java/codedifferently-instructional
run: ./gradlew check
Expand Down
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,11 @@ dist
.pnp.*

.DS_Store

# Python
*.pyc
*.pyo
*.pyd
__pycache__/
venv/
pip-log.txt
28 changes: 28 additions & 0 deletions lesson_14/README.md
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
9 changes: 9 additions & 0 deletions lesson_14/algos/.gitattributes
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

5 changes: 5 additions & 0 deletions lesson_14/algos/.gitignore
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
64 changes: 64 additions & 0 deletions lesson_14/algos/algos_app/build.gradle.kts
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()
}
}
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;
}
}
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 added lesson_14/algos/gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
6 changes: 6 additions & 0 deletions lesson_14/algos/gradle/wrapper/gradle-wrapper.properties
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

0 comments on commit 971d16f

Please sign in to comment.