From f1e7419ccc845f088d0d22ce420924573aa1b731 Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 28 Feb 2025 12:29:37 +0100 Subject: [PATCH 01/25] add classes and command lines of Spring Boot --- homework03/.gitattributes | 2 + homework03/.gitignore | 33 +++ .../.mvn/wrapper/maven-wrapper.properties | 19 ++ README.md => homework03/README.md | 0 homework03/mvnw | 259 ++++++++++++++++++ homework03/mvnw.cmd | 149 ++++++++++ homework03/pom.xml | 63 +++++ .../homework03/Homework03Application.java | 13 + .../com/ironhack/homework03/model/Author.java | 4 + .../com/ironhack/homework03/model/Book.java | 4 + .../com/ironhack/homework03/model/Issue.java | 4 + .../ironhack/homework03/model/Student.java | 4 + .../src/main/resources/application.properties | 9 + .../Homework03ApplicationTests.java | 13 + 14 files changed, 576 insertions(+) create mode 100644 homework03/.gitattributes create mode 100644 homework03/.gitignore create mode 100644 homework03/.mvn/wrapper/maven-wrapper.properties rename README.md => homework03/README.md (100%) create mode 100644 homework03/mvnw create mode 100644 homework03/mvnw.cmd create mode 100644 homework03/pom.xml create mode 100644 homework03/src/main/java/com/ironhack/homework03/Homework03Application.java create mode 100644 homework03/src/main/java/com/ironhack/homework03/model/Author.java create mode 100644 homework03/src/main/java/com/ironhack/homework03/model/Book.java create mode 100644 homework03/src/main/java/com/ironhack/homework03/model/Issue.java create mode 100644 homework03/src/main/java/com/ironhack/homework03/model/Student.java create mode 100644 homework03/src/main/resources/application.properties create mode 100644 homework03/src/test/java/com/ironhack/homework03/Homework03ApplicationTests.java diff --git a/homework03/.gitattributes b/homework03/.gitattributes new file mode 100644 index 00000000..3b41682a --- /dev/null +++ b/homework03/.gitattributes @@ -0,0 +1,2 @@ +/mvnw text eol=lf +*.cmd text eol=crlf diff --git a/homework03/.gitignore b/homework03/.gitignore new file mode 100644 index 00000000..549e00a2 --- /dev/null +++ b/homework03/.gitignore @@ -0,0 +1,33 @@ +HELP.md +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ diff --git a/homework03/.mvn/wrapper/maven-wrapper.properties b/homework03/.mvn/wrapper/maven-wrapper.properties new file mode 100644 index 00000000..d58dfb70 --- /dev/null +++ b/homework03/.mvn/wrapper/maven-wrapper.properties @@ -0,0 +1,19 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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 +# +# http://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. +wrapperVersion=3.3.2 +distributionType=only-script +distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.9/apache-maven-3.9.9-bin.zip diff --git a/README.md b/homework03/README.md similarity index 100% rename from README.md rename to homework03/README.md diff --git a/homework03/mvnw b/homework03/mvnw new file mode 100644 index 00000000..19529ddf --- /dev/null +++ b/homework03/mvnw @@ -0,0 +1,259 @@ +#!/bin/sh +# ---------------------------------------------------------------------------- +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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 +# +# http://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. +# ---------------------------------------------------------------------------- + +# ---------------------------------------------------------------------------- +# Apache Maven Wrapper startup batch script, version 3.3.2 +# +# Optional ENV vars +# ----------------- +# JAVA_HOME - location of a JDK home dir, required when download maven via java source +# MVNW_REPOURL - repo url base for downloading maven distribution +# MVNW_USERNAME/MVNW_PASSWORD - user and password for downloading maven +# MVNW_VERBOSE - true: enable verbose log; debug: trace the mvnw script; others: silence the output +# ---------------------------------------------------------------------------- + +set -euf +[ "${MVNW_VERBOSE-}" != debug ] || set -x + +# OS specific support. +native_path() { printf %s\\n "$1"; } +case "$(uname)" in +CYGWIN* | MINGW*) + [ -z "${JAVA_HOME-}" ] || JAVA_HOME="$(cygpath --unix "$JAVA_HOME")" + native_path() { cygpath --path --windows "$1"; } + ;; +esac + +# set JAVACMD and JAVACCMD +set_java_home() { + # For Cygwin and MinGW, ensure paths are in Unix format before anything is touched + 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" + JAVACCMD="$JAVA_HOME/jre/sh/javac" + else + JAVACMD="$JAVA_HOME/bin/java" + JAVACCMD="$JAVA_HOME/bin/javac" + + if [ ! -x "$JAVACMD" ] || [ ! -x "$JAVACCMD" ]; then + echo "The JAVA_HOME environment variable is not defined correctly, so mvnw cannot run." >&2 + echo "JAVA_HOME is set to \"$JAVA_HOME\", but \"\$JAVA_HOME/bin/java\" or \"\$JAVA_HOME/bin/javac\" does not exist." >&2 + return 1 + fi + fi + else + JAVACMD="$( + 'set' +e + 'unset' -f command 2>/dev/null + 'command' -v java + )" || : + JAVACCMD="$( + 'set' +e + 'unset' -f command 2>/dev/null + 'command' -v javac + )" || : + + if [ ! -x "${JAVACMD-}" ] || [ ! -x "${JAVACCMD-}" ]; then + echo "The java/javac command does not exist in PATH nor is JAVA_HOME set, so mvnw cannot run." >&2 + return 1 + fi + fi +} + +# hash string like Java String::hashCode +hash_string() { + str="${1:-}" h=0 + while [ -n "$str" ]; do + char="${str%"${str#?}"}" + h=$(((h * 31 + $(LC_CTYPE=C printf %d "'$char")) % 4294967296)) + str="${str#?}" + done + printf %x\\n $h +} + +verbose() { :; } +[ "${MVNW_VERBOSE-}" != true ] || verbose() { printf %s\\n "${1-}"; } + +die() { + printf %s\\n "$1" >&2 + exit 1 +} + +trim() { + # MWRAPPER-139: + # Trims trailing and leading whitespace, carriage returns, tabs, and linefeeds. + # Needed for removing poorly interpreted newline sequences when running in more + # exotic environments such as mingw bash on Windows. + printf "%s" "${1}" | tr -d '[:space:]' +} + +# parse distributionUrl and optional distributionSha256Sum, requires .mvn/wrapper/maven-wrapper.properties +while IFS="=" read -r key value; do + case "${key-}" in + distributionUrl) distributionUrl=$(trim "${value-}") ;; + distributionSha256Sum) distributionSha256Sum=$(trim "${value-}") ;; + esac +done <"${0%/*}/.mvn/wrapper/maven-wrapper.properties" +[ -n "${distributionUrl-}" ] || die "cannot read distributionUrl property in ${0%/*}/.mvn/wrapper/maven-wrapper.properties" + +case "${distributionUrl##*/}" in +maven-mvnd-*bin.*) + MVN_CMD=mvnd.sh _MVNW_REPO_PATTERN=/maven/mvnd/ + case "${PROCESSOR_ARCHITECTURE-}${PROCESSOR_ARCHITEW6432-}:$(uname -a)" in + *AMD64:CYGWIN* | *AMD64:MINGW*) distributionPlatform=windows-amd64 ;; + :Darwin*x86_64) distributionPlatform=darwin-amd64 ;; + :Darwin*arm64) distributionPlatform=darwin-aarch64 ;; + :Linux*x86_64*) distributionPlatform=linux-amd64 ;; + *) + echo "Cannot detect native platform for mvnd on $(uname)-$(uname -m), use pure java version" >&2 + distributionPlatform=linux-amd64 + ;; + esac + distributionUrl="${distributionUrl%-bin.*}-$distributionPlatform.zip" + ;; +maven-mvnd-*) MVN_CMD=mvnd.sh _MVNW_REPO_PATTERN=/maven/mvnd/ ;; +*) MVN_CMD="mvn${0##*/mvnw}" _MVNW_REPO_PATTERN=/org/apache/maven/ ;; +esac + +# apply MVNW_REPOURL and calculate MAVEN_HOME +# maven home pattern: ~/.m2/wrapper/dists/{apache-maven-,maven-mvnd--}/ +[ -z "${MVNW_REPOURL-}" ] || distributionUrl="$MVNW_REPOURL$_MVNW_REPO_PATTERN${distributionUrl#*"$_MVNW_REPO_PATTERN"}" +distributionUrlName="${distributionUrl##*/}" +distributionUrlNameMain="${distributionUrlName%.*}" +distributionUrlNameMain="${distributionUrlNameMain%-bin}" +MAVEN_USER_HOME="${MAVEN_USER_HOME:-${HOME}/.m2}" +MAVEN_HOME="${MAVEN_USER_HOME}/wrapper/dists/${distributionUrlNameMain-}/$(hash_string "$distributionUrl")" + +exec_maven() { + unset MVNW_VERBOSE MVNW_USERNAME MVNW_PASSWORD MVNW_REPOURL || : + exec "$MAVEN_HOME/bin/$MVN_CMD" "$@" || die "cannot exec $MAVEN_HOME/bin/$MVN_CMD" +} + +if [ -d "$MAVEN_HOME" ]; then + verbose "found existing MAVEN_HOME at $MAVEN_HOME" + exec_maven "$@" +fi + +case "${distributionUrl-}" in +*?-bin.zip | *?maven-mvnd-?*-?*.zip) ;; +*) die "distributionUrl is not valid, must match *-bin.zip or maven-mvnd-*.zip, but found '${distributionUrl-}'" ;; +esac + +# prepare tmp dir +if TMP_DOWNLOAD_DIR="$(mktemp -d)" && [ -d "$TMP_DOWNLOAD_DIR" ]; then + clean() { rm -rf -- "$TMP_DOWNLOAD_DIR"; } + trap clean HUP INT TERM EXIT +else + die "cannot create temp dir" +fi + +mkdir -p -- "${MAVEN_HOME%/*}" + +# Download and Install Apache Maven +verbose "Couldn't find MAVEN_HOME, downloading and installing it ..." +verbose "Downloading from: $distributionUrl" +verbose "Downloading to: $TMP_DOWNLOAD_DIR/$distributionUrlName" + +# select .zip or .tar.gz +if ! command -v unzip >/dev/null; then + distributionUrl="${distributionUrl%.zip}.tar.gz" + distributionUrlName="${distributionUrl##*/}" +fi + +# verbose opt +__MVNW_QUIET_WGET=--quiet __MVNW_QUIET_CURL=--silent __MVNW_QUIET_UNZIP=-q __MVNW_QUIET_TAR='' +[ "${MVNW_VERBOSE-}" != true ] || __MVNW_QUIET_WGET='' __MVNW_QUIET_CURL='' __MVNW_QUIET_UNZIP='' __MVNW_QUIET_TAR=v + +# normalize http auth +case "${MVNW_PASSWORD:+has-password}" in +'') MVNW_USERNAME='' MVNW_PASSWORD='' ;; +has-password) [ -n "${MVNW_USERNAME-}" ] || MVNW_USERNAME='' MVNW_PASSWORD='' ;; +esac + +if [ -z "${MVNW_USERNAME-}" ] && command -v wget >/dev/null; then + verbose "Found wget ... using wget" + wget ${__MVNW_QUIET_WGET:+"$__MVNW_QUIET_WGET"} "$distributionUrl" -O "$TMP_DOWNLOAD_DIR/$distributionUrlName" || die "wget: Failed to fetch $distributionUrl" +elif [ -z "${MVNW_USERNAME-}" ] && command -v curl >/dev/null; then + verbose "Found curl ... using curl" + curl ${__MVNW_QUIET_CURL:+"$__MVNW_QUIET_CURL"} -f -L -o "$TMP_DOWNLOAD_DIR/$distributionUrlName" "$distributionUrl" || die "curl: Failed to fetch $distributionUrl" +elif set_java_home; then + verbose "Falling back to use Java to download" + javaSource="$TMP_DOWNLOAD_DIR/Downloader.java" + targetZip="$TMP_DOWNLOAD_DIR/$distributionUrlName" + cat >"$javaSource" <<-END + public class Downloader extends java.net.Authenticator + { + protected java.net.PasswordAuthentication getPasswordAuthentication() + { + return new java.net.PasswordAuthentication( System.getenv( "MVNW_USERNAME" ), System.getenv( "MVNW_PASSWORD" ).toCharArray() ); + } + public static void main( String[] args ) throws Exception + { + setDefault( new Downloader() ); + java.nio.file.Files.copy( java.net.URI.create( args[0] ).toURL().openStream(), java.nio.file.Paths.get( args[1] ).toAbsolutePath().normalize() ); + } + } + END + # For Cygwin/MinGW, switch paths to Windows format before running javac and java + verbose " - Compiling Downloader.java ..." + "$(native_path "$JAVACCMD")" "$(native_path "$javaSource")" || die "Failed to compile Downloader.java" + verbose " - Running Downloader.java ..." + "$(native_path "$JAVACMD")" -cp "$(native_path "$TMP_DOWNLOAD_DIR")" Downloader "$distributionUrl" "$(native_path "$targetZip")" +fi + +# If specified, validate the SHA-256 sum of the Maven distribution zip file +if [ -n "${distributionSha256Sum-}" ]; then + distributionSha256Result=false + if [ "$MVN_CMD" = mvnd.sh ]; then + echo "Checksum validation is not supported for maven-mvnd." >&2 + echo "Please disable validation by removing 'distributionSha256Sum' from your maven-wrapper.properties." >&2 + exit 1 + elif command -v sha256sum >/dev/null; then + if echo "$distributionSha256Sum $TMP_DOWNLOAD_DIR/$distributionUrlName" | sha256sum -c >/dev/null 2>&1; then + distributionSha256Result=true + fi + elif command -v shasum >/dev/null; then + if echo "$distributionSha256Sum $TMP_DOWNLOAD_DIR/$distributionUrlName" | shasum -a 256 -c >/dev/null 2>&1; then + distributionSha256Result=true + fi + else + echo "Checksum validation was requested but neither 'sha256sum' or 'shasum' are available." >&2 + echo "Please install either command, or disable validation by removing 'distributionSha256Sum' from your maven-wrapper.properties." >&2 + exit 1 + fi + if [ $distributionSha256Result = false ]; then + echo "Error: Failed to validate Maven distribution SHA-256, your Maven distribution might be compromised." >&2 + echo "If you updated your Maven version, you need to update the specified distributionSha256Sum property." >&2 + exit 1 + fi +fi + +# unzip and move +if command -v unzip >/dev/null; then + unzip ${__MVNW_QUIET_UNZIP:+"$__MVNW_QUIET_UNZIP"} "$TMP_DOWNLOAD_DIR/$distributionUrlName" -d "$TMP_DOWNLOAD_DIR" || die "failed to unzip" +else + tar xzf${__MVNW_QUIET_TAR:+"$__MVNW_QUIET_TAR"} "$TMP_DOWNLOAD_DIR/$distributionUrlName" -C "$TMP_DOWNLOAD_DIR" || die "failed to untar" +fi +printf %s\\n "$distributionUrl" >"$TMP_DOWNLOAD_DIR/$distributionUrlNameMain/mvnw.url" +mv -- "$TMP_DOWNLOAD_DIR/$distributionUrlNameMain" "$MAVEN_HOME" || [ -d "$MAVEN_HOME" ] || die "fail to move MAVEN_HOME" + +clean || : +exec_maven "$@" diff --git a/homework03/mvnw.cmd b/homework03/mvnw.cmd new file mode 100644 index 00000000..249bdf38 --- /dev/null +++ b/homework03/mvnw.cmd @@ -0,0 +1,149 @@ +<# : batch portion +@REM ---------------------------------------------------------------------------- +@REM Licensed to the Apache Software Foundation (ASF) under one +@REM or more contributor license agreements. See the NOTICE file +@REM distributed with this work for additional information +@REM regarding copyright ownership. The ASF licenses this file +@REM to you under the Apache License, Version 2.0 (the +@REM "License"); you may not use this file except in compliance +@REM with the License. You may obtain a copy of the License at +@REM +@REM http://www.apache.org/licenses/LICENSE-2.0 +@REM +@REM Unless required by applicable law or agreed to in writing, +@REM software distributed under the License is distributed on an +@REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +@REM KIND, either express or implied. See the License for the +@REM specific language governing permissions and limitations +@REM under the License. +@REM ---------------------------------------------------------------------------- + +@REM ---------------------------------------------------------------------------- +@REM Apache Maven Wrapper startup batch script, version 3.3.2 +@REM +@REM Optional ENV vars +@REM MVNW_REPOURL - repo url base for downloading maven distribution +@REM MVNW_USERNAME/MVNW_PASSWORD - user and password for downloading maven +@REM MVNW_VERBOSE - true: enable verbose log; others: silence the output +@REM ---------------------------------------------------------------------------- + +@IF "%__MVNW_ARG0_NAME__%"=="" (SET __MVNW_ARG0_NAME__=%~nx0) +@SET __MVNW_CMD__= +@SET __MVNW_ERROR__= +@SET __MVNW_PSMODULEP_SAVE=%PSModulePath% +@SET PSModulePath= +@FOR /F "usebackq tokens=1* delims==" %%A IN (`powershell -noprofile "& {$scriptDir='%~dp0'; $script='%__MVNW_ARG0_NAME__%'; icm -ScriptBlock ([Scriptblock]::Create((Get-Content -Raw '%~f0'))) -NoNewScope}"`) DO @( + IF "%%A"=="MVN_CMD" (set __MVNW_CMD__=%%B) ELSE IF "%%B"=="" (echo %%A) ELSE (echo %%A=%%B) +) +@SET PSModulePath=%__MVNW_PSMODULEP_SAVE% +@SET __MVNW_PSMODULEP_SAVE= +@SET __MVNW_ARG0_NAME__= +@SET MVNW_USERNAME= +@SET MVNW_PASSWORD= +@IF NOT "%__MVNW_CMD__%"=="" (%__MVNW_CMD__% %*) +@echo Cannot start maven from wrapper >&2 && exit /b 1 +@GOTO :EOF +: end batch / begin powershell #> + +$ErrorActionPreference = "Stop" +if ($env:MVNW_VERBOSE -eq "true") { + $VerbosePreference = "Continue" +} + +# calculate distributionUrl, requires .mvn/wrapper/maven-wrapper.properties +$distributionUrl = (Get-Content -Raw "$scriptDir/.mvn/wrapper/maven-wrapper.properties" | ConvertFrom-StringData).distributionUrl +if (!$distributionUrl) { + Write-Error "cannot read distributionUrl property in $scriptDir/.mvn/wrapper/maven-wrapper.properties" +} + +switch -wildcard -casesensitive ( $($distributionUrl -replace '^.*/','') ) { + "maven-mvnd-*" { + $USE_MVND = $true + $distributionUrl = $distributionUrl -replace '-bin\.[^.]*$',"-windows-amd64.zip" + $MVN_CMD = "mvnd.cmd" + break + } + default { + $USE_MVND = $false + $MVN_CMD = $script -replace '^mvnw','mvn' + break + } +} + +# apply MVNW_REPOURL and calculate MAVEN_HOME +# maven home pattern: ~/.m2/wrapper/dists/{apache-maven-,maven-mvnd--}/ +if ($env:MVNW_REPOURL) { + $MVNW_REPO_PATTERN = if ($USE_MVND) { "/org/apache/maven/" } else { "/maven/mvnd/" } + $distributionUrl = "$env:MVNW_REPOURL$MVNW_REPO_PATTERN$($distributionUrl -replace '^.*'+$MVNW_REPO_PATTERN,'')" +} +$distributionUrlName = $distributionUrl -replace '^.*/','' +$distributionUrlNameMain = $distributionUrlName -replace '\.[^.]*$','' -replace '-bin$','' +$MAVEN_HOME_PARENT = "$HOME/.m2/wrapper/dists/$distributionUrlNameMain" +if ($env:MAVEN_USER_HOME) { + $MAVEN_HOME_PARENT = "$env:MAVEN_USER_HOME/wrapper/dists/$distributionUrlNameMain" +} +$MAVEN_HOME_NAME = ([System.Security.Cryptography.MD5]::Create().ComputeHash([byte[]][char[]]$distributionUrl) | ForEach-Object {$_.ToString("x2")}) -join '' +$MAVEN_HOME = "$MAVEN_HOME_PARENT/$MAVEN_HOME_NAME" + +if (Test-Path -Path "$MAVEN_HOME" -PathType Container) { + Write-Verbose "found existing MAVEN_HOME at $MAVEN_HOME" + Write-Output "MVN_CMD=$MAVEN_HOME/bin/$MVN_CMD" + exit $? +} + +if (! $distributionUrlNameMain -or ($distributionUrlName -eq $distributionUrlNameMain)) { + Write-Error "distributionUrl is not valid, must end with *-bin.zip, but found $distributionUrl" +} + +# prepare tmp dir +$TMP_DOWNLOAD_DIR_HOLDER = New-TemporaryFile +$TMP_DOWNLOAD_DIR = New-Item -Itemtype Directory -Path "$TMP_DOWNLOAD_DIR_HOLDER.dir" +$TMP_DOWNLOAD_DIR_HOLDER.Delete() | Out-Null +trap { + if ($TMP_DOWNLOAD_DIR.Exists) { + try { Remove-Item $TMP_DOWNLOAD_DIR -Recurse -Force | Out-Null } + catch { Write-Warning "Cannot remove $TMP_DOWNLOAD_DIR" } + } +} + +New-Item -Itemtype Directory -Path "$MAVEN_HOME_PARENT" -Force | Out-Null + +# Download and Install Apache Maven +Write-Verbose "Couldn't find MAVEN_HOME, downloading and installing it ..." +Write-Verbose "Downloading from: $distributionUrl" +Write-Verbose "Downloading to: $TMP_DOWNLOAD_DIR/$distributionUrlName" + +$webclient = New-Object System.Net.WebClient +if ($env:MVNW_USERNAME -and $env:MVNW_PASSWORD) { + $webclient.Credentials = New-Object System.Net.NetworkCredential($env:MVNW_USERNAME, $env:MVNW_PASSWORD) +} +[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 +$webclient.DownloadFile($distributionUrl, "$TMP_DOWNLOAD_DIR/$distributionUrlName") | Out-Null + +# If specified, validate the SHA-256 sum of the Maven distribution zip file +$distributionSha256Sum = (Get-Content -Raw "$scriptDir/.mvn/wrapper/maven-wrapper.properties" | ConvertFrom-StringData).distributionSha256Sum +if ($distributionSha256Sum) { + if ($USE_MVND) { + Write-Error "Checksum validation is not supported for maven-mvnd. `nPlease disable validation by removing 'distributionSha256Sum' from your maven-wrapper.properties." + } + Import-Module $PSHOME\Modules\Microsoft.PowerShell.Utility -Function Get-FileHash + if ((Get-FileHash "$TMP_DOWNLOAD_DIR/$distributionUrlName" -Algorithm SHA256).Hash.ToLower() -ne $distributionSha256Sum) { + Write-Error "Error: Failed to validate Maven distribution SHA-256, your Maven distribution might be compromised. If you updated your Maven version, you need to update the specified distributionSha256Sum property." + } +} + +# unzip and move +Expand-Archive "$TMP_DOWNLOAD_DIR/$distributionUrlName" -DestinationPath "$TMP_DOWNLOAD_DIR" | Out-Null +Rename-Item -Path "$TMP_DOWNLOAD_DIR/$distributionUrlNameMain" -NewName $MAVEN_HOME_NAME | Out-Null +try { + Move-Item -Path "$TMP_DOWNLOAD_DIR/$MAVEN_HOME_NAME" -Destination $MAVEN_HOME_PARENT | Out-Null +} catch { + if (! (Test-Path -Path "$MAVEN_HOME" -PathType Container)) { + Write-Error "fail to move MAVEN_HOME" + } +} finally { + try { Remove-Item $TMP_DOWNLOAD_DIR -Recurse -Force | Out-Null } + catch { Write-Warning "Cannot remove $TMP_DOWNLOAD_DIR" } +} + +Write-Output "MVN_CMD=$MAVEN_HOME/bin/$MVN_CMD" diff --git a/homework03/pom.xml b/homework03/pom.xml new file mode 100644 index 00000000..ae1a0d12 --- /dev/null +++ b/homework03/pom.xml @@ -0,0 +1,63 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 3.3.9 + + + com.ironhack + homework03 + 0.0.1-SNAPSHOT + homework03 + Demo project for Spring Boot + + + + + + + + + + + + + + + 17 + + + + org.springframework.boot + spring-boot-starter-data-jpa + + + org.springframework.boot + spring-boot-starter-web + + + + com.mysql + mysql-connector-j + runtime + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/homework03/src/main/java/com/ironhack/homework03/Homework03Application.java b/homework03/src/main/java/com/ironhack/homework03/Homework03Application.java new file mode 100644 index 00000000..78f6547b --- /dev/null +++ b/homework03/src/main/java/com/ironhack/homework03/Homework03Application.java @@ -0,0 +1,13 @@ +package com.ironhack.homework03; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class Homework03Application { + + public static void main(String[] args) { + SpringApplication.run(Homework03Application.class, args); + } + +} diff --git a/homework03/src/main/java/com/ironhack/homework03/model/Author.java b/homework03/src/main/java/com/ironhack/homework03/model/Author.java new file mode 100644 index 00000000..0d8fdbfe --- /dev/null +++ b/homework03/src/main/java/com/ironhack/homework03/model/Author.java @@ -0,0 +1,4 @@ +package com.ironhack.homework03.model; + +public class Author { +} diff --git a/homework03/src/main/java/com/ironhack/homework03/model/Book.java b/homework03/src/main/java/com/ironhack/homework03/model/Book.java new file mode 100644 index 00000000..10a3717d --- /dev/null +++ b/homework03/src/main/java/com/ironhack/homework03/model/Book.java @@ -0,0 +1,4 @@ +package com.ironhack.homework03.model; + +public class Book { +} diff --git a/homework03/src/main/java/com/ironhack/homework03/model/Issue.java b/homework03/src/main/java/com/ironhack/homework03/model/Issue.java new file mode 100644 index 00000000..6b39d457 --- /dev/null +++ b/homework03/src/main/java/com/ironhack/homework03/model/Issue.java @@ -0,0 +1,4 @@ +package com.ironhack.homework03.model; + +public class Issue { +} diff --git a/homework03/src/main/java/com/ironhack/homework03/model/Student.java b/homework03/src/main/java/com/ironhack/homework03/model/Student.java new file mode 100644 index 00000000..1a26f588 --- /dev/null +++ b/homework03/src/main/java/com/ironhack/homework03/model/Student.java @@ -0,0 +1,4 @@ +package com.ironhack.homework03.model; + +public class Student { +} diff --git a/homework03/src/main/resources/application.properties b/homework03/src/main/resources/application.properties new file mode 100644 index 00000000..964ec8b3 --- /dev/null +++ b/homework03/src/main/resources/application.properties @@ -0,0 +1,9 @@ +spring.application.name=homework03 + +#spring.datasource.url=jdbc:mysql://localhost:3306/course?createDatabaseIfNotExist=true&serverTimezone=UTC +#spring.datasource.username=root +#spring.datasource.password= +#spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver +#spring.jpa.hibernate.ddl-auto=update +#spring.jpa.show-sql=true +#spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true \ No newline at end of file diff --git a/homework03/src/test/java/com/ironhack/homework03/Homework03ApplicationTests.java b/homework03/src/test/java/com/ironhack/homework03/Homework03ApplicationTests.java new file mode 100644 index 00000000..3da784a0 --- /dev/null +++ b/homework03/src/test/java/com/ironhack/homework03/Homework03ApplicationTests.java @@ -0,0 +1,13 @@ +package com.ironhack.homework03; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class Homework03ApplicationTests { + + @Test + void contextLoads() { + } + +} From 766942946a01c9a30d4c4c76f983df7a6d643ea2 Mon Sep 17 00:00:00 2001 From: alvaro Date: Mon, 3 Mar 2025 08:59:04 +0100 Subject: [PATCH 02/25] setup -error --- homework03/pom.xml | 10 ++++++++ .../com/ironhack/homework03/model/Author.java | 18 +++++++++++++ .../com/ironhack/homework03/model/Book.java | 22 ++++++++++++++++ .../com/ironhack/homework03/model/Issue.java | 25 ++++++++++++++++++- .../ironhack/homework03/model/Student.java | 19 +++++++++++++- .../src/main/resources/application.properties | 14 +++++------ 6 files changed, 99 insertions(+), 9 deletions(-) diff --git a/homework03/pom.xml b/homework03/pom.xml index ae1a0d12..6c57bb4d 100644 --- a/homework03/pom.xml +++ b/homework03/pom.xml @@ -49,6 +49,16 @@ spring-boot-starter-test test + + org.projectlombok + lombok + true + + + org.projectlombok + lombok + provided + diff --git a/homework03/src/main/java/com/ironhack/homework03/model/Author.java b/homework03/src/main/java/com/ironhack/homework03/model/Author.java index 0d8fdbfe..33039bb4 100644 --- a/homework03/src/main/java/com/ironhack/homework03/model/Author.java +++ b/homework03/src/main/java/com/ironhack/homework03/model/Author.java @@ -1,4 +1,22 @@ package com.ironhack.homework03.model; +import jakarta.persistence.*; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Entity +@Data +@NoArgsConstructor +@AllArgsConstructor public class Author { + @Id + @GeneratedValue( strategy = GenerationType.IDENTITY) + private Integer id; + private String name; + private String email; + + @OneToOne + @JoinColumn(name = "book_id") + private Book authorBook; } diff --git a/homework03/src/main/java/com/ironhack/homework03/model/Book.java b/homework03/src/main/java/com/ironhack/homework03/model/Book.java index 10a3717d..df050320 100644 --- a/homework03/src/main/java/com/ironhack/homework03/model/Book.java +++ b/homework03/src/main/java/com/ironhack/homework03/model/Book.java @@ -1,4 +1,26 @@ package com.ironhack.homework03.model; +import jakarta.persistence.Entity; +import jakarta.persistence.Id; +import jakarta.persistence.OneToOne; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Entity +@Data +@NoArgsConstructor +@AllArgsConstructor public class Book { + @Id + private String isbn; + private String title; + private String category; + private int quantity; + + @OneToOne(mappedBy = "authorBook") + private Book book; + + @OneToOne(mappedBy = "issueBook") + private Issue issue; } diff --git a/homework03/src/main/java/com/ironhack/homework03/model/Issue.java b/homework03/src/main/java/com/ironhack/homework03/model/Issue.java index 6b39d457..15f01dd1 100644 --- a/homework03/src/main/java/com/ironhack/homework03/model/Issue.java +++ b/homework03/src/main/java/com/ironhack/homework03/model/Issue.java @@ -1,4 +1,27 @@ package com.ironhack.homework03.model; +import jakarta.persistence.*; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Entity +@Data +@NoArgsConstructor +@AllArgsConstructor public class Issue { -} + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Integer issueId; + private String issueDate; + private String returnDate; + + @OneToOne + @JoinColumn(name = "student_id") + private Student issueStudent; + + @OneToOne + @JoinColumn(name = "book_id") + private Book issueBook; + +} \ No newline at end of file diff --git a/homework03/src/main/java/com/ironhack/homework03/model/Student.java b/homework03/src/main/java/com/ironhack/homework03/model/Student.java index 1a26f588..aebabacb 100644 --- a/homework03/src/main/java/com/ironhack/homework03/model/Student.java +++ b/homework03/src/main/java/com/ironhack/homework03/model/Student.java @@ -1,4 +1,21 @@ package com.ironhack.homework03.model; +import jakarta.persistence.Entity; +import jakarta.persistence.Id; +import jakarta.persistence.OneToOne; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Entity +@Data +@NoArgsConstructor +@AllArgsConstructor public class Student { -} + @Id + private String usn; + private String name; + + @OneToOne(mappedBy = "studentIssue") + private Issue issue; +} \ No newline at end of file diff --git a/homework03/src/main/resources/application.properties b/homework03/src/main/resources/application.properties index 964ec8b3..745b1b14 100644 --- a/homework03/src/main/resources/application.properties +++ b/homework03/src/main/resources/application.properties @@ -1,9 +1,9 @@ spring.application.name=homework03 -#spring.datasource.url=jdbc:mysql://localhost:3306/course?createDatabaseIfNotExist=true&serverTimezone=UTC -#spring.datasource.username=root -#spring.datasource.password= -#spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver -#spring.jpa.hibernate.ddl-auto=update -#spring.jpa.show-sql=true -#spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true \ No newline at end of file +spring.datasource.url=jdbc:mysql://localhost:3306/homework3?createDatabaseIfNotExist=true&serverTimezone=UTC +spring.datasource.username=root +spring.datasource.password=root +spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver +spring.jpa.hibernate.ddl-auto=update +spring.jpa.show-sql=true +spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true \ No newline at end of file From 14616b1ad796133febc268a323da92e1d8d6b22c Mon Sep 17 00:00:00 2001 From: alvaro Date: Mon, 3 Mar 2025 11:53:37 +0100 Subject: [PATCH 03/25] fix errors --- .../java/com/ironhack/homework03/model/Author.java | 4 ++-- .../main/java/com/ironhack/homework03/model/Book.java | 10 +++++----- .../main/java/com/ironhack/homework03/model/Issue.java | 4 ++-- .../java/com/ironhack/homework03/model/Student.java | 4 ++-- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/homework03/src/main/java/com/ironhack/homework03/model/Author.java b/homework03/src/main/java/com/ironhack/homework03/model/Author.java index 33039bb4..a9ecdec1 100644 --- a/homework03/src/main/java/com/ironhack/homework03/model/Author.java +++ b/homework03/src/main/java/com/ironhack/homework03/model/Author.java @@ -16,7 +16,7 @@ public class Author { private String name; private String email; - @OneToOne + @OneToOne(cascade = CascadeType.ALL) @JoinColumn(name = "book_id") - private Book authorBook; + private Book authorbook; } diff --git a/homework03/src/main/java/com/ironhack/homework03/model/Book.java b/homework03/src/main/java/com/ironhack/homework03/model/Book.java index df050320..294c45f7 100644 --- a/homework03/src/main/java/com/ironhack/homework03/model/Book.java +++ b/homework03/src/main/java/com/ironhack/homework03/model/Book.java @@ -18,9 +18,9 @@ public class Book { private String category; private int quantity; - @OneToOne(mappedBy = "authorBook") - private Book book; - - @OneToOne(mappedBy = "issueBook") - private Issue issue; +// @OneToOne(mappedBy = "authorbook") +// private Book book; +// +// @OneToOne(mappedBy = "issuebook") +// private Issue issue; } diff --git a/homework03/src/main/java/com/ironhack/homework03/model/Issue.java b/homework03/src/main/java/com/ironhack/homework03/model/Issue.java index 15f01dd1..cdf6fd3f 100644 --- a/homework03/src/main/java/com/ironhack/homework03/model/Issue.java +++ b/homework03/src/main/java/com/ironhack/homework03/model/Issue.java @@ -18,10 +18,10 @@ public class Issue { @OneToOne @JoinColumn(name = "student_id") - private Student issueStudent; + private Student issuestudent; @OneToOne @JoinColumn(name = "book_id") - private Book issueBook; + private Book issuebook; } \ No newline at end of file diff --git a/homework03/src/main/java/com/ironhack/homework03/model/Student.java b/homework03/src/main/java/com/ironhack/homework03/model/Student.java index aebabacb..c4207d2c 100644 --- a/homework03/src/main/java/com/ironhack/homework03/model/Student.java +++ b/homework03/src/main/java/com/ironhack/homework03/model/Student.java @@ -16,6 +16,6 @@ public class Student { private String usn; private String name; - @OneToOne(mappedBy = "studentIssue") - private Issue issue; +// @OneToOne(mappedBy = "studentissue") +// private Issue issue; } \ No newline at end of file From 07dc6b6d34e30b961239ade20af4f698e5d2d393 Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 4 Mar 2025 10:37:38 +0100 Subject: [PATCH 04/25] command --- homework03/pom.xml | 14 +++++++-- .../com/ironhack/homework03/model/Book.java | 31 ++++++++++++++++++- .../homework03/model/IBookRepository.java | 21 +++++++++++++ .../com/ironhack/homework03/model/Menu.java | 24 ++++++++++++++ .../ironhack/homework03/model/Options.java | 8 +++++ .../src/main/resources/application.properties | 14 ++++----- 6 files changed, 102 insertions(+), 10 deletions(-) create mode 100644 homework03/src/main/java/com/ironhack/homework03/model/IBookRepository.java create mode 100644 homework03/src/main/java/com/ironhack/homework03/model/Menu.java create mode 100644 homework03/src/main/java/com/ironhack/homework03/model/Options.java diff --git a/homework03/pom.xml b/homework03/pom.xml index ae1a0d12..3c27d596 100644 --- a/homework03/pom.xml +++ b/homework03/pom.xml @@ -1,6 +1,6 @@ + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 org.springframework.boot @@ -49,6 +49,16 @@ spring-boot-starter-test test + + org.projectlombok + lombok + true + + + org.projectlombok + lombok + provided + @@ -60,4 +70,4 @@ - + \ No newline at end of file diff --git a/homework03/src/main/java/com/ironhack/homework03/model/Book.java b/homework03/src/main/java/com/ironhack/homework03/model/Book.java index 10a3717d..dab28399 100644 --- a/homework03/src/main/java/com/ironhack/homework03/model/Book.java +++ b/homework03/src/main/java/com/ironhack/homework03/model/Book.java @@ -1,4 +1,33 @@ package com.ironhack.homework03.model; -public class Book { +import jakarta.persistence.Entity; +import jakarta.persistence.Id; +import jakarta.persistence.OneToOne; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + + @Entity + @Data + @NoArgsConstructor + @AllArgsConstructor + public class Book { + @Id + private String isbn; + private String title; + private String category; + private int quantity; + + public Book(String isbn, String title, String category, int quantity) { + this.isbn = isbn; + this.title = title; + this.category = category; + this.quantity = quantity; + } + + // @OneToOne(mappedBy = "authorbook") +// private Book book; +// +// @OneToOne(mappedBy = "issuebook") +// private Issue issue; } diff --git a/homework03/src/main/java/com/ironhack/homework03/model/IBookRepository.java b/homework03/src/main/java/com/ironhack/homework03/model/IBookRepository.java new file mode 100644 index 00000000..6d428711 --- /dev/null +++ b/homework03/src/main/java/com/ironhack/homework03/model/IBookRepository.java @@ -0,0 +1,21 @@ +package com.ironhack.homework03.model; + +import jakarta.transaction.Transactional; +import org.springframework.data.jpa.repository.Modifying; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; +import org.springframework.stereotype.Repository; + +@Repository +public interface IBookRepository { + + @Modifying + @Transactional + @Query("INSERT INTO Book (isbn, title, category, quality) VALUES (:isbn, :title, :category, :quality)") + void addBook(@Param("isbn") String isbn ,@Param("title") String title, @Param("category") String category, @Param("quality") Integer quality); + + Book findByTitle(String title); + Book findByCategory(String category); + Book findByAuthor(String author); + +} diff --git a/homework03/src/main/java/com/ironhack/homework03/model/Menu.java b/homework03/src/main/java/com/ironhack/homework03/model/Menu.java new file mode 100644 index 00000000..db6f9692 --- /dev/null +++ b/homework03/src/main/java/com/ironhack/homework03/model/Menu.java @@ -0,0 +1,24 @@ +package com.ironhack.homework03.model; + +public class Menu { + +// public void showMenu() { +// System.out.println(Style.SUCCESS+Style.BOLD + "\nFantastic! All data introduced is correct. Then, we provide a command list " + +// "for managing or adding a new information." + Style.RESET); +// System.out.println(Style.BOLD + "\n--------------------------------- COMMAND LIST ---------------------------------\n" + Style.RESET); +// System.out.println( +// "- "+Style.SUCCESS_BG+Style.BOLD+" Add a book "+Style.RESET+ +// "\n- "+Style.SUCCESS_BG+Style.BOLD+" Search book by title "+Style.RESET+ +// "\n- "+Style.SUCCESS_BG+Style.BOLD+" Search book by category " +Style.RESET+ +// "\n- "+Style.SUCCESS_BG+Style.BOLD+" Search book by Author "+ Style.RESET+ +// "\n- "+Style.SUCCESS_BG+Style.BOLD+" List all books along with author " +Style.RESET+ +// "\n- "+Style.SUCCESS_BG+Style.BOLD+" Issue book to student "+ Style.RESET+ +// "\n- "+Style.SUCCESS_BG+Style.BOLD+" List books by usn " +Style.RESET+ +// "\n- "+Style.SUCCESS_BG+Style.BOLD+" Exit "+ Style.RESET); +// System.out.println(Style.BOLD + "\n--------------------------------------------------------------------------------" + Style.RESET); +// } + + + + +} diff --git a/homework03/src/main/java/com/ironhack/homework03/model/Options.java b/homework03/src/main/java/com/ironhack/homework03/model/Options.java new file mode 100644 index 00000000..d3fd1087 --- /dev/null +++ b/homework03/src/main/java/com/ironhack/homework03/model/Options.java @@ -0,0 +1,8 @@ +package com.ironhack.homework03.model; + +public class Options { + + + + +} diff --git a/homework03/src/main/resources/application.properties b/homework03/src/main/resources/application.properties index 964ec8b3..c15881a9 100644 --- a/homework03/src/main/resources/application.properties +++ b/homework03/src/main/resources/application.properties @@ -1,9 +1,9 @@ spring.application.name=homework03 -#spring.datasource.url=jdbc:mysql://localhost:3306/course?createDatabaseIfNotExist=true&serverTimezone=UTC -#spring.datasource.username=root -#spring.datasource.password= -#spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver -#spring.jpa.hibernate.ddl-auto=update -#spring.jpa.show-sql=true -#spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true \ No newline at end of file +spring.datasource.url=jdbc:mysql://localhost:3306/course?createDatabaseIfNotExist=true&serverTimezone=UTC +spring.datasource.username=root +spring.datasource.password= +spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver +spring.jpa.hibernate.ddl-auto=update +spring.jpa.show-sql=true +spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true \ No newline at end of file From 0f1a8cec503e548fe1bde5b587fdf467fef6c930 Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 4 Mar 2025 11:22:19 +0100 Subject: [PATCH 05/25] Add the test structure in BookRepositoryTest. There are nothing test. --- .../com/ironhack/homework03/model/Book.java | 24 ++---------- .../homework03/model/IBookRepositoryTest.java | 38 +++++++++++++++++++ 2 files changed, 41 insertions(+), 21 deletions(-) create mode 100644 homework03/src/test/java/com/ironhack/homework03/model/IBookRepositoryTest.java diff --git a/homework03/src/main/java/com/ironhack/homework03/model/Book.java b/homework03/src/main/java/com/ironhack/homework03/model/Book.java index eab07034..a30e06cb 100644 --- a/homework03/src/main/java/com/ironhack/homework03/model/Book.java +++ b/homework03/src/main/java/com/ironhack/homework03/model/Book.java @@ -7,20 +7,6 @@ import lombok.Data; import lombok.NoArgsConstructor; -<<<<<<< HEAD -@Entity -@Data -@NoArgsConstructor -@AllArgsConstructor -public class Book { - @Id - private String isbn; - private String title; - private String category; - private int quantity; - -// @OneToOne(mappedBy = "authorbook") -======= @Entity @Data @NoArgsConstructor @@ -32,15 +18,11 @@ public class Book { private String category; private int quantity; - public Book(String isbn, String title, String category, int quantity) { - this.isbn = isbn; - this.title = title; - this.category = category; - this.quantity = quantity; - } + + // @OneToOne(mappedBy = "authorbook") ->>>>>>> maindev03 + // private Book book; // // @OneToOne(mappedBy = "issuebook") diff --git a/homework03/src/test/java/com/ironhack/homework03/model/IBookRepositoryTest.java b/homework03/src/test/java/com/ironhack/homework03/model/IBookRepositoryTest.java new file mode 100644 index 00000000..7bda43bd --- /dev/null +++ b/homework03/src/test/java/com/ironhack/homework03/model/IBookRepositoryTest.java @@ -0,0 +1,38 @@ +package com.ironhack.homework03.model; + +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; + +class IBookRepositoryTest { +// +// @BeforeAll +// public void setUp(){ +// +// } +// +// @AfterAll +// public void tearDown(){ +// +// } +// +// @Test +// void addBook() { +// } +// +// @Test +// void findByTitle() { +// } +// +// @Test +// void findByCategory() { +// } +// +// @Test +// void findByAuthor() { +// } +} \ No newline at end of file From 0f9a1432fa2b53fbb1863b1a8d91d3a9d5fdc3ea Mon Sep 17 00:00:00 2001 From: alvaro Date: Tue, 4 Mar 2025 12:43:21 +0100 Subject: [PATCH 06/25] repositories and tests --- .../com/ironhack/homework03/model/Issue.java | 4 +- .../repository/AuthorRepository.java | 10 +++ .../homework03/repository/BookRepository.java | 9 +++ .../repository/IssueRepository.java | 9 +++ .../repository/StudentRepository.java | 9 +++ .../ironhack/homework03/model/IssueTest.java | 79 +++++++++++++++++++ 6 files changed, 118 insertions(+), 2 deletions(-) create mode 100644 homework03/src/main/java/com/ironhack/homework03/repository/AuthorRepository.java create mode 100644 homework03/src/main/java/com/ironhack/homework03/repository/BookRepository.java create mode 100644 homework03/src/main/java/com/ironhack/homework03/repository/IssueRepository.java create mode 100644 homework03/src/main/java/com/ironhack/homework03/repository/StudentRepository.java create mode 100644 homework03/src/test/java/com/ironhack/homework03/model/IssueTest.java diff --git a/homework03/src/main/java/com/ironhack/homework03/model/Issue.java b/homework03/src/main/java/com/ironhack/homework03/model/Issue.java index cdf6fd3f..ad60857a 100644 --- a/homework03/src/main/java/com/ironhack/homework03/model/Issue.java +++ b/homework03/src/main/java/com/ironhack/homework03/model/Issue.java @@ -16,11 +16,11 @@ public class Issue { private String issueDate; private String returnDate; - @OneToOne + @OneToOne(cascade = CascadeType.ALL) @JoinColumn(name = "student_id") private Student issuestudent; - @OneToOne + @OneToOne(cascade = CascadeType.ALL) @JoinColumn(name = "book_id") private Book issuebook; diff --git a/homework03/src/main/java/com/ironhack/homework03/repository/AuthorRepository.java b/homework03/src/main/java/com/ironhack/homework03/repository/AuthorRepository.java new file mode 100644 index 00000000..06841d65 --- /dev/null +++ b/homework03/src/main/java/com/ironhack/homework03/repository/AuthorRepository.java @@ -0,0 +1,10 @@ +package com.ironhack.homework03.repository; + + +import com.ironhack.homework03.model.Author; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface AuthorRepository extends JpaRepository{ +} \ No newline at end of file diff --git a/homework03/src/main/java/com/ironhack/homework03/repository/BookRepository.java b/homework03/src/main/java/com/ironhack/homework03/repository/BookRepository.java new file mode 100644 index 00000000..6ec4f478 --- /dev/null +++ b/homework03/src/main/java/com/ironhack/homework03/repository/BookRepository.java @@ -0,0 +1,9 @@ +package com.ironhack.homework03.repository; + +import com.ironhack.homework03.model.Book; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface BookRepository extends JpaRepository { +} diff --git a/homework03/src/main/java/com/ironhack/homework03/repository/IssueRepository.java b/homework03/src/main/java/com/ironhack/homework03/repository/IssueRepository.java new file mode 100644 index 00000000..20c12545 --- /dev/null +++ b/homework03/src/main/java/com/ironhack/homework03/repository/IssueRepository.java @@ -0,0 +1,9 @@ +package com.ironhack.homework03.repository; + +import com.ironhack.homework03.model.Issue; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface IssueRepository extends JpaRepository { +} diff --git a/homework03/src/main/java/com/ironhack/homework03/repository/StudentRepository.java b/homework03/src/main/java/com/ironhack/homework03/repository/StudentRepository.java new file mode 100644 index 00000000..eb0aec72 --- /dev/null +++ b/homework03/src/main/java/com/ironhack/homework03/repository/StudentRepository.java @@ -0,0 +1,9 @@ +package com.ironhack.homework03.repository; + +import com.ironhack.homework03.model.Student; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface StudentRepository extends JpaRepository { +} diff --git a/homework03/src/test/java/com/ironhack/homework03/model/IssueTest.java b/homework03/src/test/java/com/ironhack/homework03/model/IssueTest.java new file mode 100644 index 00000000..1e3719b0 --- /dev/null +++ b/homework03/src/test/java/com/ironhack/homework03/model/IssueTest.java @@ -0,0 +1,79 @@ +package com.ironhack.homework03.model; + +import com.ironhack.homework03.repository.AuthorRepository; +import com.ironhack.homework03.repository.BookRepository; +import com.ironhack.homework03.repository.IssueRepository; +import com.ironhack.homework03.repository.StudentRepository; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; + +import static org.junit.jupiter.api.Assertions.*; + +@SpringBootTest +class IssueTest { + + + @Autowired + IssueRepository issueRepository; + + @Autowired + AuthorRepository authorRepository; + + @Autowired + BookRepository bookRepository; + + @Autowired + StudentRepository studentRepository; + + Issue issue; + Author author; + Book book; + Student student; + + @BeforeEach + public void setUp(){ + student = new Student(); + student.setUsn("STU001"); + book = new Book(); + book.setIsbn("SBN01"); + book.setTitle("Long Jaw"); + author = new Author(); + issue = new Issue(); + } + + @AfterEach + public void tearDown(){ + issueRepository.deleteAll(); + authorRepository.deleteAll(); + bookRepository.deleteAll(); + studentRepository.deleteAll(); + } + + @Test + public void savingAuthorCascadesBook_validAuthor_expectedBook(){ + author.setName("Robert"); + author.setAuthorbook(book); + authorRepository.save(author); + assertEquals("Long Jaw",bookRepository.findAll().get(0).getTitle()); + } + + @Test + public void savingIssueCascadesBookOrStudent_validIssue_expectedBookAndStudent(){ + assertEquals(0,studentRepository.findAll().size()); + assertEquals(0,bookRepository.findAll().size()); + assertEquals(0,issueRepository.findAll().size()); + issue.setIssuebook(book); + issue.setIssuestudent(student); +// bookRepository.save(book); +// studentRepository.save(student); + issueRepository.save(issue); + assertEquals(1,studentRepository.findAll().size()); + assertEquals(1,bookRepository.findAll().size()); + assertEquals(1,issueRepository.findAll().size()); + } + + +} \ No newline at end of file From c823e310afd91888cbed33968dc6c3a1c3f960d4 Mon Sep 17 00:00:00 2001 From: alvaro Date: Tue, 4 Mar 2025 16:30:59 +0100 Subject: [PATCH 07/25] mapped by and more tests --- homework03/.gitignore | 1 + .../com/ironhack/homework03/model/Author.java | 5 + .../com/ironhack/homework03/model/Book.java | 11 +- .../com/ironhack/homework03/model/Issue.java | 18 ++- .../ironhack/homework03/model/Student.java | 5 +- .../src/main/resources/application.properties | 2 +- .../ironhack/homework03/model/IssueTest.java | 139 +++++++++++++++++- 7 files changed, 162 insertions(+), 19 deletions(-) diff --git a/homework03/.gitignore b/homework03/.gitignore index 549e00a2..d2a6437d 100644 --- a/homework03/.gitignore +++ b/homework03/.gitignore @@ -1,5 +1,6 @@ HELP.md target/ +src/main/resources/application.properties !.mvn/wrapper/maven-wrapper.jar !**/src/main/**/target/ !**/src/test/**/target/ diff --git a/homework03/src/main/java/com/ironhack/homework03/model/Author.java b/homework03/src/main/java/com/ironhack/homework03/model/Author.java index a9ecdec1..430b15ac 100644 --- a/homework03/src/main/java/com/ironhack/homework03/model/Author.java +++ b/homework03/src/main/java/com/ironhack/homework03/model/Author.java @@ -19,4 +19,9 @@ public class Author { @OneToOne(cascade = CascadeType.ALL) @JoinColumn(name = "book_id") private Book authorbook; + + public void addBook(Book book){ + this.authorbook = book; + book.setAuthor(this); + } } diff --git a/homework03/src/main/java/com/ironhack/homework03/model/Book.java b/homework03/src/main/java/com/ironhack/homework03/model/Book.java index a30e06cb..541e07f6 100644 --- a/homework03/src/main/java/com/ironhack/homework03/model/Book.java +++ b/homework03/src/main/java/com/ironhack/homework03/model/Book.java @@ -1,5 +1,6 @@ package com.ironhack.homework03.model; +import jakarta.persistence.CascadeType; import jakarta.persistence.Entity; import jakarta.persistence.Id; import jakarta.persistence.OneToOne; @@ -21,10 +22,10 @@ public class Book { - // @OneToOne(mappedBy = "authorbook") - + @OneToOne(mappedBy = "authorbook") + private Author author; // private Book book; -// -// @OneToOne(mappedBy = "issuebook") -// private Issue issue; + + @OneToOne(mappedBy = "issuebook") //, cascade = CascadeType.REMOVE, orphanRemoval = true + private Issue issue; } diff --git a/homework03/src/main/java/com/ironhack/homework03/model/Issue.java b/homework03/src/main/java/com/ironhack/homework03/model/Issue.java index ad60857a..19cf733f 100644 --- a/homework03/src/main/java/com/ironhack/homework03/model/Issue.java +++ b/homework03/src/main/java/com/ironhack/homework03/model/Issue.java @@ -16,12 +16,22 @@ public class Issue { private String issueDate; private String returnDate; - @OneToOne(cascade = CascadeType.ALL) - @JoinColumn(name = "student_id") + @OneToOne + @JoinColumn(name = "student_id", unique = true) private Student issuestudent; - @OneToOne(cascade = CascadeType.ALL) - @JoinColumn(name = "book_id") + @OneToOne + @JoinColumn(name = "book_id", unique = true) private Book issuebook; + public void addBook(Book book){ + this.issuebook = book; + book.setIssue(this); + } + + public void addStudent(Student student){ + this.issuestudent=student; + student.setIssue(this); + } + } \ No newline at end of file diff --git a/homework03/src/main/java/com/ironhack/homework03/model/Student.java b/homework03/src/main/java/com/ironhack/homework03/model/Student.java index c4207d2c..06f3d516 100644 --- a/homework03/src/main/java/com/ironhack/homework03/model/Student.java +++ b/homework03/src/main/java/com/ironhack/homework03/model/Student.java @@ -1,5 +1,6 @@ package com.ironhack.homework03.model; +import jakarta.persistence.CascadeType; import jakarta.persistence.Entity; import jakarta.persistence.Id; import jakarta.persistence.OneToOne; @@ -16,6 +17,6 @@ public class Student { private String usn; private String name; -// @OneToOne(mappedBy = "studentissue") -// private Issue issue; + @OneToOne(mappedBy = "issuestudent" , cascade = CascadeType.ALL, orphanRemoval = true) + private Issue issue; } \ No newline at end of file diff --git a/homework03/src/main/resources/application.properties b/homework03/src/main/resources/application.properties index 8bf9b917..eefa370e 100644 --- a/homework03/src/main/resources/application.properties +++ b/homework03/src/main/resources/application.properties @@ -2,7 +2,7 @@ spring.application.name=homework03 spring.datasource.url=jdbc:mysql://localhost:3306/homework3?createDatabaseIfNotExist=true&serverTimezone=UTC spring.datasource.username=root -spring.datasource.password= +spring.datasource.password=root spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.jpa.hibernate.ddl-auto=create-drop spring.jpa.show-sql=true diff --git a/homework03/src/test/java/com/ironhack/homework03/model/IssueTest.java b/homework03/src/test/java/com/ironhack/homework03/model/IssueTest.java index 1e3719b0..6f72bc32 100644 --- a/homework03/src/test/java/com/ironhack/homework03/model/IssueTest.java +++ b/homework03/src/test/java/com/ironhack/homework03/model/IssueTest.java @@ -10,6 +10,8 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; +import java.util.Optional; + import static org.junit.jupiter.api.Assertions.*; @SpringBootTest @@ -41,38 +43,161 @@ public void setUp(){ book.setIsbn("SBN01"); book.setTitle("Long Jaw"); author = new Author(); + author.setName("Robert"); issue = new Issue(); } @AfterEach public void tearDown(){ issueRepository.deleteAll(); + studentRepository.deleteAll(); authorRepository.deleteAll(); bookRepository.deleteAll(); - studentRepository.deleteAll(); } @Test public void savingAuthorCascadesBook_validAuthor_expectedBook(){ - author.setName("Robert"); + book.setIsbn("SBN01"); + book.setTitle("Long Jaw"); author.setAuthorbook(book); + author.setId(123); + author.setName("Gionino"); authorRepository.save(author); - assertEquals("Long Jaw",bookRepository.findAll().get(0).getTitle()); + Optional caseBook = bookRepository.findById("SBN01"); + assertEquals("Long Jaw",caseBook.get().getTitle()); } @Test - public void savingIssueCascadesBookOrStudent_validIssue_expectedBookAndStudent(){ + public void savingIssueDoesNotCascadesBookOrStudent_validIssue_expectedBookAndStudent(){ assertEquals(0,studentRepository.findAll().size()); assertEquals(0,bookRepository.findAll().size()); assertEquals(0,issueRepository.findAll().size()); + assertEquals(0,authorRepository.findAll().size()); issue.setIssuebook(book); issue.setIssuestudent(student); -// bookRepository.save(book); -// studentRepository.save(student); - issueRepository.save(issue); + bookRepository.save(book); + studentRepository.save(student); + issueRepository.save(issue);//necesary to save student and book items before assertEquals(1,studentRepository.findAll().size()); assertEquals(1,bookRepository.findAll().size()); assertEquals(1,issueRepository.findAll().size()); + assertEquals(0,authorRepository.findAll().size()); + } + + @Test + public void savingIssueThenUsingMappedBy_validInput_mappedByWorking(){ + Author author = new Author(); + author.setName("Robert2"); + + Book book = new Book(); + book.setIsbn("ISBN-022"); + book.setTitle("Introduction to Scripting"); + + author.setAuthorbook(book); + authorRepository.save(author); + + Student student = new Student(); + student.setUsn("USN-131"); + student.setName("Juanita Doe"); + studentRepository.save(student); + + Issue firstIssue = new Issue(); + firstIssue.setIssueDate("2025-03-04"); + firstIssue.setReturnDate("2025-03-10"); + firstIssue.setIssuestudent(student); + firstIssue.setIssuebook(book); + issueRepository.save(firstIssue); + + // NOW--- MAPPED BY!! + + Optional testBook = bookRepository.findById("ISBN-022"); + + System.out.println(testBook.get().getIssue().getIssueId()+" is the issue id of this book," + + "it is currently issued to "+testBook.get().getIssue().getIssuestudent().getName()); + + System.out.println(testBook.get().getAuthor().getName()+" is the author of "+testBook.get().getTitle()); + assertEquals("Robert2",testBook.get().getAuthor().getName()); + assertEquals("Juanita Doe",testBook.get().getIssue().getIssuestudent().getName()); + } + + @Test + public void createAuthorThenBookSaveAuthorSaveStudentSaveIssue_issueSaved(){ + Author author = new Author(); + author.setName("Robert"); + + Book book = new Book(); + book.setIsbn("ISBN-001"); + book.setTitle("Introduction to Java"); + + author.setAuthorbook(book); + authorRepository.save(author); + + Student student = new Student(); + student.setUsn("USN-001"); + student.setName("Jane Doe"); + studentRepository.save(student); + + Issue firstIssue = new Issue(); + firstIssue.setIssueDate("2025-03-04"); + firstIssue.setReturnDate("2025-03-10"); + firstIssue.setIssuestudent(student); + firstIssue.setIssuebook(book); + issueRepository.save(firstIssue); + + // Uncomment to test exception + /*Issue secondIssue = new Issue(); + secondIssue.setIssueDate("2025-03-05"); + secondIssue.setReturnDate("2025-03-15"); + secondIssue.setIssuestudent(student); + secondIssue.setIssuebook(bookRepository.findById("ISBN-001") + .orElseThrow(() -> new AssertionError("Book not found"))); + assertThrows(DataIntegrityViolationException.class, () -> { + issueRepository.save(secondIssue); + });*/ + + assertNotNull(issueRepository.findAll()); + } + + @Test + public void getBookWithoutAuthorAndStudentFromDatabaseAndAssignIssue_issueSaved(){ + Issue secondIssue = new Issue(); + secondIssue.setIssueDate("2025-03-04"); + secondIssue.setReturnDate("2025-03-10"); + student = new Student(); + student.setUsn("STUDENT"); + book = new Book(); + book.setIsbn("COOLBOOK"); + studentRepository.save(student); + bookRepository.save(book); + Optional newBook = bookRepository.findById("COOLBOOK"); + Optional newStudent = studentRepository.findById("STUDENT"); + secondIssue.setIssuestudent(newStudent.get()); + secondIssue.setIssuebook(newBook.get()); + issueRepository.save(secondIssue); + System.out.println("Issue Repository Size: "+issueRepository.findAll().size()); + assertNotNull(issueRepository.findAll()); + } + + @Test + public void getBookWithAuthorAndStudentFromDatabaseAndAssignIssue_issueSaved(){ + Issue secondIssue = new Issue(); + secondIssue.setIssueDate("2025-03-04"); + secondIssue.setReturnDate("2025-03-10"); + student = new Student(); + student.setUsn("STUDENT2"); + book = new Book(); + book.setIsbn("COOLBOOK2"); + author = new Author(); + author.setAuthorbook(book); + studentRepository.save(student); + authorRepository.save(author); + Optional newBook = bookRepository.findById("COOLBOOK2"); + Optional newStudent = studentRepository.findById("STUDENT2"); + secondIssue.setIssuestudent(newStudent.get()); + secondIssue.setIssuebook(newBook.get()); + issueRepository.save(secondIssue); + + assertNotNull(issueRepository.findAll()); } From e90665fdfa924c3b8f59f62bccbd6099a0de40e6 Mon Sep 17 00:00:00 2001 From: alvaro Date: Tue, 4 Mar 2025 16:34:58 +0100 Subject: [PATCH 08/25] final tests for mappedBy --- .../test/java/com/ironhack/homework03/model/IssueTest.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/homework03/src/test/java/com/ironhack/homework03/model/IssueTest.java b/homework03/src/test/java/com/ironhack/homework03/model/IssueTest.java index 6f72bc32..1d5ef1ed 100644 --- a/homework03/src/test/java/com/ironhack/homework03/model/IssueTest.java +++ b/homework03/src/test/java/com/ironhack/homework03/model/IssueTest.java @@ -48,7 +48,7 @@ public void setUp(){ } @AfterEach - public void tearDown(){ + public void tearDown(){ // EL ORDEN ES IMPORTANTE issueRepository.deleteAll(); studentRepository.deleteAll(); authorRepository.deleteAll(); @@ -118,6 +118,11 @@ public void savingIssueThenUsingMappedBy_validInput_mappedByWorking(){ System.out.println(testBook.get().getAuthor().getName()+" is the author of "+testBook.get().getTitle()); assertEquals("Robert2",testBook.get().getAuthor().getName()); assertEquals("Juanita Doe",testBook.get().getIssue().getIssuestudent().getName()); + + + Optional testStudent = studentRepository.findById("USN-131"); + System.out.println(testStudent.get().getName()+" has been issued the book "+testStudent.get().getIssue().getIssuebook().getTitle()); + assertEquals("Introduction to Scripting",testStudent.get().getIssue().getIssuebook().getTitle()); } @Test From 838e95a84ed6d9b0e006aa997068dda89293f3aa Mon Sep 17 00:00:00 2001 From: alvaro Date: Tue, 4 Mar 2025 16:37:01 +0100 Subject: [PATCH 09/25] deleted unwanted functions --- .../main/java/com/ironhack/homework03/model/Author.java | 4 ---- .../main/java/com/ironhack/homework03/model/Issue.java | 9 --------- 2 files changed, 13 deletions(-) diff --git a/homework03/src/main/java/com/ironhack/homework03/model/Author.java b/homework03/src/main/java/com/ironhack/homework03/model/Author.java index 430b15ac..02a323c6 100644 --- a/homework03/src/main/java/com/ironhack/homework03/model/Author.java +++ b/homework03/src/main/java/com/ironhack/homework03/model/Author.java @@ -20,8 +20,4 @@ public class Author { @JoinColumn(name = "book_id") private Book authorbook; - public void addBook(Book book){ - this.authorbook = book; - book.setAuthor(this); - } } diff --git a/homework03/src/main/java/com/ironhack/homework03/model/Issue.java b/homework03/src/main/java/com/ironhack/homework03/model/Issue.java index 19cf733f..a155199c 100644 --- a/homework03/src/main/java/com/ironhack/homework03/model/Issue.java +++ b/homework03/src/main/java/com/ironhack/homework03/model/Issue.java @@ -24,14 +24,5 @@ public class Issue { @JoinColumn(name = "book_id", unique = true) private Book issuebook; - public void addBook(Book book){ - this.issuebook = book; - book.setIssue(this); - } - - public void addStudent(Student student){ - this.issuestudent=student; - student.setIssue(this); - } } \ No newline at end of file From 9a52a7670017fdb64716c5f001a4243d5fd8ca67 Mon Sep 17 00:00:00 2001 From: Fernando Date: Wed, 5 Mar 2025 11:36:08 +0100 Subject: [PATCH 10/25] Menu CommandLineRunner --- .../homework03/Homework03Application.java | 11 +++++++- .../java/com/ironhack/homework03/Menu.java | 25 +++++++++++++++++++ .../controller/dto/StudentBooksDTO.java | 4 +++ .../com/ironhack/homework03/model/Style.java | 4 +++ .../repository/BookRepositoryTest.java | 4 +++ 5 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 homework03/src/main/java/com/ironhack/homework03/Menu.java create mode 100644 homework03/src/main/java/com/ironhack/homework03/controller/dto/StudentBooksDTO.java create mode 100644 homework03/src/main/java/com/ironhack/homework03/model/Style.java create mode 100644 homework03/src/test/java/com/ironhack/homework03/repository/BookRepositoryTest.java diff --git a/homework03/src/main/java/com/ironhack/homework03/Homework03Application.java b/homework03/src/main/java/com/ironhack/homework03/Homework03Application.java index 78f6547b..e06ffef8 100644 --- a/homework03/src/main/java/com/ironhack/homework03/Homework03Application.java +++ b/homework03/src/main/java/com/ironhack/homework03/Homework03Application.java @@ -1,13 +1,22 @@ package com.ironhack.homework03; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication -public class Homework03Application { +public class Homework03Application implements CommandLineRunner { + + @Autowired + Menu menu; public static void main(String[] args) { SpringApplication.run(Homework03Application.class, args); } + @Override + public void run(String... args) throws Exception { + menu.showMenu(); + } } diff --git a/homework03/src/main/java/com/ironhack/homework03/Menu.java b/homework03/src/main/java/com/ironhack/homework03/Menu.java new file mode 100644 index 00000000..b3c04750 --- /dev/null +++ b/homework03/src/main/java/com/ironhack/homework03/Menu.java @@ -0,0 +1,25 @@ +package com.ironhack.homework03; + +import com.ironhack.homework03.model.Style; +import org.springframework.stereotype.Component; + +@Component +public class Menu { + + public void showMenu(){ + System.out.println(Style.SUCCESS+Style.BOLD + "\nFantastic! All data introduced is correct. Then, we provide a command list " + + "for managing or adding a new information." + Style.RESET); + System.out.println(Style.BOLD + "\n--------------------------------- COMMAND LIST ---------------------------------\n" + Style.RESET); + System.out.println( + "1 "+Style.SUCCESS_BG+Style.BOLD+" Add a book "+Style.RESET+ + "\n2 "+Style.SUCCESS_BG+Style.BOLD+" Search book by title "+Style.RESET+ + "\n3 "+Style.SUCCESS_BG+Style.BOLD+" Search book by category " +Style.RESET+ + "\n4 "+Style.SUCCESS_BG+Style.BOLD+" Search book by Author "+ Style.RESET+ + "\n5 "+Style.SUCCESS_BG+Style.BOLD+" List all books along with author " +Style.RESET+ + "\n6 "+Style.SUCCESS_BG+Style.BOLD+" Issue book to student "+ Style.RESET+ + "\n7 "+Style.SUCCESS_BG+Style.BOLD+" List books by usn " +Style.RESET+ + "\n8 "+Style.SUCCESS_BG+Style.BOLD+" Exit "+ Style.RESET); + System.out.println(Style.BOLD + "\n--------------------------------------------------------------------------------" + Style.RESET); + + } +} diff --git a/homework03/src/main/java/com/ironhack/homework03/controller/dto/StudentBooksDTO.java b/homework03/src/main/java/com/ironhack/homework03/controller/dto/StudentBooksDTO.java new file mode 100644 index 00000000..96dc7304 --- /dev/null +++ b/homework03/src/main/java/com/ironhack/homework03/controller/dto/StudentBooksDTO.java @@ -0,0 +1,4 @@ +package com.ironhack.homework03.controller.dto; + +public class StudentBooksDTO { +} diff --git a/homework03/src/main/java/com/ironhack/homework03/model/Style.java b/homework03/src/main/java/com/ironhack/homework03/model/Style.java new file mode 100644 index 00000000..e61a7079 --- /dev/null +++ b/homework03/src/main/java/com/ironhack/homework03/model/Style.java @@ -0,0 +1,4 @@ +package com.ironhack.homework03.model; + +public class Style { +} diff --git a/homework03/src/test/java/com/ironhack/homework03/repository/BookRepositoryTest.java b/homework03/src/test/java/com/ironhack/homework03/repository/BookRepositoryTest.java new file mode 100644 index 00000000..693dc2f0 --- /dev/null +++ b/homework03/src/test/java/com/ironhack/homework03/repository/BookRepositoryTest.java @@ -0,0 +1,4 @@ +import static org.junit.jupiter.api.Assertions.*; +class BookRepositoryTest { + +} \ No newline at end of file From b7f454aaf0dddb2a7db996f527a7218c4508464c Mon Sep 17 00:00:00 2001 From: Fernando Date: Wed, 5 Mar 2025 11:38:15 +0100 Subject: [PATCH 11/25] Style Class --- .../com/ironhack/homework03/model/Style.java | 33 ++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/homework03/src/main/java/com/ironhack/homework03/model/Style.java b/homework03/src/main/java/com/ironhack/homework03/model/Style.java index e61a7079..c0c325a0 100644 --- a/homework03/src/main/java/com/ironhack/homework03/model/Style.java +++ b/homework03/src/main/java/com/ironhack/homework03/model/Style.java @@ -1,4 +1,35 @@ package com.ironhack.homework03.model; public class Style { -} + // ANSI Code + public static final String RESET = "\u001B[0m"; + public static final String BOLD = "\u001B[1m"; + public static final String UNDERLINE = "\u001B[4m"; + public static final String REVERSED = "\u001B[7m"; // Invierte los colores + + // RGB COLORS + public static final String SCHOOL = rgbColor(70, 130, 255); + public static final String TEACHER = rgbColor(186, 85, 211); + public static final String COURSE = rgbColor(255, 165, 50); + public static final String STUDENT = rgbColor(0, 179, 168); + public static final String HIGHLIGHT = rgbColor(255, 255, 100); + public static final String ERROR = rgbColor(255, 99, 71); + public static final String SUCCESS = rgbColor(144, 238, 144); + public static final String SALARY = rgbColor(179, 173, 0); + public static final String NOT_FOUND = rgbColor(255, 165, 50); + + // RGB BACKGROUND +// public static final String SUCCESS_BG = rgbBackground(64, 93, 72); + public static final String SUCCESS_BG = rgbBackground(58, 58, 58); + // public static final String SUCCESS_BG = rgbBackground(30, 30, 30); // CONSOLE COLOR + public static final String WARNING_BG = rgbBackground(255, 255, 102); + public static final String ERROR_BG = rgbBackground(255, 69, 69); + + public static String rgbColor(int r, int g, int b) { + return "\u001B[38;2;" + r + ";" + g + ";" + b + "m"; + } + + public static String rgbBackground(int r, int g, int b) { + return "\u001B[48;2;" + r + ";" + g + ";" + b + "m"; + } +} \ No newline at end of file From be6f14b008d576ccfb284eb85aacce58aab4d69a Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 5 Mar 2025 14:04:32 +0100 Subject: [PATCH 12/25] commit --- .../homework03/model/IBookRepository.java | 21 --------- .../com/ironhack/homework03/model/Menu.java | 24 ---------- .../ironhack/homework03/model/Options.java | 8 ---- .../ironhack/homework03/model/ServerMenu.java | 47 +++++++++++++++++++ .../homework03/repository/BookRepository.java | 11 +++++ .../src/main/resources/application.properties | 2 +- .../BookRepositoryTest.java} | 12 ++--- 7 files changed, 62 insertions(+), 63 deletions(-) delete mode 100644 homework03/src/main/java/com/ironhack/homework03/model/IBookRepository.java delete mode 100644 homework03/src/main/java/com/ironhack/homework03/model/Menu.java delete mode 100644 homework03/src/main/java/com/ironhack/homework03/model/Options.java create mode 100644 homework03/src/main/java/com/ironhack/homework03/model/ServerMenu.java rename homework03/src/test/java/com/ironhack/homework03/{model/IBookRepositoryTest.java => repository/BookRepositoryTest.java} (62%) diff --git a/homework03/src/main/java/com/ironhack/homework03/model/IBookRepository.java b/homework03/src/main/java/com/ironhack/homework03/model/IBookRepository.java deleted file mode 100644 index 6d428711..00000000 --- a/homework03/src/main/java/com/ironhack/homework03/model/IBookRepository.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.ironhack.homework03.model; - -import jakarta.transaction.Transactional; -import org.springframework.data.jpa.repository.Modifying; -import org.springframework.data.jpa.repository.Query; -import org.springframework.data.repository.query.Param; -import org.springframework.stereotype.Repository; - -@Repository -public interface IBookRepository { - - @Modifying - @Transactional - @Query("INSERT INTO Book (isbn, title, category, quality) VALUES (:isbn, :title, :category, :quality)") - void addBook(@Param("isbn") String isbn ,@Param("title") String title, @Param("category") String category, @Param("quality") Integer quality); - - Book findByTitle(String title); - Book findByCategory(String category); - Book findByAuthor(String author); - -} diff --git a/homework03/src/main/java/com/ironhack/homework03/model/Menu.java b/homework03/src/main/java/com/ironhack/homework03/model/Menu.java deleted file mode 100644 index db6f9692..00000000 --- a/homework03/src/main/java/com/ironhack/homework03/model/Menu.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.ironhack.homework03.model; - -public class Menu { - -// public void showMenu() { -// System.out.println(Style.SUCCESS+Style.BOLD + "\nFantastic! All data introduced is correct. Then, we provide a command list " + -// "for managing or adding a new information." + Style.RESET); -// System.out.println(Style.BOLD + "\n--------------------------------- COMMAND LIST ---------------------------------\n" + Style.RESET); -// System.out.println( -// "- "+Style.SUCCESS_BG+Style.BOLD+" Add a book "+Style.RESET+ -// "\n- "+Style.SUCCESS_BG+Style.BOLD+" Search book by title "+Style.RESET+ -// "\n- "+Style.SUCCESS_BG+Style.BOLD+" Search book by category " +Style.RESET+ -// "\n- "+Style.SUCCESS_BG+Style.BOLD+" Search book by Author "+ Style.RESET+ -// "\n- "+Style.SUCCESS_BG+Style.BOLD+" List all books along with author " +Style.RESET+ -// "\n- "+Style.SUCCESS_BG+Style.BOLD+" Issue book to student "+ Style.RESET+ -// "\n- "+Style.SUCCESS_BG+Style.BOLD+" List books by usn " +Style.RESET+ -// "\n- "+Style.SUCCESS_BG+Style.BOLD+" Exit "+ Style.RESET); -// System.out.println(Style.BOLD + "\n--------------------------------------------------------------------------------" + Style.RESET); -// } - - - - -} diff --git a/homework03/src/main/java/com/ironhack/homework03/model/Options.java b/homework03/src/main/java/com/ironhack/homework03/model/Options.java deleted file mode 100644 index d3fd1087..00000000 --- a/homework03/src/main/java/com/ironhack/homework03/model/Options.java +++ /dev/null @@ -1,8 +0,0 @@ -package com.ironhack.homework03.model; - -public class Options { - - - - -} diff --git a/homework03/src/main/java/com/ironhack/homework03/model/ServerMenu.java b/homework03/src/main/java/com/ironhack/homework03/model/ServerMenu.java new file mode 100644 index 00000000..8f4d3917 --- /dev/null +++ b/homework03/src/main/java/com/ironhack/homework03/model/ServerMenu.java @@ -0,0 +1,47 @@ +package com.ironhack.homework03.model; + +import java.util.Scanner; + +public class ServerMenu { + + Scanner scanner = new Scanner(System.in); + +// public void showMenu() { +// System.out.println(Style.SUCCESS+Style.BOLD + "\nFantastic! All data introduced is correct. Then, we provide a command list " + +// "for managing or adding a new information." + Style.RESET); +// System.out.println(Style.BOLD + "\n--------------------------------- COMMAND LIST ---------------------------------\n" + Style.RESET); +// System.out.println( +// "- "+Style.SUCCESS_BG+Style.BOLD+"1 - ADD BOOK "+Style.RESET+ +// "\n- "+Style.SUCCESS_BG+Style.BOLD+"2 - SEARCH BOOK TITLE "+Style.RESET+ +// "\n- "+Style.SUCCESS_BG+Style.BOLD+"3 - SEARCH BOOK CATEGORY " +Style.RESET+ +// "\n- "+Style.SUCCESS_BG+Style.BOLD+"4 - SEARCH BOOK AUTHOR "+ Style.RESET+ +// "\n- "+Style.SUCCESS_BG+Style.BOLD+"5 - LIST BOOK AUTHOR " +Style.RESET+ +// "\n- "+Style.SUCCESS_BG+Style.BOLD+"6 - ISSUE BOOK STUDENT "+ Style.RESET+ +// "\n- "+Style.SUCCESS_BG+Style.BOLD+"7 - LIST BOOK USN " +Style.RESET+ +// "\n- "+Style.SUCCESS_BG+Style.BOLD+"8 - EXIT "+ Style.RESET); +// System.out.println(Style.BOLD + "\n--------------------------------------------------------------------------------" + Style.RESET); +// } + +// public void selectCommand() { +// System.out.println("Select a command number.") +// try{ +// int option = scanner.nextInt(); +// }catch(Exception e) +// System.out.println(e.getMessage()); +// } +// +// switch(option){ +// case 1: +// case 2: +// case 3: +// case 4: +// case 5: +// case 6: +// case 7: +// case 8: +// } +// +// } + + +} \ No newline at end of file diff --git a/homework03/src/main/java/com/ironhack/homework03/repository/BookRepository.java b/homework03/src/main/java/com/ironhack/homework03/repository/BookRepository.java index 6ec4f478..8dee157e 100644 --- a/homework03/src/main/java/com/ironhack/homework03/repository/BookRepository.java +++ b/homework03/src/main/java/com/ironhack/homework03/repository/BookRepository.java @@ -1,9 +1,20 @@ package com.ironhack.homework03.repository; import com.ironhack.homework03.model.Book; +import jakarta.transaction.Transactional; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Modifying; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Repository; +import java.util.Optional; + @Repository public interface BookRepository extends JpaRepository { + + public Optional findByTitle(String title); + public Optional findByCategory(String category); +// public Optional findByAuthor(String author); } diff --git a/homework03/src/main/resources/application.properties b/homework03/src/main/resources/application.properties index eefa370e..8bf9b917 100644 --- a/homework03/src/main/resources/application.properties +++ b/homework03/src/main/resources/application.properties @@ -2,7 +2,7 @@ spring.application.name=homework03 spring.datasource.url=jdbc:mysql://localhost:3306/homework3?createDatabaseIfNotExist=true&serverTimezone=UTC spring.datasource.username=root -spring.datasource.password=root +spring.datasource.password= spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.jpa.hibernate.ddl-auto=create-drop spring.jpa.show-sql=true diff --git a/homework03/src/test/java/com/ironhack/homework03/model/IBookRepositoryTest.java b/homework03/src/test/java/com/ironhack/homework03/repository/BookRepositoryTest.java similarity index 62% rename from homework03/src/test/java/com/ironhack/homework03/model/IBookRepositoryTest.java rename to homework03/src/test/java/com/ironhack/homework03/repository/BookRepositoryTest.java index 7bda43bd..41120049 100644 --- a/homework03/src/test/java/com/ironhack/homework03/model/IBookRepositoryTest.java +++ b/homework03/src/test/java/com/ironhack/homework03/repository/BookRepositoryTest.java @@ -1,14 +1,8 @@ -package com.ironhack.homework03.model; - -import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; - -import java.util.List; +package com.ironhack.homework03.repository; import static org.junit.jupiter.api.Assertions.*; -class IBookRepositoryTest { +class BookRepositoryTest { // // @BeforeAll // public void setUp(){ @@ -34,5 +28,5 @@ class IBookRepositoryTest { // // @Test // void findByAuthor() { -// } +// } \ No newline at end of file From 7ff3937780330db0af956be8db8535a0cab1a713 Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 5 Mar 2025 16:13:31 +0100 Subject: [PATCH 13/25] commit --- .../homework03/Homework03Application.java | 6 +++ .../java/com/ironhack/homework03/Menu.java | 27 ++++++----- .../ironhack/homework03/model/ServerMenu.java | 47 ------------------- 3 files changed, 22 insertions(+), 58 deletions(-) delete mode 100644 homework03/src/main/java/com/ironhack/homework03/model/ServerMenu.java diff --git a/homework03/src/main/java/com/ironhack/homework03/Homework03Application.java b/homework03/src/main/java/com/ironhack/homework03/Homework03Application.java index e06ffef8..1cfa881e 100644 --- a/homework03/src/main/java/com/ironhack/homework03/Homework03Application.java +++ b/homework03/src/main/java/com/ironhack/homework03/Homework03Application.java @@ -5,6 +5,8 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import java.util.InputMismatchException; + @SpringBootApplication public class Homework03Application implements CommandLineRunner { @@ -18,5 +20,9 @@ public static void main(String[] args) { @Override public void run(String... args) throws Exception { menu.showMenu(); + + + + } } diff --git a/homework03/src/main/java/com/ironhack/homework03/Menu.java b/homework03/src/main/java/com/ironhack/homework03/Menu.java index b3c04750..c4912e0b 100644 --- a/homework03/src/main/java/com/ironhack/homework03/Menu.java +++ b/homework03/src/main/java/com/ironhack/homework03/Menu.java @@ -3,23 +3,28 @@ import com.ironhack.homework03.model.Style; import org.springframework.stereotype.Component; +import java.util.ArrayList; +import java.util.InputMismatchException; +import java.util.List; +import java.util.Scanner; + @Component public class Menu { + Scanner scanner = new Scanner(System.in); - public void showMenu(){ - System.out.println(Style.SUCCESS+Style.BOLD + "\nFantastic! All data introduced is correct. Then, we provide a command list " + - "for managing or adding a new information." + Style.RESET); + public void showMenu() { System.out.println(Style.BOLD + "\n--------------------------------- COMMAND LIST ---------------------------------\n" + Style.RESET); System.out.println( - "1 "+Style.SUCCESS_BG+Style.BOLD+" Add a book "+Style.RESET+ - "\n2 "+Style.SUCCESS_BG+Style.BOLD+" Search book by title "+Style.RESET+ - "\n3 "+Style.SUCCESS_BG+Style.BOLD+" Search book by category " +Style.RESET+ - "\n4 "+Style.SUCCESS_BG+Style.BOLD+" Search book by Author "+ Style.RESET+ - "\n5 "+Style.SUCCESS_BG+Style.BOLD+" List all books along with author " +Style.RESET+ - "\n6 "+Style.SUCCESS_BG+Style.BOLD+" Issue book to student "+ Style.RESET+ - "\n7 "+Style.SUCCESS_BG+Style.BOLD+" List books by usn " +Style.RESET+ - "\n8 "+Style.SUCCESS_BG+Style.BOLD+" Exit "+ Style.RESET); + "1 " + Style.SUCCESS_BG + Style.BOLD + " ADD BOOK " + Style.RESET + + "\n2 " + Style.SUCCESS_BG + Style.BOLD + " SEARCH BOOK TITLE " + Style.RESET + + "\n3 " + Style.SUCCESS_BG + Style.BOLD + " SEARCH BOOK CATEGORY " + Style.RESET + + "\n4 " + Style.SUCCESS_BG + Style.BOLD + " SEARCH BOOK AUTHOR " + Style.RESET + + "\n5 " + Style.SUCCESS_BG + Style.BOLD + " LIST BOOK AUTHOR " + Style.RESET + + "\n6 " + Style.SUCCESS_BG + Style.BOLD + " ISSUE BOOK STUDENT " + Style.RESET + + "\n7 " + Style.SUCCESS_BG + Style.BOLD + " LIST BOOK USN " + Style.RESET + + "\n8 " + Style.SUCCESS_BG + Style.BOLD + " EXIT " + Style.RESET); System.out.println(Style.BOLD + "\n--------------------------------------------------------------------------------" + Style.RESET); } } + diff --git a/homework03/src/main/java/com/ironhack/homework03/model/ServerMenu.java b/homework03/src/main/java/com/ironhack/homework03/model/ServerMenu.java deleted file mode 100644 index 8f4d3917..00000000 --- a/homework03/src/main/java/com/ironhack/homework03/model/ServerMenu.java +++ /dev/null @@ -1,47 +0,0 @@ -package com.ironhack.homework03.model; - -import java.util.Scanner; - -public class ServerMenu { - - Scanner scanner = new Scanner(System.in); - -// public void showMenu() { -// System.out.println(Style.SUCCESS+Style.BOLD + "\nFantastic! All data introduced is correct. Then, we provide a command list " + -// "for managing or adding a new information." + Style.RESET); -// System.out.println(Style.BOLD + "\n--------------------------------- COMMAND LIST ---------------------------------\n" + Style.RESET); -// System.out.println( -// "- "+Style.SUCCESS_BG+Style.BOLD+"1 - ADD BOOK "+Style.RESET+ -// "\n- "+Style.SUCCESS_BG+Style.BOLD+"2 - SEARCH BOOK TITLE "+Style.RESET+ -// "\n- "+Style.SUCCESS_BG+Style.BOLD+"3 - SEARCH BOOK CATEGORY " +Style.RESET+ -// "\n- "+Style.SUCCESS_BG+Style.BOLD+"4 - SEARCH BOOK AUTHOR "+ Style.RESET+ -// "\n- "+Style.SUCCESS_BG+Style.BOLD+"5 - LIST BOOK AUTHOR " +Style.RESET+ -// "\n- "+Style.SUCCESS_BG+Style.BOLD+"6 - ISSUE BOOK STUDENT "+ Style.RESET+ -// "\n- "+Style.SUCCESS_BG+Style.BOLD+"7 - LIST BOOK USN " +Style.RESET+ -// "\n- "+Style.SUCCESS_BG+Style.BOLD+"8 - EXIT "+ Style.RESET); -// System.out.println(Style.BOLD + "\n--------------------------------------------------------------------------------" + Style.RESET); -// } - -// public void selectCommand() { -// System.out.println("Select a command number.") -// try{ -// int option = scanner.nextInt(); -// }catch(Exception e) -// System.out.println(e.getMessage()); -// } -// -// switch(option){ -// case 1: -// case 2: -// case 3: -// case 4: -// case 5: -// case 6: -// case 7: -// case 8: -// } -// -// } - - -} \ No newline at end of file From 6d9b27953676fd3382f2f65aed1a11d4422bf6bb Mon Sep 17 00:00:00 2001 From: alvaro Date: Wed, 5 Mar 2025 17:29:23 +0100 Subject: [PATCH 14/25] working user input --- .../homework03/Homework03Application.java | 10 ++++--- .../java/com/ironhack/homework03/Menu.java | 30 +++++++++++++++++++ .../src/main/resources/application.properties | 2 +- 3 files changed, 37 insertions(+), 5 deletions(-) diff --git a/homework03/src/main/java/com/ironhack/homework03/Homework03Application.java b/homework03/src/main/java/com/ironhack/homework03/Homework03Application.java index 1cfa881e..ab1d60f5 100644 --- a/homework03/src/main/java/com/ironhack/homework03/Homework03Application.java +++ b/homework03/src/main/java/com/ironhack/homework03/Homework03Application.java @@ -19,10 +19,12 @@ public static void main(String[] args) { @Override public void run(String... args) throws Exception { - menu.showMenu(); - - - + menu.showMenu(); + boolean res = true; + do{ + res = menu.selectCommand(); + }while(res); + menu.closeScanner(); } } diff --git a/homework03/src/main/java/com/ironhack/homework03/Menu.java b/homework03/src/main/java/com/ironhack/homework03/Menu.java index c4912e0b..1738bf57 100644 --- a/homework03/src/main/java/com/ironhack/homework03/Menu.java +++ b/homework03/src/main/java/com/ironhack/homework03/Menu.java @@ -26,5 +26,35 @@ public void showMenu() { System.out.println(Style.BOLD + "\n--------------------------------------------------------------------------------" + Style.RESET); } + + public boolean selectCommand(){ + boolean option = true; + + System.out.println("\nPlease introduce a command:"); + String cmd = scanner.nextLine(); + cmd = cmd.toUpperCase(); + if(cmd.equals("EXIT")){ + option = false; + scanner.close(); + }else{ + try{ + sanitize(cmd); + } catch (Exception e) { + //System.out.println("Command not recognized, please try again."); + System.out.println(e.getMessage()); + } + } + //scanner.close(); + return option; + } + + private void sanitize(String cmd) { + System.out.println("The command introduced is: "+ cmd); + } + + public void closeScanner(){ + scanner.close(); + } + } diff --git a/homework03/src/main/resources/application.properties b/homework03/src/main/resources/application.properties index 8bf9b917..eefa370e 100644 --- a/homework03/src/main/resources/application.properties +++ b/homework03/src/main/resources/application.properties @@ -2,7 +2,7 @@ spring.application.name=homework03 spring.datasource.url=jdbc:mysql://localhost:3306/homework3?createDatabaseIfNotExist=true&serverTimezone=UTC spring.datasource.username=root -spring.datasource.password= +spring.datasource.password=root spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.jpa.hibernate.ddl-auto=create-drop spring.jpa.show-sql=true From b4df830ce5493e1a2f8a9c89e3996bdc47b93aba Mon Sep 17 00:00:00 2001 From: Fernando Date: Wed, 5 Mar 2025 18:03:47 +0100 Subject: [PATCH 15/25] Changes before pull --- .../homework03/Homework03Application.java | 6 ++ .../java/com/ironhack/homework03/Menu.java | 95 +++++++++++++++++++ .../java/com/ironhack/homework03/Options.java | 4 + .../controller/dto/StudentBooksDTO.java | 4 - .../homework03/repository/BookRepository.java | 13 +++ .../repository/BookRepositoryTest.java | 95 ++++++++++++++++++- 6 files changed, 212 insertions(+), 5 deletions(-) create mode 100644 homework03/src/main/java/com/ironhack/homework03/Options.java delete mode 100644 homework03/src/main/java/com/ironhack/homework03/controller/dto/StudentBooksDTO.java diff --git a/homework03/src/main/java/com/ironhack/homework03/Homework03Application.java b/homework03/src/main/java/com/ironhack/homework03/Homework03Application.java index e06ffef8..101e81d9 100644 --- a/homework03/src/main/java/com/ironhack/homework03/Homework03Application.java +++ b/homework03/src/main/java/com/ironhack/homework03/Homework03Application.java @@ -18,5 +18,11 @@ public static void main(String[] args) { @Override public void run(String... args) throws Exception { menu.showMenu(); +// boolean res = true; +// do{ +// res = menu.selectCommand(); +// }while(res); +// +// menu.closeScanner(); } } diff --git a/homework03/src/main/java/com/ironhack/homework03/Menu.java b/homework03/src/main/java/com/ironhack/homework03/Menu.java index b3c04750..72da9fe2 100644 --- a/homework03/src/main/java/com/ironhack/homework03/Menu.java +++ b/homework03/src/main/java/com/ironhack/homework03/Menu.java @@ -3,8 +3,13 @@ import com.ironhack.homework03.model.Style; import org.springframework.stereotype.Component; +import java.util.Scanner; + @Component public class Menu { + Scanner scanner = new Scanner(System.in); + public Menu() { + } public void showMenu(){ System.out.println(Style.SUCCESS+Style.BOLD + "\nFantastic! All data introduced is correct. Then, we provide a command list " + @@ -22,4 +27,94 @@ public void showMenu(){ System.out.println(Style.BOLD + "\n--------------------------------------------------------------------------------" + Style.RESET); } + + public boolean selectCommand(){ + boolean option = true; + + System.out.println("\nPlease introduce a command:"); + String cmd = scanner.nextLine(); + cmd = cmd.toUpperCase(); + if(cmd.equals("EXIT")){ + option = false; + scanner.close(); + }else{ + try{ +// sanitize(cmd); + } catch (Exception e) { + //System.out.println("Command not recognized, please try again."); + System.out.println(e.getMessage()); + } + } + //scanner.close(); + return option; + } + +// public void sanitize(String args){ +// //boolean sanitize = false; +// String[] values = args.split( " "); +// if (values.length > 3 && !values[0].equals("EXIT")) throw new IllegalArgumentException(Style.ERROR + "Too many arguments." + Style.RESET); +// if (values.length < 2 && !values[0].equals("EXIT")) throw new IllegalArgumentException(Style.ERROR + "Too few arguments." + Style.RESET); +// +// CommandFunction command = new CommandFunction(); +// +// // COMMANDO ENROLL +// if(values.length == 3 && values[0].equals("ENROLL")){ +// if(isValidStudentId(values[1]) && isValidCourseId(values[2])){ +// command.enroll(values[1],values[2]); +// }else{ +// throw new IllegalArgumentException(Style.ERROR + "Parameters for ENROLL are incorrect." + Style.RESET); +// } +// // COMMANDO ASSIGN +// }else if (values.length == 3 && values[0].equals("ASSIGN")){ +// if(isValidTeacherId(values[1]) && isValidCourseId(values[2])){ +// command.assign(values[1],values[2]); +// }else{ +// throw new IllegalArgumentException(Style.ERROR + "Parameters for ASSIGN are incorrect." + Style.RESET); +// } +// // COMMANDO SHOW +// }else if(values[0].equals("SHOW") && values.length == 2){ +// if(values[1].equals("COURSES")){ +// command.showCourses(); +// }else if(values[1].equals("STUDENTS")){ +// command.showStudents(); +// }else if(values[1].equals("TEACHERS")){ +// command.showTeachers(); +// }else if(values[1].equals("PROFIT")){ +// command.showProfit(); +// }else{ +// throw new IllegalArgumentException(Style.ERROR + "Invalid parameters for SHOW." + Style.RESET); +// } +// // COMMANDO LOOKUP +// }else if(values[0].equals("LOOKUP") && values.length == 3) { +// if (values[1].equals("STUDENT") && isValidStudentId(values[2])) { +// command.lookupStudent(values[2]); +// } else if (values[1].equals("COURSE") && isValidCourseId(values[2])) { +// command.lookupCourse(values[2]); +// } else if (values[1].equals("TEACHER") && isValidTeacherId(values[2])) { +// command.lookupTeacher(values[2]); +// } else { +// throw new IllegalArgumentException(Style.ERROR + "Invalid parameters for LOOKUP." + Style.RESET); +// } +// //showStudentsByCourse and showTeachersByCourse +// }else if(values[0].equals("SHOW") && values.length == 3){ +// if(values[1].equals("ENLISTED") && isValidCourseId(values[2])){ +// command.showStudentsByCourse(values[2]); +// }else if(values[1].equals("ASSIGNED") && isValidCourseId(values[2])){ +// command.showTeachersByCourse(values[2]); +// }else if(values[1].equals("MONEY") && values[2].equals("SPENT")){ +// command.showMoneySpent(); +// }else if(values[1].equals("MONEY") && values[2].equals("EARNED")){ +// command.showMoneyEarned(); +// }else{ +// throw new IllegalArgumentException("Invalid parameters for SHOW"); +// } +// }else{ +// throw new IllegalArgumentException(Style.ERROR + "Invalid Command." + Style.RESET); +// } +// //return sanitize; +// } + + public void closeScanner() { + this.scanner.close(); + } } diff --git a/homework03/src/main/java/com/ironhack/homework03/Options.java b/homework03/src/main/java/com/ironhack/homework03/Options.java new file mode 100644 index 00000000..fa94dbde --- /dev/null +++ b/homework03/src/main/java/com/ironhack/homework03/Options.java @@ -0,0 +1,4 @@ +package com.ironhack.homework03; + +public class Options { +} diff --git a/homework03/src/main/java/com/ironhack/homework03/controller/dto/StudentBooksDTO.java b/homework03/src/main/java/com/ironhack/homework03/controller/dto/StudentBooksDTO.java deleted file mode 100644 index 96dc7304..00000000 --- a/homework03/src/main/java/com/ironhack/homework03/controller/dto/StudentBooksDTO.java +++ /dev/null @@ -1,4 +0,0 @@ -package com.ironhack.homework03.controller.dto; - -public class StudentBooksDTO { -} diff --git a/homework03/src/main/java/com/ironhack/homework03/repository/BookRepository.java b/homework03/src/main/java/com/ironhack/homework03/repository/BookRepository.java index 6ec4f478..4ea6292a 100644 --- a/homework03/src/main/java/com/ironhack/homework03/repository/BookRepository.java +++ b/homework03/src/main/java/com/ironhack/homework03/repository/BookRepository.java @@ -2,8 +2,21 @@ import com.ironhack.homework03.model.Book; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; import org.springframework.stereotype.Repository; +import java.util.List; + @Repository public interface BookRepository extends JpaRepository { + + @Query(value = "SELECT b.title, s.name, i.return_date " + + "FROM book b " + + "LEFT JOIN issue i ON b.isbn = i.book_id " + + "LEFT JOIN student s ON i.student_id = s.usn " + + "WHERE s.usn = ?1", nativeQuery = true) + List findAllWhereUsnParams(String usn); + + List findIssueByUsn(String usn); + } diff --git a/homework03/src/test/java/com/ironhack/homework03/repository/BookRepositoryTest.java b/homework03/src/test/java/com/ironhack/homework03/repository/BookRepositoryTest.java index 693dc2f0..932019d0 100644 --- a/homework03/src/test/java/com/ironhack/homework03/repository/BookRepositoryTest.java +++ b/homework03/src/test/java/com/ironhack/homework03/repository/BookRepositoryTest.java @@ -1,4 +1,97 @@ +package com.ironhack.homework03.repository; + +import com.ironhack.homework03.model.Author; +import com.ironhack.homework03.model.Book; +import com.ironhack.homework03.model.Issue; +import com.ironhack.homework03.model.Student; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; + +import java.util.List; +import java.util.Optional; + import static org.junit.jupiter.api.Assertions.*; + +@SpringBootTest class BookRepositoryTest { - + + @Autowired + IssueRepository issueRepository; + + @Autowired + AuthorRepository authorRepository; + + @Autowired + BookRepository bookRepository; + + @Autowired + StudentRepository studentRepository; + + Issue issue; + Author author; + Book book; + Student student; + + @BeforeEach +// public void setUp(){ +// Student student = new Student("STUDENT130", "John Doe", null); +// Student student2 = new Student("STUDENT500", "Alice Dean", null); +// studentRepository.save(student); +// studentRepository.save(student2); +// +// Book book1 = new Book("123456789", "Da Vinci Code", "Mystery", 5, null, null); +// Book book2 = new Book("987654321", "Inferno", "Thriller", 3, null, null); +// bookRepository.save(book1); +// bookRepository.save(book2); +// Issue issue1 = new Issue(1,"2022-08-01", "2022-08-15", student, book1); +// +// Issue issue2 = new Issue(2,"2022-09-01", "2022-09-15", student2, book2); +// issueRepository.save(issue1); +// issueRepository.save(issue2); +// } + + @AfterEach + public void tearDown(){ + issueRepository.deleteAll(); + studentRepository.deleteAll(); + authorRepository.deleteAll(); + bookRepository.deleteAll(); + } + +// @Test +// void findAllWhereUsnParams() { +// List result = bookRepository.findAllWhereUsnParams("STUDENT130"); +// System.out.println("Book Title: " + result.get(0)[0] + " - Student Name: " + result.get(0)[1] + " - Return Date: " + result.get(0)[2]); +// +// assertEquals("Da Vinci Code", result.get(0)[0]); +// +// } + + @Test + void findAllWhereUsnParams() { + Issue secondIssue = new Issue(); + secondIssue.setIssueDate("2025-03-04"); + secondIssue.setReturnDate("2025-03-10"); + student = new Student(); + student.setUsn("STUDENT2"); + book = new Book(); + book.setIsbn("COOLBOOK2"); + author = new Author(); + author.setAuthorbook(book); + studentRepository.save(student); + authorRepository.save(author); + + Optional newBook = bookRepository.findById("COOLBOOK2"); + Optional newStudent = studentRepository.findById("STUDENT2"); + + secondIssue.setIssuestudent(newStudent.get()); + secondIssue.setIssuebook(newBook.get()); + + issueRepository.save(secondIssue); + + assertNotNull(issueRepository.findAll()); + } } \ No newline at end of file From ed1f80a94b5c3204f8406a0d31ae55b5a77a58ee Mon Sep 17 00:00:00 2001 From: Fernando Date: Wed, 5 Mar 2025 19:52:07 +0100 Subject: [PATCH 16/25] Menu method --- .../homework03/Homework03Application.java | 3 + .../java/com/ironhack/homework03/Menu.java | 81 +++++++++++++++---- .../java/com/ironhack/homework03/Options.java | 4 - 3 files changed, 69 insertions(+), 19 deletions(-) delete mode 100644 homework03/src/main/java/com/ironhack/homework03/Options.java diff --git a/homework03/src/main/java/com/ironhack/homework03/Homework03Application.java b/homework03/src/main/java/com/ironhack/homework03/Homework03Application.java index ab1d60f5..5b6211ec 100644 --- a/homework03/src/main/java/com/ironhack/homework03/Homework03Application.java +++ b/homework03/src/main/java/com/ironhack/homework03/Homework03Application.java @@ -26,5 +26,8 @@ public void run(String... args) throws Exception { res = menu.selectCommand(); }while(res); menu.closeScanner(); + + // End the program +// System.exit(0); } } diff --git a/homework03/src/main/java/com/ironhack/homework03/Menu.java b/homework03/src/main/java/com/ironhack/homework03/Menu.java index 1738bf57..60f4eaa8 100644 --- a/homework03/src/main/java/com/ironhack/homework03/Menu.java +++ b/homework03/src/main/java/com/ironhack/homework03/Menu.java @@ -1,6 +1,11 @@ package com.ironhack.homework03; import com.ironhack.homework03.model.Style; +import com.ironhack.homework03.repository.AuthorRepository; +import com.ironhack.homework03.repository.BookRepository; +import com.ironhack.homework03.repository.IssueRepository; +import com.ironhack.homework03.repository.StudentRepository; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.util.ArrayList; @@ -10,6 +15,13 @@ @Component public class Menu { + + @Autowired + AuthorRepository authorRepository; + BookRepository bookRepository; + IssueRepository issueRepository; + StudentRepository studentRepository; + Scanner scanner = new Scanner(System.in); public void showMenu() { @@ -31,27 +43,66 @@ public boolean selectCommand(){ boolean option = true; System.out.println("\nPlease introduce a command:"); - String cmd = scanner.nextLine(); - cmd = cmd.toUpperCase(); - if(cmd.equals("EXIT")){ - option = false; - scanner.close(); - }else{ - try{ - sanitize(cmd); - } catch (Exception e) { - //System.out.println("Command not recognized, please try again."); - System.out.println(e.getMessage()); - } + try { + int cmd = scanner.nextInt(); + try{ + sanitize(cmd); + } catch (Exception e) { + System.out.println(e.getMessage()); + } + } catch (InputMismatchException e) { + // If the input is not a number + System.out.println(Style.ERROR + "Invalid input. Please enter a valid number." + Style.RESET); + scanner.nextLine(); } - //scanner.close(); + return option; } - private void sanitize(String cmd) { - System.out.println("The command introduced is: "+ cmd); + public void sanitize(int option){ + + switch (option) { + case 1: + // ADD BOOK + + break; + case 2: + // SEARCH BOOK BY TITLE + + break; + case 3: + // SEARCH BOOK BY CATEGORY + + break; + case 4: + // SEARCH BOOK BY AUTHOR + + break; + case 5: + // LIST ALL BOOKS ALONG WITH AUTHOR + + break; + case 6: + // ISSUE BOOK TO STUDENT + + break; + case 7: + // LIST BOOKS BY USN + + break; + case 8: + // EXIT + System.out.println(Style.SUCCESS_BG+Style.BOLD + "Exiting program..." + Style.RESET); + + // End the program safe + System.exit(0); + break; + default: + throw new IllegalArgumentException(Style.ERROR + "Invalid Option." + Style.RESET); + } } + public void closeScanner(){ scanner.close(); } diff --git a/homework03/src/main/java/com/ironhack/homework03/Options.java b/homework03/src/main/java/com/ironhack/homework03/Options.java deleted file mode 100644 index fa94dbde..00000000 --- a/homework03/src/main/java/com/ironhack/homework03/Options.java +++ /dev/null @@ -1,4 +0,0 @@ -package com.ironhack.homework03; - -public class Options { -} From 2a55b471f0138f3328cdc3ef7f8b57e89eecc72a Mon Sep 17 00:00:00 2001 From: alvaro Date: Wed, 5 Mar 2025 22:36:37 +0100 Subject: [PATCH 17/25] added 1,4,5,6 options --- .../java/com/ironhack/homework03/Menu.java | 79 +++++++++++++++++-- .../com/ironhack/homework03/model/Book.java | 12 ++- .../repository/AuthorRepository.java | 3 + .../homework03/repository/BookRepository.java | 2 +- .../src/main/resources/application.properties | 2 +- 5 files changed, 87 insertions(+), 11 deletions(-) diff --git a/homework03/src/main/java/com/ironhack/homework03/Menu.java b/homework03/src/main/java/com/ironhack/homework03/Menu.java index 60f4eaa8..7cb8a204 100644 --- a/homework03/src/main/java/com/ironhack/homework03/Menu.java +++ b/homework03/src/main/java/com/ironhack/homework03/Menu.java @@ -1,6 +1,6 @@ package com.ironhack.homework03; -import com.ironhack.homework03.model.Style; +import com.ironhack.homework03.model.*; import com.ironhack.homework03.repository.AuthorRepository; import com.ironhack.homework03.repository.BookRepository; import com.ironhack.homework03.repository.IssueRepository; @@ -8,18 +8,18 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; -import java.util.ArrayList; -import java.util.InputMismatchException; -import java.util.List; -import java.util.Scanner; +import java.util.*; @Component public class Menu { @Autowired AuthorRepository authorRepository; - BookRepository bookRepository; + @Autowired + public BookRepository bookRepository; + @Autowired IssueRepository issueRepository; + @Autowired StudentRepository studentRepository; Scanner scanner = new Scanner(System.in); @@ -45,6 +45,7 @@ public boolean selectCommand(){ System.out.println("\nPlease introduce a command:"); try { int cmd = scanner.nextInt(); + scanner.nextLine(); try{ sanitize(cmd); } catch (Exception e) { @@ -62,7 +63,35 @@ public boolean selectCommand(){ public void sanitize(int option){ switch (option) { + case 0: + showMenu(); + break; case 1: + System.out.println("Please introduce the data to add a new book."); + Book book = new Book(); + System.out.print("Introduce ISBN: "); + String res = scanner.nextLine(); + book.setIsbn(res); + System.out.print("Introduce Title: "); + res = scanner.nextLine(); + book.setTitle(res); + System.out.print("Introduce Category: "); + res = scanner.nextLine(); + book.setCategory(res); + Author author = new Author(); + System.out.print("Introduce Author Name: "); + res = scanner.nextLine(); + author.setName(res); + System.out.print("Introduce Author Email: "); + res = scanner.nextLine(); + author.setEmail(res); + System.out.print("Enter number of books: "); + res = scanner.nextLine(); + book.setQuantity(Integer.parseInt(res)); + author.setAuthorbook(book); + authorRepository.save(author); + System.out.println("Book saved succesfully\n"); + // ADD BOOK break; @@ -76,14 +105,48 @@ public void sanitize(int option){ break; case 4: // SEARCH BOOK BY AUTHOR - + System.out.println("Please Introduce the Author's Name: "); + String res4 = scanner.nextLine(); + Optional author4 = authorRepository.findByName(res4); + if(author4.isPresent()){ + System.out.println("The following book matches the author: "); + System.out.println(author4.get().getAuthorbook().toString()); + }else{ + System.out.println("No book found for author."); + } break; case 5: // LIST ALL BOOKS ALONG WITH AUTHOR - + List authorList = authorRepository.findAll(); + for(Author a : authorList){ + System.out.println(a.toString()); + } break; case 6: // ISSUE BOOK TO STUDENT + Issue issue = new Issue(); + Student student = new Student(); + System.out.println("Enter usn: "); + String res6 = scanner.nextLine(); + student.setUsn(res6); + System.out.println("Enter student name: "); + res6 = scanner.nextLine(); + student.setName(res6); + System.out.println("Enter isbn: "); + res6 = scanner.nextLine(); + Optional book6 = bookRepository.findById(res6); + if(book6.isPresent()){// CASCADE EFFECT + studentRepository.save(student); + issue.setIssuestudent(student); + issue.setIssuebook(book6.get()); + issue.setIssueDate("Now"); + issue.setReturnDate("In one week"); + issueRepository.save(issue); + System.out.println("Issue and student registered correctly."); + }else{ + System.out.println("Could not find that book."); + } + break; case 7: diff --git a/homework03/src/main/java/com/ironhack/homework03/model/Book.java b/homework03/src/main/java/com/ironhack/homework03/model/Book.java index 541e07f6..6da2e084 100644 --- a/homework03/src/main/java/com/ironhack/homework03/model/Book.java +++ b/homework03/src/main/java/com/ironhack/homework03/model/Book.java @@ -28,4 +28,14 @@ public class Book { @OneToOne(mappedBy = "issuebook") //, cascade = CascadeType.REMOVE, orphanRemoval = true private Issue issue; -} + + @Override + public String toString() { + return "Book{" + + "isbn='" + isbn + '\'' + + ", title='" + title + '\'' + + ", category='" + category + '\'' + + ", quantity=" + quantity + + '}'; + } + } diff --git a/homework03/src/main/java/com/ironhack/homework03/repository/AuthorRepository.java b/homework03/src/main/java/com/ironhack/homework03/repository/AuthorRepository.java index 06841d65..e04f83c8 100644 --- a/homework03/src/main/java/com/ironhack/homework03/repository/AuthorRepository.java +++ b/homework03/src/main/java/com/ironhack/homework03/repository/AuthorRepository.java @@ -5,6 +5,9 @@ import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; +import java.util.Optional; + @Repository public interface AuthorRepository extends JpaRepository{ + Optional findByName(String name); } \ No newline at end of file diff --git a/homework03/src/main/java/com/ironhack/homework03/repository/BookRepository.java b/homework03/src/main/java/com/ironhack/homework03/repository/BookRepository.java index a8cdab5b..a5a083d5 100644 --- a/homework03/src/main/java/com/ironhack/homework03/repository/BookRepository.java +++ b/homework03/src/main/java/com/ironhack/homework03/repository/BookRepository.java @@ -27,7 +27,7 @@ public interface BookRepository extends JpaRepository { "WHERE s.usn = ?1", nativeQuery = true) List findAllWhereUsnParams(String usn); - List findIssueByUsn(String usn); + //List findIssueByUsn(String usn); } diff --git a/homework03/src/main/resources/application.properties b/homework03/src/main/resources/application.properties index eefa370e..745b1b14 100644 --- a/homework03/src/main/resources/application.properties +++ b/homework03/src/main/resources/application.properties @@ -4,6 +4,6 @@ spring.datasource.url=jdbc:mysql://localhost:3306/homework3?createDatabaseIfNotE spring.datasource.username=root spring.datasource.password=root spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver -spring.jpa.hibernate.ddl-auto=create-drop +spring.jpa.hibernate.ddl-auto=update spring.jpa.show-sql=true spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true \ No newline at end of file From 67c4338fd4094314433d1fdf35755dbf67fa8fad Mon Sep 17 00:00:00 2001 From: Fernando Date: Wed, 5 Mar 2025 22:46:16 +0100 Subject: [PATCH 18/25] Merge functions --- .../com/ironhack/homework03/Function.java | 79 +++++++++++++++++++ .../java/com/ironhack/homework03/Menu.java | 10 +-- .../com/ironhack/homework03/model/Issue.java | 2 - .../com/ironhack/homework03/model/Style.java | 2 +- .../homework03/repository/BookRepository.java | 2 +- .../src/main/resources/application.properties | 20 ++++- 6 files changed, 101 insertions(+), 14 deletions(-) create mode 100644 homework03/src/main/java/com/ironhack/homework03/Function.java diff --git a/homework03/src/main/java/com/ironhack/homework03/Function.java b/homework03/src/main/java/com/ironhack/homework03/Function.java new file mode 100644 index 00000000..e6fc1313 --- /dev/null +++ b/homework03/src/main/java/com/ironhack/homework03/Function.java @@ -0,0 +1,79 @@ +package com.ironhack.homework03; + +import com.ironhack.homework03.model.Author; +import com.ironhack.homework03.model.Book; +import com.ironhack.homework03.model.Style; +import com.ironhack.homework03.repository.AuthorRepository; +import com.ironhack.homework03.repository.BookRepository; +import com.ironhack.homework03.repository.IssueRepository; +import com.ironhack.homework03.repository.StudentRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import java.util.Scanner; + +@Component +public class Function { + + @Autowired + AuthorRepository authorRepository; + @Autowired + BookRepository bookRepository; + @Autowired + IssueRepository issueRepository; + @Autowired + StudentRepository studentRepository; + + private final Scanner scanner = new Scanner(System.in); + + public void addBook(){ +// Author author = new Author(); +// Book book = new Book(); + + System.out.print("Enter isbn : "); + String isbn = scanner.nextLine(); + + System.out.print("Enter title : "); + String title = scanner.nextLine(); + + System.out.print("Enter category : "); + String category = scanner.nextLine(); + + System.out.print("Enter Author name : "); + String authorName = scanner.nextLine(); + + System.out.print("Enter Author mail : "); + String authorMail = scanner.nextLine(); + + System.out.print("Enter number of books : "); + int numBooks = scanner.nextInt(); + + Book book = new Book(); + book.setIsbn(isbn); + book.setTitle(title); + book.setCategory(category); + book.setQuantity(numBooks); + + Author author = new Author(); + author.setName(authorName); + author.setEmail(authorMail); + + book.setAuthor(author); + author.setAuthorbook(book); + + // Save author before assign to book + authorRepository.save(author); + + // Save book with the author + bookRepository.save(book); + + System.out.println(Style.SUCCESS_BG+Style.BOLD+ "The book with isbn " + + "( " + Style.SUCCESS + book.getIsbn() + Style.RESET+Style.SUCCESS_BG+Style.BOLD + " ) " + + "has been saved " + Style.SUCCESS + "successfully." + Style.RESET+Style.SUCCESS_BG+Style.BOLD + " " + Style.RESET); + } + + public void searchBookByTitle(String title){ + + } + +} diff --git a/homework03/src/main/java/com/ironhack/homework03/Menu.java b/homework03/src/main/java/com/ironhack/homework03/Menu.java index 60f4eaa8..bee8eb4d 100644 --- a/homework03/src/main/java/com/ironhack/homework03/Menu.java +++ b/homework03/src/main/java/com/ironhack/homework03/Menu.java @@ -17,10 +17,7 @@ public class Menu { @Autowired - AuthorRepository authorRepository; - BookRepository bookRepository; - IssueRepository issueRepository; - StudentRepository studentRepository; + Function function; Scanner scanner = new Scanner(System.in); @@ -42,7 +39,7 @@ public void showMenu() { public boolean selectCommand(){ boolean option = true; - System.out.println("\nPlease introduce a command:"); + System.out.print("\nPlease enter your choice : "); try { int cmd = scanner.nextInt(); try{ @@ -64,11 +61,10 @@ public void sanitize(int option){ switch (option) { case 1: // ADD BOOK - + function.addBook(); break; case 2: // SEARCH BOOK BY TITLE - break; case 3: // SEARCH BOOK BY CATEGORY diff --git a/homework03/src/main/java/com/ironhack/homework03/model/Issue.java b/homework03/src/main/java/com/ironhack/homework03/model/Issue.java index a155199c..7f66d8dc 100644 --- a/homework03/src/main/java/com/ironhack/homework03/model/Issue.java +++ b/homework03/src/main/java/com/ironhack/homework03/model/Issue.java @@ -23,6 +23,4 @@ public class Issue { @OneToOne @JoinColumn(name = "book_id", unique = true) private Book issuebook; - - } \ No newline at end of file diff --git a/homework03/src/main/java/com/ironhack/homework03/model/Style.java b/homework03/src/main/java/com/ironhack/homework03/model/Style.java index c0c325a0..cea32547 100644 --- a/homework03/src/main/java/com/ironhack/homework03/model/Style.java +++ b/homework03/src/main/java/com/ironhack/homework03/model/Style.java @@ -14,7 +14,7 @@ public class Style { public static final String STUDENT = rgbColor(0, 179, 168); public static final String HIGHLIGHT = rgbColor(255, 255, 100); public static final String ERROR = rgbColor(255, 99, 71); - public static final String SUCCESS = rgbColor(144, 238, 144); + public static final String SUCCESS = rgbColor(48, 160, 79); public static final String SALARY = rgbColor(179, 173, 0); public static final String NOT_FOUND = rgbColor(255, 165, 50); diff --git a/homework03/src/main/java/com/ironhack/homework03/repository/BookRepository.java b/homework03/src/main/java/com/ironhack/homework03/repository/BookRepository.java index a8cdab5b..6c5d2a85 100644 --- a/homework03/src/main/java/com/ironhack/homework03/repository/BookRepository.java +++ b/homework03/src/main/java/com/ironhack/homework03/repository/BookRepository.java @@ -27,7 +27,7 @@ public interface BookRepository extends JpaRepository { "WHERE s.usn = ?1", nativeQuery = true) List findAllWhereUsnParams(String usn); - List findIssueByUsn(String usn); +// List findIssueByUsn(String usn); } diff --git a/homework03/src/main/resources/application.properties b/homework03/src/main/resources/application.properties index eefa370e..0a968346 100644 --- a/homework03/src/main/resources/application.properties +++ b/homework03/src/main/resources/application.properties @@ -4,6 +4,20 @@ spring.datasource.url=jdbc:mysql://localhost:3306/homework3?createDatabaseIfNotE spring.datasource.username=root spring.datasource.password=root spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver -spring.jpa.hibernate.ddl-auto=create-drop -spring.jpa.show-sql=true -spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true \ No newline at end of file +spring.jpa.hibernate.ddl-auto=update +#spring.jpa.show-sql=true +spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true + +# DESACTIVAR EL LOG DE HIBERNATE +spring.jpa.show-sql=false +spring.jpa.properties.hibernate.format_sql=false +logging.level.org.hibernate.SQL=OFF +logging.level.org.hibernate.type.descriptor.sql=OFF + +#PARA OCULTAR EL BANNER DE SPRING +spring.main.banner-mode=off +#PARA HABILITAR COLORES +spring.output.ansi.enabled=ALWAYS + +// PARA QUE NO SALGA TANTO CONTENIDO CUANDO SALE UN ERROR +server.error.include-stacktrace=never \ No newline at end of file From 0967e475457e15e29f887efb11c927f34578cc67 Mon Sep 17 00:00:00 2001 From: Fernando Date: Thu, 6 Mar 2025 01:28:02 +0100 Subject: [PATCH 19/25] Functional Menu --- .../com/ironhack/homework03/Function.java | 233 +++++++++++++++--- .../java/com/ironhack/homework03/Menu.java | 75 +----- .../repository/BookRepositoryTest.java | 55 +++-- 3 files changed, 233 insertions(+), 130 deletions(-) diff --git a/homework03/src/main/java/com/ironhack/homework03/Function.java b/homework03/src/main/java/com/ironhack/homework03/Function.java index e6fc1313..3d6bbea1 100644 --- a/homework03/src/main/java/com/ironhack/homework03/Function.java +++ b/homework03/src/main/java/com/ironhack/homework03/Function.java @@ -1,8 +1,6 @@ package com.ironhack.homework03; -import com.ironhack.homework03.model.Author; -import com.ironhack.homework03.model.Book; -import com.ironhack.homework03.model.Style; +import com.ironhack.homework03.model.*; import com.ironhack.homework03.repository.AuthorRepository; import com.ironhack.homework03.repository.BookRepository; import com.ironhack.homework03.repository.IssueRepository; @@ -10,6 +8,9 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; +import java.util.List; +import java.util.Objects; +import java.util.Optional; import java.util.Scanner; @Component @@ -27,53 +28,209 @@ public class Function { private final Scanner scanner = new Scanner(System.in); public void addBook(){ -// Author author = new Author(); -// Book book = new Book(); - - System.out.print("Enter isbn : "); - String isbn = scanner.nextLine(); - - System.out.print("Enter title : "); - String title = scanner.nextLine(); - - System.out.print("Enter category : "); - String category = scanner.nextLine(); - - System.out.print("Enter Author name : "); - String authorName = scanner.nextLine(); - - System.out.print("Enter Author mail : "); - String authorMail = scanner.nextLine(); - - System.out.print("Enter number of books : "); - int numBooks = scanner.nextInt(); - - Book book = new Book(); - book.setIsbn(isbn); - book.setTitle(title); - book.setCategory(category); - book.setQuantity(numBooks); + System.out.println("Please introduce the data to add a new book."); + Book book = new Book(); + System.out.print("Introduce ISBN: "); + String res = scanner.nextLine(); + book.setIsbn(res); + System.out.print("Introduce Title: "); + res = scanner.nextLine(); + book.setTitle(res); + System.out.print("Introduce Category: "); + res = scanner.nextLine(); + book.setCategory(res); Author author = new Author(); - author.setName(authorName); - author.setEmail(authorMail); - - book.setAuthor(author); + System.out.print("Introduce Author Name: "); + res = scanner.nextLine(); + author.setName(res); + System.out.print("Introduce Author Email: "); + res = scanner.nextLine(); + author.setEmail(res); + System.out.print("Enter number of books: "); + res = scanner.nextLine(); + book.setQuantity(Integer.parseInt(res)); author.setAuthorbook(book); - - // Save author before assign to book authorRepository.save(author); - // Save book with the author - bookRepository.save(book); +// System.out.print("Enter isbn : "); +// String isbn = scanner.nextLine(); +// +// System.out.print("Enter title : "); +// String title = scanner.nextLine(); +// +// System.out.print("Enter category : "); +// String category = scanner.nextLine(); +// +// System.out.print("Enter Author name : "); +// String authorName = scanner.nextLine(); +// +// System.out.print("Enter Author mail : "); +// String authorMail = scanner.nextLine(); +// +// System.out.print("Enter number of books : "); +// int numBooks = scanner.nextInt(); +// +// Book book = new Book(); +// book.setIsbn(isbn); +// book.setTitle(title); +// book.setCategory(category); +// book.setQuantity(numBooks); +// +// Author author = new Author(); +// author.setName(authorName); +// author.setEmail(authorMail); +// +// book.setAuthor(author); +// author.setAuthorbook(book); +// +// // Save author before assign to book +// authorRepository.save(author); +// +// // Save book with the author +// bookRepository.save(book); System.out.println(Style.SUCCESS_BG+Style.BOLD+ "The book with isbn " + "( " + Style.SUCCESS + book.getIsbn() + Style.RESET+Style.SUCCESS_BG+Style.BOLD + " ) " + "has been saved " + Style.SUCCESS + "successfully." + Style.RESET+Style.SUCCESS_BG+Style.BOLD + " " + Style.RESET); } - public void searchBookByTitle(String title){ + public void searchBookByTitle(){ + System.out.print("Please Introduce the Book's Title : "); + String title = scanner.nextLine(); + Optional book2 = bookRepository.findByTitle(title); + if(book2.isPresent()){ +// System.out.println("The following book matches the author : "); +// System.out.println(book2.get().getIsbn() + " - " + book2.get().getTitle() + " - " + book2.get().getCategory() + " - " + book2.get().getQuantity()); + + System.out.printf(Style.BOLD + "\n%-20s %-20s %-15s %-10s%n" + Style.RESET, + "Book ISBN", + "Book Title", + "Category", + "No of Books"); + + System.out.printf(Style.STUDENT + "%-20s %-20s %-15s %-10d%n" + Style.RESET, + book2.get().getIsbn(), + book2.get().getTitle(), + book2.get().getCategory(), + book2.get().getQuantity()); + + }else{ + System.out.println(Style.NOT_FOUND + "No book found by title." + Style.RESET); + } + } + + public void searchBookByCategory(){ + System.out.print("Please Introduce the Book's Category : "); + String category = scanner.nextLine(); + Optional book3 = bookRepository.findByCategory(category); + if(book3.isPresent()){ +// System.out.println("The following book matches the author : "); +// System.out.println(book3.get().getIsbn() + " - " + book3.get().getTitle() + " - " + book3.get().getCategory() + " - " + book3.get().getQuantity()); + System.out.printf(Style.BOLD + "\n%-20s %-20s %-15s %-10s%n" + Style.RESET, + "Book ISBN", + "Book Title", + "Category", + "No of Books"); + + System.out.printf(Style.STUDENT + "%-20s %-20s %-15s %-10d%n" + Style.RESET, + book3.get().getIsbn(), + book3.get().getTitle(), + book3.get().getCategory(), + book3.get().getQuantity()); + }else{ + System.out.println(Style.NOT_FOUND + "No book found by category." + Style.RESET); + } + } + + public void searchBookByAuthor(){ + System.out.print("Please Introduce the Author's Name : "); + String res4 = scanner.nextLine(); + Optional author4 = authorRepository.findByName(res4); + if(author4.isPresent()){ +// System.out.println("The following book matches the author : "); +// System.out.println(author4.get().getAuthorbook().toString()); + System.out.printf(Style.BOLD + "\n%-20s %-20s %-15s %-10s%n" + Style.RESET, + "Book ISBN", + "Book Title", + "Category", + "No of Books"); + + System.out.printf(Style.STUDENT + "%-20s %-20s %-15s %-10d%n" + Style.RESET, + author4.get().getAuthorbook().getIsbn(), + author4.get().getAuthorbook().getTitle(), + author4.get().getAuthorbook().getCategory(), + author4.get().getAuthorbook().getQuantity()); + }else{ + System.out.println(Style.NOT_FOUND + "No book found for author." + Style.RESET); + } + } + + public void searchBooksWithAuthor(){ + List authorList = authorRepository.findAll(); + System.out.printf(Style.BOLD + "\n%-20s %-20s %-15s %-15s %-25s %-20s%n" + Style.RESET, + "Book ISBN", + "Book Title", + "Category", + "No of Books", + "Author name", + "Author mail"); + + for(Author a : authorList){ +// System.out.println(a.toString()); + System.out.printf(Style.STUDENT + "%-20s %-20s %-15s %-15d %-25s %-20s%n" + Style.RESET, + a.getAuthorbook().getIsbn(), + a.getAuthorbook().getTitle(), + a.getAuthorbook().getCategory(), + a.getAuthorbook().getQuantity(), + a.getName(), + a.getEmail()); + } + } + + public void issueBookToStudent(){ + Issue issue = new Issue(); + Student student = new Student(); + System.out.println("Enter usn: "); + String res6 = scanner.nextLine(); + student.setUsn(res6); + System.out.println("Enter student name: "); + res6 = scanner.nextLine(); + student.setName(res6); + System.out.println("Enter isbn: "); + res6 = scanner.nextLine(); + Optional book6 = bookRepository.findById(res6); + if(book6.isPresent()){// CASCADE EFFECT + studentRepository.save(student); + issue.setIssuestudent(student); + issue.setIssuebook(book6.get()); + issue.setIssueDate("Now"); + issue.setReturnDate("In one week"); + issueRepository.save(issue); + System.out.println("Issue and student registered correctly."); + }else{ + System.out.println("Could not find that book."); + } + } + public void searchBooksByUsn(){ + System.out.print("Please Introduce an USN : "); + String res7 = scanner.nextLine(); + List bookList = bookRepository.findAllWhereUsnParams(res7); + if(!bookList.isEmpty()){ +// System.out.println(bookList.get(0)[0] + " - " + bookList.get(0)[1] + " - " + bookList.get(0)[2]); + System.out.printf(Style.BOLD + "\n%-20s %-20s %-30s%n" + Style.RESET, + "Book Title", + "Student Name", + "Return date"); + + System.out.printf(Style.STUDENT + "%-20s %-20s %-30s%n" + Style.RESET, + bookList.get(0)[0], + bookList.get(0)[1], + bookList.get(0)[2]); + }else{ + System.out.println(Style.NOT_FOUND + "No book found by usn." + Style.RESET); + } } } diff --git a/homework03/src/main/java/com/ironhack/homework03/Menu.java b/homework03/src/main/java/com/ironhack/homework03/Menu.java index 4f9fe3b6..d5d7bab6 100644 --- a/homework03/src/main/java/com/ironhack/homework03/Menu.java +++ b/homework03/src/main/java/com/ironhack/homework03/Menu.java @@ -34,7 +34,7 @@ public void showMenu() { "\n2 " + Style.SUCCESS_BG + Style.BOLD + " SEARCH BOOK TITLE " + Style.RESET + "\n3 " + Style.SUCCESS_BG + Style.BOLD + " SEARCH BOOK CATEGORY " + Style.RESET + "\n4 " + Style.SUCCESS_BG + Style.BOLD + " SEARCH BOOK AUTHOR " + Style.RESET + - "\n5 " + Style.SUCCESS_BG + Style.BOLD + " LIST BOOK AUTHOR " + Style.RESET + + "\n5 " + Style.SUCCESS_BG + Style.BOLD + " LIST OF BOOKS WITH AUTHOR " + Style.RESET + "\n6 " + Style.SUCCESS_BG + Style.BOLD + " ISSUE BOOK STUDENT " + Style.RESET + "\n7 " + Style.SUCCESS_BG + Style.BOLD + " LIST BOOK USN " + Style.RESET + "\n8 " + Style.SUCCESS_BG + Style.BOLD + " EXIT " + Style.RESET); @@ -70,91 +70,32 @@ public void sanitize(int option){ showMenu(); break; case 1: - System.out.println("Please introduce the data to add a new book."); - Book book = new Book(); - System.out.print("Introduce ISBN: "); - String res = scanner.nextLine(); - book.setIsbn(res); - System.out.print("Introduce Title: "); - res = scanner.nextLine(); - book.setTitle(res); - System.out.print("Introduce Category: "); - res = scanner.nextLine(); - book.setCategory(res); - Author author = new Author(); - System.out.print("Introduce Author Name: "); - res = scanner.nextLine(); - author.setName(res); - System.out.print("Introduce Author Email: "); - res = scanner.nextLine(); - author.setEmail(res); - System.out.print("Enter number of books: "); - res = scanner.nextLine(); - book.setQuantity(Integer.parseInt(res)); - author.setAuthorbook(book); - authorRepository.save(author); - System.out.println("Book saved succesfully\n"); - // ADD BOOK - + function.addBook(); break; case 2: // SEARCH BOOK BY TITLE - + function.searchBookByTitle(); break; case 3: // SEARCH BOOK BY CATEGORY - + function.searchBookByCategory(); break; case 4: // SEARCH BOOK BY AUTHOR - System.out.println("Please Introduce the Author's Name: "); - String res4 = scanner.nextLine(); - Optional author4 = authorRepository.findByName(res4); - if(author4.isPresent()){ - System.out.println("The following book matches the author: "); - System.out.println(author4.get().getAuthorbook().toString()); - }else{ - System.out.println("No book found for author."); - } + function.searchBookByAuthor(); break; case 5: // LIST ALL BOOKS ALONG WITH AUTHOR - List authorList = authorRepository.findAll(); - for(Author a : authorList){ - System.out.println(a.toString()); - } + function.searchBooksWithAuthor(); break; case 6: // ISSUE BOOK TO STUDENT - Issue issue = new Issue(); - Student student = new Student(); - System.out.println("Enter usn: "); - String res6 = scanner.nextLine(); - student.setUsn(res6); - System.out.println("Enter student name: "); - res6 = scanner.nextLine(); - student.setName(res6); - System.out.println("Enter isbn: "); - res6 = scanner.nextLine(); - Optional book6 = bookRepository.findById(res6); - if(book6.isPresent()){// CASCADE EFFECT - studentRepository.save(student); - issue.setIssuestudent(student); - issue.setIssuebook(book6.get()); - issue.setIssueDate("Now"); - issue.setReturnDate("In one week"); - issueRepository.save(issue); - System.out.println("Issue and student registered correctly."); - }else{ - System.out.println("Could not find that book."); - } - - + function.issueBookToStudent(); break; case 7: // LIST BOOKS BY USN - + function.searchBooksByUsn(); break; case 8: // EXIT diff --git a/homework03/src/test/java/com/ironhack/homework03/repository/BookRepositoryTest.java b/homework03/src/test/java/com/ironhack/homework03/repository/BookRepositoryTest.java index 39277da5..b26d792a 100644 --- a/homework03/src/test/java/com/ironhack/homework03/repository/BookRepositoryTest.java +++ b/homework03/src/test/java/com/ironhack/homework03/repository/BookRepositoryTest.java @@ -1,5 +1,6 @@ package com.ironhack.homework03.repository; +import com.ironhack.homework03.Menu; import com.ironhack.homework03.model.Author; import com.ironhack.homework03.model.Book; import com.ironhack.homework03.model.Issue; @@ -12,6 +13,7 @@ import java.util.List; import java.util.Optional; +import java.util.Scanner; import static org.junit.jupiter.api.Assertions.*; @@ -30,28 +32,31 @@ class BookRepositoryTest { @Autowired StudentRepository studentRepository; + @Autowired + Menu menu; + Issue issue; Author author; Book book; Student student; - @BeforeEach -// public void setUp(){ -// Student student = new Student("STUDENT130", "John Doe", null); -// Student student2 = new Student("STUDENT500", "Alice Dean", null); -// studentRepository.save(student); -// studentRepository.save(student2); -// -// Book book1 = new Book("123456789", "Da Vinci Code", "Mystery", 5, null, null); -// Book book2 = new Book("987654321", "Inferno", "Thriller", 3, null, null); -// bookRepository.save(book1); -// bookRepository.save(book2); -// Issue issue1 = new Issue(1,"2022-08-01", "2022-08-15", student, book1); -// -// Issue issue2 = new Issue(2,"2022-09-01", "2022-09-15", student2, book2); -// issueRepository.save(issue1); -// issueRepository.save(issue2); -// } + public void setUp(){ + Student student = new Student("STUDENT130", "John Doe", null); + Student student2 = new Student("STUDENT500", "Alice Dean", null); + studentRepository.save(student); + studentRepository.save(student2); + + Book book1 = new Book("123456789", "Da Vinci Code", "Mystery", 5, null, null); + Book book2 = new Book("987654321", "Inferno", "Thriller", 3, null, null); + bookRepository.save(book1); + bookRepository.save(book2); + Issue issue1 = new Issue(1,"2022-08-01", "2022-08-15", student, book1); + + Issue issue2 = new Issue(2,"2022-09-01", "2022-09-15", student2, book2); + issueRepository.save(issue1); + issueRepository.save(issue2); + + } @AfterEach public void tearDown(){ @@ -61,14 +66,14 @@ public void tearDown(){ bookRepository.deleteAll(); } -// @Test -// void findAllWhereUsnParams() { -// List result = bookRepository.findAllWhereUsnParams("STUDENT130"); -// System.out.println("Book Title: " + result.get(0)[0] + " - Student Name: " + result.get(0)[1] + " - Return Date: " + result.get(0)[2]); -// -// assertEquals("Da Vinci Code", result.get(0)[0]); -// -// } + @Test + void findAllWhereUsnParams() { + List result = bookRepository.findAllWhereUsnParams("STUDENT130"); + System.out.println("Book Title: " + result.get(0)[0] + " - Student Name: " + result.get(0)[1] + " - Return Date: " + result.get(0)[2]); + + assertEquals("Da Vinci Code", result.get(0)[0]); + + } // From 9a0bdadf66854c78b951c23d5caaf860d312a032 Mon Sep 17 00:00:00 2001 From: alvaro Date: Thu, 6 Mar 2025 12:08:33 +0100 Subject: [PATCH 20/25] added data protection --- .../com/ironhack/homework03/Function.java | 44 +++++++++++++------ .../java/com/ironhack/homework03/Menu.java | 12 ++++- 2 files changed, 40 insertions(+), 16 deletions(-) diff --git a/homework03/src/main/java/com/ironhack/homework03/Function.java b/homework03/src/main/java/com/ironhack/homework03/Function.java index 3d6bbea1..fb218856 100644 --- a/homework03/src/main/java/com/ironhack/homework03/Function.java +++ b/homework03/src/main/java/com/ironhack/homework03/Function.java @@ -50,8 +50,14 @@ public void addBook(){ System.out.print("Enter number of books: "); res = scanner.nextLine(); book.setQuantity(Integer.parseInt(res)); - author.setAuthorbook(book); - authorRepository.save(author); + Optional check = bookRepository.findById(book.getIsbn()); + Optional checkAuthor = authorRepository.findByName(author.getName()); + if(check.isEmpty() && checkAuthor.isEmpty()){ + author.setAuthorbook(book); + authorRepository.save(author); + }else{ + throw new IllegalArgumentException("Author or Book already exist."); + } // System.out.print("Enter isbn : "); // String isbn = scanner.nextLine(); @@ -197,20 +203,30 @@ public void issueBookToStudent(){ System.out.println("Enter student name: "); res6 = scanner.nextLine(); student.setName(res6); - System.out.println("Enter isbn: "); - res6 = scanner.nextLine(); - Optional book6 = bookRepository.findById(res6); - if(book6.isPresent()){// CASCADE EFFECT - studentRepository.save(student); - issue.setIssuestudent(student); - issue.setIssuebook(book6.get()); - issue.setIssueDate("Now"); - issue.setReturnDate("In one week"); - issueRepository.save(issue); - System.out.println("Issue and student registered correctly."); + Optional studentCheck = studentRepository.findById(student.getUsn()); + if(studentCheck.isEmpty()){ + System.out.println("Enter isbn: "); + res6 = scanner.nextLine(); + Optional book6 = bookRepository.findById(res6); + if(book6.isPresent()){// CASCADE EFFECT + if(book6.get().getIssue()==null){ + studentRepository.save(student); + issue.setIssuestudent(student); + issue.setIssuebook(book6.get()); + issue.setIssueDate("Now"); + issue.setReturnDate("In one week"); + issueRepository.save(issue); + System.out.println("Issue and student registered correctly."); + }else{ + System.out.println("This book has already been issued."); + } + }else{ + System.out.println("Could not find that book."); + } }else{ - System.out.println("Could not find that book."); + System.out.println("The student all ready exists."); } + } public void searchBooksByUsn(){ diff --git a/homework03/src/main/java/com/ironhack/homework03/Menu.java b/homework03/src/main/java/com/ironhack/homework03/Menu.java index d5d7bab6..b35243f2 100644 --- a/homework03/src/main/java/com/ironhack/homework03/Menu.java +++ b/homework03/src/main/java/com/ironhack/homework03/Menu.java @@ -71,7 +71,11 @@ public void sanitize(int option){ break; case 1: // ADD BOOK - function.addBook(); + try{ + function.addBook(); + } catch (Exception e) { + System.out.println(e.getMessage()); + } break; case 2: // SEARCH BOOK BY TITLE @@ -91,7 +95,11 @@ public void sanitize(int option){ break; case 6: // ISSUE BOOK TO STUDENT - function.issueBookToStudent(); + try{ + function.issueBookToStudent(); + } catch (Exception e) { + System.out.println(e.getMessage()); + } break; case 7: // LIST BOOKS BY USN From 551b964ebe1da9c0ae4a88920f558d27efa1c14f Mon Sep 17 00:00:00 2001 From: alvaro Date: Thu, 6 Mar 2025 12:20:03 +0100 Subject: [PATCH 21/25] fixed date on model issue --- .../java/com/ironhack/homework03/Function.java | 9 +++------ .../com/ironhack/homework03/model/Issue.java | 6 ++++-- .../src/main/resources/application.properties | 2 +- .../ironhack/homework03/model/IssueTest.java | 17 +++++++++-------- 4 files changed, 17 insertions(+), 17 deletions(-) diff --git a/homework03/src/main/java/com/ironhack/homework03/Function.java b/homework03/src/main/java/com/ironhack/homework03/Function.java index fb218856..72afd823 100644 --- a/homework03/src/main/java/com/ironhack/homework03/Function.java +++ b/homework03/src/main/java/com/ironhack/homework03/Function.java @@ -8,10 +8,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; -import java.util.List; -import java.util.Objects; -import java.util.Optional; -import java.util.Scanner; +import java.util.*; @Component public class Function { @@ -213,8 +210,8 @@ public void issueBookToStudent(){ studentRepository.save(student); issue.setIssuestudent(student); issue.setIssuebook(book6.get()); - issue.setIssueDate("Now"); - issue.setReturnDate("In one week"); + issue.setIssueDate(new Date(System.currentTimeMillis())); + issue.setReturnDate(new Date(System.currentTimeMillis() + 7 * 24 * 60 * 60 * 1000)); issueRepository.save(issue); System.out.println("Issue and student registered correctly."); }else{ diff --git a/homework03/src/main/java/com/ironhack/homework03/model/Issue.java b/homework03/src/main/java/com/ironhack/homework03/model/Issue.java index 7f66d8dc..6bb25d10 100644 --- a/homework03/src/main/java/com/ironhack/homework03/model/Issue.java +++ b/homework03/src/main/java/com/ironhack/homework03/model/Issue.java @@ -5,6 +5,8 @@ import lombok.Data; import lombok.NoArgsConstructor; +import java.util.Date; + @Entity @Data @NoArgsConstructor @@ -13,8 +15,8 @@ public class Issue { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer issueId; - private String issueDate; - private String returnDate; + private Date issueDate; + private Date returnDate; @OneToOne @JoinColumn(name = "student_id", unique = true) diff --git a/homework03/src/main/resources/application.properties b/homework03/src/main/resources/application.properties index 0a968346..ec033ca3 100644 --- a/homework03/src/main/resources/application.properties +++ b/homework03/src/main/resources/application.properties @@ -4,7 +4,7 @@ spring.datasource.url=jdbc:mysql://localhost:3306/homework3?createDatabaseIfNotE spring.datasource.username=root spring.datasource.password=root spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver -spring.jpa.hibernate.ddl-auto=update +spring.jpa.hibernate.ddl-auto=create-drop #spring.jpa.show-sql=true spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true diff --git a/homework03/src/test/java/com/ironhack/homework03/model/IssueTest.java b/homework03/src/test/java/com/ironhack/homework03/model/IssueTest.java index 1d5ef1ed..82c3708a 100644 --- a/homework03/src/test/java/com/ironhack/homework03/model/IssueTest.java +++ b/homework03/src/test/java/com/ironhack/homework03/model/IssueTest.java @@ -10,6 +10,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; +import java.util.Date; import java.util.Optional; import static org.junit.jupiter.api.Assertions.*; @@ -102,8 +103,8 @@ public void savingIssueThenUsingMappedBy_validInput_mappedByWorking(){ studentRepository.save(student); Issue firstIssue = new Issue(); - firstIssue.setIssueDate("2025-03-04"); - firstIssue.setReturnDate("2025-03-10"); + firstIssue.setIssueDate(new Date(System.currentTimeMillis())); + firstIssue.setReturnDate(new Date(System.currentTimeMillis() + 7 * 24 * 60 * 60 * 1000)); firstIssue.setIssuestudent(student); firstIssue.setIssuebook(book); issueRepository.save(firstIssue); @@ -143,8 +144,8 @@ public void createAuthorThenBookSaveAuthorSaveStudentSaveIssue_issueSaved(){ studentRepository.save(student); Issue firstIssue = new Issue(); - firstIssue.setIssueDate("2025-03-04"); - firstIssue.setReturnDate("2025-03-10"); + firstIssue.setIssueDate(new Date(System.currentTimeMillis())); + firstIssue.setReturnDate(new Date(System.currentTimeMillis() + 7 * 24 * 60 * 60 * 1000)); firstIssue.setIssuestudent(student); firstIssue.setIssuebook(book); issueRepository.save(firstIssue); @@ -166,8 +167,8 @@ public void createAuthorThenBookSaveAuthorSaveStudentSaveIssue_issueSaved(){ @Test public void getBookWithoutAuthorAndStudentFromDatabaseAndAssignIssue_issueSaved(){ Issue secondIssue = new Issue(); - secondIssue.setIssueDate("2025-03-04"); - secondIssue.setReturnDate("2025-03-10"); + secondIssue.setIssueDate(new Date(System.currentTimeMillis())); + secondIssue.setReturnDate(new Date(System.currentTimeMillis() + 7 * 24 * 60 * 60 * 1000)); student = new Student(); student.setUsn("STUDENT"); book = new Book(); @@ -186,8 +187,8 @@ public void getBookWithoutAuthorAndStudentFromDatabaseAndAssignIssue_issueSaved( @Test public void getBookWithAuthorAndStudentFromDatabaseAndAssignIssue_issueSaved(){ Issue secondIssue = new Issue(); - secondIssue.setIssueDate("2025-03-04"); - secondIssue.setReturnDate("2025-03-10"); + secondIssue.setIssueDate(new Date(System.currentTimeMillis())); + secondIssue.setReturnDate(new Date(System.currentTimeMillis() + 7 * 24 * 60 * 60 * 1000)); student = new Student(); student.setUsn("STUDENT2"); book = new Book(); From 9f7a0acb611d8bbb563e229acceb6ea5593dd3a6 Mon Sep 17 00:00:00 2001 From: Fernando Date: Thu, 6 Mar 2025 12:22:12 +0100 Subject: [PATCH 22/25] testing --- .../java/com/ironhack/homework03/Homework03Application.java | 2 ++ homework03/src/main/resources/application-test.properties | 0 .../src/test/java/com/ironhack/homework03/model/IssueTest.java | 2 ++ 3 files changed, 4 insertions(+) create mode 100644 homework03/src/main/resources/application-test.properties diff --git a/homework03/src/main/java/com/ironhack/homework03/Homework03Application.java b/homework03/src/main/java/com/ironhack/homework03/Homework03Application.java index 5b6211ec..7f6c910e 100644 --- a/homework03/src/main/java/com/ironhack/homework03/Homework03Application.java +++ b/homework03/src/main/java/com/ironhack/homework03/Homework03Application.java @@ -4,9 +4,11 @@ import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Profile; import java.util.InputMismatchException; +@Profile("!test") @SpringBootApplication public class Homework03Application implements CommandLineRunner { diff --git a/homework03/src/main/resources/application-test.properties b/homework03/src/main/resources/application-test.properties new file mode 100644 index 00000000..e69de29b diff --git a/homework03/src/test/java/com/ironhack/homework03/model/IssueTest.java b/homework03/src/test/java/com/ironhack/homework03/model/IssueTest.java index 1d5ef1ed..6180d45a 100644 --- a/homework03/src/test/java/com/ironhack/homework03/model/IssueTest.java +++ b/homework03/src/test/java/com/ironhack/homework03/model/IssueTest.java @@ -9,11 +9,13 @@ import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.ActiveProfiles; import java.util.Optional; import static org.junit.jupiter.api.Assertions.*; +@ActiveProfiles("test") @SpringBootTest class IssueTest { From 033555eced8d107229239cf5d7958800d114dd2e Mon Sep 17 00:00:00 2001 From: Fernando Date: Thu, 6 Mar 2025 13:15:33 +0100 Subject: [PATCH 23/25] Styles and email validation --- .../com/ironhack/homework03/Function.java | 69 ++++++------------- .../com/ironhack/homework03/model/Author.java | 16 +++++ .../src/main/resources/application.properties | 2 +- 3 files changed, 38 insertions(+), 49 deletions(-) diff --git a/homework03/src/main/java/com/ironhack/homework03/Function.java b/homework03/src/main/java/com/ironhack/homework03/Function.java index 72afd823..6c556c9e 100644 --- a/homework03/src/main/java/com/ironhack/homework03/Function.java +++ b/homework03/src/main/java/com/ironhack/homework03/Function.java @@ -41,9 +41,18 @@ public void addBook(){ System.out.print("Introduce Author Name: "); res = scanner.nextLine(); author.setName(res); - System.out.print("Introduce Author Email: "); - res = scanner.nextLine(); - author.setEmail(res); + do { + System.out.print("Introduce Author Email: "); + res = scanner.nextLine(); + + //This line is for proving that email format is correct + try { + author.setEmail(res); + break; + } catch (Exception e) { + System.out.println(e.getMessage()); + } + } while (true); System.out.print("Enter number of books: "); res = scanner.nextLine(); book.setQuantity(Integer.parseInt(res)); @@ -53,46 +62,9 @@ public void addBook(){ author.setAuthorbook(book); authorRepository.save(author); }else{ - throw new IllegalArgumentException("Author or Book already exist."); + throw new IllegalArgumentException(Style.ERROR + "Author or Book already exist." + Style.RESET); } -// System.out.print("Enter isbn : "); -// String isbn = scanner.nextLine(); -// -// System.out.print("Enter title : "); -// String title = scanner.nextLine(); -// -// System.out.print("Enter category : "); -// String category = scanner.nextLine(); -// -// System.out.print("Enter Author name : "); -// String authorName = scanner.nextLine(); -// -// System.out.print("Enter Author mail : "); -// String authorMail = scanner.nextLine(); -// -// System.out.print("Enter number of books : "); -// int numBooks = scanner.nextInt(); -// -// Book book = new Book(); -// book.setIsbn(isbn); -// book.setTitle(title); -// book.setCategory(category); -// book.setQuantity(numBooks); -// -// Author author = new Author(); -// author.setName(authorName); -// author.setEmail(authorMail); -// -// book.setAuthor(author); -// author.setAuthorbook(book); -// -// // Save author before assign to book -// authorRepository.save(author); -// -// // Save book with the author -// bookRepository.save(book); - System.out.println(Style.SUCCESS_BG+Style.BOLD+ "The book with isbn " + "( " + Style.SUCCESS + book.getIsbn() + Style.RESET+Style.SUCCESS_BG+Style.BOLD + " ) " + "has been saved " + Style.SUCCESS + "successfully." + Style.RESET+Style.SUCCESS_BG+Style.BOLD + " " + Style.RESET); @@ -194,15 +166,15 @@ public void searchBooksWithAuthor(){ public void issueBookToStudent(){ Issue issue = new Issue(); Student student = new Student(); - System.out.println("Enter usn: "); + System.out.print("Enter USN : "); String res6 = scanner.nextLine(); student.setUsn(res6); - System.out.println("Enter student name: "); + System.out.print("Enter Student Name : "); res6 = scanner.nextLine(); student.setName(res6); Optional studentCheck = studentRepository.findById(student.getUsn()); if(studentCheck.isEmpty()){ - System.out.println("Enter isbn: "); + System.out.print("Enter Book ISBN : "); res6 = scanner.nextLine(); Optional book6 = bookRepository.findById(res6); if(book6.isPresent()){// CASCADE EFFECT @@ -213,15 +185,16 @@ public void issueBookToStudent(){ issue.setIssueDate(new Date(System.currentTimeMillis())); issue.setReturnDate(new Date(System.currentTimeMillis() + 7 * 24 * 60 * 60 * 1000)); issueRepository.save(issue); - System.out.println("Issue and student registered correctly."); +// System.out.println(Style.SUCCESS_BG+Style.BOLD + " Issue and student registered " + Style.SUCCESS + "correctly" + Style.RESET+Style.SUCCESS_BG+Style.BOLD + ". " + Style.RESET); + System.out.println(Style.SUCCESS_BG+Style.BOLD+Style.SUCCESS + " Book issued "+ Style.RESET+Style.BOLD + " Return date : " + Style.RESET+Style.STUDENT + issue.getReturnDate() + Style.RESET); }else{ - System.out.println("This book has already been issued."); + System.out.println(Style.HIGHLIGHT + "This book has already been issued." + Style.RESET); } }else{ - System.out.println("Could not find that book."); + System.out.println(Style.NOT_FOUND + "Could not find that book." + Style.RESET); } }else{ - System.out.println("The student all ready exists."); + System.out.println(Style.HIGHLIGHT + "The student already exists." + Style.RESET); } } diff --git a/homework03/src/main/java/com/ironhack/homework03/model/Author.java b/homework03/src/main/java/com/ironhack/homework03/model/Author.java index 02a323c6..48d302b2 100644 --- a/homework03/src/main/java/com/ironhack/homework03/model/Author.java +++ b/homework03/src/main/java/com/ironhack/homework03/model/Author.java @@ -5,6 +5,8 @@ import lombok.Data; import lombok.NoArgsConstructor; +import java.util.regex.Pattern; + @Entity @Data @NoArgsConstructor @@ -20,4 +22,18 @@ public class Author { @JoinColumn(name = "book_id") private Book authorbook; + // Set email with validation + public void setEmail(String email) { + if (isValidEmail(email)) { + this.email = email; + } else { + throw new IllegalArgumentException(Style.ERROR + "Invalid email format. Please use a valid format like 'example@mail.com'.\n" + Style.RESET); + } + } + + private boolean isValidEmail(String email) { + String emailRegex = "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$"; + Pattern pattern = Pattern.compile(emailRegex); + return pattern.matcher(email).matches(); + } } diff --git a/homework03/src/main/resources/application.properties b/homework03/src/main/resources/application.properties index ec033ca3..0a968346 100644 --- a/homework03/src/main/resources/application.properties +++ b/homework03/src/main/resources/application.properties @@ -4,7 +4,7 @@ spring.datasource.url=jdbc:mysql://localhost:3306/homework3?createDatabaseIfNotE spring.datasource.username=root spring.datasource.password=root spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver -spring.jpa.hibernate.ddl-auto=create-drop +spring.jpa.hibernate.ddl-auto=update #spring.jpa.show-sql=true spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true From d2241d0a2f8c5eb5790bf5aeff9196da41cb01e7 Mon Sep 17 00:00:00 2001 From: alvaro Date: Thu, 6 Mar 2025 13:20:53 +0100 Subject: [PATCH 24/25] fixed date in test --- .../ironhack/homework03/repository/BookRepositoryTest.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/homework03/src/test/java/com/ironhack/homework03/repository/BookRepositoryTest.java b/homework03/src/test/java/com/ironhack/homework03/repository/BookRepositoryTest.java index b26d792a..01221b25 100644 --- a/homework03/src/test/java/com/ironhack/homework03/repository/BookRepositoryTest.java +++ b/homework03/src/test/java/com/ironhack/homework03/repository/BookRepositoryTest.java @@ -11,6 +11,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; +import java.util.Date; import java.util.List; import java.util.Optional; import java.util.Scanner; @@ -50,9 +51,8 @@ public void setUp(){ Book book2 = new Book("987654321", "Inferno", "Thriller", 3, null, null); bookRepository.save(book1); bookRepository.save(book2); - Issue issue1 = new Issue(1,"2022-08-01", "2022-08-15", student, book1); - - Issue issue2 = new Issue(2,"2022-09-01", "2022-09-15", student2, book2); + Issue issue1 = new Issue(1,new Date(System.currentTimeMillis()), new Date(System.currentTimeMillis()+ 7 * 24 * 60 * 60 * 1000), student, book1); + Issue issue2 = new Issue(2,new Date(System.currentTimeMillis()), new Date(System.currentTimeMillis()+ 7 * 24 * 60 * 60 * 1000), student2, book2); issueRepository.save(issue1); issueRepository.save(issue2); From 12909fcc422a3a5ab582b32f351896c7dd3b36c8 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 6 Mar 2025 13:23:20 +0100 Subject: [PATCH 25/25] list book category --- .../com/ironhack/homework03/Function.java | 24 ++++++++++--------- .../homework03/Homework03Application.java | 2 +- .../homework03/repository/BookRepository.java | 2 +- .../src/main/resources/application.properties | 2 +- .../Homework03ApplicationTests.java | 1 + .../ironhack/homework03/model/IssueTest.java | 2 +- .../repository/BookRepositoryTest.java | 1 + 7 files changed, 19 insertions(+), 15 deletions(-) diff --git a/homework03/src/main/java/com/ironhack/homework03/Function.java b/homework03/src/main/java/com/ironhack/homework03/Function.java index 72afd823..cb6f1712 100644 --- a/homework03/src/main/java/com/ironhack/homework03/Function.java +++ b/homework03/src/main/java/com/ironhack/homework03/Function.java @@ -126,21 +126,23 @@ public void searchBookByTitle(){ public void searchBookByCategory(){ System.out.print("Please Introduce the Book's Category : "); String category = scanner.nextLine(); - Optional book3 = bookRepository.findByCategory(category); - if(book3.isPresent()){ + List book3 = bookRepository.findByCategory(category); + if(!book3.isEmpty()) { // System.out.println("The following book matches the author : "); // System.out.println(book3.get().getIsbn() + " - " + book3.get().getTitle() + " - " + book3.get().getCategory() + " - " + book3.get().getQuantity()); - System.out.printf(Style.BOLD + "\n%-20s %-20s %-15s %-10s%n" + Style.RESET, - "Book ISBN", - "Book Title", - "Category", - "No of Books"); + for (int i = 0; i < book3.size(); i++){ + System.out.printf(Style.BOLD + "\n%-20s %-20s %-15s %-10s%n" + Style.RESET, + "Book ISBN", + "Book Title", + "Category", + "No of Books"); System.out.printf(Style.STUDENT + "%-20s %-20s %-15s %-10d%n" + Style.RESET, - book3.get().getIsbn(), - book3.get().getTitle(), - book3.get().getCategory(), - book3.get().getQuantity()); + book3.get(i).getIsbn(), + book3.get(i).getTitle(), + book3.get(i).getCategory(), + book3.get(i).getQuantity()); + } }else{ System.out.println(Style.NOT_FOUND + "No book found by category." + Style.RESET); } diff --git a/homework03/src/main/java/com/ironhack/homework03/Homework03Application.java b/homework03/src/main/java/com/ironhack/homework03/Homework03Application.java index 7f6c910e..757f4c35 100644 --- a/homework03/src/main/java/com/ironhack/homework03/Homework03Application.java +++ b/homework03/src/main/java/com/ironhack/homework03/Homework03Application.java @@ -8,7 +8,7 @@ import java.util.InputMismatchException; -@Profile("!test") +//@Profile("!test") @SpringBootApplication public class Homework03Application implements CommandLineRunner { diff --git a/homework03/src/main/java/com/ironhack/homework03/repository/BookRepository.java b/homework03/src/main/java/com/ironhack/homework03/repository/BookRepository.java index a5a083d5..41c0e21f 100644 --- a/homework03/src/main/java/com/ironhack/homework03/repository/BookRepository.java +++ b/homework03/src/main/java/com/ironhack/homework03/repository/BookRepository.java @@ -17,7 +17,7 @@ public interface BookRepository extends JpaRepository { public Optional findByTitle(String title); - public Optional findByCategory(String category); + public List findByCategory(String category); // public Optional findByAuthor(String author); @Query(value = "SELECT b.title, s.name, i.return_date " + diff --git a/homework03/src/main/resources/application.properties b/homework03/src/main/resources/application.properties index ec033ca3..59a07fba 100644 --- a/homework03/src/main/resources/application.properties +++ b/homework03/src/main/resources/application.properties @@ -2,7 +2,7 @@ spring.application.name=homework03 spring.datasource.url=jdbc:mysql://localhost:3306/homework3?createDatabaseIfNotExist=true&serverTimezone=UTC spring.datasource.username=root -spring.datasource.password=root +spring.datasource.password= spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.jpa.hibernate.ddl-auto=create-drop #spring.jpa.show-sql=true diff --git a/homework03/src/test/java/com/ironhack/homework03/Homework03ApplicationTests.java b/homework03/src/test/java/com/ironhack/homework03/Homework03ApplicationTests.java index 3da784a0..88b86d45 100644 --- a/homework03/src/test/java/com/ironhack/homework03/Homework03ApplicationTests.java +++ b/homework03/src/test/java/com/ironhack/homework03/Homework03ApplicationTests.java @@ -3,6 +3,7 @@ import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; +//@ActiveProfiles("test") @SpringBootTest class Homework03ApplicationTests { diff --git a/homework03/src/test/java/com/ironhack/homework03/model/IssueTest.java b/homework03/src/test/java/com/ironhack/homework03/model/IssueTest.java index 9381c25b..23a86a4d 100644 --- a/homework03/src/test/java/com/ironhack/homework03/model/IssueTest.java +++ b/homework03/src/test/java/com/ironhack/homework03/model/IssueTest.java @@ -16,7 +16,7 @@ import static org.junit.jupiter.api.Assertions.*; -@ActiveProfiles("test") +//@ActiveProfiles("test") @SpringBootTest class IssueTest { diff --git a/homework03/src/test/java/com/ironhack/homework03/repository/BookRepositoryTest.java b/homework03/src/test/java/com/ironhack/homework03/repository/BookRepositoryTest.java index b26d792a..7d3739ab 100644 --- a/homework03/src/test/java/com/ironhack/homework03/repository/BookRepositoryTest.java +++ b/homework03/src/test/java/com/ironhack/homework03/repository/BookRepositoryTest.java @@ -17,6 +17,7 @@ import static org.junit.jupiter.api.Assertions.*; +//@ActiveProfiles("test") @SpringBootTest class BookRepositoryTest {