diff --git a/.github/workflows/check_lesson_25_java_pr.yaml b/.github/workflows/check_lesson_25_java_pr.yaml new file mode 100644 index 000000000..8103d49f2 --- /dev/null +++ b/.github/workflows/check_lesson_25_java_pr.yaml @@ -0,0 +1,28 @@ +name: Check Lesson 25 Java Pull Request + +on: + pull_request: + branches: [ "main" ] + paths: + - "lesson_25/db/**" + +jobs: + build: + + runs-on: ubuntu-latest + permissions: + contents: read + pull-requests: write + + steps: + - uses: actions/checkout@v4 + + - name: Set up JDK + uses: actions/setup-java@v4 + with: + java-version: '21' + distribution: 'temurin' + + - name: Build Lesson 26 with Java + working-directory: ./lesson_25/db + run: ./gradlew check \ No newline at end of file diff --git a/lesson_25/README.md b/lesson_25/README.md index 8099277b7..cb2131689 100644 --- a/lesson_25/README.md +++ b/lesson_25/README.md @@ -13,4 +13,27 @@ Please review the following resources before lecture: ## Homework -- TODO(anthonydmays): Add more information. \ No newline at end of file +- [ ] 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 as we did in [lesson_10](/lesson_10/), 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][sqlite-db] 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 (see `LibraryGuestModel` and `LibraryGuestRepository` as examples). 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 +``` + +[queries-dir]: ./db/db_app/src/main/resources/queries/ +[sqlite-db]: ./db/db_app/src/main/resources/sqlite/ +[library-app]: ./db/db_app/src/main/java/com/codedifferently/lesson25/cli/LibraryApp.java#L26 diff --git a/lesson_25/createdb/createdb.py b/lesson_25/createdb/createdb.py new file mode 100644 index 000000000..a8af9b776 --- /dev/null +++ b/lesson_25/createdb/createdb.py @@ -0,0 +1,23 @@ +import os +import pandas as pd +import numpy as np +import sqlite3 + +# Step 1: Load the CSV file into a pandas DataFrame +media_items_df = pd.read_csv('/workspaces/code-society-25-2/lesson_12/io/io_app/src/main/resources/csv/media_items.csv') +guests_df = pd.read_csv('/workspaces/code-society-25-2/lesson_12/io/io_app/src/main/resources/csv/guests.csv') +checked_out_items_df = pd.read_csv('/workspaces/code-society-25-2/lesson_12/io/io_app/src/main/resources/csv/checked_out_items.csv') +checked_out_items_df['due_date'] = pd.to_datetime(checked_out_items_df['due_date']).values.astype(np.int64) + +# Step 2: Create a connection to the SQLite database +# Note: This will create the database file if it doesn't exist already +os.makedirs('/workspaces/code-society-25-2/lesson_25/db/db_app/src/main/resources/sqlite/', exist_ok=True) +conn = sqlite3.connect('../db/db_app/src/main/resources/sqlite/data.db') + +# Step 3: Write the DataFrame to the SQLite database +media_items_df.to_sql('media_items', conn, if_exists='replace', index=False) +guests_df.to_sql('guests', conn, if_exists='replace', index=False) +checked_out_items_df.to_sql('checked_out_items', conn, if_exists='replace', index=False) + +# Don't forget to close the connection +conn.close() \ No newline at end of file diff --git a/lesson_25/createdb/requirements.txt b/lesson_25/createdb/requirements.txt new file mode 100644 index 000000000..1411a4a0b --- /dev/null +++ b/lesson_25/createdb/requirements.txt @@ -0,0 +1 @@ +pandas \ No newline at end of file diff --git a/lesson_25/db/.gitattributes b/lesson_25/db/.gitattributes new file mode 100644 index 000000000..097f9f98d --- /dev/null +++ b/lesson_25/db/.gitattributes @@ -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 + diff --git a/lesson_25/db/.gitignore b/lesson_25/db/.gitignore new file mode 100644 index 000000000..1b6985c00 --- /dev/null +++ b/lesson_25/db/.gitignore @@ -0,0 +1,5 @@ +# Ignore Gradle project-specific cache directory +.gradle + +# Ignore Gradle build output directory +build diff --git a/lesson_25/db/db_app/build.gradle.kts b/lesson_25/db/db_app/build.gradle.kts new file mode 100644 index 000000000..88c3a8b1d --- /dev/null +++ b/lesson_25/db/db_app/build.gradle.kts @@ -0,0 +1,79 @@ +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.4.0" + 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.11.3") + testImplementation("org.springframework.boot:spring-boot-starter-test") + testImplementation("org.assertj:assertj-core:3.26.3") + testImplementation("at.favre.lib:bcrypt:0.10.2") + testCompileOnly("org.projectlombok:lombok:1.18.38") + testAnnotationProcessor("org.projectlombok:lombok:1.18.38") + + // This dependency is used by the application. + implementation("com.codedifferently.instructional:instructional-lib") + implementation("com.google.guava:guava:33.3.1-jre") + implementation("com.google.code.gson:gson:2.11.0") + implementation("commons-cli:commons-cli:1.6.0") + implementation("org.springframework.boot:spring-boot-starter") + implementation("org.springframework.boot:spring-boot-starter-data-jpa") + implementation("com.fasterxml.jackson.core:jackson-databind:2.13.0") + implementation("com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.12.3") + implementation("com.opencsv:opencsv:5.9") + implementation("org.apache.commons:commons-csv:1.10.0") + implementation("org.xerial:sqlite-jdbc:3.36.0") + implementation("org.hibernate.orm:hibernate-community-dialects:6.2.7.Final") + compileOnly("org.projectlombok:lombok:1.18.38") + annotationProcessor("org.projectlombok:lombok:1.18.38") +} + +application { + // Define the main class for the application. + mainClass.set("com.codedifferently.lesson25.Lesson25") +} + +tasks.named("run") { + standardInput = System.`in` +} + +tasks.named("test") { + // Use JUnit Platform for unit tests. + useJUnitPlatform() +} + + +configure { + + 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() + } +} diff --git a/lesson_25/db/db_app/lombok.config b/lesson_25/db/db_app/lombok.config new file mode 100644 index 000000000..6aa51d71e --- /dev/null +++ b/lesson_25/db/db_app/lombok.config @@ -0,0 +1,2 @@ +# This file is generated by the 'io.freefair.lombok' Gradle plugin +config.stopBubbling = true diff --git a/lesson_25/db/db_app/src/main/java/com/codedifferently/lesson25/Lesson25.java b/lesson_25/db/db_app/src/main/java/com/codedifferently/lesson25/Lesson25.java new file mode 100644 index 000000000..9de75af9f --- /dev/null +++ b/lesson_25/db/db_app/src/main/java/com/codedifferently/lesson25/Lesson25.java @@ -0,0 +1,39 @@ +package com.codedifferently.lesson25; + +import com.codedifferently.lesson25.cli.LibraryApp; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.CommandLineRunner; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Configuration; + +@Configuration +@SpringBootApplication(scanBasePackages = "com.codedifferently") +public class Lesson25 implements CommandLineRunner { + + @Autowired private LibraryApp libraryApp; + + public static void main(String[] args) { + var application = new SpringApplication(Lesson25.class); + application.run(args); + } + + @Override + public void run(String... args) throws Exception { + // Don't run as an app if we're running as a JUnit test. + if (isJUnitTest()) { + return; + } + + libraryApp.run(args); + } + + private static boolean isJUnitTest() { + for (StackTraceElement element : Thread.currentThread().getStackTrace()) { + if (element.getClassName().startsWith("org.junit.")) { + return true; + } + } + return false; + } +} diff --git a/lesson_25/db/db_app/src/main/java/com/codedifferently/lesson25/cli/LibraryApp.java b/lesson_25/db/db_app/src/main/java/com/codedifferently/lesson25/cli/LibraryApp.java new file mode 100644 index 000000000..44053bd61 --- /dev/null +++ b/lesson_25/db/db_app/src/main/java/com/codedifferently/lesson25/cli/LibraryApp.java @@ -0,0 +1,196 @@ +package com.codedifferently.lesson25.cli; + +import com.codedifferently.lesson25.factory.LibraryDataLoader; +import com.codedifferently.lesson25.factory.LibraryDbDataLoader; +import com.codedifferently.lesson25.factory.LibraryFactory; +import com.codedifferently.lesson25.library.Book; +import com.codedifferently.lesson25.library.Library; +import com.codedifferently.lesson25.library.LibraryInfo; +import com.codedifferently.lesson25.library.MediaItem; +import com.codedifferently.lesson25.library.search.SearchCriteria; +import java.util.Map; +import java.util.Scanner; +import java.util.Set; +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.CommandLineParser; +import org.apache.commons.cli.DefaultParser; +import org.apache.commons.cli.HelpFormatter; +import org.apache.commons.cli.Option; +import org.apache.commons.cli.Options; +import org.apache.commons.cli.ParseException; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +@Service +public final class LibraryApp { + + @Autowired private LibraryDbDataLoader defaultLibraryDataLoader; + + public void run(String[] args) throws Exception { + // Load the library using the specified loader from the command line or the default. + LibraryDataLoader loader = getLoaderOrDefault(args, defaultLibraryDataLoader); + Library library = LibraryFactory.createWithLoader(loader); + + // Show stats about the loaded library to the user. + printLibraryInfo(library); + + try (var scanner = new Scanner(System.in)) { + LibraryCommand command; + // Main application loop. + while ((command = promptForCommand(scanner)) != LibraryCommand.EXIT) { + switch (command) { + case SEARCH -> doSearch(scanner, library); + default -> System.out.println("\nNot ready yet, coming soon!"); + } + } + } + } + + private void printLibraryInfo(Library library) { + LibraryInfo info = library.getInfo(); + Map> checkedOutItemsByGuest = info.getCheckedOutItemsByGuest(); + int numCheckedOutItems = checkedOutItemsByGuest.values().stream().mapToInt(Set::size).sum(); + System.out.println(); + System.out.println("========================================"); + System.out.println("Library id: " + library.getId()); + System.out.println("Number of items: " + info.getItems().size()); + System.out.println("Number of guests: " + info.getGuests().size()); + System.out.println("Number of checked out items: " + numCheckedOutItems); + System.out.println("========================================"); + System.out.println(); + } + + private static LibraryDataLoader getLoaderOrDefault( + String[] args, LibraryDataLoader defaultLoader) throws Exception { + String loaderType = getLoaderFromCommandLine(args); + return loaderType == null + ? defaultLoader + : Class.forName(loaderType) + .asSubclass(LibraryDataLoader.class) + .getDeclaredConstructor() + .newInstance(); + } + + private static String getLoaderFromCommandLine(String[] args) throws IllegalArgumentException { + Options options = new Options(); + Option input = new Option("l", "loader", true, "data loader type"); + input.setRequired(false); + options.addOption(input); + CommandLineParser parser = new DefaultParser(); + HelpFormatter formatter = new HelpFormatter(); + try { + CommandLine cmd = parser.parse(options, args); + return cmd.getOptionValue("loader"); + } catch (ParseException e) { + System.out.println(); + System.out.println(e.getMessage()); + formatter.printHelp("utility-name", options); + + System.exit(1); + } + return null; + } + + private static LibraryCommand promptForCommand(Scanner scanner) { + var command = LibraryCommand.UNKNOWN; + while (command == LibraryCommand.UNKNOWN) { + printMenu(); + var input = scanner.nextLine(); + try { + command = LibraryCommand.fromValue(Integer.parseInt(input.trim())); + } catch (IllegalArgumentException e) { + System.out.println("Invalid command: " + input); + } + } + return command; + } + + private static void printMenu() { + System.out.println("\nEnter the number of the desired command:"); + System.out.println("1) << EXIT"); + System.out.println("2) SEARCH"); + System.out.println("3) CHECKOUT"); + System.out.println("4) RETURN"); + System.out.print("command> "); + } + + private void doSearch(Scanner scanner, Library library) { + LibrarySearchCommand command = promptForSearchCommand(scanner); + if (command == LibrarySearchCommand.RETURN) { + return; + } + SearchCriteria criteria = getSearchCriteria(scanner, command); + Set results = library.search(criteria); + printSearchResults(results); + } + + private LibrarySearchCommand promptForSearchCommand(Scanner scanner) { + var command = LibrarySearchCommand.UNKNOWN; + while (command == LibrarySearchCommand.UNKNOWN) { + printSearchMenu(); + var input = scanner.nextLine(); + try { + command = LibrarySearchCommand.fromValue(Integer.parseInt(input.trim())); + } catch (IllegalArgumentException e) { + System.out.println("Invalid command: " + input); + } + } + return command; + } + + private void printSearchMenu() { + System.out.println("\nEnter the number of the desired search criteria:"); + System.out.println("1) << RETURN"); + System.out.println("2) TITLE"); + System.out.println("3) AUTHOR"); + System.out.println("4) TYPE"); + System.out.print("search> "); + } + + private SearchCriteria getSearchCriteria(Scanner scanner, LibrarySearchCommand command) { + System.out.println(); + switch (command) { + case TITLE -> { + System.out.println("Enter the title to search for: "); + System.out.print("title> "); + var title = scanner.nextLine(); + return SearchCriteria.builder().title(title).build(); + } + case AUTHOR -> { + System.out.println("Enter the author to search for: "); + System.out.print("author> "); + var author = scanner.nextLine(); + return SearchCriteria.builder().author(author).build(); + } + case TYPE -> { + System.out.println("Enter the type to search for: "); + System.out.print("type> "); + var type = scanner.nextLine(); + return SearchCriteria.builder().type(type).build(); + } + default -> System.out.println("Invalid search command: " + command); + } + return null; + } + + private void printSearchResults(Set results) { + System.out.println(); + + if (results.isEmpty()) { + System.out.println("No results found."); + return; + } + + System.out.println("Search results:\n"); + for (MediaItem item : results) { + System.out.println("ID: " + item.getId()); + System.out.println("TITLE: " + item.getTitle()); + if (item instanceof Book book) { + System.out.println("AUTHOR(S): " + String.join(", ", book.getAuthors())); + } + System.out.println("TYPE: " + item.getType().toUpperCase()); + System.out.println(); + } + System.out.println("Found " + results.size() + " result(s).\n"); + } +} diff --git a/lesson_25/db/db_app/src/main/java/com/codedifferently/lesson25/cli/LibraryCommand.java b/lesson_25/db/db_app/src/main/java/com/codedifferently/lesson25/cli/LibraryCommand.java new file mode 100644 index 000000000..2d49d5592 --- /dev/null +++ b/lesson_25/db/db_app/src/main/java/com/codedifferently/lesson25/cli/LibraryCommand.java @@ -0,0 +1,28 @@ +package com.codedifferently.lesson25.cli; + +public enum LibraryCommand { + UNKNOWN(0), + EXIT(1), + SEARCH(2), + CHECKOUT(3), + RETURN(4); + + private final int value; + + LibraryCommand(int value) { + this.value = value; + } + + public int getValue() { + return value; + } + + public static LibraryCommand fromValue(int value) { + for (LibraryCommand command : LibraryCommand.values()) { + if (command.getValue() == value) { + return command; + } + } + return UNKNOWN; + } +} diff --git a/lesson_25/db/db_app/src/main/java/com/codedifferently/lesson25/cli/LibrarySearchCommand.java b/lesson_25/db/db_app/src/main/java/com/codedifferently/lesson25/cli/LibrarySearchCommand.java new file mode 100644 index 000000000..df180eaa5 --- /dev/null +++ b/lesson_25/db/db_app/src/main/java/com/codedifferently/lesson25/cli/LibrarySearchCommand.java @@ -0,0 +1,28 @@ +package com.codedifferently.lesson25.cli; + +public enum LibrarySearchCommand { + UNKNOWN(0), + RETURN(1), + TITLE(2), + AUTHOR(3), + TYPE(4); + + private final int value; + + LibrarySearchCommand(int value) { + this.value = value; + } + + public int getValue() { + return value; + } + + public static LibrarySearchCommand fromValue(int value) { + for (LibrarySearchCommand criteria : LibrarySearchCommand.values()) { + if (criteria.getValue() == value) { + return criteria; + } + } + return UNKNOWN; + } +} diff --git a/lesson_25/db/db_app/src/main/java/com/codedifferently/lesson25/factory/LibraryCsvDataLoader.java b/lesson_25/db/db_app/src/main/java/com/codedifferently/lesson25/factory/LibraryCsvDataLoader.java new file mode 100644 index 000000000..4c3f99049 --- /dev/null +++ b/lesson_25/db/db_app/src/main/java/com/codedifferently/lesson25/factory/LibraryCsvDataLoader.java @@ -0,0 +1,126 @@ +package com.codedifferently.lesson25.factory; + +import com.codedifferently.lesson25.models.CheckoutModel; +import com.codedifferently.lesson25.models.LibraryDataModel; +import com.codedifferently.lesson25.models.LibraryGuestModel; +import com.codedifferently.lesson25.models.MediaItemModel; +import java.io.FileReader; +import java.io.IOException; +import java.time.Instant; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.UUID; +import org.apache.commons.csv.CSVFormat; +import org.apache.commons.csv.CSVRecord; +import org.springframework.core.io.ClassPathResource; +import org.springframework.stereotype.Service; + +/** An object that loads data from a CSV and returns a LibraryDataModel object. */ +@Service +public final class LibraryCsvDataLoader implements LibraryDataLoader { + + private static final String MEDIA_ITEMS_CSV_PATH = "csv/media_items.csv"; + private static final String GUESTS_CSV_PATH = "csv/guests.csv"; + private static final String CHECKED_OUT_ITEMS_CSV_PATH = "csv/checked_out_items.csv"; + + @Override + public LibraryDataModel loadData() throws IOException { + var model = new LibraryDataModel(); + model.mediaItems = loadMediaItemsFromCsv(MEDIA_ITEMS_CSV_PATH); + model.guests = loadGuestsFromCsv(GUESTS_CSV_PATH, CHECKED_OUT_ITEMS_CSV_PATH); + return model; + } + + private List loadMediaItemsFromCsv(String filePath) throws IOException { + List mediaItems = new ArrayList<>(); + + try (var reader = new FileReader(new ClassPathResource(filePath).getFile()); + var csvParser = CSVFormat.DEFAULT.withFirstRecordAsHeader().parse(reader)) { + for (CSVRecord csvRecord : csvParser) { + var item = new MediaItemModel(); + + item.type = csvRecord.get("type"); + item.id = UUID.fromString(csvRecord.get("id")); + item.title = csvRecord.get("title"); + item.isbn = csvRecord.get("isbn"); + item.authors = List.of(csvRecord.get("authors").split(", ")); + item.pages = parseIntOrDefault(csvRecord.get("pages"), 0); + item.runtime = parseIntOrDefault(csvRecord.get("runtime"), 0); + item.edition = csvRecord.get("edition"); + + mediaItems.add(item); + } + + } catch (IOException e) { + return new ArrayList<>(); + } + + return mediaItems; + } + + private int parseIntOrDefault(String value, int defaultVal) { + try { + return Integer.parseInt(value); + } catch (NumberFormatException e) { + return defaultVal; + } + } + + private List loadGuestsFromCsv( + String guestsCsvPath, String checkedOutCsvPath) { + List guests = loadGuestRecordsFromCsv(guestsCsvPath); + Map> checkedOutItems = loadCheckoutsFromCsv(checkedOutCsvPath); + for (LibraryGuestModel guest : guests) { + if (checkedOutItems.containsKey(guest.email)) { + guest.checkedOutItems = checkedOutItems.get(guest.email); + } else { + guest.checkedOutItems = new ArrayList<>(); + } + } + return guests; + } + + private List loadGuestRecordsFromCsv(String guestsCsvPath) { + List guests = new ArrayList<>(); + + try (var reader = new FileReader(new ClassPathResource(guestsCsvPath).getFile()); + var csvParser = CSVFormat.DEFAULT.withFirstRecordAsHeader().parse(reader)) { + for (CSVRecord csvRecord : csvParser) { + var guest = new LibraryGuestModel(); + + guest.type = csvRecord.get("type"); + guest.name = csvRecord.get("name"); + guest.email = csvRecord.get("email"); + + guests.add(guest); + } + } catch (IOException e) { + return new ArrayList<>(); + } + + return guests; + } + + private Map> loadCheckoutsFromCsv(String checkedOutCsvPath) { + Map> checkoutsByGuestEmail = new HashMap<>(); + + try (var reader = new FileReader(new ClassPathResource(checkedOutCsvPath).getFile()); + var csvParser = CSVFormat.DEFAULT.withFirstRecordAsHeader().parse(reader)) { + for (CSVRecord csvRecord : csvParser) { + var checkout = new CheckoutModel(); + + checkout.itemId = UUID.fromString(csvRecord.get("item_id")); + checkout.dueDate = Instant.parse(csvRecord.get("due_date")); + + String guestEmail = csvRecord.get("email"); + checkoutsByGuestEmail.computeIfAbsent(guestEmail, e -> new ArrayList<>()).add(checkout); + } + } catch (IOException e) { + return new HashMap<>(); + } + + return checkoutsByGuestEmail; + } +} diff --git a/lesson_25/db/db_app/src/main/java/com/codedifferently/lesson25/factory/LibraryDataLoader.java b/lesson_25/db/db_app/src/main/java/com/codedifferently/lesson25/factory/LibraryDataLoader.java new file mode 100644 index 000000000..99fb7fb58 --- /dev/null +++ b/lesson_25/db/db_app/src/main/java/com/codedifferently/lesson25/factory/LibraryDataLoader.java @@ -0,0 +1,16 @@ +package com.codedifferently.lesson25.factory; + +import com.codedifferently.lesson25.models.LibraryDataModel; +import java.io.IOException; + +/** An object that loads data from a source and returns a LibraryDataModel object. */ +public interface LibraryDataLoader { + + /** + * Load data from a source and return a LibraryDataModel object. + * + * @return A LibraryDataModel object. + * @throws IOException if an I/O error occurs. + */ + public LibraryDataModel loadData() throws IOException; +} diff --git a/lesson_25/db/db_app/src/main/java/com/codedifferently/lesson25/factory/LibraryDbDataLoader.java b/lesson_25/db/db_app/src/main/java/com/codedifferently/lesson25/factory/LibraryDbDataLoader.java new file mode 100644 index 000000000..b897de124 --- /dev/null +++ b/lesson_25/db/db_app/src/main/java/com/codedifferently/lesson25/factory/LibraryDbDataLoader.java @@ -0,0 +1,26 @@ +package com.codedifferently.lesson25.factory; + +import com.codedifferently.lesson25.models.LibraryDataModel; +import com.codedifferently.lesson25.repository.LibraryGuestRepository; +import com.codedifferently.lesson25.repository.MediaItemRepository; +import java.io.IOException; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +/** A data loader that loads library data from a database. */ +@Service +public final class LibraryDbDataLoader implements LibraryDataLoader { + + @Autowired private MediaItemRepository mediaItemsRepository; + @Autowired private LibraryGuestRepository libraryGuestRepository; + + @Override + public LibraryDataModel loadData() throws IOException { + var model = new LibraryDataModel(); + + model.mediaItems = mediaItemsRepository.findAll(); + model.guests = libraryGuestRepository.findAll(); + + return model; + } +} diff --git a/lesson_25/db/db_app/src/main/java/com/codedifferently/lesson25/factory/LibraryFactory.java b/lesson_25/db/db_app/src/main/java/com/codedifferently/lesson25/factory/LibraryFactory.java new file mode 100644 index 000000000..c1b892308 --- /dev/null +++ b/lesson_25/db/db_app/src/main/java/com/codedifferently/lesson25/factory/LibraryFactory.java @@ -0,0 +1,103 @@ +package com.codedifferently.lesson25.factory; + +import com.codedifferently.lesson25.library.Librarian; +import com.codedifferently.lesson25.library.Library; +import com.codedifferently.lesson25.library.LibraryGuest; +import com.codedifferently.lesson25.library.MediaItem; +import com.codedifferently.lesson25.models.CheckoutModel; +import com.codedifferently.lesson25.models.LibraryDataModel; +import java.io.IOException; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.UUID; + +/** A factory class that creates a Library object with a LibraryDataLoader object. */ +public final class LibraryFactory { + + /** + * Create a Library object with a LibraryDataLoader object. + * + * @param loader A LibraryDataLoader object. + * @return A Library object. + * @throws IOException + */ + public static Library createWithLoader(LibraryDataLoader loader) throws IOException { + Library library = new Library("main-library"); + + // Load library data. + LibraryDataModel data = loader.loadData(); + + // Add guests to the library. + List guests = data.getGuests(); + addLibraryGuests(library, guests); + + // Add library items using the first librarian. + Librarian firstLibrarian = findFirstLibrarian(guests); + List mediaItems = data.getMediaItems(); + addLibraryItems(library, mediaItems, firstLibrarian); + + // Check out items from the library. + Map> checkoutsByEmail = data.getCheckoutsByEmail(); + Map mediaItemById = getMediaItemsById(mediaItems); + Map guestsByEmail = getGuestsByEmail(guests); + checkOutItems(library, checkoutsByEmail, guestsByEmail, mediaItemById); + + return library; + } + + private static Map getMediaItemsById(List mediaItems) { + Map mediaItemById = new HashMap<>(); + for (MediaItem mediaItem : mediaItems) { + mediaItemById.put(mediaItem.getId(), mediaItem); + } + return mediaItemById; + } + + private static Librarian findFirstLibrarian(List guests) { + Librarian firstLibrarian = null; + for (LibraryGuest guest : guests) { + if (guest instanceof Librarian librarian) { + firstLibrarian = librarian; + } + } + return firstLibrarian; + } + + private static void addLibraryGuests(Library library, List guests) { + for (LibraryGuest guest : guests) { + library.addLibraryGuest(guest); + } + } + + private static void addLibraryItems( + Library library, List mediaItems, Librarian firstLibrarian) { + for (MediaItem mediaItem : mediaItems) { + library.addMediaItem(mediaItem, firstLibrarian); + } + } + + private static Map getGuestsByEmail(List guests) { + Map guestByEmail = new HashMap<>(); + for (LibraryGuest guest : guests) { + guestByEmail.put(guest.getEmail(), guest); + } + return guestByEmail; + } + + private static void checkOutItems( + Library library, + Map> checkoutsByEmail, + Map guestByEmail, + Map mediaItemById) { + for (var entry : checkoutsByEmail.entrySet()) { + String email = entry.getKey(); + List checkouts = entry.getValue(); + LibraryGuest guest = guestByEmail.get(email); + for (CheckoutModel checkout : checkouts) { + MediaItem mediaItem = mediaItemById.get(checkout.itemId); + library.checkOutMediaItem(mediaItem, guest); + } + } + } +} diff --git a/lesson_25/db/db_app/src/main/java/com/codedifferently/lesson25/factory/LibraryJsonDataLoader.java b/lesson_25/db/db_app/src/main/java/com/codedifferently/lesson25/factory/LibraryJsonDataLoader.java new file mode 100644 index 000000000..e512df156 --- /dev/null +++ b/lesson_25/db/db_app/src/main/java/com/codedifferently/lesson25/factory/LibraryJsonDataLoader.java @@ -0,0 +1,24 @@ +package com.codedifferently.lesson25.factory; + +import com.codedifferently.lesson25.models.LibraryDataModel; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; +import java.io.File; +import java.io.IOException; +import org.springframework.core.io.ClassPathResource; +import org.springframework.stereotype.Service; + +/** Loads data from a JSON file and returns a LibraryDataModel object. */ +@Service +public final class LibraryJsonDataLoader implements LibraryDataLoader { + + @Override + public LibraryDataModel loadData() throws IOException { + ObjectMapper objectMapper = new ObjectMapper(); + objectMapper.registerModule(new JavaTimeModule()); + + // Load data from data.json file + File file = new ClassPathResource("json/data.json").getFile(); + return objectMapper.readValue(file, LibraryDataModel.class); + } +} diff --git a/lesson_25/db/db_app/src/main/java/com/codedifferently/lesson25/library/Book.java b/lesson_25/db/db_app/src/main/java/com/codedifferently/lesson25/library/Book.java new file mode 100644 index 000000000..906ab3aaf --- /dev/null +++ b/lesson_25/db/db_app/src/main/java/com/codedifferently/lesson25/library/Book.java @@ -0,0 +1,78 @@ +package com.codedifferently.lesson25.library; + +import java.util.List; +import java.util.UUID; + +/** Represents a book. */ +public class Book extends MediaItemBase { + + private final String isbn; + private final List authors; + private final int numberOfPages; + + /** + * Create a new book with the given title, ISBN, authors, and number of pages. + * + * @param id The ID of the book. + * @param title The title of the book. + * @param isbn The ISBN of the book. + * @param authors The authors of the book. + * @param numberOfPages The number of pages in the book. + */ + public Book(UUID id, String title, String isbn, List authors, int numberOfPages) { + super(id, title); + this.isbn = isbn; + this.authors = authors; + this.numberOfPages = numberOfPages; + } + + @Override + public String getType() { + return "book"; + } + + /** + * Get the ISBN of the book. + * + * @return The ISBN of the book. + */ + public String getIsbn() { + return this.isbn; + } + + /** + * Get the authors of the book. + * + * @return The authors of the book. + */ + public List getAuthors() { + return this.authors; + } + + /** + * Get the number of pages in the book. + * + * @return The number of pages in the book. + */ + public int getNumberOfPages() { + return this.numberOfPages; + } + + @Override + protected boolean matchesAuthor(String authorQuery) { + if (authorQuery == null) { + return true; + } + for (String author : this.getAuthors()) { + if (author.toLowerCase().contains(authorQuery.toLowerCase())) { + return true; + } + } + return false; + } + + @Override + public String toString() { + return "Book{" + "id='" + getId() + '\'' + ", title='" + getTitle() + '\'' + '}'; + } +} diff --git a/lesson_25/db/db_app/src/main/java/com/codedifferently/lesson25/library/Dvd.java b/lesson_25/db/db_app/src/main/java/com/codedifferently/lesson25/library/Dvd.java new file mode 100644 index 000000000..5653559cb --- /dev/null +++ b/lesson_25/db/db_app/src/main/java/com/codedifferently/lesson25/library/Dvd.java @@ -0,0 +1,21 @@ +package com.codedifferently.lesson25.library; + +import java.util.UUID; + +/** Represents a DVD. */ +public class Dvd extends MediaItemBase { + + public Dvd(UUID id, String title) { + super(id, title); + } + + @Override + public String getType() { + return "dvd"; + } + + @Override + public String toString() { + return "Dvd{" + "id='" + getId() + '\'' + ", title='" + getTitle() + '\'' + '}'; + } +} diff --git a/lesson_25/db/db_app/src/main/java/com/codedifferently/lesson25/library/Librarian.java b/lesson_25/db/db_app/src/main/java/com/codedifferently/lesson25/library/Librarian.java new file mode 100644 index 000000000..c67274724 --- /dev/null +++ b/lesson_25/db/db_app/src/main/java/com/codedifferently/lesson25/library/Librarian.java @@ -0,0 +1,14 @@ +package com.codedifferently.lesson25.library; + +/** Represents a librarian of a library. */ +public class Librarian extends LibraryGuestBase { + + public Librarian(String name, String email) { + super(name, email); + } + + @Override + public String toString() { + return "Librarian{" + "id='" + getId() + '\'' + ", name='" + getName() + '\'' + '}'; + } +} diff --git a/lesson_25/db/db_app/src/main/java/com/codedifferently/lesson25/library/Library.java b/lesson_25/db/db_app/src/main/java/com/codedifferently/lesson25/library/Library.java new file mode 100644 index 000000000..cc40595be --- /dev/null +++ b/lesson_25/db/db_app/src/main/java/com/codedifferently/lesson25/library/Library.java @@ -0,0 +1,224 @@ +package com.codedifferently.lesson25.library; + +import com.codedifferently.lesson25.library.exceptions.MediaItemCheckedOutException; +import com.codedifferently.lesson25.library.search.CatalogSearcher; +import com.codedifferently.lesson25.library.search.SearchCriteria; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; +import java.util.UUID; + +/** Represents a library. */ +public class Library { + + private final Map itemsById = new HashMap<>(); + private final Set checkedOutItemIds = new HashSet<>(); + private final Map> checkedOutItemsByGuest = new HashMap<>(); + private final Map guestsById = new HashMap<>(); + private final String id; + private final CatalogSearcher searcher; + + /** + * Create a new library with the given id. + * + * @param id The id of the library. + */ + public Library(String id) { + this.id = id; + this.searcher = new CatalogSearcher(this.itemsById.values()); + } + + /** + * Get the id of the library. + * + * @return The id of the library. + */ + public String getId() { + return this.id; + } + + /** + * Add a item to the library. + * + * @param item The item to add. + * @param librarian The librarian adding the item. + */ + public void addMediaItem(MediaItem item, Librarian librarian) { + this.itemsById.put(item.getId(), item); + item.setLibrary(this); + } + + /** + * Remove a item from the library. + * + * @param item The item to remove. + * @param librarian The librarian removing the item. + */ + public void removeMediaItem(MediaItem item, Librarian librarian) + throws MediaItemCheckedOutException { + if (this.isCheckedOut(item)) { + throw new MediaItemCheckedOutException("Cannot remove checked out item."); + } + this.itemsById.remove(item.getId()); + item.setLibrary(null); + } + + /** + * Search the library for items matching the given query. + * + * @param query The query to search for. + * @return The items matching the query. + */ + public Set search(SearchCriteria query) { + return new HashSet<>(this.searcher.search(query)); + } + + /** + * Add a guest to the library. + * + * @param guest The guest to add. + */ + public void addLibraryGuest(LibraryGuest guest) { + this.guestsById.put(guest.getId(), guest); + this.checkedOutItemsByGuest.put(guest.getId(), new HashSet<>()); + guest.setLibrary(this); + } + + /** + * Remove a guest from the library. + * + * @param guest The guest to remove. + */ + public void removeLibraryGuest(LibraryGuest guest) throws MediaItemCheckedOutException { + if (!this.checkedOutItemsByGuest.get(guest.getId()).isEmpty()) { + throw new MediaItemCheckedOutException("Cannot remove guest with checked out items."); + } + this.guestsById.remove(guest.getId()); + this.checkedOutItemsByGuest.remove(guest.getId()); + guest.setLibrary(null); + } + + /** + * Check out a item to a guest. + * + * @param item The item to check out. + * @param guest The guest to check out the item to. + * @return True if the item was checked out, false otherwise. + */ + public boolean checkOutMediaItem(MediaItem item, LibraryGuest guest) { + if (!this.canCheckOutMediaItem(item, guest)) { + return false; + } + this.checkedOutItemIds.add(item.getId()); + this.checkedOutItemsByGuest.get(guest.getId()).add(item); + return true; + } + + private boolean canCheckOutMediaItem(MediaItem item, LibraryGuest guest) { + if (!item.canCheckOut()) { + return false; + } + if (!this.hasMediaItem(item)) { + return false; + } + if (this.isCheckedOut(item)) { + return false; + } + return this.hasLibraryGuest(guest); + } + + /** + * Check if the library has the given item. + * + * @param item The item to check for. + * @return True if the library has the item, false otherwise. + */ + public boolean hasMediaItem(MediaItem item) { + return this.itemsById.containsKey(item.getId()); + } + + /** + * Check if the given item is checked out. + * + * @param item The item to check. + * @return True if the item is checked out, false otherwise. + */ + public boolean isCheckedOut(MediaItem item) { + return this.checkedOutItemIds.contains(item.getId()); + } + + /** + * Check if the library has the given guest. + * + * @param guest The guest to check for. + * @return True if the library has the guest, false otherwise. + */ + public boolean hasLibraryGuest(LibraryGuest guest) { + return this.guestsById.containsKey(guest.getId()); + } + + /** + * Return a item to the library. + * + * @param item The item to return. + * @param guest The guest returning the item. + * @return True if the item was returned, false otherwise. + */ + public boolean checkInMediaItem(MediaItem item, LibraryGuest guest) { + if (!this.hasMediaItem(item)) { + return false; + } + this.checkedOutItemIds.remove(item.getId()); + this.checkedOutItemsByGuest.get(guest.getId()).remove(item); + return true; + } + + /** + * Get the items checked out by a guest. + * + * @param guest The guest to get the items for. + * @return The items checked out by the guest. + */ + public Set getCheckedOutByGuest(LibraryGuest guest) { + return this.checkedOutItemsByGuest.get(guest.getId()); + } + + /** + * Get a snapshot of the library info. + * + * @return The library info. + */ + public LibraryInfo getInfo() { + Map> itemsByGuest = + this.checkedOutItemsByGuest.entrySet().stream() + .collect( + HashMap::new, + (map, entry) -> + map.put( + entry.getKey(), + Collections.unmodifiableSet(new HashSet<>(entry.getValue()))), + HashMap::putAll); + return LibraryInfo.builder() + .id(this.id) + .items(Collections.unmodifiableSet(new HashSet<>(this.itemsById.values()))) + .guests(Collections.unmodifiableSet(new HashSet<>(this.guestsById.values()))) + .checkedOutItemsByGuest(Collections.unmodifiableMap(itemsByGuest)) + .build(); + } + + @Override + public String toString() { + return "Library{" + + "itemsById=" + + itemsById + + ", checkedOutItemIds=" + + checkedOutItemIds + + ", checkedOutMediaItemsByLibraryGuest=" + + checkedOutItemsByGuest + + ", guestIds=" + + guestsById + + '}'; + } +} diff --git a/lesson_25/db/db_app/src/main/java/com/codedifferently/lesson25/library/LibraryGuest.java b/lesson_25/db/db_app/src/main/java/com/codedifferently/lesson25/library/LibraryGuest.java new file mode 100644 index 000000000..274f9db6b --- /dev/null +++ b/lesson_25/db/db_app/src/main/java/com/codedifferently/lesson25/library/LibraryGuest.java @@ -0,0 +1,45 @@ +package com.codedifferently.lesson25.library; + +import com.codedifferently.lesson25.library.exceptions.LibraryNotSetException; +import com.codedifferently.lesson25.library.exceptions.WrongLibraryException; +import java.util.Set; + +public interface LibraryGuest { + + /** + * Get the library that the guest is in. + * + * @param library The library that the guest is in. + * @throws WrongLibraryException If the guest is not in the library. + */ + public void setLibrary(Library library) throws WrongLibraryException; + + /** + * Get the name of the guest. + * + * @return The name of the guest. + */ + public String getName(); + + /** + * Get the email of the guest. + * + * @return The email of the guest. + */ + public String getEmail(); + + /** + * Get the id of the guest. + * + * @return The id of the guest. + */ + public String getId(); + + /** + * Gets the items currently checked out to the guest. + * + * @return The items currently checked out to the guest. + * @throws LibraryNotSetException If the library is not set for the guest. + */ + public Set getCheckedOutMediaItems() throws LibraryNotSetException; +} diff --git a/lesson_25/db/db_app/src/main/java/com/codedifferently/lesson25/library/LibraryGuestBase.java b/lesson_25/db/db_app/src/main/java/com/codedifferently/lesson25/library/LibraryGuestBase.java new file mode 100644 index 000000000..6e1cccdfe --- /dev/null +++ b/lesson_25/db/db_app/src/main/java/com/codedifferently/lesson25/library/LibraryGuestBase.java @@ -0,0 +1,73 @@ +package com.codedifferently.lesson25.library; + +import com.codedifferently.lesson25.library.exceptions.LibraryNotSetException; +import com.codedifferently.lesson25.library.exceptions.WrongLibraryException; +import java.util.Objects; +import java.util.Set; + +/** Base implementation of a library guest. */ +public class LibraryGuestBase implements LibraryGuest { + + private Library library; + private final String name; + private final String email; + + public LibraryGuestBase(String name, String email) { + this.name = name; + this.email = email; + } + + @Override + public void setLibrary(Library library) throws WrongLibraryException { + if (library != null && !library.hasLibraryGuest(this)) { + throw new WrongLibraryException( + "Patron " + this.getId() + " is not in library " + library.getId()); + } + this.library = library; + } + + @Override + public String getName() { + return this.name; + } + + @Override + public String getEmail() { + return this.email; + } + + @Override + public String getId() { + return this.email; + } + + @Override + public Set getCheckedOutMediaItems() throws LibraryNotSetException { + if (this.library == null) { + throw new LibraryNotSetException("Library not set for patron " + this.getId()); + } + return this.library.getCheckedOutByGuest(this); + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (!(o instanceof LibraryGuestBase)) { + return false; + } + LibraryGuestBase guest = (LibraryGuestBase) o; + return Objects.equals(getId(), guest.getId()); + } + + @Override + public int hashCode() { + return Objects.hash(getId()); + } + + @Override + public String toString() { + return "LibraryGuestBase{" + "id='" + getId() + '\'' + ", name='" + getName() + '\'' + '}'; + } +} diff --git a/lesson_25/db/db_app/src/main/java/com/codedifferently/lesson25/library/LibraryInfo.java b/lesson_25/db/db_app/src/main/java/com/codedifferently/lesson25/library/LibraryInfo.java new file mode 100644 index 000000000..49b4d23de --- /dev/null +++ b/lesson_25/db/db_app/src/main/java/com/codedifferently/lesson25/library/LibraryInfo.java @@ -0,0 +1,20 @@ +package com.codedifferently.lesson25.library; + +import java.util.Map; +import java.util.Set; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class LibraryInfo { + + public String id; + public Set items; + public Set guests; + public Map> checkedOutItemsByGuest; +} diff --git a/lesson_25/db/db_app/src/main/java/com/codedifferently/lesson25/library/Magazine.java b/lesson_25/db/db_app/src/main/java/com/codedifferently/lesson25/library/Magazine.java new file mode 100644 index 000000000..53aa49bd1 --- /dev/null +++ b/lesson_25/db/db_app/src/main/java/com/codedifferently/lesson25/library/Magazine.java @@ -0,0 +1,26 @@ +package com.codedifferently.lesson25.library; + +import java.util.UUID; + +/** Represents a magazine. */ +public class Magazine extends MediaItemBase { + + public Magazine(UUID id, String title) { + super(id, title); + } + + @Override + public String getType() { + return "magazine"; + } + + @Override + public boolean canCheckOut() { + return false; + } + + @Override + public String toString() { + return "Magazine{" + "id='" + getId() + '\'' + ", title='" + getTitle() + '\'' + '}'; + } +} diff --git a/lesson_25/db/db_app/src/main/java/com/codedifferently/lesson25/library/MediaItem.java b/lesson_25/db/db_app/src/main/java/com/codedifferently/lesson25/library/MediaItem.java new file mode 100644 index 000000000..f87da2aeb --- /dev/null +++ b/lesson_25/db/db_app/src/main/java/com/codedifferently/lesson25/library/MediaItem.java @@ -0,0 +1,54 @@ +package com.codedifferently.lesson25.library; + +import com.codedifferently.lesson25.library.exceptions.LibraryNotSetException; +import com.codedifferently.lesson25.library.exceptions.WrongLibraryException; +import com.codedifferently.lesson25.library.search.Searchable; +import java.util.UUID; + +/** Represents a media item. */ +public interface MediaItem extends Searchable { + + /** + * Get the type of the media item. + * + * @return The type of the media item. + */ + public String getType(); + + /** + * Get the id of the media item. + * + * @return The id of the media item. + */ + public UUID getId(); + + /** + * Set the library that the media item is in. + * + * @param library + * @throws WrongLibraryException + */ + public void setLibrary(Library library) throws WrongLibraryException; + + /** + * Get the title of the media item. + * + * @return The title of the media item. + */ + public String getTitle(); + + /** + * Check if the media item is checked out. + * + * @return True if the media item is checked out, false otherwise. + * @throws LibraryNotSetException + */ + public boolean isCheckedOut() throws LibraryNotSetException; + + /** + * Check if the media item can be checked out. + * + * @return True if the media item can be checked out, false otherwise. + */ + public boolean canCheckOut(); +} diff --git a/lesson_25/db/db_app/src/main/java/com/codedifferently/lesson25/library/MediaItemBase.java b/lesson_25/db/db_app/src/main/java/com/codedifferently/lesson25/library/MediaItemBase.java new file mode 100644 index 000000000..6438827e2 --- /dev/null +++ b/lesson_25/db/db_app/src/main/java/com/codedifferently/lesson25/library/MediaItemBase.java @@ -0,0 +1,98 @@ +package com.codedifferently.lesson25.library; + +import com.codedifferently.lesson25.library.exceptions.LibraryNotSetException; +import com.codedifferently.lesson25.library.exceptions.WrongLibraryException; +import com.codedifferently.lesson25.library.search.SearchCriteria; +import java.util.Objects; +import java.util.UUID; + +/** Base implementation of a media item. */ +public abstract class MediaItemBase implements MediaItem { + + private Library library; + private final UUID id; + private final String title; + + public MediaItemBase(UUID id, String title) { + this.id = id; + this.title = title; + } + + @Override + public UUID getId() { + return id; + } + + @Override + public String getTitle() { + return title; + } + + @Override + public void setLibrary(Library library) throws WrongLibraryException { + if (library != null && !library.hasMediaItem(this)) { + throw new WrongLibraryException( + "Media item " + this.getId() + " is not in library " + library.getId()); + } + this.library = library; + } + + @Override + public boolean isCheckedOut() throws LibraryNotSetException { + if (this.library == null) { + throw new LibraryNotSetException("Library not set for item " + this.getId()); + } + return library.isCheckedOut(this); + } + + @Override + public boolean canCheckOut() { + return true; + } + + /** + * Check if the media item matches the given author. + * + * @param author The author to check. + * @return True if the media item matches the author, false otherwise. + */ + protected boolean matchesAuthor(String author) { + return false; + } + + @Override + public boolean matches(SearchCriteria query) { + if (query.id != null && !this.getId().toString().equalsIgnoreCase(query.id)) { + return false; + } + if (query.title != null && !this.getTitle().toLowerCase().contains(query.title.toLowerCase())) { + return false; + } + if (query.type != null && !this.getType().equalsIgnoreCase(query.type)) { + return false; + } + return query.author == null || this.matchesAuthor(query.author); + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (!(o instanceof MediaItem)) { + return false; + } + MediaItem item = (MediaItem) o; + return Objects.equals(getId(), item.getId()); + } + + @Override + public int hashCode() { + return Objects.hash(getId()); + } + + @Override + public String toString() { + return "MediaItem{" + "id='" + getId() + '\'' + ", title='" + getTitle() + '\'' + '}'; + } +} diff --git a/lesson_25/db/db_app/src/main/java/com/codedifferently/lesson25/library/Newspaper.java b/lesson_25/db/db_app/src/main/java/com/codedifferently/lesson25/library/Newspaper.java new file mode 100644 index 000000000..2c25624f1 --- /dev/null +++ b/lesson_25/db/db_app/src/main/java/com/codedifferently/lesson25/library/Newspaper.java @@ -0,0 +1,26 @@ +package com.codedifferently.lesson25.library; + +import java.util.UUID; + +/** Represents a newspaper. */ +public class Newspaper extends MediaItemBase { + + public Newspaper(UUID id, String title) { + super(id, title); + } + + @Override + public String getType() { + return "newspaper"; + } + + @Override + public boolean canCheckOut() { + return false; + } + + @Override + public String toString() { + return "Newspaper{" + "id='" + getId() + '\'' + ", title='" + getTitle() + '\'' + '}'; + } +} diff --git a/lesson_25/db/db_app/src/main/java/com/codedifferently/lesson25/library/Patron.java b/lesson_25/db/db_app/src/main/java/com/codedifferently/lesson25/library/Patron.java new file mode 100644 index 000000000..750bc5d71 --- /dev/null +++ b/lesson_25/db/db_app/src/main/java/com/codedifferently/lesson25/library/Patron.java @@ -0,0 +1,20 @@ +package com.codedifferently.lesson25.library; + +/** Represents a patron of a library. */ +public class Patron extends LibraryGuestBase { + + /** + * Create a new patron with the given name and email. + * + * @param name The name of the patron. + * @param email The email of the patron. + */ + public Patron(String name, String email) { + super(name, email); + } + + @Override + public String toString() { + return "Patron{" + "id='" + getId() + '\'' + ", name='" + getName() + '\'' + '}'; + } +} diff --git a/lesson_25/db/db_app/src/main/java/com/codedifferently/lesson25/library/exceptions/LibraryNotSetException.java b/lesson_25/db/db_app/src/main/java/com/codedifferently/lesson25/library/exceptions/LibraryNotSetException.java new file mode 100644 index 000000000..4c336b9e8 --- /dev/null +++ b/lesson_25/db/db_app/src/main/java/com/codedifferently/lesson25/library/exceptions/LibraryNotSetException.java @@ -0,0 +1,8 @@ +package com.codedifferently.lesson25.library.exceptions; + +public class LibraryNotSetException extends RuntimeException { + + public LibraryNotSetException(String message) { + super(message); + } +} diff --git a/lesson_25/db/db_app/src/main/java/com/codedifferently/lesson25/library/exceptions/MediaItemCheckedOutException.java b/lesson_25/db/db_app/src/main/java/com/codedifferently/lesson25/library/exceptions/MediaItemCheckedOutException.java new file mode 100644 index 000000000..2ea953a63 --- /dev/null +++ b/lesson_25/db/db_app/src/main/java/com/codedifferently/lesson25/library/exceptions/MediaItemCheckedOutException.java @@ -0,0 +1,8 @@ +package com.codedifferently.lesson25.library.exceptions; + +public class MediaItemCheckedOutException extends RuntimeException { + + public MediaItemCheckedOutException(String message) { + super(message); + } +} diff --git a/lesson_25/db/db_app/src/main/java/com/codedifferently/lesson25/library/exceptions/WrongLibraryException.java b/lesson_25/db/db_app/src/main/java/com/codedifferently/lesson25/library/exceptions/WrongLibraryException.java new file mode 100644 index 000000000..110e3fa93 --- /dev/null +++ b/lesson_25/db/db_app/src/main/java/com/codedifferently/lesson25/library/exceptions/WrongLibraryException.java @@ -0,0 +1,8 @@ +package com.codedifferently.lesson25.library.exceptions; + +public class WrongLibraryException extends RuntimeException { + + public WrongLibraryException(String message) { + super(message); + } +} diff --git a/lesson_25/db/db_app/src/main/java/com/codedifferently/lesson25/library/search/CatalogSearcher.java b/lesson_25/db/db_app/src/main/java/com/codedifferently/lesson25/library/search/CatalogSearcher.java new file mode 100644 index 000000000..6101120aa --- /dev/null +++ b/lesson_25/db/db_app/src/main/java/com/codedifferently/lesson25/library/search/CatalogSearcher.java @@ -0,0 +1,32 @@ +package com.codedifferently.lesson25.library.search; + +import java.util.Collection; + +/** + * Searches a catalog for items that match a query. + * + * @param + */ +public class CatalogSearcher { + + private final Collection catalog; + + /** + * Constructor for CatalogSearcher + * + * @param catalog + */ + public CatalogSearcher(Collection catalog) { + this.catalog = catalog; + } + + /** + * Searches the catalog for items that match the given query. + * + * @param query The query to search for. + * @return The items that match the query. + */ + public Collection search(SearchCriteria query) { + return catalog.stream().filter(item -> item.matches(query)).toList(); + } +} diff --git a/lesson_25/db/db_app/src/main/java/com/codedifferently/lesson25/library/search/SearchCriteria.java b/lesson_25/db/db_app/src/main/java/com/codedifferently/lesson25/library/search/SearchCriteria.java new file mode 100644 index 000000000..0480aa448 --- /dev/null +++ b/lesson_25/db/db_app/src/main/java/com/codedifferently/lesson25/library/search/SearchCriteria.java @@ -0,0 +1,25 @@ +package com.codedifferently.lesson25.library.search; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@Builder +@AllArgsConstructor +@NoArgsConstructor +public class SearchCriteria { + + /** The ID to search for (exact match). */ + public String id; + + /** The title to search for. */ + public String title; + + /** The author to search for. */ + public String author; + + /** The type to search for (exact match). */ + public String type; +} diff --git a/lesson_25/db/db_app/src/main/java/com/codedifferently/lesson25/library/search/Searchable.java b/lesson_25/db/db_app/src/main/java/com/codedifferently/lesson25/library/search/Searchable.java new file mode 100644 index 000000000..83b1ce181 --- /dev/null +++ b/lesson_25/db/db_app/src/main/java/com/codedifferently/lesson25/library/search/Searchable.java @@ -0,0 +1,12 @@ +package com.codedifferently.lesson25.library.search; + +public interface Searchable { + + /** + * Indicates whether an item matches the search criteria. + * + * @param query The query to search for. + * @return The items that match the query. + */ + boolean matches(SearchCriteria query); +} diff --git a/lesson_25/db/db_app/src/main/java/com/codedifferently/lesson25/models/AuthorsConverter.java b/lesson_25/db/db_app/src/main/java/com/codedifferently/lesson25/models/AuthorsConverter.java new file mode 100644 index 000000000..f198f819f --- /dev/null +++ b/lesson_25/db/db_app/src/main/java/com/codedifferently/lesson25/models/AuthorsConverter.java @@ -0,0 +1,18 @@ +package com.codedifferently.lesson25.models; + +import jakarta.persistence.AttributeConverter; +import jakarta.persistence.Converter; +import java.util.List; + +@Converter +public class AuthorsConverter implements AttributeConverter, String> { + @Override + public String convertToDatabaseColumn(List authors) { + return String.join(", ", authors); + } + + @Override + public List convertToEntityAttribute(String authors) { + return authors != null ? List.of(authors.split(", ")) : List.of(); + } +} diff --git a/lesson_25/db/db_app/src/main/java/com/codedifferently/lesson25/models/CheckoutModel.java b/lesson_25/db/db_app/src/main/java/com/codedifferently/lesson25/models/CheckoutModel.java new file mode 100644 index 000000000..ae8ac1c20 --- /dev/null +++ b/lesson_25/db/db_app/src/main/java/com/codedifferently/lesson25/models/CheckoutModel.java @@ -0,0 +1,16 @@ +package com.codedifferently.lesson25.models; + +import jakarta.persistence.Entity; +import jakarta.persistence.Id; +import jakarta.persistence.Table; +import java.time.Instant; +import java.util.UUID; + +@Entity +@Table(name = "checked_out_items") +public class CheckoutModel { + + @Id public UUID itemId; + public String email; + public Instant dueDate; +} diff --git a/lesson_25/db/db_app/src/main/java/com/codedifferently/lesson25/models/LibraryDataModel.java b/lesson_25/db/db_app/src/main/java/com/codedifferently/lesson25/models/LibraryDataModel.java new file mode 100644 index 000000000..6c268f962 --- /dev/null +++ b/lesson_25/db/db_app/src/main/java/com/codedifferently/lesson25/models/LibraryDataModel.java @@ -0,0 +1,62 @@ +package com.codedifferently.lesson25.models; + +import com.codedifferently.lesson25.library.Book; +import com.codedifferently.lesson25.library.Dvd; +import com.codedifferently.lesson25.library.Librarian; +import com.codedifferently.lesson25.library.LibraryGuest; +import com.codedifferently.lesson25.library.Magazine; +import com.codedifferently.lesson25.library.MediaItem; +import com.codedifferently.lesson25.library.Newspaper; +import com.codedifferently.lesson25.library.Patron; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class LibraryDataModel { + + public List mediaItems; + public List guests; + + public List getMediaItems() { + List results = new ArrayList<>(); + for (MediaItemModel mediaItemModel : mediaItems) { + switch (mediaItemModel.type) { + case "book" -> + results.add( + new Book( + mediaItemModel.id, + mediaItemModel.title, + mediaItemModel.isbn, + mediaItemModel.authors, + mediaItemModel.pages)); + case "dvd" -> results.add(new Dvd(mediaItemModel.id, mediaItemModel.title)); + case "magazine" -> results.add(new Magazine(mediaItemModel.id, mediaItemModel.title)); + case "newspaper" -> results.add(new Newspaper(mediaItemModel.id, mediaItemModel.title)); + default -> + throw new IllegalArgumentException("Unknown media item type: " + mediaItemModel.type); + } + } + return results; + } + + public List getGuests() { + List results = new ArrayList<>(); + for (LibraryGuestModel guestModel : this.guests) { + switch (guestModel.type) { + case "librarian" -> results.add(new Librarian(guestModel.name, guestModel.email)); + case "patron" -> results.add(new Patron(guestModel.name, guestModel.email)); + default -> throw new AssertionError(); + } + } + return results; + } + + public Map> getCheckoutsByEmail() { + Map> results = new HashMap<>(); + for (LibraryGuestModel guest : this.guests) { + results.put(guest.email, guest.checkedOutItems); + } + return results; + } +} diff --git a/lesson_25/db/db_app/src/main/java/com/codedifferently/lesson25/models/LibraryGuestModel.java b/lesson_25/db/db_app/src/main/java/com/codedifferently/lesson25/models/LibraryGuestModel.java new file mode 100644 index 000000000..988ba02df --- /dev/null +++ b/lesson_25/db/db_app/src/main/java/com/codedifferently/lesson25/models/LibraryGuestModel.java @@ -0,0 +1,20 @@ +package com.codedifferently.lesson25.models; + +import jakarta.persistence.Entity; +import jakarta.persistence.FetchType; +import jakarta.persistence.Id; +import jakarta.persistence.OneToMany; +import jakarta.persistence.Table; +import java.util.List; + +@Entity +@Table(name = "guests") +public class LibraryGuestModel { + + public String type; + public String name; + @Id public String email; + + @OneToMany(mappedBy = "email", fetch = FetchType.EAGER) + public List checkedOutItems; +} diff --git a/lesson_25/db/db_app/src/main/java/com/codedifferently/lesson25/models/MediaItemModel.java b/lesson_25/db/db_app/src/main/java/com/codedifferently/lesson25/models/MediaItemModel.java new file mode 100644 index 000000000..a7591d523 --- /dev/null +++ b/lesson_25/db/db_app/src/main/java/com/codedifferently/lesson25/models/MediaItemModel.java @@ -0,0 +1,24 @@ +package com.codedifferently.lesson25.models; + +import jakarta.persistence.Convert; +import jakarta.persistence.Entity; +import jakarta.persistence.Id; +import jakarta.persistence.Table; +import java.util.List; +import java.util.UUID; + +@Entity +@Table(name = "media_items") +public class MediaItemModel { + public String type; + @Id public UUID id; + public String isbn; + public String title; + + @Convert(converter = AuthorsConverter.class) + public List authors; + + public String edition; + public Integer pages = 0; + public Integer runtime = 0; +} diff --git a/lesson_25/db/db_app/src/main/java/com/codedifferently/lesson25/repository/LibraryGuestRepository.java b/lesson_25/db/db_app/src/main/java/com/codedifferently/lesson25/repository/LibraryGuestRepository.java new file mode 100644 index 000000000..149ed4be4 --- /dev/null +++ b/lesson_25/db/db_app/src/main/java/com/codedifferently/lesson25/repository/LibraryGuestRepository.java @@ -0,0 +1,11 @@ +package com.codedifferently.lesson25.repository; + +import com.codedifferently.lesson25.models.LibraryGuestModel; +import java.util.List; +import org.springframework.data.repository.CrudRepository; + +public interface LibraryGuestRepository extends CrudRepository { + + @Override + List findAll(); +} diff --git a/lesson_25/db/db_app/src/main/java/com/codedifferently/lesson25/repository/MediaItemRepository.java b/lesson_25/db/db_app/src/main/java/com/codedifferently/lesson25/repository/MediaItemRepository.java new file mode 100644 index 000000000..e22f31db8 --- /dev/null +++ b/lesson_25/db/db_app/src/main/java/com/codedifferently/lesson25/repository/MediaItemRepository.java @@ -0,0 +1,12 @@ +package com.codedifferently.lesson25.repository; + +import com.codedifferently.lesson25.models.MediaItemModel; +import java.util.List; +import java.util.UUID; +import org.springframework.data.repository.CrudRepository; + +public interface MediaItemRepository extends CrudRepository { + + @Override + List findAll(); +} diff --git a/lesson_25/db/db_app/src/main/resources/application.yml b/lesson_25/db/db_app/src/main/resources/application.yml new file mode 100644 index 000000000..fd20667af --- /dev/null +++ b/lesson_25/db/db_app/src/main/resources/application.yml @@ -0,0 +1,7 @@ +spring: + jpa: + database-platform: org.hibernate.community.dialect.SQLiteDialect + generate-ddl: true + datasource: + url: jdbc:sqlite::resource:sqlite/data.db + driver-class-name: org.sqlite.JDBC \ No newline at end of file diff --git a/lesson_25/db/db_app/src/main/resources/csv/checked_out_items.csv b/lesson_25/db/db_app/src/main/resources/csv/checked_out_items.csv new file mode 100644 index 000000000..1c2fffa70 --- /dev/null +++ b/lesson_25/db/db_app/src/main/resources/csv/checked_out_items.csv @@ -0,0 +1,5 @@ +email,item_id,due_date +jane.smith@example.com,e27a4e0d-9664-420d-955e-c0e295d0ce02,2024-04-05T00:00:00Z +jane.smith@example.com,295ea581-cd61-4319-8b0c-e5c0c03286c5,2024-04-07T00:00:00Z +alice.johnson@example.com,17dd5d20-98f5-4a26-be09-449fea88a3c3,2024-04-03T00:00:00Z +alice.johnson@example.com,28e5c91f-0e4b-4be5-abb1-8da01fd5587e,2024-04-06T00:00:00Z \ No newline at end of file diff --git a/lesson_25/db/db_app/src/main/resources/csv/guests.csv b/lesson_25/db/db_app/src/main/resources/csv/guests.csv new file mode 100644 index 000000000..bcc02b051 --- /dev/null +++ b/lesson_25/db/db_app/src/main/resources/csv/guests.csv @@ -0,0 +1,6 @@ +type,name,email +librarian,John Doe,john.doe@fakelibrary.org +patron,Jane Smith,jane.smith@example.com +patron,Alice Johnson,alice.johnson@example.com +librarian,Bob Williams,bob.williams@fakelibrary.org +patron,Emily Brown,emily.brown@example.com \ No newline at end of file diff --git a/lesson_25/db/db_app/src/main/resources/csv/media_items.csv b/lesson_25/db/db_app/src/main/resources/csv/media_items.csv new file mode 100644 index 000000000..c3f8161d5 --- /dev/null +++ b/lesson_25/db/db_app/src/main/resources/csv/media_items.csv @@ -0,0 +1,32 @@ +type,id,title,isbn,authors,pages,runtime,edition +book,e27a4e0d-9664-420d-955e-c0e295d0ce02,To Kill a Mockingbird,978-0061120084,"Harper Lee",336,, +book,17dd5d20-98f5-4a26-be09-449fea88a3c3,1984,978-0451524935,"George Orwell",328,, +dvd,28e5c91f-0e4b-4be5-abb1-8da01fd5587e,The Shawshank Redemption,,,,142, +dvd,295ea581-cd61-4319-8b0c-e5c0c03286c5,Inception,,,,148, +magazine,222111dd-c561-462a-8854-853ada4d3421,National Geographic,,,,,March 2024 +magazine,0afd425b-973f-4af9-a6aa-8febe943a8f6,Time,,,,,"March 15, 2024" +newspaper,91b74a71-97ad-4fea-b17c-a640c98d355f,The New York Times,,,,,"Morning Edition, March 22, 2024" +newspaper,45cab344-b792-484c-9156-d929237dde67,The Guardian,,,,,"March 22, 2024" +book,218b55fa-a3cd-4803-805e-7cd1ef3115ac,The Great Gatsby,978-0743273565,"F. Scott Fitzgerald",180,, +book,b4249c17-f77b-46da-aa82-7aa227eca5e2,Harry Potter and the Sorcerer's Stone,978-0590353427,"J.K. Rowling",309,, +dvd,a3cc5ccb-e2fd-4cd0-a6f8-dc1f2f07589b,The Godfather,,,,175, +dvd,6386364c-8505-4dbe-8731-ff8fa0d6e381,The Dark Knight,,,,152, +magazine,e5060cc1-33f0-431c-a1b3-d1979e38b6f2,Scientific American,,,,,April 2024 +magazine,7048bd13-49ee-4693-8900-e396bbdf3f98,Vogue,,,,,Spring 2024 +newspaper,6f80a5ce-5958-48f3-a029-ff3d76f2c3fe,The Washington Post,,,,,"Evening Edition, March 22, 2024" +newspaper,f5f1196c-7935-417e-bc52-7144f63da3cb,The Times,,,,,"March 23, 2024" +book,faf5a804-e02c-4bbc-8505-4ba90a526e28,Pride and Prejudice,978-0141439518,"Jane Austen",279,, +book,1aab8182-4345-4ead-98e8-0db53682311b,The Catcher in the Rye,978-0316769488,"J.D. Salinger",277,, +dvd,2b92ef3d-b224-4589-9a59-cdaba758affd,The Matrix,,,,136, +dvd,e5d75a1d-f3b4-430f-ba63-b6e4603228eb,Pulp Fiction,,,,154, +magazine,75fb71ad-ea84-45b8-8396-b790c833573e,Wired,,,,,"May 2024" +magazine,e30a6739-cdc9-4b2e-ac67-7278fcfb9a59,Forbes,,,,,"April 2024" +newspaper,3e228c29-6163-477a-8b70-e873a3788758,Los Angeles Times,,,,,"Morning Edition, March 23, 2024" +newspaper,8e3946e2-d5a6-4cb4-ac92-17cc44935d2d,Chicago Tribune,,,,,"March 23, 2024" +book,b08c9da7-5c01-494c-84ec-af3fef9dc480,The Lord of the Rings,978-0544003415,"J.R.R. Tolkien",1178,, +dvd,af1ae237-d29a-49d8-a18a-d6193c07a033,The Silence of the Lambs,,,,118, +dvd,215af9ba-e881-48fb-8284-a3dc6a1c096d,The Departed,,,,151, +magazine,8efcbbb2-5c1e-486c-924d-63c3503f498c,The Economist,,,,,"March 23, 2024" +magazine,d39f5cf3-9574-4fdc-b81b-99d31b26ee92,The New Yorker,,,,,"March 25, 2024" +newspaper,8b369c0f-6c68-4a84-8e15-8b85a2dd1949,USA Today,,,,,"Morning Edition, March 23, 2024" +newspaper,eaf356a3-ae28-4cc5-92a2-9b0264165b5d,The Wall Street Journal,,,,,"March 23, 2024" \ No newline at end of file diff --git a/lesson_25/db/db_app/src/main/resources/json/data.json b/lesson_25/db/db_app/src/main/resources/json/data.json new file mode 100644 index 000000000..d419365ff --- /dev/null +++ b/lesson_25/db/db_app/src/main/resources/json/data.json @@ -0,0 +1,268 @@ +{ + "mediaItems": [ + { + "type": "book", + "id": "e27a4e0d-9664-420d-955e-c0e295d0ce02", + "title": "To Kill a Mockingbird", + "isbn": "978-0061120084", + "authors": [ + "Harper Lee" + ], + "pages": 336 + }, + { + "type": "book", + "id": "17dd5d20-98f5-4a26-be09-449fea88a3c3", + "title": "1984", + "isbn": "978-0451524935", + "authors": [ + "George Orwell" + ], + "pages": 328 + }, + { + "type": "dvd", + "id": "28e5c91f-0e4b-4be5-abb1-8da01fd5587e", + "title": "The Shawshank Redemption", + "runtime": 142 + }, + { + "type": "dvd", + "id": "295ea581-cd61-4319-8b0c-e5c0c03286c5", + "title": "Inception", + "runtime": 148 + }, + { + "type": "magazine", + "id": "222111dd-c561-462a-8854-853ada4d3421", + "title": "National Geographic", + "edition": "March 2024" + }, + { + "type": "magazine", + "id": "0afd425b-973f-4af9-a6aa-8febe943a8f6", + "title": "Time", + "edition": "March 15, 2024" + }, + { + "type": "newspaper", + "id": "91b74a71-97ad-4fea-b17c-a640c98d355f", + "title": "The New York Times", + "edition": "Morning Edition, March 22, 2024" + }, + { + "type": "newspaper", + "id": "45cab344-b792-484c-9156-d929237dde67", + "title": "The Guardian", + "edition": "March 22, 2024" + }, + { + "type": "book", + "id": "218b55fa-a3cd-4803-805e-7cd1ef3115ac", + "title": "The Great Gatsby", + "isbn": "978-0743273565", + "authors": [ + "F. Scott Fitzgerald" + ], + "pages": 180 + }, + { + "type": "book", + "id": "b4249c17-f77b-46da-aa82-7aa227eca5e2", + "title": "Harry Potter and the Sorcerer's Stone", + "isbn": "978-0590353427", + "authors": [ + "J.K. Rowling" + ], + "pages": 309 + }, + { + "type": "dvd", + "id": "a3cc5ccb-e2fd-4cd0-a6f8-dc1f2f07589b", + "title": "The Godfather", + "runtime": 175 + }, + { + "type": "dvd", + "id": "6386364c-8505-4dbe-8731-ff8fa0d6e381", + "title": "The Dark Knight", + "runtime": 152 + }, + { + "type": "magazine", + "id": "e5060cc1-33f0-431c-a1b3-d1979e38b6f2", + "title": "Scientific American", + "edition": "April 2024" + }, + { + "type": "magazine", + "id": "7048bd13-49ee-4693-8900-e396bbdf3f98", + "title": "Vogue", + "edition": "Spring 2024" + }, + { + "type": "newspaper", + "id": "6f80a5ce-5958-48f3-a029-ff3d76f2c3fe", + "title": "The Washington Post", + "edition": "Evening Edition, March 22, 2024" + }, + { + "type": "newspaper", + "id": "f5f1196c-7935-417e-bc52-7144f63da3cb", + "title": "The Times", + "edition": "March 23, 2024" + }, + { + "type": "book", + "id": "faf5a804-e02c-4bbc-8505-4ba90a526e28", + "title": "Pride and Prejudice", + "isbn": "978-0141439518", + "authors": [ + "Jane Austen" + ], + "pages": 279 + }, + { + "type": "book", + "id": "1aab8182-4345-4ead-98e8-0db53682311b", + "title": "The Catcher in the Rye", + "isbn": "978-0316769488", + "authors": [ + "J.D. Salinger" + ], + "pages": 277 + }, + { + "type": "dvd", + "id": "2b92ef3d-b224-4589-9a59-cdaba758affd", + "title": "The Matrix", + "runtime": 136 + }, + { + "type": "dvd", + "id": "e5d75a1d-f3b4-430f-ba63-b6e4603228eb", + "title": "Pulp Fiction", + "runtime": 154 + }, + { + "type": "magazine", + "id": "75fb71ad-ea84-45b8-8396-b790c833573e", + "title": "Wired", + "edition": "May 2024" + }, + { + "type": "magazine", + "id": "e30a6739-cdc9-4b2e-ac67-7278fcfb9a59", + "title": "Forbes", + "edition": "April 2024" + }, + { + "type": "newspaper", + "id": "3e228c29-6163-477a-8b70-e873a3788758", + "title": "Los Angeles Times", + "edition": "Morning Edition, March 23, 2024" + }, + { + "type": "newspaper", + "id": "8e3946e2-d5a6-4cb4-ac92-17cc44935d2d", + "title": "Chicago Tribune", + "edition": "March 23, 2024" + }, + { + "type": "book", + "id": "b08c9da7-5c01-494c-84ec-af3fef9dc480", + "title": "The Lord of the Rings", + "isbn": "978-0544003415", + "authors": [ + "J.R.R. Tolkien" + ], + "pages": 1178 + }, + { + "type": "dvd", + "id": "af1ae237-d29a-49d8-a18a-d6193c07a033", + "title": "The Silence of the Lambs", + "runtime": 118 + }, + { + "type": "dvd", + "id": "215af9ba-e881-48fb-8284-a3dc6a1c096d", + "title": "The Departed", + "runtime": 151 + }, + { + "type": "magazine", + "id": "8efcbbb2-5c1e-486c-924d-63c3503f498c", + "title": "The Economist", + "edition": "March 23, 2024" + }, + { + "type": "magazine", + "id": "d39f5cf3-9574-4fdc-b81b-99d31b26ee92", + "title": "The New Yorker", + "edition": "March 25, 2024" + }, + { + "type": "newspaper", + "id": "8b369c0f-6c68-4a84-8e15-8b85a2dd1949", + "title": "USA Today", + "edition": "Morning Edition, March 23, 2024" + }, + { + "type": "newspaper", + "id": "eaf356a3-ae28-4cc5-92a2-9b0264165b5d", + "title": "The Wall Street Journal", + "edition": "March 23, 2024" + } + ], + "guests": [ + { + "type": "librarian", + "name": "John Doe", + "email": "john.doe@fakelibrary.org", + "checkedOutItems": [] + }, + { + "type": "patron", + "name": "Jane Smith", + "email": "jane.smith@example.com", + "checkedOutItems": [ + { + "itemId": "e27a4e0d-9664-420d-955e-c0e295d0ce02", + "dueDate": "2024-04-05T00:00:00Z" + }, + { + "itemId": "295ea581-cd61-4319-8b0c-e5c0c03286c5", + "dueDate": "2024-04-07T00:00:00Z" + } + ] + }, + { + "type": "patron", + "name": "Alice Johnson", + "email": "alice.johnson@example.com", + "checkedOutItems": [ + { + "itemId": "17dd5d20-98f5-4a26-be09-449fea88a3c3", + "dueDate": "2024-04-03T00:00:00Z" + }, + { + "itemId": "28e5c91f-0e4b-4be5-abb1-8da01fd5587e", + "dueDate": "2024-04-06T00:00:00Z" + } + ] + }, + { + "type": "librarian", + "name": "Bob Williams", + "email": "bob.williams@fakelibrary.org", + "checkedOutItems": [] + }, + { + "type": "patron", + "name": "Emily Brown", + "email": "emily.brown@example.com", + "checkedOutItems": [] + } + ] +} \ No newline at end of file diff --git a/lesson_25/db/db_app/src/main/resources/queries/anthonydmays.sql b/lesson_25/db/db_app/src/main/resources/queries/anthonydmays.sql new file mode 100644 index 000000000..027b7d63f --- /dev/null +++ b/lesson_25/db/db_app/src/main/resources/queries/anthonydmays.sql @@ -0,0 +1 @@ +SELECT 1; \ No newline at end of file diff --git a/lesson_25/db/db_app/src/main/resources/sqlite/data.db b/lesson_25/db/db_app/src/main/resources/sqlite/data.db new file mode 100644 index 000000000..8baa982d2 Binary files /dev/null and b/lesson_25/db/db_app/src/main/resources/sqlite/data.db differ diff --git a/lesson_25/db/db_app/src/test/java/com/codedifferently/lesson25/Lesson25Test.java b/lesson_25/db/db_app/src/test/java/com/codedifferently/lesson25/Lesson25Test.java new file mode 100644 index 000000000..e29867d7f --- /dev/null +++ b/lesson_25/db/db_app/src/test/java/com/codedifferently/lesson25/Lesson25Test.java @@ -0,0 +1,13 @@ +package com.codedifferently.lesson25; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.jupiter.api.Test; + +class Lesson25Test { + + @Test + void testInstantiate() { + assertThat(new Lesson25()).isNotNull(); + } +} diff --git a/lesson_25/db/db_app/src/test/java/com/codedifferently/lesson25/factory/LibraryCsvDataLoaderTest.java b/lesson_25/db/db_app/src/test/java/com/codedifferently/lesson25/factory/LibraryCsvDataLoaderTest.java new file mode 100644 index 000000000..b5119cd7d --- /dev/null +++ b/lesson_25/db/db_app/src/test/java/com/codedifferently/lesson25/factory/LibraryCsvDataLoaderTest.java @@ -0,0 +1,84 @@ +package com.codedifferently.lesson25.factory; + +import static org.assertj.core.api.Assertions.assertThat; + +import com.codedifferently.lesson25.Lesson25; +import com.codedifferently.lesson25.library.LibraryGuest; +import com.codedifferently.lesson25.library.MediaItem; +import com.codedifferently.lesson25.models.CheckoutModel; +import com.codedifferently.lesson25.models.LibraryDataModel; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInstance; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.ContextConfiguration; + +@SpringBootTest +@TestInstance(TestInstance.Lifecycle.PER_CLASS) +@ContextConfiguration(classes = Lesson25.class) +class LibraryCsvDataLoaderTest { + + private LibraryDataModel libraryDataModel; + + @BeforeAll + void beforeAll() throws Exception { + libraryDataModel = new LibraryCsvDataLoader().loadData(); + } + + @Test + void testDataLoader_loadsCheckedOutItems() { + Map> checkedOutItemsByGuest = + libraryDataModel.getCheckoutsByEmail(); + var numCheckedOutItems = checkedOutItemsByGuest.values().stream().mapToInt(List::size).sum(); + assertThat(numCheckedOutItems) + .describedAs("LibraryCsvDataLoader should load checked out items") + .isEqualTo(4); + } + + @Test + void testDataLoader_loadsCorrectItemTypes() { + List items = libraryDataModel.getMediaItems(); + Map countByMediaType = + items.stream() + .reduce( + new HashMap<>(), + (hashMap, e) -> { + hashMap.merge(e.getType(), 1, Integer::sum); + return hashMap; + }, + (m, m2) -> { + m.putAll(m2); + return m; + }); + assertThat(countByMediaType.get("book")).isEqualTo(7); + assertThat(countByMediaType.get("magazine")).isEqualTo(8); + assertThat(countByMediaType.get("newspaper")).isEqualTo(8); + assertThat(countByMediaType.get("dvd")).isEqualTo(8); + assertThat(items.stream().map(MediaItem::getId).distinct().count()).isEqualTo(31); + assertThat(items.stream().map(MediaItem::getTitle).distinct().count()).isEqualTo(31); + } + + @Test + void testDataLoader_loadsCorrectGuestTypes() { + List guests = libraryDataModel.getGuests(); + Map countByGuestType = + guests.stream() + .reduce( + new HashMap<>(), + (hashMap, e) -> { + hashMap.merge(e.getClass().getSimpleName(), 1, Integer::sum); + return hashMap; + }, + (m, m2) -> { + m.putAll(m2); + return m; + }); + assertThat(countByGuestType.get("Librarian")).isEqualTo(2); + assertThat(countByGuestType.get("Patron")).isEqualTo(3); + assertThat(guests.stream().map(LibraryGuest::getEmail).distinct().count()).isEqualTo(5); + assertThat(guests.stream().map(LibraryGuest::getName).distinct().count()).isEqualTo(5); + } +} diff --git a/lesson_25/db/db_app/src/test/java/com/codedifferently/lesson25/factory/LibraryJsonDataLoaderTest.java b/lesson_25/db/db_app/src/test/java/com/codedifferently/lesson25/factory/LibraryJsonDataLoaderTest.java new file mode 100644 index 000000000..4b8cb8dd0 --- /dev/null +++ b/lesson_25/db/db_app/src/test/java/com/codedifferently/lesson25/factory/LibraryJsonDataLoaderTest.java @@ -0,0 +1,76 @@ +package com.codedifferently.lesson25.factory; + +import static org.assertj.core.api.Assertions.assertThat; + +import com.codedifferently.lesson25.library.LibraryGuest; +import com.codedifferently.lesson25.library.MediaItem; +import com.codedifferently.lesson25.models.CheckoutModel; +import com.codedifferently.lesson25.models.LibraryDataModel; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +class LibraryJsonDataLoaderTest { + + private static LibraryDataModel libraryDataModel; + + @BeforeAll + static void beforeAll() throws Exception { + var libraryJsonDataLoader = new LibraryJsonDataLoader(); + libraryDataModel = libraryJsonDataLoader.loadData(); + } + + @Test + void testDataLoader_loadsCheckedOutItems() { + Map> checkedOutItemsByGuest = + libraryDataModel.getCheckoutsByEmail(); + var numCheckedOutItems = checkedOutItemsByGuest.values().stream().mapToInt(List::size).sum(); + assertThat(numCheckedOutItems).isEqualTo(4); + } + + @Test + void testDataLoader_loadsCorrectItemTypes() { + List items = libraryDataModel.getMediaItems(); + Map countByMediaType = + items.stream() + .reduce( + new HashMap<>(), + (hashMap, e) -> { + hashMap.merge(e.getType(), 1, Integer::sum); + return hashMap; + }, + (m, m2) -> { + m.putAll(m2); + return m; + }); + assertThat(countByMediaType.get("book")).isEqualTo(7); + assertThat(countByMediaType.get("magazine")).isEqualTo(8); + assertThat(countByMediaType.get("newspaper")).isEqualTo(8); + assertThat(countByMediaType.get("dvd")).isEqualTo(8); + assertThat(items.stream().map(MediaItem::getId).distinct().count()).isEqualTo(31); + assertThat(items.stream().map(MediaItem::getTitle).distinct().count()).isEqualTo(31); + } + + @Test + void testDataLoader_loadsCorrectGuestTypes() { + List guests = libraryDataModel.getGuests(); + Map countByGuestType = + guests.stream() + .reduce( + new HashMap<>(), + (hashMap, e) -> { + hashMap.merge(e.getClass().getSimpleName(), 1, Integer::sum); + return hashMap; + }, + (m, m2) -> { + m.putAll(m2); + return m; + }); + assertThat(countByGuestType.get("Librarian")).isEqualTo(2); + assertThat(countByGuestType.get("Patron")).isEqualTo(3); + assertThat(guests.stream().map(LibraryGuest::getEmail).distinct().count()).isEqualTo(5); + assertThat(guests.stream().map(LibraryGuest::getName).distinct().count()).isEqualTo(5); + } +} diff --git a/lesson_25/db/db_app/src/test/java/com/codedifferently/lesson25/library/BookTest.java b/lesson_25/db/db_app/src/test/java/com/codedifferently/lesson25/library/BookTest.java new file mode 100644 index 000000000..024c0f7e3 --- /dev/null +++ b/lesson_25/db/db_app/src/test/java/com/codedifferently/lesson25/library/BookTest.java @@ -0,0 +1,94 @@ +package com.codedifferently.lesson25.library; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import com.codedifferently.lesson25.library.exceptions.LibraryNotSetException; +import com.codedifferently.lesson25.library.exceptions.WrongLibraryException; +import java.util.List; +import java.util.UUID; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +class BookTest { + + private Book classUnderTest; + private Library library; + + @BeforeEach + void setUp() { + classUnderTest = + new Book( + UUID.fromString("2b7591dd-f418-4115-974e-45115b3bf39a"), + "To Kill a Mockingbird", + "978-0061120084", + List.of("Harper Lee"), + 281); + library = mock(Library.class); + when(library.getId()).thenReturn("Library 1"); + when(library.hasMediaItem(classUnderTest)).thenReturn(true); + classUnderTest.setLibrary(library); + } + + @Test + void testPatron_created() { + // Assert + assertThat(classUnderTest.getTitle()).isEqualTo("To Kill a Mockingbird"); + assertThat(classUnderTest.getIsbn()).isEqualTo("978-0061120084"); + assertThat(classUnderTest.getAuthors()).isEqualTo(List.of("Harper Lee")); + assertThat(classUnderTest.getNumberOfPages()).isEqualTo(281); + } + + @Test + void testSetLibrary_WrongLibrary() { + // Arrange + Library otherLibrary = mock(Library.class); + when(otherLibrary.hasMediaItem(classUnderTest)).thenReturn(false); + when(otherLibrary.getId()).thenReturn("Library 2"); + + // Act & Assert + assertThatThrownBy(() -> classUnderTest.setLibrary(otherLibrary)) + .isInstanceOf(WrongLibraryException.class) + .hasMessageContaining( + "Media item 2b7591dd-f418-4115-974e-45115b3bf39a is not in library Library 2"); + } + + @Test + void testIsCheckedOut_LibraryNotSet() { + // Arrange + classUnderTest.setLibrary(null); + + // Act & Assert + assertThatThrownBy(() -> classUnderTest.isCheckedOut()) + .isInstanceOf(LibraryNotSetException.class) + .hasMessageContaining("Library not set for item 2b7591dd-f418-4115-974e-45115b3bf39a"); + } + + @Test + void testIsCheckedOut() { + // Arrange + when(library.isCheckedOut(classUnderTest)).thenReturn(true); + + // Act & Assert + assertThat(classUnderTest.isCheckedOut()).isTrue(); + } + + @Test + void testIsCheckedOut_whenNotCheckedOut() { + // Arrange + when(library.isCheckedOut(classUnderTest)).thenReturn(false); + + // Act & Assert + assertThat(classUnderTest.isCheckedOut()).isFalse(); + } + + @Test + void testToString() { + // Act & Assert + assertThat(classUnderTest.toString()) + .isEqualTo( + "Book{id='2b7591dd-f418-4115-974e-45115b3bf39a', title='To Kill a Mockingbird'}"); + } +} diff --git a/lesson_25/db/db_app/src/test/java/com/codedifferently/lesson25/library/LibraryTest.java b/lesson_25/db/db_app/src/test/java/com/codedifferently/lesson25/library/LibraryTest.java new file mode 100644 index 000000000..3fbef4af8 --- /dev/null +++ b/lesson_25/db/db_app/src/test/java/com/codedifferently/lesson25/library/LibraryTest.java @@ -0,0 +1,338 @@ +package com.codedifferently.lesson25.library; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import com.codedifferently.lesson25.library.exceptions.MediaItemCheckedOutException; +import java.util.List; +import java.util.UUID; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +class LibraryTest { + + private Library classUnderTest; + + @BeforeEach + void setUp() { + classUnderTest = new Library("compton-library"); + } + + @Test + void testLibrary_canAddItems() { + // Arrange + Book book1 = + new Book( + UUID.randomUUID(), + "The Great Gatsby", + "978-0743273565", + List.of("F. Scott Fitzgerald"), + 180); + Book book2 = + new Book( + UUID.randomUUID(), + "To Kill a Mockingbird", + "978-0061120084", + List.of("Harper Lee"), + 281); + Librarian librarian = new Librarian("Anthony Mays", "anthony@example.com"); + // Act + classUnderTest.addMediaItem(book1, librarian); + classUnderTest.addMediaItem(book2, librarian); + // Assert + assertThat(classUnderTest.hasMediaItem(book1)).isTrue(); + assertThat(classUnderTest.hasMediaItem(book2)).isTrue(); + } + + @Test + void testLibrary_canRemoveItems() { + // Arrange + Book book1 = + new Book( + UUID.randomUUID(), + "The Great Gatsby", + "978-0743273565", + List.of("F. Scott Fitzgerald"), + 180); + Book book2 = + new Book( + UUID.randomUUID(), + "To Kill a Mockingbird", + "978-0061120084", + List.of("Harper Lee"), + 281); + Librarian librarian = new Librarian("Anthony Mays", "anthony@example.com"); + classUnderTest.addMediaItem(book1, librarian); + classUnderTest.addMediaItem(book2, librarian); + // Act + classUnderTest.removeMediaItem(book1, librarian); + classUnderTest.removeMediaItem(book2, librarian); + // Assert + assertThat(classUnderTest.hasMediaItem(book1)).isFalse(); + assertThat(classUnderTest.hasMediaItem(book2)).isFalse(); + } + + @Test + void testLibrary_canAddPatrons() { + // Arrange + Patron patron1 = new Patron("John Doe", "john@example.com"); + Patron patron2 = new Patron("Jane Doe", "jane@example.com"); + // Act + classUnderTest.addLibraryGuest(patron1); + classUnderTest.addLibraryGuest(patron2); + // Assert + assertThat(classUnderTest.hasLibraryGuest(patron1)).isTrue(); + assertThat(classUnderTest.hasLibraryGuest(patron2)).isTrue(); + } + + @Test + void testLibrary_canRemovePatrons() { + // Arrange + Patron patron1 = new Patron("John Doe", "john@example.com"); + Patron patron2 = new Patron("Jane Doe", "jane@example.com"); + classUnderTest.addLibraryGuest(patron1); + classUnderTest.addLibraryGuest(patron2); + // Act + classUnderTest.removeLibraryGuest(patron1); + classUnderTest.removeLibraryGuest(patron2); + // Assert + assertThat(classUnderTest.hasLibraryGuest(patron1)).isFalse(); + assertThat(classUnderTest.hasLibraryGuest(patron2)).isFalse(); + } + + @Test + void testLibrary_allowsPatronToCheckoutBook() { + // Arrange + Book book = + new Book( + UUID.randomUUID(), + "The Great Gatsby", + "978-0743273565", + List.of("F. Scott Fitzgerald"), + 180); + Patron patron = new Patron("John Doe", "john@example.com"); + Librarian librarian = new Librarian("Anthony Mays", "anthony@example.com"); + classUnderTest.addMediaItem(book, librarian); + classUnderTest.addLibraryGuest(patron); + // Act + boolean wasCheckedOut = classUnderTest.checkOutMediaItem(book, patron); + // Assert + assertThat(wasCheckedOut).isTrue(); + assertThat(classUnderTest.isCheckedOut(book)).isTrue(); + assertThat(patron.getCheckedOutMediaItems().contains(book)).isTrue(); + } + + @Test + void testLibrary_allowPatronToCheckInBook() { + // Arrange + Book book = + new Book( + UUID.randomUUID(), + "The Great Gatsby", + "978-0743273565", + List.of("F. Scott Fitzgerald"), + 180); + Patron patron = new Patron("John Doe", "john@example.com"); + Librarian librarian = new Librarian("Anthony Mays", "anthony@example.com"); + classUnderTest.addMediaItem(book, librarian); + classUnderTest.addLibraryGuest(patron); + classUnderTest.checkOutMediaItem(book, patron); + // Act + boolean wasReturned = classUnderTest.checkInMediaItem(book, patron); + // Assert + assertThat(wasReturned).isTrue(); + assertThat(classUnderTest.isCheckedOut(book)).isFalse(); + assertThat(patron.getCheckedOutMediaItems().contains(book)).isFalse(); + } + + @Test + void testLibrary_allowLibrarianToCheckOutBook() { + // Arrange + Librarian librarian = new Librarian("Anthony Mays", "anthony@example.com"); + Book book = + new Book( + UUID.randomUUID(), + "The Great Gatsby", + "978-0743273565", + List.of("F. Scott Fitzgerald"), + 180); + classUnderTest.addMediaItem(book, librarian); + classUnderTest.addLibraryGuest(librarian); + // Act + boolean wasCheckedOut = classUnderTest.checkOutMediaItem(book, librarian); + // Assert + assertThat(wasCheckedOut).isTrue(); + assertThat(librarian.getCheckedOutMediaItems().contains(book)).isTrue(); + } + + @Test + void testLibrary_allowLibrarianToCheckInBook() { + // Arrange + Book book = + new Book( + UUID.randomUUID(), + "The Great Gatsby", + "978-0743273565", + List.of("F. Scott Fitzgerald"), + 180); + Librarian librarian = new Librarian("John Doe", "john@example.com"); + classUnderTest.addMediaItem(book, librarian); + classUnderTest.addLibraryGuest(librarian); + classUnderTest.checkOutMediaItem(book, librarian); + // Act + boolean wasReturned = classUnderTest.checkInMediaItem(book, librarian); + // Assert + assertThat(wasReturned).isTrue(); + assertThat(classUnderTest.isCheckedOut(book)).isFalse(); + assertThat(librarian.getCheckedOutMediaItems().contains(book)).isFalse(); + } + + @Test + void testLibrary_preventsMultipleCheckouts() { + // Arrange + Book book = + new Book( + UUID.randomUUID(), + "The Great Gatsby", + "978-0743273565", + List.of("F. Scott Fitzgerald"), + 180); + Patron patron = new Patron("John Doe", "john@example.com"); + Librarian librarian = new Librarian("Anthony Mays", "anthony@example.com"); + classUnderTest.addMediaItem(book, librarian); + classUnderTest.addLibraryGuest(patron); + classUnderTest.checkOutMediaItem(book, patron); + // Act + boolean wasCheckedOut = classUnderTest.checkOutMediaItem(book, patron); + // Assert + assertThat(wasCheckedOut).isFalse(); + assertThat(classUnderTest.isCheckedOut(book)).isTrue(); + } + + @Test + void testLibrary_preventsRemovingPatronWithCheckedOutItems() { + // Arrange + Book book = + new Book( + UUID.randomUUID(), + "The Great Gatsby", + "978-0743273565", + List.of("F. Scott Fitzgerald"), + 180); + Patron patron = new Patron("John Doe", "john@example.com"); + Librarian librarian = new Librarian("Anthony Mays", "anthony@example.com"); + classUnderTest.addMediaItem(book, librarian); + classUnderTest.addLibraryGuest(patron); + classUnderTest.checkOutMediaItem(book, patron); + // Act + assertThatThrownBy(() -> classUnderTest.removeLibraryGuest(patron)) + .isInstanceOf(MediaItemCheckedOutException.class) + .hasMessage("Cannot remove guest with checked out items."); + } + + @Test + void testLibrary_preventsRemovingCheckedOutItems() { + // Arrange + Book book = + new Book( + UUID.randomUUID(), + "The Great Gatsby", + "978-0743273565", + List.of("F. Scott Fitzgerald"), + 180); + Patron patron = new Patron("Jane Doe", "jane@example.com"); + Librarian librarian = new Librarian("Anthony Mays", "anthony@example.com"); + classUnderTest.addMediaItem(book, librarian); + classUnderTest.addLibraryGuest(patron); + classUnderTest.checkOutMediaItem(book, patron); + // Act + assertThatThrownBy(() -> classUnderTest.removeMediaItem(book, librarian)) + .isInstanceOf(MediaItemCheckedOutException.class) + .hasMessage("Cannot remove checked out item."); + } + + @Test + void testLibrary_canAddDvd() { + // Arrange + Dvd dvd = new Dvd(UUID.randomUUID(), "The Great Gatsby"); + Librarian librarian = new Librarian("Anthony Mays", "anthony@example.com"); + // Act + classUnderTest.addMediaItem(dvd, librarian); + // Assert + assertThat(classUnderTest.hasMediaItem(dvd)).isTrue(); + } + + @Test + void testLibrary_canRemoveDvd() { + // Arrange + Dvd dvd = new Dvd(UUID.randomUUID(), "The Great Gatsby"); + Librarian librarian = new Librarian("Anthony Mays", "anthony@example.com"); + // Act + classUnderTest.removeMediaItem(dvd, librarian); + // Assert + assertThat(classUnderTest.hasMediaItem(dvd)).isFalse(); + } + + @Test + void testLibrary_allowLibrarianToCheckOutDvd() { + // Arrange + Librarian librarian = new Librarian("Anthony Mays", "anthony@example.com"); + Dvd dvd = new Dvd(UUID.randomUUID(), "The Great Gatsby"); + classUnderTest.addMediaItem(dvd, librarian); + classUnderTest.addLibraryGuest(librarian); + // Act + boolean wasCheckedOut = classUnderTest.checkOutMediaItem(dvd, librarian); + // Assert + assertThat(wasCheckedOut).isTrue(); + assertThat(librarian.getCheckedOutMediaItems().contains(dvd)).isTrue(); + } + + @Test + void testLibrary_allowPatronToCheckInDvd() { + // Arrange + Dvd dvd = new Dvd(UUID.randomUUID(), "The Great Gatsby"); + Patron patron = new Patron("John Doe", "john@example.com"); + Librarian librarian = new Librarian("Anthony Mays", "anthony@example.com"); + classUnderTest.addMediaItem(dvd, librarian); + classUnderTest.addLibraryGuest(patron); + classUnderTest.checkOutMediaItem(dvd, patron); + // Act + boolean wasReturned = classUnderTest.checkInMediaItem(dvd, patron); + // Assert + assertThat(wasReturned).isTrue(); + assertThat(classUnderTest.isCheckedOut(dvd)).isFalse(); + assertThat(patron.getCheckedOutMediaItems().contains(dvd)).isFalse(); + } + + @Test + void testLibrary_preventsGuestFromCheckingOutMagazine() { + // Arrange + Patron patron = new Patron("John Doe", "john@example.com"); + Librarian librarian = new Librarian("Anthony Mays", "anthony@example.com"); + Magazine magazine = new Magazine(UUID.randomUUID(), "The Great Gatsby"); + classUnderTest.addMediaItem(magazine, librarian); + classUnderTest.addLibraryGuest(librarian); + classUnderTest.addLibraryGuest(patron); + // Act + boolean wasCheckedOut = classUnderTest.checkOutMediaItem(magazine, librarian); + // Assert + assertThat(wasCheckedOut).isFalse(); + assertThat(patron.getCheckedOutMediaItems().contains(magazine)).isFalse(); + } + + @Test + void testLibrary_preventsGuestFromCheckingOutNewspaper() { + // Arrange + Patron patron = new Patron("John Doe", "john@example.com"); + Librarian librarian = new Librarian("Anthony Mays", "anthony@example.com"); + Newspaper newspaper = new Newspaper(UUID.randomUUID(), "LA Times"); + classUnderTest.addMediaItem(newspaper, librarian); + classUnderTest.addLibraryGuest(librarian); + classUnderTest.addLibraryGuest(patron); + // Act + boolean wasCheckedOut = classUnderTest.checkOutMediaItem(newspaper, librarian); + // Assert + assertThat(wasCheckedOut).isFalse(); + assertThat(patron.getCheckedOutMediaItems().contains(newspaper)).isFalse(); + } +} diff --git a/lesson_25/db/db_app/src/test/java/com/codedifferently/lesson25/library/MediaItemBaseTest.java b/lesson_25/db/db_app/src/test/java/com/codedifferently/lesson25/library/MediaItemBaseTest.java new file mode 100644 index 000000000..9387a9a44 --- /dev/null +++ b/lesson_25/db/db_app/src/test/java/com/codedifferently/lesson25/library/MediaItemBaseTest.java @@ -0,0 +1,96 @@ +package com.codedifferently.lesson25.library; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import com.codedifferently.lesson25.library.exceptions.LibraryNotSetException; +import com.codedifferently.lesson25.library.exceptions.WrongLibraryException; +import java.util.UUID; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +class MediaItemBaseTest { + + private MediaItemBase mediaItem; + private static final UUID ITEM_ID = UUID.fromString("af71ac38-7628-415f-a2cd-bcaf7e001b97"); + + class MockMediaItem extends MediaItemBase { + + public MockMediaItem(UUID id, String title) { + super(id, title); + } + + @Override + public String getType() { + return "mock"; + } + } + + @BeforeEach + void setUp() { + mediaItem = new MockMediaItem(ITEM_ID, "Sample Title"); + } + + @Test + void getId() { + assertEquals(ITEM_ID, mediaItem.getId()); + } + + @Test + void getTitle() { + assertEquals("Sample Title", mediaItem.getTitle()); + } + + @Test + void setLibrary_withWrongLibraryException() { + Library library = mock(Library.class); + when(library.getId()).thenReturn("compton-library"); + when(library.hasMediaItem(mediaItem)).thenReturn(false); + assertThatThrownBy(() -> mediaItem.setLibrary(library)) + .isInstanceOf(WrongLibraryException.class) + .hasMessage( + "Media item af71ac38-7628-415f-a2cd-bcaf7e001b97 is not in library compton-library"); + } + + @Test + void isCheckedOut() throws LibraryNotSetException { + Library library = mock(Library.class); + when(library.hasMediaItem(mediaItem)).thenReturn(true); + when(library.isCheckedOut(mediaItem)).thenReturn(true); + mediaItem.setLibrary(library); + assertTrue(mediaItem.isCheckedOut()); + } + + @Test + void isCheckedOut_withLibraryNotSetException() { + assertThatThrownBy(() -> mediaItem.isCheckedOut()) + .isInstanceOf(LibraryNotSetException.class) + .hasMessage("Library not set for item af71ac38-7628-415f-a2cd-bcaf7e001b97"); + } + + @Test + void canCheckOut() { + assertTrue(mediaItem.canCheckOut()); + } + + @Test + void equals() { + MediaItemBase mediaItem2 = new MockMediaItem(ITEM_ID, "Sample Title"); + assertEquals(mediaItem, mediaItem2); + } + + @Test + void hashCodeTest() { + MediaItemBase mediaItem2 = new MockMediaItem(ITEM_ID, "Sample Title"); + assertEquals(mediaItem.hashCode(), mediaItem2.hashCode()); + } + + @Test + void toStringTest() { + String expected = "MediaItem{id='af71ac38-7628-415f-a2cd-bcaf7e001b97', title='Sample Title'}"; + assertEquals(expected, mediaItem.toString()); + } +} diff --git a/lesson_25/db/db_app/src/test/java/com/codedifferently/lesson25/library/PatronTest.java b/lesson_25/db/db_app/src/test/java/com/codedifferently/lesson25/library/PatronTest.java new file mode 100644 index 000000000..69aab997e --- /dev/null +++ b/lesson_25/db/db_app/src/test/java/com/codedifferently/lesson25/library/PatronTest.java @@ -0,0 +1,93 @@ +package com.codedifferently.lesson25.library; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import com.codedifferently.lesson25.library.exceptions.LibraryNotSetException; +import com.codedifferently.lesson25.library.exceptions.WrongLibraryException; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.UUID; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +class PatronTest { + + private Patron classUnderTest; + private Library library; + + @BeforeEach + void setUp() { + classUnderTest = new Patron("John Doe", "johndoe@example.com"); + library = new Library("Library 1"); + library.addLibraryGuest(classUnderTest); + } + + @Test + void testPatron_created() { + // Assert + assertThat(classUnderTest.getName()).isEqualTo("John Doe"); + assertThat(classUnderTest.getId()).isEqualTo("johndoe@example.com"); + } + + @Test + void testSetLibrary_WrongLibrary() { + // Arrange + Library otherLibrary = new Library("Library 2"); + + // Act & Assert + assertThatThrownBy(() -> classUnderTest.setLibrary(otherLibrary)) + .isInstanceOf(WrongLibraryException.class) + .hasMessageContaining("Patron johndoe@example.com is not in library Library 2"); + } + + @Test + void testGetCheckedOutBooks_LibraryNotSet() { + // Arrange + classUnderTest.setLibrary(null); + + // Act & Assert + assertThatThrownBy(() -> classUnderTest.getCheckedOutMediaItems()) + .isInstanceOf(LibraryNotSetException.class) + .hasMessageContaining("Library not set for patron johndoe@example.com"); + } + + @Test + void testGetCheckedOutBooks() { + // Arrange + Book book1 = + new Book( + UUID.randomUUID(), + "The Great Gatsby", + "978-0743273565", + List.of("F. Scott Fitzgerald"), + 180); + Book book2 = + new Book( + UUID.randomUUID(), + "To Kill a Mockingbird", + "978-0061120084", + List.of("Harper Lee"), + 281); + Librarian librarian = new Librarian("Anthony Mays", "anthony@example.com"); + Set expectedBooks = new HashSet<>(); + expectedBooks.add(book1); + expectedBooks.add(book2); + + library.addMediaItem(book1, librarian); + library.addMediaItem(book2, librarian); + library.checkOutMediaItem(book1, classUnderTest); + library.checkOutMediaItem(book2, classUnderTest); + + // Act & Assert + assertThat(classUnderTest.getCheckedOutMediaItems()).isEqualTo(expectedBooks); + } + + @Test + void testToString() { + // Act & Assert + assertThat(classUnderTest.toString()) + .isEqualTo("Patron{id='johndoe@example.com', name='John Doe'}"); + } +} diff --git a/lesson_25/db/gradle/wrapper/gradle-wrapper.jar b/lesson_25/db/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 000000000..a4b76b953 Binary files /dev/null and b/lesson_25/db/gradle/wrapper/gradle-wrapper.jar differ diff --git a/lesson_25/db/gradle/wrapper/gradle-wrapper.properties b/lesson_25/db/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 000000000..e2847c820 --- /dev/null +++ b/lesson_25/db/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,7 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-8.11.1-bin.zip +networkTimeout=10000 +validateDistributionUrl=true +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/lesson_25/db/gradlew b/lesson_25/db/gradlew new file mode 100755 index 000000000..f5feea6d6 --- /dev/null +++ b/lesson_25/db/gradlew @@ -0,0 +1,252 @@ +#!/bin/sh + +# +# Copyright © 2015-2021 the original authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# SPDX-License-Identifier: Apache-2.0 +# + +############################################################################## +# +# Gradle start up script for POSIX generated by Gradle. +# +# Important for running: +# +# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is +# noncompliant, but you have some other compliant shell such as ksh or +# bash, then to run this script, type that shell name before the whole +# command line, like: +# +# ksh Gradle +# +# Busybox and similar reduced shells will NOT work, because this script +# requires all of these POSIX shell features: +# * functions; +# * expansions «$var», «${var}», «${var:-default}», «${var+SET}», +# «${var#prefix}», «${var%suffix}», and «$( cmd )»; +# * compound commands having a testable exit status, especially «case»; +# * various built-in commands including «command», «set», and «ulimit». +# +# Important for patching: +# +# (2) This script targets any POSIX shell, so it avoids extensions provided +# by Bash, Ksh, etc; in particular arrays are avoided. +# +# The "traditional" practice of packing multiple parameters into a +# space-separated string is a well documented source of bugs and security +# problems, so this is (mostly) avoided, by progressively accumulating +# options in "$@", and eventually passing that to Java. +# +# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, +# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; +# see the in-line comments for details. +# +# There are tweaks for specific operating systems such as AIX, CygWin, +# Darwin, MinGW, and NonStop. +# +# (3) This script is generated from the Groovy template +# https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt +# within the Gradle project. +# +# You can find Gradle at https://github.com/gradle/gradle/. +# +############################################################################## + +# Attempt to set APP_HOME + +# Resolve links: $0 may be a link +app_path=$0 + +# Need this for daisy-chained symlinks. +while + APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path + [ -h "$app_path" ] +do + ls=$( ls -ld "$app_path" ) + link=${ls#*' -> '} + case $link in #( + /*) app_path=$link ;; #( + *) app_path=$APP_HOME$link ;; + esac +done + +# This is normally unused +# shellcheck disable=SC2034 +APP_BASE_NAME=${0##*/} +# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036) +APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s +' "$PWD" ) || exit + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD=maximum + +warn () { + echo "$*" +} >&2 + +die () { + echo + echo "$*" + echo + exit 1 +} >&2 + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "$( uname )" in #( + CYGWIN* ) cygwin=true ;; #( + Darwin* ) darwin=true ;; #( + MSYS* | MINGW* ) msys=true ;; #( + NONSTOP* ) nonstop=true ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD=$JAVA_HOME/jre/sh/java + else + JAVACMD=$JAVA_HOME/bin/java + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD=java + if ! command -v java >/dev/null 2>&1 + then + die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +fi + +# Increase the maximum file descriptors if we can. +if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then + case $MAX_FD in #( + max*) + # In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 + MAX_FD=$( ulimit -H -n ) || + warn "Could not query maximum file descriptor limit" + esac + case $MAX_FD in #( + '' | soft) :;; #( + *) + # In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 + ulimit -n "$MAX_FD" || + warn "Could not set maximum file descriptor limit to $MAX_FD" + esac +fi + +# Collect all arguments for the java command, stacking in reverse order: +# * args from the command line +# * the main class name +# * -classpath +# * -D...appname settings +# * --module-path (only if needed) +# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. + +# For Cygwin or MSYS, switch paths to Windows format before running java +if "$cygwin" || "$msys" ; then + APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) + CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) + + JAVACMD=$( cygpath --unix "$JAVACMD" ) + + # Now convert the arguments - kludge to limit ourselves to /bin/sh + for arg do + if + case $arg in #( + -*) false ;; # don't mess with options #( + /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath + [ -e "$t" ] ;; #( + *) false ;; + esac + then + arg=$( cygpath --path --ignore --mixed "$arg" ) + fi + # Roll the args list around exactly as many times as the number of + # args, so each arg winds up back in the position where it started, but + # possibly modified. + # + # NB: a `for` loop captures its iteration list before it begins, so + # changing the positional parameters here affects neither the number of + # iterations, nor the values presented in `arg`. + shift # remove old arg + set -- "$@" "$arg" # push replacement arg + done +fi + + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' + +# Collect all arguments for the java command: +# * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments, +# and any embedded shellness will be escaped. +# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be +# treated as '${Hostname}' itself on the command line. + +set -- \ + "-Dorg.gradle.appname=$APP_BASE_NAME" \ + -classpath "$CLASSPATH" \ + org.gradle.wrapper.GradleWrapperMain \ + "$@" + +# Stop when "xargs" is not available. +if ! command -v xargs >/dev/null 2>&1 +then + die "xargs is not available" +fi + +# Use "xargs" to parse quoted args. +# +# With -n1 it outputs one arg per line, with the quotes and backslashes removed. +# +# In Bash we could simply go: +# +# readarray ARGS < <( xargs -n1 <<<"$var" ) && +# set -- "${ARGS[@]}" "$@" +# +# but POSIX shell has neither arrays nor command substitution, so instead we +# post-process each arg (as a line of input to sed) to backslash-escape any +# character that might be a shell metacharacter, then use eval to reverse +# that process (while maintaining the separation between arguments), and wrap +# the whole thing up as a single "set" statement. +# +# This will of course break if any of these variables contains a newline or +# an unmatched quote. +# + +eval "set -- $( + printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | + xargs -n1 | + sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | + tr '\n' ' ' + )" '"$@"' + +exec "$JAVACMD" "$@" diff --git a/lesson_25/db/gradlew.bat b/lesson_25/db/gradlew.bat new file mode 100644 index 000000000..9d21a2183 --- /dev/null +++ b/lesson_25/db/gradlew.bat @@ -0,0 +1,94 @@ +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem +@rem SPDX-License-Identifier: Apache-2.0 +@rem + +@if "%DEBUG%"=="" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%"=="" set DIRNAME=. +@rem This is normally unused +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if %ERRORLEVEL% equ 0 goto execute + +echo. 1>&2 +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto execute + +echo. 1>&2 +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 + +goto fail + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* + +:end +@rem End local scope for the variables with windows NT shell +if %ERRORLEVEL% equ 0 goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +set EXIT_CODE=%ERRORLEVEL% +if %EXIT_CODE% equ 0 set EXIT_CODE=1 +if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% +exit /b %EXIT_CODE% + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/lesson_25/db/settings.gradle.kts b/lesson_25/db/settings.gradle.kts new file mode 100644 index 000000000..68449d331 --- /dev/null +++ b/lesson_25/db/settings.gradle.kts @@ -0,0 +1,13 @@ +/* + * This file was generated by the Gradle 'init' task. + * + * The settings file is used to specify which projects to include in your build. + * + * Detailed information about configuring a multi-project build in Gradle can be found + * in the user manual at https://docs.gradle.org/8.0.2/userguide/multi_project_builds.html + */ + +includeBuild("../../lib/java/codedifferently-instructional") + +rootProject.name = "lesson_25" +include("db_app")