Laba5
This commit is contained in:
commit
b6e8fef986
36
.gitignore
vendored
Normal file
36
.gitignore
vendored
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
HELP.md
|
||||||
|
.gradle
|
||||||
|
build/
|
||||||
|
!gradle/wrapper/gradle-wrapper.jar
|
||||||
|
!**/src/main/**/build/
|
||||||
|
!**/src/test/**/build/
|
||||||
|
|
||||||
|
### STS ###
|
||||||
|
.apt_generated
|
||||||
|
.classpath
|
||||||
|
.factorypath
|
||||||
|
.project
|
||||||
|
.settings
|
||||||
|
.springBeans
|
||||||
|
.sts4-cache
|
||||||
|
bin/
|
||||||
|
!**/src/main/**/bin/
|
||||||
|
!**/src/test/**/bin/
|
||||||
|
|
||||||
|
### IntelliJ IDEA ###
|
||||||
|
.idea
|
||||||
|
*.iws
|
||||||
|
*.iml
|
||||||
|
*.ipr
|
||||||
|
out/
|
||||||
|
!**/src/main/**/out/
|
||||||
|
!**/src/test/**/out/
|
||||||
|
|
||||||
|
### NetBeans ###
|
||||||
|
/nbproject/private/
|
||||||
|
/nbbuild/
|
||||||
|
/dist/
|
||||||
|
/nbdist/
|
||||||
|
/.nb-gradle/
|
||||||
|
|
||||||
|
data.*.db
|
12
.vscode/extensions.json
vendored
Normal file
12
.vscode/extensions.json
vendored
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
{
|
||||||
|
"recommendations": [
|
||||||
|
// fronted
|
||||||
|
"AndersEAndersen.html-class-suggestions",
|
||||||
|
"dbaeumer.vscode-eslint",
|
||||||
|
// backend
|
||||||
|
"vscjava.vscode-java-pack",
|
||||||
|
"vmware.vscode-boot-dev-pack",
|
||||||
|
"vscjava.vscode-gradle",
|
||||||
|
"redhat.vscode-xml"
|
||||||
|
]
|
||||||
|
}
|
14
.vscode/launch.json
vendored
Normal file
14
.vscode/launch.json
vendored
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
{
|
||||||
|
"configurations": [
|
||||||
|
{
|
||||||
|
"type": "java",
|
||||||
|
"name": "Demo",
|
||||||
|
"request": "launch",
|
||||||
|
"cwd": "${workspaceFolder}",
|
||||||
|
"mainClass": "com.example.demo.DemoApplication",
|
||||||
|
"projectName": "lec6",
|
||||||
|
"args": "--populate",
|
||||||
|
"envFile": "${workspaceFolder}/.env"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
24
.vscode/settings.json
vendored
Normal file
24
.vscode/settings.json
vendored
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
{
|
||||||
|
"editor.tabSize": 4,
|
||||||
|
"editor.detectIndentation": false,
|
||||||
|
"editor.insertSpaces": true,
|
||||||
|
"editor.formatOnPaste": true,
|
||||||
|
"editor.formatOnSave": true,
|
||||||
|
"editor.formatOnType": false,
|
||||||
|
"java.compile.nullAnalysis.mode": "disabled",
|
||||||
|
"java.configuration.updateBuildConfiguration": "automatic",
|
||||||
|
"[java]": {
|
||||||
|
"editor.pasteAs.enabled": false,
|
||||||
|
},
|
||||||
|
"gradle.nestedProjects": true,
|
||||||
|
"java.saveActions.organizeImports": true,
|
||||||
|
"java.dependency.packagePresentation": "hierarchical",
|
||||||
|
"spring-boot.ls.problem.boot2.JAVA_CONSTRUCTOR_PARAMETER_INJECTION": "WARNING",
|
||||||
|
"spring.initializr.defaultLanguage": "Java",
|
||||||
|
"java.format.settings.url": ".vscode/eclipse-formatter.xml",
|
||||||
|
"java.project.explorer.showNonJavaResources": true,
|
||||||
|
"java.codeGeneration.hashCodeEquals.useJava7Objects": true,
|
||||||
|
"cSpell.words": [
|
||||||
|
"classappend"
|
||||||
|
],
|
||||||
|
}
|
51
build.gradle
Normal file
51
build.gradle
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
plugins {
|
||||||
|
id 'java'
|
||||||
|
id 'org.springframework.boot' version '3.2.4'
|
||||||
|
id 'io.spring.dependency-management' version '1.1.4'
|
||||||
|
}
|
||||||
|
|
||||||
|
group = 'com.example'
|
||||||
|
version = '0.0.1-SNAPSHOT'
|
||||||
|
|
||||||
|
defaultTasks 'bootRun'
|
||||||
|
|
||||||
|
jar {
|
||||||
|
enabled = false
|
||||||
|
}
|
||||||
|
|
||||||
|
bootJar {
|
||||||
|
archiveFileName = String.format('%s-%s.jar', rootProject.name, version)
|
||||||
|
}
|
||||||
|
|
||||||
|
assert System.properties['java.specification.version'] == '17' || '21'
|
||||||
|
java {
|
||||||
|
sourceCompatibility = '17'
|
||||||
|
}
|
||||||
|
|
||||||
|
repositories {
|
||||||
|
mavenCentral()
|
||||||
|
}
|
||||||
|
|
||||||
|
dependencies {
|
||||||
|
implementation 'org.springframework.boot:spring-boot-starter-web'
|
||||||
|
implementation 'org.springframework.boot:spring-boot-starter-validation'
|
||||||
|
implementation 'org.modelmapper:modelmapper:3.2.0'
|
||||||
|
|
||||||
|
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
|
||||||
|
implementation 'com.h2database:h2:2.2.224'
|
||||||
|
|
||||||
|
implementation 'org.springframework.boot:spring-boot-devtools'
|
||||||
|
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
|
||||||
|
implementation 'nz.net.ultraq.thymeleaf:thymeleaf-layout-dialect:3.3.0'
|
||||||
|
runtimeOnly 'org.webjars.npm:bootstrap:5.3.3'
|
||||||
|
runtimeOnly 'org.webjars.npm:bootstrap-icons:1.11.3'
|
||||||
|
|
||||||
|
implementation 'org.springframework.boot:spring-boot-starter-security'
|
||||||
|
implementation 'org.thymeleaf.extras:thymeleaf-extras-springsecurity6'
|
||||||
|
|
||||||
|
testImplementation 'org.springframework.boot:spring-boot-starter-test'
|
||||||
|
}
|
||||||
|
|
||||||
|
tasks.named('test') {
|
||||||
|
useJUnitPlatform()
|
||||||
|
}
|
BIN
gradle/wrapper/gradle-wrapper.jar
vendored
Normal file
BIN
gradle/wrapper/gradle-wrapper.jar
vendored
Normal file
Binary file not shown.
7
gradle/wrapper/gradle-wrapper.properties
vendored
Normal file
7
gradle/wrapper/gradle-wrapper.properties
vendored
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
distributionBase=GRADLE_USER_HOME
|
||||||
|
distributionPath=wrapper/dists
|
||||||
|
distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-bin.zip
|
||||||
|
networkTimeout=10000
|
||||||
|
validateDistributionUrl=true
|
||||||
|
zipStoreBase=GRADLE_USER_HOME
|
||||||
|
zipStorePath=wrapper/dists
|
249
gradlew
vendored
Normal file
249
gradlew
vendored
Normal file
@ -0,0 +1,249 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
#
|
||||||
|
# Copyright © 2015-2021 the original authors.
|
||||||
|
#
|
||||||
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
# you may not use this file except in compliance with the License.
|
||||||
|
# You may obtain a copy of the License at
|
||||||
|
#
|
||||||
|
# https://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
#
|
||||||
|
# Unless required by applicable law or agreed to in writing, software
|
||||||
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
# See the License for the specific language governing permissions and
|
||||||
|
# limitations under the License.
|
||||||
|
#
|
||||||
|
|
||||||
|
##############################################################################
|
||||||
|
#
|
||||||
|
# Gradle start up script for POSIX generated by Gradle.
|
||||||
|
#
|
||||||
|
# Important for running:
|
||||||
|
#
|
||||||
|
# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is
|
||||||
|
# noncompliant, but you have some other compliant shell such as ksh or
|
||||||
|
# bash, then to run this script, type that shell name before the whole
|
||||||
|
# command line, like:
|
||||||
|
#
|
||||||
|
# ksh Gradle
|
||||||
|
#
|
||||||
|
# Busybox and similar reduced shells will NOT work, because this script
|
||||||
|
# requires all of these POSIX shell features:
|
||||||
|
# * functions;
|
||||||
|
# * expansions «$var», «${var}», «${var:-default}», «${var+SET}»,
|
||||||
|
# «${var#prefix}», «${var%suffix}», and «$( cmd )»;
|
||||||
|
# * compound commands having a testable exit status, especially «case»;
|
||||||
|
# * various built-in commands including «command», «set», and «ulimit».
|
||||||
|
#
|
||||||
|
# Important for patching:
|
||||||
|
#
|
||||||
|
# (2) This script targets any POSIX shell, so it avoids extensions provided
|
||||||
|
# by Bash, Ksh, etc; in particular arrays are avoided.
|
||||||
|
#
|
||||||
|
# The "traditional" practice of packing multiple parameters into a
|
||||||
|
# space-separated string is a well documented source of bugs and security
|
||||||
|
# problems, so this is (mostly) avoided, by progressively accumulating
|
||||||
|
# options in "$@", and eventually passing that to Java.
|
||||||
|
#
|
||||||
|
# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS,
|
||||||
|
# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly;
|
||||||
|
# see the in-line comments for details.
|
||||||
|
#
|
||||||
|
# There are tweaks for specific operating systems such as AIX, CygWin,
|
||||||
|
# Darwin, MinGW, and NonStop.
|
||||||
|
#
|
||||||
|
# (3) This script is generated from the Groovy template
|
||||||
|
# https://github.com/gradle/gradle/blob/HEAD/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
|
||||||
|
# within the Gradle project.
|
||||||
|
#
|
||||||
|
# You can find Gradle at https://github.com/gradle/gradle/.
|
||||||
|
#
|
||||||
|
##############################################################################
|
||||||
|
|
||||||
|
# Attempt to set APP_HOME
|
||||||
|
|
||||||
|
# Resolve links: $0 may be a link
|
||||||
|
app_path=$0
|
||||||
|
|
||||||
|
# Need this for daisy-chained symlinks.
|
||||||
|
while
|
||||||
|
APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path
|
||||||
|
[ -h "$app_path" ]
|
||||||
|
do
|
||||||
|
ls=$( ls -ld "$app_path" )
|
||||||
|
link=${ls#*' -> '}
|
||||||
|
case $link in #(
|
||||||
|
/*) app_path=$link ;; #(
|
||||||
|
*) app_path=$APP_HOME$link ;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
|
||||||
|
# This is normally unused
|
||||||
|
# shellcheck disable=SC2034
|
||||||
|
APP_BASE_NAME=${0##*/}
|
||||||
|
# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036)
|
||||||
|
APP_HOME=$( cd "${APP_HOME:-./}" > /dev/null && pwd -P ) || exit
|
||||||
|
|
||||||
|
# Use the maximum available, or set MAX_FD != -1 to use that value.
|
||||||
|
MAX_FD=maximum
|
||||||
|
|
||||||
|
warn () {
|
||||||
|
echo "$*"
|
||||||
|
} >&2
|
||||||
|
|
||||||
|
die () {
|
||||||
|
echo
|
||||||
|
echo "$*"
|
||||||
|
echo
|
||||||
|
exit 1
|
||||||
|
} >&2
|
||||||
|
|
||||||
|
# OS specific support (must be 'true' or 'false').
|
||||||
|
cygwin=false
|
||||||
|
msys=false
|
||||||
|
darwin=false
|
||||||
|
nonstop=false
|
||||||
|
case "$( uname )" in #(
|
||||||
|
CYGWIN* ) cygwin=true ;; #(
|
||||||
|
Darwin* ) darwin=true ;; #(
|
||||||
|
MSYS* | MINGW* ) msys=true ;; #(
|
||||||
|
NONSTOP* ) nonstop=true ;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
|
||||||
|
|
||||||
|
|
||||||
|
# Determine the Java command to use to start the JVM.
|
||||||
|
if [ -n "$JAVA_HOME" ] ; then
|
||||||
|
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
|
||||||
|
# IBM's JDK on AIX uses strange locations for the executables
|
||||||
|
JAVACMD=$JAVA_HOME/jre/sh/java
|
||||||
|
else
|
||||||
|
JAVACMD=$JAVA_HOME/bin/java
|
||||||
|
fi
|
||||||
|
if [ ! -x "$JAVACMD" ] ; then
|
||||||
|
die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
|
||||||
|
|
||||||
|
Please set the JAVA_HOME variable in your environment to match the
|
||||||
|
location of your Java installation."
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
JAVACMD=java
|
||||||
|
if ! command -v java >/dev/null 2>&1
|
||||||
|
then
|
||||||
|
die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
|
||||||
|
|
||||||
|
Please set the JAVA_HOME variable in your environment to match the
|
||||||
|
location of your Java installation."
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Increase the maximum file descriptors if we can.
|
||||||
|
if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then
|
||||||
|
case $MAX_FD in #(
|
||||||
|
max*)
|
||||||
|
# In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked.
|
||||||
|
# shellcheck disable=SC2039,SC3045
|
||||||
|
MAX_FD=$( ulimit -H -n ) ||
|
||||||
|
warn "Could not query maximum file descriptor limit"
|
||||||
|
esac
|
||||||
|
case $MAX_FD in #(
|
||||||
|
'' | soft) :;; #(
|
||||||
|
*)
|
||||||
|
# In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked.
|
||||||
|
# shellcheck disable=SC2039,SC3045
|
||||||
|
ulimit -n "$MAX_FD" ||
|
||||||
|
warn "Could not set maximum file descriptor limit to $MAX_FD"
|
||||||
|
esac
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Collect all arguments for the java command, stacking in reverse order:
|
||||||
|
# * args from the command line
|
||||||
|
# * the main class name
|
||||||
|
# * -classpath
|
||||||
|
# * -D...appname settings
|
||||||
|
# * --module-path (only if needed)
|
||||||
|
# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables.
|
||||||
|
|
||||||
|
# For Cygwin or MSYS, switch paths to Windows format before running java
|
||||||
|
if "$cygwin" || "$msys" ; then
|
||||||
|
APP_HOME=$( cygpath --path --mixed "$APP_HOME" )
|
||||||
|
CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" )
|
||||||
|
|
||||||
|
JAVACMD=$( cygpath --unix "$JAVACMD" )
|
||||||
|
|
||||||
|
# Now convert the arguments - kludge to limit ourselves to /bin/sh
|
||||||
|
for arg do
|
||||||
|
if
|
||||||
|
case $arg in #(
|
||||||
|
-*) false ;; # don't mess with options #(
|
||||||
|
/?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath
|
||||||
|
[ -e "$t" ] ;; #(
|
||||||
|
*) false ;;
|
||||||
|
esac
|
||||||
|
then
|
||||||
|
arg=$( cygpath --path --ignore --mixed "$arg" )
|
||||||
|
fi
|
||||||
|
# Roll the args list around exactly as many times as the number of
|
||||||
|
# args, so each arg winds up back in the position where it started, but
|
||||||
|
# possibly modified.
|
||||||
|
#
|
||||||
|
# NB: a `for` loop captures its iteration list before it begins, so
|
||||||
|
# changing the positional parameters here affects neither the number of
|
||||||
|
# iterations, nor the values presented in `arg`.
|
||||||
|
shift # remove old arg
|
||||||
|
set -- "$@" "$arg" # push replacement arg
|
||||||
|
done
|
||||||
|
fi
|
||||||
|
|
||||||
|
|
||||||
|
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
|
||||||
|
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
|
||||||
|
|
||||||
|
# Collect all arguments for the java command:
|
||||||
|
# * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments,
|
||||||
|
# and any embedded shellness will be escaped.
|
||||||
|
# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be
|
||||||
|
# treated as '${Hostname}' itself on the command line.
|
||||||
|
|
||||||
|
set -- \
|
||||||
|
"-Dorg.gradle.appname=$APP_BASE_NAME" \
|
||||||
|
-classpath "$CLASSPATH" \
|
||||||
|
org.gradle.wrapper.GradleWrapperMain \
|
||||||
|
"$@"
|
||||||
|
|
||||||
|
# Stop when "xargs" is not available.
|
||||||
|
if ! command -v xargs >/dev/null 2>&1
|
||||||
|
then
|
||||||
|
die "xargs is not available"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Use "xargs" to parse quoted args.
|
||||||
|
#
|
||||||
|
# With -n1 it outputs one arg per line, with the quotes and backslashes removed.
|
||||||
|
#
|
||||||
|
# In Bash we could simply go:
|
||||||
|
#
|
||||||
|
# readarray ARGS < <( xargs -n1 <<<"$var" ) &&
|
||||||
|
# set -- "${ARGS[@]}" "$@"
|
||||||
|
#
|
||||||
|
# but POSIX shell has neither arrays nor command substitution, so instead we
|
||||||
|
# post-process each arg (as a line of input to sed) to backslash-escape any
|
||||||
|
# character that might be a shell metacharacter, then use eval to reverse
|
||||||
|
# that process (while maintaining the separation between arguments), and wrap
|
||||||
|
# the whole thing up as a single "set" statement.
|
||||||
|
#
|
||||||
|
# This will of course break if any of these variables contains a newline or
|
||||||
|
# an unmatched quote.
|
||||||
|
#
|
||||||
|
|
||||||
|
eval "set -- $(
|
||||||
|
printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" |
|
||||||
|
xargs -n1 |
|
||||||
|
sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' |
|
||||||
|
tr '\n' ' '
|
||||||
|
)" '"$@"'
|
||||||
|
|
||||||
|
exec "$JAVACMD" "$@"
|
92
gradlew.bat
vendored
Normal file
92
gradlew.bat
vendored
Normal file
@ -0,0 +1,92 @@
|
|||||||
|
@rem
|
||||||
|
@rem Copyright 2015 the original author or authors.
|
||||||
|
@rem
|
||||||
|
@rem Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
@rem you may not use this file except in compliance with the License.
|
||||||
|
@rem You may obtain a copy of the License at
|
||||||
|
@rem
|
||||||
|
@rem https://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
@rem
|
||||||
|
@rem Unless required by applicable law or agreed to in writing, software
|
||||||
|
@rem distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
@rem See the License for the specific language governing permissions and
|
||||||
|
@rem limitations under the License.
|
||||||
|
@rem
|
||||||
|
|
||||||
|
@if "%DEBUG%"=="" @echo off
|
||||||
|
@rem ##########################################################################
|
||||||
|
@rem
|
||||||
|
@rem Gradle startup script for Windows
|
||||||
|
@rem
|
||||||
|
@rem ##########################################################################
|
||||||
|
|
||||||
|
@rem Set local scope for the variables with windows NT shell
|
||||||
|
if "%OS%"=="Windows_NT" setlocal
|
||||||
|
|
||||||
|
set DIRNAME=%~dp0
|
||||||
|
if "%DIRNAME%"=="" set DIRNAME=.
|
||||||
|
@rem This is normally unused
|
||||||
|
set APP_BASE_NAME=%~n0
|
||||||
|
set APP_HOME=%DIRNAME%
|
||||||
|
|
||||||
|
@rem Resolve any "." and ".." in APP_HOME to make it shorter.
|
||||||
|
for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi
|
||||||
|
|
||||||
|
@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
|
||||||
|
set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"
|
||||||
|
|
||||||
|
@rem Find java.exe
|
||||||
|
if defined JAVA_HOME goto findJavaFromJavaHome
|
||||||
|
|
||||||
|
set JAVA_EXE=java.exe
|
||||||
|
%JAVA_EXE% -version >NUL 2>&1
|
||||||
|
if %ERRORLEVEL% equ 0 goto execute
|
||||||
|
|
||||||
|
echo.
|
||||||
|
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
|
||||||
|
echo.
|
||||||
|
echo Please set the JAVA_HOME variable in your environment to match the
|
||||||
|
echo location of your Java installation.
|
||||||
|
|
||||||
|
goto fail
|
||||||
|
|
||||||
|
:findJavaFromJavaHome
|
||||||
|
set JAVA_HOME=%JAVA_HOME:"=%
|
||||||
|
set JAVA_EXE=%JAVA_HOME%/bin/java.exe
|
||||||
|
|
||||||
|
if exist "%JAVA_EXE%" goto execute
|
||||||
|
|
||||||
|
echo.
|
||||||
|
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
|
||||||
|
echo.
|
||||||
|
echo Please set the JAVA_HOME variable in your environment to match the
|
||||||
|
echo location of your Java installation.
|
||||||
|
|
||||||
|
goto fail
|
||||||
|
|
||||||
|
:execute
|
||||||
|
@rem Setup the command line
|
||||||
|
|
||||||
|
set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
|
||||||
|
|
||||||
|
|
||||||
|
@rem Execute Gradle
|
||||||
|
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %*
|
||||||
|
|
||||||
|
:end
|
||||||
|
@rem End local scope for the variables with windows NT shell
|
||||||
|
if %ERRORLEVEL% equ 0 goto mainEnd
|
||||||
|
|
||||||
|
:fail
|
||||||
|
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
|
||||||
|
rem the _cmd.exe /c_ return code!
|
||||||
|
set EXIT_CODE=%ERRORLEVEL%
|
||||||
|
if %EXIT_CODE% equ 0 set EXIT_CODE=1
|
||||||
|
if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE%
|
||||||
|
exit /b %EXIT_CODE%
|
||||||
|
|
||||||
|
:mainEnd
|
||||||
|
if "%OS%"=="Windows_NT" endlocal
|
||||||
|
|
||||||
|
:omega
|
15
readme.md
Normal file
15
readme.md
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
H2 Console: \
|
||||||
|
http://localhost:8080/h2-console
|
||||||
|
|
||||||
|
JDBC URL: jdbc:h2:file:./data \
|
||||||
|
User Name: sa \
|
||||||
|
Password: password
|
||||||
|
|
||||||
|
Почитать:
|
||||||
|
|
||||||
|
- Spring Boot CRUD Application with Thymeleaf https://www.baeldung.com/spring-boot-crud-thymeleaf
|
||||||
|
- Thymeleaf Layout Dialect https://ultraq.github.io/thymeleaf-layout-dialect/
|
||||||
|
- Tutorial: Using Thymeleaf https://www.thymeleaf.org/doc/tutorials/3.1/usingthymeleaf.html#introducing-thymeleaf
|
||||||
|
- Working with Cookies in Spring MVC using @CookieValue Annotation https://www.geeksforgeeks.org/working-with-cookies-in-spring-mvc-using-cookievalue-annotation/
|
||||||
|
- Session Attributes in Spring MVC https://www.baeldung.com/spring-mvc-session-attributes
|
||||||
|
- LazyInitializationException – What it is and the best way to fix it https://thorben-janssen.com/lazyinitializationexception/
|
1
settings.gradle
Normal file
1
settings.gradle
Normal file
@ -0,0 +1 @@
|
|||||||
|
rootProject.name = 'demo'
|
84
src/main/java/com/example/demo/DemoApplication.java
Normal file
84
src/main/java/com/example/demo/DemoApplication.java
Normal file
@ -0,0 +1,84 @@
|
|||||||
|
package com.example.demo;
|
||||||
|
|
||||||
|
import java.util.stream.IntStream;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.boot.CommandLineRunner;
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
|
||||||
|
import com.example.demo.core.configuration.Constants;
|
||||||
|
import com.example.demo.films.model.FilmEntity;
|
||||||
|
import com.example.demo.films.service.FilmService;
|
||||||
|
import com.example.demo.genres.model.GenreEntity;
|
||||||
|
import com.example.demo.genres.service.GenreService;
|
||||||
|
import com.example.demo.langs.model.LangEntity;
|
||||||
|
import com.example.demo.langs.service.LangService;
|
||||||
|
import com.example.demo.users.model.UserEntity;
|
||||||
|
import com.example.demo.users.model.UserRole;
|
||||||
|
import com.example.demo.users.service.UserService;
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
public class DemoApplication implements CommandLineRunner {
|
||||||
|
private final Logger log = LoggerFactory.getLogger(DemoApplication.class);
|
||||||
|
|
||||||
|
private final UserService userService;
|
||||||
|
private final FilmService filmService;
|
||||||
|
private final LangService langService;
|
||||||
|
private final GenreService genreService;
|
||||||
|
|
||||||
|
public DemoApplication(
|
||||||
|
UserService userService,
|
||||||
|
LangService langService,
|
||||||
|
FilmService filmService,
|
||||||
|
GenreService genreService) {
|
||||||
|
this.userService = userService;
|
||||||
|
this.filmService = filmService;
|
||||||
|
this.genreService = genreService;
|
||||||
|
this.langService = langService;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(DemoApplication.class, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run(String... args) throws Exception {
|
||||||
|
|
||||||
|
log.info("Create default lang values");
|
||||||
|
final var lang1 = langService.create(new LangEntity("angl"));
|
||||||
|
final var lang2 = langService.create(new LangEntity("nenafasf"));
|
||||||
|
final var lang3 = langService.create(new LangEntity("Русский"));
|
||||||
|
|
||||||
|
log.info("Create default genre values");
|
||||||
|
final var genre1 = genreService.create(new GenreEntity("жнр 1"));
|
||||||
|
final var genre2 = genreService.create(new GenreEntity("жнр 2"));
|
||||||
|
genreService.create(new GenreEntity("жнр 3"));
|
||||||
|
|
||||||
|
log.info("Create default user values");
|
||||||
|
final var admin = new UserEntity("admin", "admin");
|
||||||
|
admin.setRole(UserRole.ADMIN);
|
||||||
|
userService.create(admin);
|
||||||
|
|
||||||
|
userService.create(new UserEntity("user1", Constants.DEFAULT_PASSWORD));
|
||||||
|
|
||||||
|
IntStream.range(2, 27)
|
||||||
|
.forEach(value -> userService.create(
|
||||||
|
new UserEntity("user".concat(String.valueOf(value)), Constants.DEFAULT_PASSWORD)));
|
||||||
|
|
||||||
|
log.info("Create default film values");
|
||||||
|
filmService.create(new FilmEntity("smth 1", "hoh1o", lang1));
|
||||||
|
filmService.create(new FilmEntity("smth 2", "hoh2o", lang2));
|
||||||
|
filmService.create(new FilmEntity("smth 3", "hoh3o", lang3));
|
||||||
|
filmService.create(new FilmEntity("smth 4", "hoh4o", lang1));
|
||||||
|
filmService.create(new FilmEntity("smth 5", "hoh5o", lang2));
|
||||||
|
filmService.create(new FilmEntity("smth 6", "hoh6o", lang3));
|
||||||
|
|
||||||
|
var films = filmService.getAll();
|
||||||
|
for (FilmEntity filmEntity : films) {
|
||||||
|
filmService.addFilmGenres(filmEntity.getId(), genre1.getId());
|
||||||
|
filmService.addFilmGenres(filmEntity.getId(), genre2.getId());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,16 @@
|
|||||||
|
package com.example.demo.core.api;
|
||||||
|
|
||||||
|
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||||
|
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||||
|
|
||||||
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
|
|
||||||
|
@ControllerAdvice
|
||||||
|
public class GlobalController {
|
||||||
|
|
||||||
|
@ModelAttribute("servletPath")
|
||||||
|
String getRequestServletPath(HttpServletRequest request) {
|
||||||
|
return request.getServletPath();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,18 @@
|
|||||||
|
package com.example.demo.core.api;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.function.Function;
|
||||||
|
|
||||||
|
import org.springframework.data.domain.Page;
|
||||||
|
|
||||||
|
public class PageAttributesMapper {
|
||||||
|
private PageAttributesMapper() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <E, D> Map<String, Object> toAttributes(Page<E> page, Function<E, D> mapper) {
|
||||||
|
return Map.of(
|
||||||
|
"items", page.getContent().stream().map(mapper::apply).toList(),
|
||||||
|
"currentPage", page.getNumber(),
|
||||||
|
"totalPages", page.getTotalPages());
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,19 @@
|
|||||||
|
package com.example.demo.core.configuration;
|
||||||
|
|
||||||
|
public class Constants {
|
||||||
|
public static final String SEQUENCE_NAME = "hibernate_sequence";
|
||||||
|
|
||||||
|
public static final int DEFUALT_PAGE_SIZE = 4;
|
||||||
|
|
||||||
|
public static final String REDIRECT_VIEW = "redirect:";
|
||||||
|
|
||||||
|
public static final String ADMIN_PREFIX = "/admin";
|
||||||
|
|
||||||
|
public static final String LOGIN_URL = "/login";
|
||||||
|
public static final String LOGOUT_URL = "/logout";
|
||||||
|
|
||||||
|
public static final String DEFAULT_PASSWORD = "123456";
|
||||||
|
|
||||||
|
private Constants() {
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,23 @@
|
|||||||
|
package com.example.demo.core.configuration;
|
||||||
|
|
||||||
|
import org.modelmapper.ModelMapper;
|
||||||
|
import org.modelmapper.PropertyMap;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
import com.example.demo.core.model.BaseEntity;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
public class MapperConfiguration {
|
||||||
|
@Bean
|
||||||
|
ModelMapper modelMapper() {
|
||||||
|
final ModelMapper mapper = new ModelMapper();
|
||||||
|
mapper.addMappings(new PropertyMap<Object, BaseEntity>() {
|
||||||
|
@Override
|
||||||
|
protected void configure() {
|
||||||
|
skip(destination.getId());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return mapper;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,13 @@
|
|||||||
|
package com.example.demo.core.configuration;
|
||||||
|
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
|
||||||
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
public class WebConfiguration implements WebMvcConfigurer {
|
||||||
|
@Override
|
||||||
|
public void addViewControllers(ViewControllerRegistry registry) {
|
||||||
|
registry.addViewController("/login").setViewName("login");
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,53 @@
|
|||||||
|
package com.example.demo.core.error;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.core.annotation.AnnotationUtils;
|
||||||
|
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||||
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||||
|
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||||
|
import org.springframework.web.servlet.ModelAndView;
|
||||||
|
|
||||||
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
|
|
||||||
|
@ControllerAdvice
|
||||||
|
public class AdviceController {
|
||||||
|
private final Logger log = LoggerFactory.getLogger(AdviceController.class);
|
||||||
|
|
||||||
|
private static Throwable getRootCause(Throwable throwable) {
|
||||||
|
Throwable rootCause = throwable;
|
||||||
|
while (rootCause.getCause() != null && rootCause.getCause() != rootCause) {
|
||||||
|
rootCause = rootCause.getCause();
|
||||||
|
}
|
||||||
|
return rootCause;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Map<String, Object> getAttributes(HttpServletRequest request, Throwable throwable) {
|
||||||
|
final Throwable rootCause = getRootCause(throwable);
|
||||||
|
final StackTraceElement firstError = rootCause.getStackTrace()[0];
|
||||||
|
return Map.of(
|
||||||
|
"message", rootCause.getMessage(),
|
||||||
|
"url", request.getRequestURL(),
|
||||||
|
"exception", rootCause.getClass().getName(),
|
||||||
|
"file", firstError.getFileName(),
|
||||||
|
"method", firstError.getMethodName(),
|
||||||
|
"line", firstError.getLineNumber());
|
||||||
|
}
|
||||||
|
|
||||||
|
@ExceptionHandler(value = Exception.class)
|
||||||
|
public ModelAndView defaultErrorHandler(HttpServletRequest request, Throwable throwable) throws Throwable {
|
||||||
|
if (AnnotationUtils.findAnnotation(throwable.getClass(),
|
||||||
|
ResponseStatus.class) != null) {
|
||||||
|
throw throwable;
|
||||||
|
}
|
||||||
|
|
||||||
|
log.error("{}", throwable.getMessage());
|
||||||
|
throwable.printStackTrace();
|
||||||
|
final ModelAndView model = new ModelAndView();
|
||||||
|
model.addAllObjects(getAttributes(request, throwable));
|
||||||
|
model.setViewName("error");
|
||||||
|
return model;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,7 @@
|
|||||||
|
package com.example.demo.core.error;
|
||||||
|
|
||||||
|
public class NotFoundException extends RuntimeException {
|
||||||
|
public <T> NotFoundException(Class<T> clazz, Long id) {
|
||||||
|
super(String.format("%s with id [%s] is not found or not exists", clazz.getSimpleName(), id));
|
||||||
|
}
|
||||||
|
}
|
27
src/main/java/com/example/demo/core/model/BaseEntity.java
Normal file
27
src/main/java/com/example/demo/core/model/BaseEntity.java
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
package com.example.demo.core.model;
|
||||||
|
|
||||||
|
import com.example.demo.core.configuration.Constants;
|
||||||
|
|
||||||
|
import jakarta.persistence.GeneratedValue;
|
||||||
|
import jakarta.persistence.Id;
|
||||||
|
import jakarta.persistence.MappedSuperclass;
|
||||||
|
import jakarta.persistence.SequenceGenerator;
|
||||||
|
|
||||||
|
@MappedSuperclass
|
||||||
|
public abstract class BaseEntity {
|
||||||
|
@Id
|
||||||
|
@GeneratedValue
|
||||||
|
@SequenceGenerator(name = Constants.SEQUENCE_NAME, sequenceName = Constants.SEQUENCE_NAME, allocationSize = 1)
|
||||||
|
protected Long id;
|
||||||
|
|
||||||
|
protected BaseEntity() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(Long id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,63 @@
|
|||||||
|
package com.example.demo.core.security;
|
||||||
|
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
|
||||||
|
import org.springframework.security.config.Customizer;
|
||||||
|
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||||
|
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
||||||
|
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
|
||||||
|
import org.springframework.security.config.annotation.web.configurers.HeadersConfigurer.FrameOptionsConfig;
|
||||||
|
import org.springframework.security.core.userdetails.UserDetailsService;
|
||||||
|
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||||
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||||
|
import org.springframework.security.web.SecurityFilterChain;
|
||||||
|
|
||||||
|
import com.example.demo.core.configuration.Constants;
|
||||||
|
import com.example.demo.users.api.UserSignupController;
|
||||||
|
import com.example.demo.users.model.UserRole;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@EnableWebSecurity
|
||||||
|
public class SecurityConfiguration {
|
||||||
|
@Bean
|
||||||
|
SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Exception {
|
||||||
|
httpSecurity.headers(headers -> headers.frameOptions(FrameOptionsConfig::sameOrigin));
|
||||||
|
httpSecurity.csrf(AbstractHttpConfigurer::disable);
|
||||||
|
httpSecurity.cors(Customizer.withDefaults());
|
||||||
|
|
||||||
|
httpSecurity.authorizeHttpRequests(requests -> requests
|
||||||
|
.requestMatchers("/css/**", "/webjars/**", "/*.svg")
|
||||||
|
.permitAll());
|
||||||
|
|
||||||
|
httpSecurity.authorizeHttpRequests(requests -> requests
|
||||||
|
.requestMatchers(Constants.ADMIN_PREFIX + "/**").hasRole(UserRole.ADMIN.name())
|
||||||
|
.requestMatchers("/h2-console/**").hasRole(UserRole.ADMIN.name())
|
||||||
|
.requestMatchers(UserSignupController.URL).anonymous()
|
||||||
|
.requestMatchers(Constants.LOGIN_URL).anonymous()
|
||||||
|
.anyRequest().authenticated());
|
||||||
|
|
||||||
|
httpSecurity.formLogin(formLogin -> formLogin
|
||||||
|
.loginPage(Constants.LOGIN_URL));
|
||||||
|
|
||||||
|
httpSecurity.rememberMe(rememberMe -> rememberMe.key("uniqueAndSecret"));
|
||||||
|
|
||||||
|
httpSecurity.logout(logout -> logout
|
||||||
|
.deleteCookies("JSESSIONID"));
|
||||||
|
|
||||||
|
return httpSecurity.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
DaoAuthenticationProvider authenticationProvider(UserDetailsService userDetailsService) {
|
||||||
|
final DaoAuthenticationProvider authProvider = new DaoAuthenticationProvider();
|
||||||
|
authProvider.setUserDetailsService(userDetailsService);
|
||||||
|
authProvider.setPasswordEncoder(passwordEncoder());
|
||||||
|
return authProvider;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
PasswordEncoder passwordEncoder() {
|
||||||
|
return new BCryptPasswordEncoder();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,64 @@
|
|||||||
|
package com.example.demo.core.security;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import org.springframework.security.core.GrantedAuthority;
|
||||||
|
import org.springframework.security.core.userdetails.UserDetails;
|
||||||
|
|
||||||
|
import com.example.demo.users.model.UserEntity;
|
||||||
|
|
||||||
|
public class UserPrincipal implements UserDetails {
|
||||||
|
private final long id;
|
||||||
|
private final String username;
|
||||||
|
private final String password;
|
||||||
|
private final Set<? extends GrantedAuthority> roles;
|
||||||
|
private final boolean active;
|
||||||
|
|
||||||
|
public UserPrincipal(UserEntity user) {
|
||||||
|
this.id = user.getId();
|
||||||
|
this.username = user.getLogin();
|
||||||
|
this.password = user.getPassword();
|
||||||
|
this.roles = Set.of(user.getRole());
|
||||||
|
this.active = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getUsername() {
|
||||||
|
return username;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getPassword() {
|
||||||
|
return password;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Collection<? extends GrantedAuthority> getAuthorities() {
|
||||||
|
return roles;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isEnabled() {
|
||||||
|
return active;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isAccountNonExpired() {
|
||||||
|
return isEnabled();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isAccountNonLocked() {
|
||||||
|
return isEnabled();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isCredentialsNonExpired() {
|
||||||
|
return isEnabled();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,162 @@
|
|||||||
|
package com.example.demo.films.api;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import org.modelmapper.ModelMapper;
|
||||||
|
import org.springframework.stereotype.Controller;
|
||||||
|
import org.springframework.ui.Model;
|
||||||
|
import org.springframework.validation.BindingResult;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||||
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
|
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
|
||||||
|
|
||||||
|
import com.example.demo.core.api.PageAttributesMapper;
|
||||||
|
import com.example.demo.core.configuration.Constants;
|
||||||
|
import com.example.demo.films.model.FilmEntity;
|
||||||
|
import com.example.demo.films.service.FilmService;
|
||||||
|
import com.example.demo.genres.api.GenreDto;
|
||||||
|
import com.example.demo.genres.model.GenreEntity;
|
||||||
|
import com.example.demo.genres.service.GenreService;
|
||||||
|
import com.example.demo.langs.api.LangDto;
|
||||||
|
import com.example.demo.langs.model.LangEntity;
|
||||||
|
import com.example.demo.langs.service.LangService;
|
||||||
|
|
||||||
|
import jakarta.validation.Valid;
|
||||||
|
|
||||||
|
@Controller
|
||||||
|
@RequestMapping(FilmAdminController.URL)
|
||||||
|
public class FilmAdminController {
|
||||||
|
public static final String URL = Constants.ADMIN_PREFIX + "/film";
|
||||||
|
private static final String FILM_VIEW = "film-admin";
|
||||||
|
private static final String FILM_EDIT_VIEW = "film-edit";
|
||||||
|
private static final String FILM_ATTRIBUTE = "film";
|
||||||
|
private static final String PAGE_ATTRIBUTE = "page";
|
||||||
|
|
||||||
|
private final FilmService filmService;
|
||||||
|
private final LangService langService;
|
||||||
|
private final GenreService genreService;
|
||||||
|
private final ModelMapper modelMapper;
|
||||||
|
|
||||||
|
public FilmAdminController(FilmService filmService, ModelMapper modelMapper, GenreService genreService,
|
||||||
|
LangService langService) {
|
||||||
|
this.genreService = genreService;
|
||||||
|
this.langService = langService;
|
||||||
|
this.filmService = filmService;
|
||||||
|
this.modelMapper = modelMapper;
|
||||||
|
}
|
||||||
|
|
||||||
|
private FilmDto toDto(FilmEntity entity) {
|
||||||
|
var dto = new FilmDto();
|
||||||
|
dto.setId(entity.getId());
|
||||||
|
dto.setGenres(entity.getFilmGenres().stream().map(GenreEntity::getId).toList());
|
||||||
|
dto.setDescription(entity.getDescription());
|
||||||
|
dto.setName(entity.getName());
|
||||||
|
dto.setLangId(entity.getLang().getId());
|
||||||
|
return dto;
|
||||||
|
}
|
||||||
|
|
||||||
|
private FilmEntity toEntity(FilmDto dto) {
|
||||||
|
final FilmEntity entity = modelMapper.map(dto, FilmEntity.class);
|
||||||
|
entity.setLang(langService.get(dto.getLangId()));
|
||||||
|
var genres = dto.getGenres();
|
||||||
|
List<GenreEntity> genresList = genreService.getAllById(genres);
|
||||||
|
for (var genre : genresList) {
|
||||||
|
entity.addGenre(genre);
|
||||||
|
}
|
||||||
|
return entity;
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping
|
||||||
|
public String getAll(@RequestParam(name = PAGE_ATTRIBUTE, defaultValue = "0") int page, Model model) {
|
||||||
|
final Map<String, Object> attrib = PageAttributesMapper.toAttributes(
|
||||||
|
filmService.getAll(page, Constants.DEFUALT_PAGE_SIZE), this::toDto);
|
||||||
|
model.addAllAttributes(attrib);
|
||||||
|
model.addAttribute(PAGE_ATTRIBUTE, page);
|
||||||
|
return FILM_VIEW;
|
||||||
|
}
|
||||||
|
|
||||||
|
private LangDto toLangDto(LangEntity entity) {
|
||||||
|
return modelMapper.map(entity, LangDto.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
private GenreDto toGenreDto(GenreEntity entity) {
|
||||||
|
return modelMapper.map(entity, GenreDto.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/edit/")
|
||||||
|
public String create(@RequestParam(name = PAGE_ATTRIBUTE, defaultValue = "0") int page, Model model) {
|
||||||
|
model.addAttribute(FILM_ATTRIBUTE, new FilmDto());
|
||||||
|
model.addAttribute(PAGE_ATTRIBUTE, page);
|
||||||
|
model.addAttribute("langs", langService.getAll().stream().map(this::toLangDto).toList());
|
||||||
|
model.addAttribute("genresCheck", genreService.getAll().stream().map(this::toGenreDto).toList());
|
||||||
|
return FILM_EDIT_VIEW;
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping("/edit/")
|
||||||
|
public String create(
|
||||||
|
@RequestParam(name = PAGE_ATTRIBUTE, defaultValue = "0") int page,
|
||||||
|
@ModelAttribute(name = FILM_ATTRIBUTE) @Valid FilmDto film,
|
||||||
|
BindingResult bindingResult,
|
||||||
|
Model model,
|
||||||
|
RedirectAttributes redirectAttributes) {
|
||||||
|
if (bindingResult.hasErrors()) {
|
||||||
|
model.addAttribute(PAGE_ATTRIBUTE, page);
|
||||||
|
return FILM_EDIT_VIEW;
|
||||||
|
}
|
||||||
|
redirectAttributes.addAttribute(PAGE_ATTRIBUTE, page);
|
||||||
|
filmService.create(toEntity(film));
|
||||||
|
return Constants.REDIRECT_VIEW + URL;
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/edit/{id}")
|
||||||
|
public String update(
|
||||||
|
@PathVariable(name = "id") Long id,
|
||||||
|
@RequestParam(name = PAGE_ATTRIBUTE, defaultValue = "0") int page,
|
||||||
|
Model model) {
|
||||||
|
if (id <= 0) {
|
||||||
|
throw new IllegalArgumentException();
|
||||||
|
}
|
||||||
|
model.addAttribute(PAGE_ATTRIBUTE, page);
|
||||||
|
model.addAttribute(FILM_ATTRIBUTE, toDto(filmService.get(id)));
|
||||||
|
model.addAttribute("langs", langService.getAll().stream().map(this::toLangDto).toList());
|
||||||
|
model.addAttribute("genresCheck", genreService.getAll().stream().map(this::toGenreDto).toList());
|
||||||
|
return FILM_EDIT_VIEW;
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping("/edit/{id}")
|
||||||
|
public String update(
|
||||||
|
@PathVariable(name = "id") Long id,
|
||||||
|
@RequestParam(name = PAGE_ATTRIBUTE, defaultValue = "0") int page,
|
||||||
|
@ModelAttribute(name = FILM_ATTRIBUTE) @Valid FilmDto film,
|
||||||
|
BindingResult bindingResult,
|
||||||
|
RedirectAttributes redirectAttributes,
|
||||||
|
Model model) {
|
||||||
|
if (bindingResult.hasErrors()) {
|
||||||
|
model.addAttribute(PAGE_ATTRIBUTE, page);
|
||||||
|
return FILM_EDIT_VIEW;
|
||||||
|
}
|
||||||
|
if (id <= 0) {
|
||||||
|
throw new IllegalArgumentException();
|
||||||
|
}
|
||||||
|
redirectAttributes.addAttribute(PAGE_ATTRIBUTE, page);
|
||||||
|
model.addAttribute("langs", langService.getAll().stream().map(this::toLangDto).toList());
|
||||||
|
model.addAttribute("genresCheck", genreService.getAll().stream().map(this::toGenreDto).toList());
|
||||||
|
filmService.update(id, toEntity(film));
|
||||||
|
return Constants.REDIRECT_VIEW + URL;
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping("/delete/{id}")
|
||||||
|
public String delete(
|
||||||
|
@RequestParam(name = PAGE_ATTRIBUTE, defaultValue = "0") int page,
|
||||||
|
@PathVariable(name = "id") Long id,
|
||||||
|
RedirectAttributes redirectAttributes) {
|
||||||
|
redirectAttributes.addAttribute(PAGE_ATTRIBUTE, page);
|
||||||
|
filmService.delete(id);
|
||||||
|
return Constants.REDIRECT_VIEW + URL;
|
||||||
|
}
|
||||||
|
}
|
113
src/main/java/com/example/demo/films/api/FilmController.java
Normal file
113
src/main/java/com/example/demo/films/api/FilmController.java
Normal file
@ -0,0 +1,113 @@
|
|||||||
|
package com.example.demo.films.api;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Controller;
|
||||||
|
import org.springframework.ui.Model;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
|
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
|
||||||
|
|
||||||
|
import com.example.demo.core.api.PageAttributesMapper;
|
||||||
|
import com.example.demo.core.configuration.Constants;
|
||||||
|
import com.example.demo.films.model.FilmEntity;
|
||||||
|
import com.example.demo.films.service.FilmService;
|
||||||
|
import com.example.demo.genres.model.GenreEntity;
|
||||||
|
import com.example.demo.langs.model.LangEntity;
|
||||||
|
import com.example.demo.langs.service.LangService;
|
||||||
|
import com.example.demo.users.service.UserService;
|
||||||
|
|
||||||
|
@Controller
|
||||||
|
@RequestMapping(FilmController.URL)
|
||||||
|
public class FilmController {
|
||||||
|
public static final String URL = "/film";
|
||||||
|
private static final String FILM_VIEW = "film";
|
||||||
|
private static final String PAGE_ATTRIBUTE = "page";
|
||||||
|
|
||||||
|
private static final String LIKE_VIEW = "film-like";
|
||||||
|
|
||||||
|
private final FilmService filmService;
|
||||||
|
private final LangService langService;
|
||||||
|
private final UserService userService;
|
||||||
|
|
||||||
|
public FilmController(FilmService filmService,
|
||||||
|
LangService langService, UserService userService) {
|
||||||
|
this.filmService = filmService;
|
||||||
|
this.langService = langService;
|
||||||
|
this.userService = userService;
|
||||||
|
}
|
||||||
|
|
||||||
|
private FilmDtoLike toDto(FilmEntity entity) {
|
||||||
|
var dto = new FilmDtoLike();
|
||||||
|
dto.setId(entity.getId());
|
||||||
|
dto.setGenres(entity.getFilmGenres().stream().map(GenreEntity::getId).toList());
|
||||||
|
dto.setDescription(entity.getDescription());
|
||||||
|
dto.setName(entity.getName());
|
||||||
|
dto.setLangId(entity.getLang().getId());
|
||||||
|
dto.setIsLiked(entity.isLiked);
|
||||||
|
return dto;
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping
|
||||||
|
public String getAll(
|
||||||
|
@RequestParam(name = PAGE_ATTRIBUTE, defaultValue = "0") int page,
|
||||||
|
Model model) {
|
||||||
|
final Map<String, Object> attrib = PageAttributesMapper.toAttributes(
|
||||||
|
filmService.getAll(page, Constants.DEFUALT_PAGE_SIZE), this::toDto);
|
||||||
|
var langs = langService.getAll();
|
||||||
|
Map<Long, String> languages = langs.stream()
|
||||||
|
.collect(Collectors.toMap(LangEntity::getId, LangEntity::getName));
|
||||||
|
model.addAllAttributes(attrib);
|
||||||
|
model.addAttribute(PAGE_ATTRIBUTE, page);
|
||||||
|
model.addAttribute("languages", languages);
|
||||||
|
return FILM_VIEW;
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/like")
|
||||||
|
public String getLikes(@RequestParam(name = PAGE_ATTRIBUTE, defaultValue = "0") int page,
|
||||||
|
Model model) {
|
||||||
|
final Long id = userService.getUserIdLong();
|
||||||
|
var langs = langService.getAll();
|
||||||
|
Map<Long, String> languages = langs.stream()
|
||||||
|
.collect(Collectors.toMap(LangEntity::getId, LangEntity::getName));
|
||||||
|
model.addAttribute("items",
|
||||||
|
filmService.findAllWithFavourite(id, "", page, Constants.DEFUALT_PAGE_SIZE).stream().map(this::toDto)
|
||||||
|
.toList());
|
||||||
|
model.addAttribute("languages", languages);
|
||||||
|
return LIKE_VIEW;
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping("/like/{filmId}")
|
||||||
|
public String addLike(@PathVariable(name = "filmId") Long filmId,
|
||||||
|
@RequestParam(name = PAGE_ATTRIBUTE, defaultValue = "0") int page,
|
||||||
|
RedirectAttributes redirectAttributes) {
|
||||||
|
redirectAttributes.addAttribute(PAGE_ATTRIBUTE, page);
|
||||||
|
final Long userid = userService.getUserIdLong();
|
||||||
|
userService.addFilm(userid, filmId);
|
||||||
|
return Constants.REDIRECT_VIEW + "/list/film";
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping("/dislike1/{id}")
|
||||||
|
public String deleteLike1(
|
||||||
|
@PathVariable(name = "id") Long id, @RequestParam(name = PAGE_ATTRIBUTE, defaultValue = "0") int page,
|
||||||
|
RedirectAttributes redirectAttributes) {
|
||||||
|
redirectAttributes.addAttribute(PAGE_ATTRIBUTE, page);
|
||||||
|
final Long userid = userService.getUserIdLong();
|
||||||
|
userService.deleteFilm(userid, id);
|
||||||
|
return Constants.REDIRECT_VIEW + "/list/film";
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping("/dislike2/{id}")
|
||||||
|
public String deleteLike2(
|
||||||
|
@PathVariable(name = "id") Long id, @RequestParam(name = PAGE_ATTRIBUTE, defaultValue = "0") int page,
|
||||||
|
RedirectAttributes redirectAttributes) {
|
||||||
|
redirectAttributes.addAttribute(PAGE_ATTRIBUTE, page);
|
||||||
|
final Long userid = userService.getUserIdLong();
|
||||||
|
userService.deleteFilm(userid, id);
|
||||||
|
return Constants.REDIRECT_VIEW + URL + "/like";
|
||||||
|
}
|
||||||
|
}
|
66
src/main/java/com/example/demo/films/api/FilmDto.java
Normal file
66
src/main/java/com/example/demo/films/api/FilmDto.java
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
package com.example.demo.films.api;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
|
|
||||||
|
import jakarta.validation.constraints.NotBlank;
|
||||||
|
import jakarta.validation.constraints.NotNull;
|
||||||
|
import jakarta.validation.constraints.Size;
|
||||||
|
import jakarta.validation.constraints.Min;
|
||||||
|
|
||||||
|
public class FilmDto {
|
||||||
|
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
|
||||||
|
private Long id;
|
||||||
|
@NotBlank
|
||||||
|
@Size(min = 1)
|
||||||
|
private String name;
|
||||||
|
@Size(min = 1)
|
||||||
|
private String description;
|
||||||
|
@NotNull
|
||||||
|
private final List<Long> genres = new ArrayList<>();
|
||||||
|
@Min(1)
|
||||||
|
private Long langId;
|
||||||
|
|
||||||
|
public Long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(Long id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getDescription() {
|
||||||
|
return description;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDescription(String desc) {
|
||||||
|
this.description = desc;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long getLangId() {
|
||||||
|
return langId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLangId(Long langId) {
|
||||||
|
this.langId = langId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Long> getGenres() {
|
||||||
|
return genres;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setGenres(List<Long> genres) {
|
||||||
|
this.genres.clear();
|
||||||
|
this.genres.addAll(genres);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
}
|
14
src/main/java/com/example/demo/films/api/FilmDtoLike.java
Normal file
14
src/main/java/com/example/demo/films/api/FilmDtoLike.java
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
package com.example.demo.films.api;
|
||||||
|
|
||||||
|
public class FilmDtoLike extends FilmDto {
|
||||||
|
public Boolean isLiked;
|
||||||
|
|
||||||
|
public Boolean getIsLiked() {
|
||||||
|
return isLiked;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setIsLiked(Boolean isLiked) {
|
||||||
|
this.isLiked = isLiked;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,69 @@
|
|||||||
|
package com.example.demo.films.api;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
import org.springframework.data.domain.Page;
|
||||||
|
import org.springframework.stereotype.Controller;
|
||||||
|
import org.springframework.ui.Model;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
|
import com.example.demo.core.configuration.Constants;
|
||||||
|
import com.example.demo.users.service.UserService;
|
||||||
|
import com.example.demo.core.api.PageAttributesMapper;
|
||||||
|
import com.example.demo.films.model.FilmEntity;
|
||||||
|
import com.example.demo.films.service.FilmService;
|
||||||
|
import com.example.demo.genres.model.GenreEntity;
|
||||||
|
import com.example.demo.langs.model.LangEntity;
|
||||||
|
import com.example.demo.langs.service.LangService;
|
||||||
|
|
||||||
|
@Controller
|
||||||
|
@RequestMapping(FilmListController.URL)
|
||||||
|
public class FilmListController {
|
||||||
|
public static final String URL = "/list";
|
||||||
|
private static final String FILM_VIEW = "film";
|
||||||
|
private static final String PAGE_ATTRIBUTE = "page";
|
||||||
|
|
||||||
|
private final FilmService filmService;
|
||||||
|
private final UserService userService;
|
||||||
|
private final LangService langService;
|
||||||
|
|
||||||
|
public FilmListController(FilmService film,
|
||||||
|
UserService user, LangService lang) {
|
||||||
|
filmService = film;
|
||||||
|
userService = user;
|
||||||
|
langService = lang;
|
||||||
|
}
|
||||||
|
|
||||||
|
private FilmDtoLike toDto(FilmEntity entity) {
|
||||||
|
final Long userId = userService.getUserIdLong();
|
||||||
|
final FilmDtoLike dto = new FilmDtoLike();
|
||||||
|
dto.setId(entity.getId());
|
||||||
|
dto.setGenres(entity.getFilmGenres().stream().map(GenreEntity::getId).toList());
|
||||||
|
dto.setDescription(entity.getDescription());
|
||||||
|
dto.setName(entity.getName());
|
||||||
|
dto.setLangId(entity.getLang().getId());
|
||||||
|
dto.setIsLiked(entity.getIsLiked());
|
||||||
|
return dto;
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/film")
|
||||||
|
public String getAll(@RequestParam(name = PAGE_ATTRIBUTE, defaultValue = "0") int page,
|
||||||
|
@RequestParam(name = "query", required = false) String query,
|
||||||
|
Model model) {
|
||||||
|
Page<FilmEntity> filmPage;
|
||||||
|
filmPage = filmService.findAllWithFavourite(userService.getUserIdLong(), query, page,
|
||||||
|
Constants.DEFUALT_PAGE_SIZE);
|
||||||
|
final Map<String, Object> attrib = PageAttributesMapper.toAttributes(
|
||||||
|
filmPage, this::toDto);
|
||||||
|
var langs = langService.getAll();
|
||||||
|
Map<Long, String> languages = langs.stream()
|
||||||
|
.collect(Collectors.toMap(LangEntity::getId, LangEntity::getName));
|
||||||
|
model.addAllAttributes(attrib);
|
||||||
|
model.addAttribute(PAGE_ATTRIBUTE, page);
|
||||||
|
model.addAttribute("languages", languages);
|
||||||
|
model.addAttribute("query", query);
|
||||||
|
return FILM_VIEW;
|
||||||
|
}
|
||||||
|
}
|
109
src/main/java/com/example/demo/films/model/FilmEntity.java
Normal file
109
src/main/java/com/example/demo/films/model/FilmEntity.java
Normal file
@ -0,0 +1,109 @@
|
|||||||
|
package com.example.demo.films.model;
|
||||||
|
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Objects;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import com.example.demo.core.model.BaseEntity;
|
||||||
|
import com.example.demo.genres.model.GenreEntity;
|
||||||
|
import com.example.demo.langs.model.LangEntity;
|
||||||
|
|
||||||
|
import jakarta.persistence.Column;
|
||||||
|
import jakarta.persistence.Entity;
|
||||||
|
import jakarta.persistence.Table;
|
||||||
|
import jakarta.persistence.ManyToOne;
|
||||||
|
import jakarta.persistence.ManyToMany;
|
||||||
|
import jakarta.persistence.JoinColumn;
|
||||||
|
import jakarta.persistence.JoinTable;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
@Table(name = "films")
|
||||||
|
public class FilmEntity extends BaseEntity {
|
||||||
|
@Column(nullable = false)
|
||||||
|
private String name;
|
||||||
|
@Column(nullable = false)
|
||||||
|
private String description;
|
||||||
|
@ManyToMany
|
||||||
|
@JoinTable(name = "genres_films", joinColumns = @JoinColumn(name = "film_id"), inverseJoinColumns = @JoinColumn(name = "genre_id"))
|
||||||
|
private Set<GenreEntity> filmGenres = new HashSet<>();
|
||||||
|
@ManyToOne
|
||||||
|
@JoinColumn(name = "langId", nullable = false)
|
||||||
|
private LangEntity lang;
|
||||||
|
|
||||||
|
public Boolean isLiked;
|
||||||
|
|
||||||
|
public Boolean getIsLiked() {
|
||||||
|
return isLiked;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setIsLiked(Boolean isLiked) {
|
||||||
|
this.isLiked = isLiked;
|
||||||
|
}
|
||||||
|
|
||||||
|
public FilmEntity() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public FilmEntity(String name, String decs, LangEntity lang) {
|
||||||
|
this.name = name;
|
||||||
|
this.description = decs;
|
||||||
|
this.lang = lang;
|
||||||
|
}
|
||||||
|
|
||||||
|
public FilmEntity(FilmEntity fm, Boolean isLiked) {
|
||||||
|
this.id = fm.getId();
|
||||||
|
this.name = fm.getName();
|
||||||
|
this.description = fm.getDescription();
|
||||||
|
this.lang = fm.getLang();
|
||||||
|
this.isLiked = isLiked;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public LangEntity getLang() {
|
||||||
|
return lang;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLang(LangEntity lang) {
|
||||||
|
this.lang = lang;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getDescription() {
|
||||||
|
return description;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDescription(String desc) {
|
||||||
|
this.description = desc;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Set<GenreEntity> getFilmGenres() {
|
||||||
|
return filmGenres;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addGenre(GenreEntity filmGenre) {
|
||||||
|
filmGenres.add(filmGenre);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return Objects.hash(id, name, description, lang);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object obj) {
|
||||||
|
if (this == obj)
|
||||||
|
return true;
|
||||||
|
if (obj == null || getClass() != obj.getClass())
|
||||||
|
return false;
|
||||||
|
FilmEntity other = (FilmEntity) obj;
|
||||||
|
return Objects.equals(id, other.id)
|
||||||
|
&& Objects.equals(name, other.name)
|
||||||
|
&& Objects.equals(lang, other.lang)
|
||||||
|
&& Objects.equals(description, other.description);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,39 @@
|
|||||||
|
package com.example.demo.films.repository;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
import org.springframework.data.domain.Page;
|
||||||
|
import org.springframework.data.domain.Pageable;
|
||||||
|
import org.springframework.data.jpa.repository.Modifying;
|
||||||
|
import org.springframework.data.jpa.repository.Query;
|
||||||
|
import org.springframework.data.repository.CrudRepository;
|
||||||
|
import org.springframework.data.repository.PagingAndSortingRepository;
|
||||||
|
import org.springframework.data.repository.query.Param;
|
||||||
|
|
||||||
|
import com.example.demo.films.api.FilmDtoLike;
|
||||||
|
import com.example.demo.films.model.FilmEntity;
|
||||||
|
import com.example.demo.genres.model.GenreEntity;
|
||||||
|
|
||||||
|
import jakarta.transaction.Transactional;
|
||||||
|
|
||||||
|
public interface FilmRepository extends CrudRepository<FilmEntity, Long>, PagingAndSortingRepository<FilmEntity, Long> {
|
||||||
|
Optional<FilmEntity> findByNameIgnoreCase(String name);
|
||||||
|
|
||||||
|
@Query("SELECT f FROM FilmEntity f JOIN f.filmGenres g WHERE g.id = :genreId")
|
||||||
|
List<FilmEntity> findAllByGenreId(@Param("genreId") long genreId);
|
||||||
|
|
||||||
|
List<FilmEntity> findAllByLangId(Long langId);
|
||||||
|
|
||||||
|
@Query("SELECT new com.example.demo.films.model.FilmEntity(f, CASE WHEN f.id IN (SELECT uf.id FROM UserEntity u JOIN u.userFilms uf WHERE u.id = :userId) THEN true ELSE false END) FROM FilmEntity f WHERE f.name LIKE %:name%")
|
||||||
|
Page<FilmEntity> findAllWithFavouriteInfo(@Param("userId") Long userId, @Param("name") String name,
|
||||||
|
Pageable pageable);
|
||||||
|
|
||||||
|
Page<FilmEntity> findByNameContainingIgnoreCase(String name, Pageable pageable);
|
||||||
|
|
||||||
|
@Modifying
|
||||||
|
@Transactional
|
||||||
|
@Query("DELETE FROM FilmEntity f WHERE :genre MEMBER OF f.filmGenres")
|
||||||
|
void deleteAllByGenre(@Param("genre") GenreEntity genre);
|
||||||
|
|
||||||
|
}
|
116
src/main/java/com/example/demo/films/service/FilmService.java
Normal file
116
src/main/java/com/example/demo/films/service/FilmService.java
Normal file
@ -0,0 +1,116 @@
|
|||||||
|
package com.example.demo.films.service;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.stream.StreamSupport;
|
||||||
|
|
||||||
|
import org.springframework.data.domain.Page;
|
||||||
|
import org.springframework.data.domain.PageRequest;
|
||||||
|
import org.springframework.data.domain.Sort;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
|
import com.example.demo.core.error.NotFoundException;
|
||||||
|
import com.example.demo.films.model.FilmEntity;
|
||||||
|
import com.example.demo.films.repository.FilmRepository;
|
||||||
|
import com.example.demo.genres.model.GenreEntity;
|
||||||
|
import com.example.demo.genres.repository.GenreRepository;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class FilmService {
|
||||||
|
private final FilmRepository repository;
|
||||||
|
private final GenreRepository genreRepository;
|
||||||
|
|
||||||
|
public FilmService(FilmRepository repository, GenreRepository genreRepository) {
|
||||||
|
this.repository = repository;
|
||||||
|
this.genreRepository = genreRepository;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional(readOnly = true)
|
||||||
|
public List<FilmEntity> getAll() {
|
||||||
|
List<FilmEntity> list = StreamSupport.stream(repository.findAll().spliterator(), false).toList();
|
||||||
|
list.forEach(film -> film.getFilmGenres().size());
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional(readOnly = true)
|
||||||
|
public Page<FilmEntity> getAll(int page, int size) {
|
||||||
|
Page<FilmEntity> list = repository.findAll(PageRequest.of(page, size, Sort.by("id")));
|
||||||
|
list.forEach(film -> film.getFilmGenres().size());
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional(readOnly = true)
|
||||||
|
public FilmEntity get(long id) {
|
||||||
|
FilmEntity film = repository.findById(id)
|
||||||
|
.orElseThrow(() -> new NotFoundException(FilmEntity.class, id));
|
||||||
|
film.getFilmGenres().size();
|
||||||
|
return film;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional(readOnly = true)
|
||||||
|
public Page<FilmEntity> searchByName(String name, int page, int size) {
|
||||||
|
if (name == null) {
|
||||||
|
return getAll(page, size);
|
||||||
|
}
|
||||||
|
Page<FilmEntity> list = repository.findByNameContainingIgnoreCase(name,
|
||||||
|
PageRequest.of(page, size, Sort.by("id")));
|
||||||
|
list.forEach(film -> film.getFilmGenres().size());
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional(readOnly = true)
|
||||||
|
public Page<FilmEntity> findAllWithFavourite(Long userId, String name, int page, int size) {
|
||||||
|
if (name == null) {
|
||||||
|
name = "";
|
||||||
|
}
|
||||||
|
Page<FilmEntity> list = repository.findAllWithFavouriteInfo(userId, name,
|
||||||
|
PageRequest.of(page, size, Sort.by("id")));
|
||||||
|
list.forEach(film -> film.getFilmGenres().size());
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional(readOnly = true)
|
||||||
|
public List<GenreEntity> getFilmGenres(long id) {
|
||||||
|
return get(id).getFilmGenres().stream()
|
||||||
|
.toList();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public FilmEntity addFilmGenres(long filmId, long genreId) {
|
||||||
|
FilmEntity fm = repository.findById(filmId)
|
||||||
|
.orElseThrow(() -> new NotFoundException(FilmEntity.class, filmId));
|
||||||
|
GenreEntity gn = genreRepository.findById(genreId)
|
||||||
|
.orElseThrow(() -> new NotFoundException(GenreEntity.class, genreId));
|
||||||
|
fm.addGenre(gn);
|
||||||
|
return repository.save(fm);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public FilmEntity create(FilmEntity entity) {
|
||||||
|
if (entity == null) {
|
||||||
|
throw new IllegalArgumentException("Entity is null");
|
||||||
|
}
|
||||||
|
return repository.save(entity);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public FilmEntity update(Long id, FilmEntity entity) {
|
||||||
|
final FilmEntity existsEntity = get(id);
|
||||||
|
existsEntity.setName(entity.getName());
|
||||||
|
existsEntity.setDescription(entity.getDescription());
|
||||||
|
existsEntity.setLang(entity.getLang());
|
||||||
|
var genres = entity.getFilmGenres();
|
||||||
|
for (var genre : genres) {
|
||||||
|
existsEntity.addGenre(genre);
|
||||||
|
}
|
||||||
|
return repository.save(existsEntity);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public FilmEntity delete(Long id) {
|
||||||
|
final FilmEntity existsEntity = get(id);
|
||||||
|
repository.delete(existsEntity);
|
||||||
|
return existsEntity;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
127
src/main/java/com/example/demo/genres/api/GenreController.java
Normal file
127
src/main/java/com/example/demo/genres/api/GenreController.java
Normal file
@ -0,0 +1,127 @@
|
|||||||
|
package com.example.demo.genres.api;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import org.modelmapper.ModelMapper;
|
||||||
|
import org.springframework.stereotype.Controller;
|
||||||
|
import org.springframework.ui.Model;
|
||||||
|
import org.springframework.validation.BindingResult;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||||
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
|
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
|
||||||
|
|
||||||
|
import com.example.demo.core.api.PageAttributesMapper;
|
||||||
|
import com.example.demo.core.configuration.Constants;
|
||||||
|
import com.example.demo.genres.model.GenreEntity;
|
||||||
|
import com.example.demo.genres.service.GenreService;
|
||||||
|
|
||||||
|
import jakarta.validation.Valid;
|
||||||
|
|
||||||
|
@Controller
|
||||||
|
@RequestMapping(GenreController.URL)
|
||||||
|
public class GenreController {
|
||||||
|
public static final String URL = Constants.ADMIN_PREFIX + "/genre";
|
||||||
|
private static final String GENRE_VIEW = "genre";
|
||||||
|
private static final String GENRE_EDIT_VIEW = "genre-edit";
|
||||||
|
private static final String GENRE_ATTRIBUTE = "genre";
|
||||||
|
private static final String PAGE_ATTRIBUTE = "page";
|
||||||
|
|
||||||
|
private final GenreService genreService;
|
||||||
|
private final ModelMapper modelMapper;
|
||||||
|
|
||||||
|
public GenreController(GenreService genreService, ModelMapper modelMapper) {
|
||||||
|
this.genreService = genreService;
|
||||||
|
this.modelMapper = modelMapper;
|
||||||
|
}
|
||||||
|
|
||||||
|
private GenreDto toDto(GenreEntity entity) {
|
||||||
|
return modelMapper.map(entity, GenreDto.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
private GenreEntity toEntity(GenreDto dto) {
|
||||||
|
return modelMapper.map(dto, GenreEntity.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping
|
||||||
|
public String getAll(
|
||||||
|
@RequestParam(name = PAGE_ATTRIBUTE, defaultValue = "0") int page,
|
||||||
|
Model model) {
|
||||||
|
final Map<String, Object> attributes = PageAttributesMapper.toAttributes(
|
||||||
|
genreService.getAll(page, Constants.DEFUALT_PAGE_SIZE), this::toDto);
|
||||||
|
model.addAllAttributes(attributes);
|
||||||
|
model.addAttribute(PAGE_ATTRIBUTE, page);
|
||||||
|
return GENRE_VIEW;
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/edit/")
|
||||||
|
public String create(
|
||||||
|
@RequestParam(name = PAGE_ATTRIBUTE, defaultValue = "0") int page,
|
||||||
|
Model model) {
|
||||||
|
model.addAttribute(GENRE_ATTRIBUTE, new GenreDto());
|
||||||
|
model.addAttribute(PAGE_ATTRIBUTE, page);
|
||||||
|
return GENRE_EDIT_VIEW;
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping("/edit/")
|
||||||
|
public String create(
|
||||||
|
@RequestParam(name = PAGE_ATTRIBUTE, defaultValue = "0") int page,
|
||||||
|
@ModelAttribute(name = GENRE_ATTRIBUTE) @Valid GenreDto genre,
|
||||||
|
BindingResult bindingResult,
|
||||||
|
Model model,
|
||||||
|
RedirectAttributes redirectAttributes) {
|
||||||
|
if (bindingResult.hasErrors()) {
|
||||||
|
model.addAttribute(PAGE_ATTRIBUTE, page);
|
||||||
|
return GENRE_EDIT_VIEW;
|
||||||
|
}
|
||||||
|
redirectAttributes.addAttribute(PAGE_ATTRIBUTE, page);
|
||||||
|
genreService.create(toEntity(genre));
|
||||||
|
return Constants.REDIRECT_VIEW + URL;
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/edit/{id}")
|
||||||
|
public String update(
|
||||||
|
@PathVariable(name = "id") Long id,
|
||||||
|
@RequestParam(name = PAGE_ATTRIBUTE, defaultValue = "0") int page,
|
||||||
|
Model model) {
|
||||||
|
if (id <= 0) {
|
||||||
|
throw new IllegalArgumentException();
|
||||||
|
}
|
||||||
|
model.addAttribute(GENRE_ATTRIBUTE, toDto(genreService.get(id)));
|
||||||
|
model.addAttribute(PAGE_ATTRIBUTE, page);
|
||||||
|
return GENRE_EDIT_VIEW;
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping("/edit/{id}")
|
||||||
|
public String update(
|
||||||
|
@PathVariable(name = "id") Long id,
|
||||||
|
@RequestParam(name = PAGE_ATTRIBUTE, defaultValue = "0") int page,
|
||||||
|
@ModelAttribute(name = GENRE_ATTRIBUTE) @Valid GenreDto genre,
|
||||||
|
BindingResult bindingResult,
|
||||||
|
Model model,
|
||||||
|
RedirectAttributes redirectAttributes) {
|
||||||
|
if (bindingResult.hasErrors()) {
|
||||||
|
model.addAttribute(PAGE_ATTRIBUTE, page);
|
||||||
|
return GENRE_EDIT_VIEW;
|
||||||
|
}
|
||||||
|
if (id <= 0) {
|
||||||
|
throw new IllegalArgumentException();
|
||||||
|
}
|
||||||
|
redirectAttributes.addAttribute(PAGE_ATTRIBUTE, page);
|
||||||
|
genreService.update(id, toEntity(genre));
|
||||||
|
return Constants.REDIRECT_VIEW + URL;
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping("/delete/{id}")
|
||||||
|
public String delete(
|
||||||
|
@PathVariable(name = "id") Long id,
|
||||||
|
@RequestParam(name = PAGE_ATTRIBUTE, defaultValue = "0") int page,
|
||||||
|
RedirectAttributes redirectAttributes) {
|
||||||
|
redirectAttributes.addAttribute(PAGE_ATTRIBUTE, page);
|
||||||
|
genreService.delete(id);
|
||||||
|
return Constants.REDIRECT_VIEW + URL;
|
||||||
|
}
|
||||||
|
}
|
30
src/main/java/com/example/demo/genres/api/GenreDto.java
Normal file
30
src/main/java/com/example/demo/genres/api/GenreDto.java
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
package com.example.demo.genres.api;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
|
|
||||||
|
import jakarta.validation.constraints.NotBlank;
|
||||||
|
import jakarta.validation.constraints.Size;
|
||||||
|
|
||||||
|
public class GenreDto {
|
||||||
|
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
|
||||||
|
private Long id;
|
||||||
|
@NotBlank
|
||||||
|
@Size(min = 5, max = 50)
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
public Long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(Long id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
}
|
48
src/main/java/com/example/demo/genres/model/GenreEntity.java
Normal file
48
src/main/java/com/example/demo/genres/model/GenreEntity.java
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
package com.example.demo.genres.model;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
import com.example.demo.core.model.BaseEntity;
|
||||||
|
|
||||||
|
import jakarta.persistence.Column;
|
||||||
|
import jakarta.persistence.Entity;
|
||||||
|
import jakarta.persistence.Table;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
@Table(name = "genres")
|
||||||
|
public class GenreEntity extends BaseEntity {
|
||||||
|
@Column(nullable = false, unique = true, length = 50)
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
public GenreEntity() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public GenreEntity(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return Objects.hash(id, name);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object obj) {
|
||||||
|
if (this == obj)
|
||||||
|
return true;
|
||||||
|
if (obj == null || getClass() != obj.getClass())
|
||||||
|
return false;
|
||||||
|
final GenreEntity other = (GenreEntity) obj;
|
||||||
|
return Objects.equals(other.getId(), id)
|
||||||
|
&& Objects.equals(other.getName(), name);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,13 @@
|
|||||||
|
package com.example.demo.genres.repository;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
import org.springframework.data.repository.CrudRepository;
|
||||||
|
import org.springframework.data.repository.PagingAndSortingRepository;
|
||||||
|
|
||||||
|
import com.example.demo.genres.model.GenreEntity;
|
||||||
|
|
||||||
|
public interface GenreRepository extends CrudRepository<GenreEntity, Long>,
|
||||||
|
PagingAndSortingRepository<GenreEntity, Long> {
|
||||||
|
Optional<GenreEntity> findByNameIgnoreCase(String name);
|
||||||
|
}
|
@ -0,0 +1,80 @@
|
|||||||
|
package com.example.demo.genres.service;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.stream.StreamSupport;
|
||||||
|
|
||||||
|
import org.springframework.data.domain.Page;
|
||||||
|
import org.springframework.data.domain.PageRequest;
|
||||||
|
import org.springframework.data.domain.Sort;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
|
import com.example.demo.core.error.NotFoundException;
|
||||||
|
import com.example.demo.genres.model.GenreEntity;
|
||||||
|
import com.example.demo.genres.repository.GenreRepository;
|
||||||
|
|
||||||
|
import com.example.demo.films.repository.FilmRepository;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class GenreService {
|
||||||
|
private final GenreRepository repository;
|
||||||
|
private final FilmRepository fmrepository;
|
||||||
|
|
||||||
|
public GenreService(GenreRepository repository, FilmRepository fmrepository) {
|
||||||
|
this.repository = repository;
|
||||||
|
this.fmrepository = fmrepository;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void checkName(String name) {
|
||||||
|
if (repository.findByNameIgnoreCase(name).isPresent()) {
|
||||||
|
throw new IllegalArgumentException(
|
||||||
|
String.format("Genre with name %s is already exists", name));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional(readOnly = true)
|
||||||
|
public List<GenreEntity> getAll() {
|
||||||
|
return StreamSupport.stream(repository.findAll().spliterator(), false).toList();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional(readOnly = true)
|
||||||
|
public List<GenreEntity> getAllById(List<Long> listIds) {
|
||||||
|
return StreamSupport.stream(repository.findAllById(listIds).spliterator(), false).toList();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional(readOnly = true)
|
||||||
|
public Page<GenreEntity> getAll(int page, int size) {
|
||||||
|
return repository.findAll(PageRequest.of(page, size, Sort.by("id")));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional(readOnly = true)
|
||||||
|
public GenreEntity get(long id) {
|
||||||
|
return repository.findById(id)
|
||||||
|
.orElseThrow(() -> new NotFoundException(GenreEntity.class, id));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public GenreEntity create(GenreEntity entity) {
|
||||||
|
if (entity == null) {
|
||||||
|
throw new IllegalArgumentException("Entity is null");
|
||||||
|
}
|
||||||
|
checkName(entity.getName());
|
||||||
|
return repository.save(entity);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public GenreEntity update(Long id, GenreEntity entity) {
|
||||||
|
final GenreEntity existsEntity = get(id);
|
||||||
|
checkName(entity.getName());
|
||||||
|
existsEntity.setName(entity.getName());
|
||||||
|
return repository.save(existsEntity);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public GenreEntity delete(Long id) {
|
||||||
|
final GenreEntity existsEntity = get(id);
|
||||||
|
fmrepository.deleteAllByGenre(existsEntity);
|
||||||
|
repository.delete(existsEntity);
|
||||||
|
return existsEntity;
|
||||||
|
}
|
||||||
|
}
|
127
src/main/java/com/example/demo/langs/api/LangController.java
Normal file
127
src/main/java/com/example/demo/langs/api/LangController.java
Normal file
@ -0,0 +1,127 @@
|
|||||||
|
package com.example.demo.langs.api;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import org.modelmapper.ModelMapper;
|
||||||
|
import org.springframework.stereotype.Controller;
|
||||||
|
import org.springframework.ui.Model;
|
||||||
|
import org.springframework.validation.BindingResult;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||||
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
|
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
|
||||||
|
|
||||||
|
import com.example.demo.core.api.PageAttributesMapper;
|
||||||
|
import com.example.demo.core.configuration.Constants;
|
||||||
|
import com.example.demo.langs.model.LangEntity;
|
||||||
|
import com.example.demo.langs.service.LangService;
|
||||||
|
|
||||||
|
import jakarta.validation.Valid;
|
||||||
|
|
||||||
|
@Controller
|
||||||
|
@RequestMapping(LangController.URL)
|
||||||
|
public class LangController {
|
||||||
|
public static final String URL = Constants.ADMIN_PREFIX + "/lang";
|
||||||
|
private static final String LANG_VIEW = "lang";
|
||||||
|
private static final String LANG_EDIT_VIEW = "lang-edit";
|
||||||
|
private static final String LANG_ATTRIBUTE = "lang";
|
||||||
|
private static final String PAGE_ATTRIBUTE = "page";
|
||||||
|
|
||||||
|
private final LangService langService;
|
||||||
|
private final ModelMapper modelMapper;
|
||||||
|
|
||||||
|
public LangController(LangService langService, ModelMapper modelMapper) {
|
||||||
|
this.langService = langService;
|
||||||
|
this.modelMapper = modelMapper;
|
||||||
|
}
|
||||||
|
|
||||||
|
private LangDto toDto(LangEntity entity) {
|
||||||
|
return modelMapper.map(entity, LangDto.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
private LangEntity toEntity(LangDto dto) {
|
||||||
|
return modelMapper.map(dto, LangEntity.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping
|
||||||
|
public String getAll(
|
||||||
|
@RequestParam(name = PAGE_ATTRIBUTE, defaultValue = "0") int page,
|
||||||
|
Model model) {
|
||||||
|
final Map<String, Object> attributes = PageAttributesMapper.toAttributes(
|
||||||
|
langService.getAll(page, Constants.DEFUALT_PAGE_SIZE), this::toDto);
|
||||||
|
model.addAllAttributes(attributes);
|
||||||
|
model.addAttribute(PAGE_ATTRIBUTE, page);
|
||||||
|
return LANG_VIEW;
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/edit/")
|
||||||
|
public String create(
|
||||||
|
@RequestParam(name = PAGE_ATTRIBUTE, defaultValue = "0") int page,
|
||||||
|
Model model) {
|
||||||
|
model.addAttribute(LANG_ATTRIBUTE, new LangDto());
|
||||||
|
model.addAttribute(PAGE_ATTRIBUTE, page);
|
||||||
|
return LANG_EDIT_VIEW;
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping("/edit/")
|
||||||
|
public String create(
|
||||||
|
@RequestParam(name = PAGE_ATTRIBUTE, defaultValue = "0") int page,
|
||||||
|
@ModelAttribute(name = LANG_ATTRIBUTE) @Valid LangDto lang,
|
||||||
|
BindingResult bindingResult,
|
||||||
|
Model model,
|
||||||
|
RedirectAttributes redirectAttributes) {
|
||||||
|
if (bindingResult.hasErrors()) {
|
||||||
|
model.addAttribute(PAGE_ATTRIBUTE, page);
|
||||||
|
return LANG_EDIT_VIEW;
|
||||||
|
}
|
||||||
|
redirectAttributes.addAttribute(PAGE_ATTRIBUTE, page);
|
||||||
|
langService.create(toEntity(lang));
|
||||||
|
return Constants.REDIRECT_VIEW + URL;
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/edit/{id}")
|
||||||
|
public String update(
|
||||||
|
@PathVariable(name = "id") Long id,
|
||||||
|
@RequestParam(name = PAGE_ATTRIBUTE, defaultValue = "0") int page,
|
||||||
|
Model model) {
|
||||||
|
if (id <= 0) {
|
||||||
|
throw new IllegalArgumentException();
|
||||||
|
}
|
||||||
|
model.addAttribute(LANG_ATTRIBUTE, toDto(langService.get(id)));
|
||||||
|
model.addAttribute(PAGE_ATTRIBUTE, page);
|
||||||
|
return LANG_EDIT_VIEW;
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping("/edit/{id}")
|
||||||
|
public String update(
|
||||||
|
@PathVariable(name = "id") Long id,
|
||||||
|
@RequestParam(name = PAGE_ATTRIBUTE, defaultValue = "0") int page,
|
||||||
|
@ModelAttribute(name = LANG_ATTRIBUTE) @Valid LangDto lang,
|
||||||
|
BindingResult bindingResult,
|
||||||
|
Model model,
|
||||||
|
RedirectAttributes redirectAttributes) {
|
||||||
|
if (bindingResult.hasErrors()) {
|
||||||
|
model.addAttribute(PAGE_ATTRIBUTE, page);
|
||||||
|
return LANG_EDIT_VIEW;
|
||||||
|
}
|
||||||
|
if (id <= 0) {
|
||||||
|
throw new IllegalArgumentException();
|
||||||
|
}
|
||||||
|
redirectAttributes.addAttribute(PAGE_ATTRIBUTE, page);
|
||||||
|
langService.update(id, toEntity(lang));
|
||||||
|
return Constants.REDIRECT_VIEW + URL;
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping("/delete/{id}")
|
||||||
|
public String delete(
|
||||||
|
@PathVariable(name = "id") Long id,
|
||||||
|
@RequestParam(name = PAGE_ATTRIBUTE, defaultValue = "0") int page,
|
||||||
|
RedirectAttributes redirectAttributes) {
|
||||||
|
redirectAttributes.addAttribute(PAGE_ATTRIBUTE, page);
|
||||||
|
langService.delete(id);
|
||||||
|
return Constants.REDIRECT_VIEW + URL;
|
||||||
|
}
|
||||||
|
}
|
30
src/main/java/com/example/demo/langs/api/LangDto.java
Normal file
30
src/main/java/com/example/demo/langs/api/LangDto.java
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
package com.example.demo.langs.api;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
|
|
||||||
|
import jakarta.validation.constraints.NotBlank;
|
||||||
|
import jakarta.validation.constraints.Size;
|
||||||
|
|
||||||
|
public class LangDto {
|
||||||
|
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
|
||||||
|
private Long id;
|
||||||
|
@NotBlank
|
||||||
|
@Size(min = 5, max = 50)
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
public Long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(Long id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
}
|
48
src/main/java/com/example/demo/langs/model/LangEntity.java
Normal file
48
src/main/java/com/example/demo/langs/model/LangEntity.java
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
package com.example.demo.langs.model;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
import com.example.demo.core.model.BaseEntity;
|
||||||
|
|
||||||
|
import jakarta.persistence.Column;
|
||||||
|
import jakarta.persistence.Entity;
|
||||||
|
import jakarta.persistence.Table;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
@Table(name = "langs")
|
||||||
|
public class LangEntity extends BaseEntity {
|
||||||
|
@Column(nullable = false, unique = true, length = 50)
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
public LangEntity() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public LangEntity(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return Objects.hash(id, name);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object obj) {
|
||||||
|
if (this == obj)
|
||||||
|
return true;
|
||||||
|
if (obj == null || getClass() != obj.getClass())
|
||||||
|
return false;
|
||||||
|
final LangEntity other = (LangEntity) obj;
|
||||||
|
return Objects.equals(other.getId(), id)
|
||||||
|
&& Objects.equals(other.getName(), name);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,13 @@
|
|||||||
|
package com.example.demo.langs.repository;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
import org.springframework.data.repository.CrudRepository;
|
||||||
|
import org.springframework.data.repository.PagingAndSortingRepository;
|
||||||
|
|
||||||
|
import com.example.demo.langs.model.LangEntity;
|
||||||
|
|
||||||
|
public interface LangRepository extends CrudRepository<LangEntity, Long>, PagingAndSortingRepository<LangEntity, Long> {
|
||||||
|
Optional<LangEntity> findByNameIgnoreCase(String name);
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,77 @@
|
|||||||
|
package com.example.demo.langs.service;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.stream.StreamSupport;
|
||||||
|
|
||||||
|
import org.springframework.data.domain.Page;
|
||||||
|
import org.springframework.data.domain.PageRequest;
|
||||||
|
import org.springframework.data.domain.Sort;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
|
import com.example.demo.core.error.NotFoundException;
|
||||||
|
import com.example.demo.langs.model.LangEntity;
|
||||||
|
import com.example.demo.langs.repository.LangRepository;
|
||||||
|
|
||||||
|
import jakarta.persistence.EntityManager;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class LangService {
|
||||||
|
private final LangRepository repository;
|
||||||
|
private final EntityManager entityManager;
|
||||||
|
|
||||||
|
public LangService(LangRepository repository, EntityManager entityManager) {
|
||||||
|
this.repository = repository;
|
||||||
|
this.entityManager = entityManager;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void checkName(String name) {
|
||||||
|
if (repository.findByNameIgnoreCase(name).isPresent()) {
|
||||||
|
throw new IllegalArgumentException(
|
||||||
|
String.format("Lang with name %s is already exists", name));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional(readOnly = true)
|
||||||
|
public List<LangEntity> getAll() {
|
||||||
|
return StreamSupport.stream(repository.findAll().spliterator(), false).toList();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional(readOnly = true)
|
||||||
|
public LangEntity get(long id) {
|
||||||
|
return repository.findById(id)
|
||||||
|
.orElseThrow(() -> new NotFoundException(LangEntity.class, id));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional(readOnly = true)
|
||||||
|
public Page<LangEntity> getAll(int page, int size) {
|
||||||
|
return repository.findAll(PageRequest.of(page, size, Sort.by("id")));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public LangEntity create(LangEntity entity) {
|
||||||
|
if (entity == null) {
|
||||||
|
throw new IllegalArgumentException("Entity is null");
|
||||||
|
}
|
||||||
|
checkName(entity.getName());
|
||||||
|
return repository.save(entity);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public LangEntity update(Long id, LangEntity entity) {
|
||||||
|
final LangEntity existsEntity = get(id);
|
||||||
|
checkName(entity.getName());
|
||||||
|
existsEntity.setName(entity.getName());
|
||||||
|
return repository.save(existsEntity);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public LangEntity delete(Long id) {
|
||||||
|
final LangEntity existsEntity = get(id);
|
||||||
|
entityManager.createQuery("DELETE FROM FilmEntity f WHERE f.lang = :lang")
|
||||||
|
.setParameter("lang", existsEntity)
|
||||||
|
.executeUpdate();
|
||||||
|
repository.delete(existsEntity);
|
||||||
|
return existsEntity;
|
||||||
|
}
|
||||||
|
}
|
127
src/main/java/com/example/demo/users/api/UserController.java
Normal file
127
src/main/java/com/example/demo/users/api/UserController.java
Normal file
@ -0,0 +1,127 @@
|
|||||||
|
package com.example.demo.users.api;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import org.modelmapper.ModelMapper;
|
||||||
|
import org.springframework.stereotype.Controller;
|
||||||
|
import org.springframework.ui.Model;
|
||||||
|
import org.springframework.validation.BindingResult;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||||
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
|
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
|
||||||
|
|
||||||
|
import com.example.demo.core.api.PageAttributesMapper;
|
||||||
|
import com.example.demo.core.configuration.Constants;
|
||||||
|
import com.example.demo.users.model.UserEntity;
|
||||||
|
import com.example.demo.users.service.UserService;
|
||||||
|
|
||||||
|
import jakarta.validation.Valid;
|
||||||
|
|
||||||
|
@Controller
|
||||||
|
@RequestMapping(UserController.URL)
|
||||||
|
public class UserController {
|
||||||
|
public static final String URL = Constants.ADMIN_PREFIX + "/user";
|
||||||
|
private static final String USER_VIEW = "user";
|
||||||
|
private static final String USER_EDIT_VIEW = "user-edit";
|
||||||
|
private static final String PAGE_ATTRIBUTE = "page";
|
||||||
|
private static final String USER_ATTRIBUTE = "user";
|
||||||
|
|
||||||
|
private final UserService userService;
|
||||||
|
private final ModelMapper modelMapper;
|
||||||
|
|
||||||
|
public UserController(UserService userService, ModelMapper modelMapper) {
|
||||||
|
this.userService = userService;
|
||||||
|
this.modelMapper = modelMapper;
|
||||||
|
}
|
||||||
|
|
||||||
|
private UserDto toDto(UserEntity entity) {
|
||||||
|
return modelMapper.map(entity, UserDto.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
private UserEntity toEntity(UserDto dto) {
|
||||||
|
return modelMapper.map(dto, UserEntity.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping
|
||||||
|
public String getAll(
|
||||||
|
@RequestParam(name = PAGE_ATTRIBUTE, defaultValue = "0") int page,
|
||||||
|
Model model) {
|
||||||
|
final Map<String, Object> attributes = PageAttributesMapper.toAttributes(
|
||||||
|
userService.getAll(page, Constants.DEFUALT_PAGE_SIZE), this::toDto);
|
||||||
|
model.addAllAttributes(attributes);
|
||||||
|
model.addAttribute(PAGE_ATTRIBUTE, page);
|
||||||
|
return USER_VIEW;
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/edit/")
|
||||||
|
public String create(
|
||||||
|
@RequestParam(name = PAGE_ATTRIBUTE, defaultValue = "0") int page,
|
||||||
|
Model model) {
|
||||||
|
model.addAttribute(USER_ATTRIBUTE, new UserDto());
|
||||||
|
model.addAttribute(PAGE_ATTRIBUTE, page);
|
||||||
|
return USER_EDIT_VIEW;
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping("/edit/")
|
||||||
|
public String create(
|
||||||
|
@RequestParam(name = PAGE_ATTRIBUTE, defaultValue = "0") int page,
|
||||||
|
@ModelAttribute(name = USER_ATTRIBUTE) @Valid UserDto user,
|
||||||
|
BindingResult bindingResult,
|
||||||
|
Model model,
|
||||||
|
RedirectAttributes redirectAttributes) {
|
||||||
|
if (bindingResult.hasErrors()) {
|
||||||
|
model.addAttribute(PAGE_ATTRIBUTE, page);
|
||||||
|
return USER_EDIT_VIEW;
|
||||||
|
}
|
||||||
|
redirectAttributes.addAttribute(PAGE_ATTRIBUTE, page);
|
||||||
|
userService.create(toEntity(user));
|
||||||
|
return Constants.REDIRECT_VIEW + URL;
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/edit/{id}")
|
||||||
|
public String update(
|
||||||
|
@PathVariable(name = "id") Long id,
|
||||||
|
@RequestParam(name = PAGE_ATTRIBUTE, defaultValue = "0") int page,
|
||||||
|
Model model) {
|
||||||
|
if (id <= 0) {
|
||||||
|
throw new IllegalArgumentException();
|
||||||
|
}
|
||||||
|
model.addAttribute(USER_ATTRIBUTE, toDto(userService.get(id)));
|
||||||
|
model.addAttribute(PAGE_ATTRIBUTE, page);
|
||||||
|
return USER_EDIT_VIEW;
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping("/edit/{id}")
|
||||||
|
public String update(
|
||||||
|
@PathVariable(name = "id") Long id,
|
||||||
|
@RequestParam(name = PAGE_ATTRIBUTE, defaultValue = "0") int page,
|
||||||
|
@ModelAttribute(name = USER_ATTRIBUTE) @Valid UserDto user,
|
||||||
|
BindingResult bindingResult,
|
||||||
|
Model model,
|
||||||
|
RedirectAttributes redirectAttributes) {
|
||||||
|
if (bindingResult.hasErrors()) {
|
||||||
|
model.addAttribute(PAGE_ATTRIBUTE, page);
|
||||||
|
return USER_EDIT_VIEW;
|
||||||
|
}
|
||||||
|
if (id <= 0) {
|
||||||
|
throw new IllegalArgumentException();
|
||||||
|
}
|
||||||
|
redirectAttributes.addAttribute(PAGE_ATTRIBUTE, page);
|
||||||
|
userService.update(id, toEntity(user));
|
||||||
|
return Constants.REDIRECT_VIEW + URL;
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping("/delete/{id}")
|
||||||
|
public String delete(
|
||||||
|
@PathVariable(name = "id") Long id,
|
||||||
|
@RequestParam(name = PAGE_ATTRIBUTE, defaultValue = "0") int page,
|
||||||
|
RedirectAttributes redirectAttributes) {
|
||||||
|
redirectAttributes.addAttribute(PAGE_ATTRIBUTE, page);
|
||||||
|
userService.delete(id);
|
||||||
|
return Constants.REDIRECT_VIEW + URL;
|
||||||
|
}
|
||||||
|
}
|
36
src/main/java/com/example/demo/users/api/UserDto.java
Normal file
36
src/main/java/com/example/demo/users/api/UserDto.java
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
package com.example.demo.users.api;
|
||||||
|
|
||||||
|
import jakarta.validation.constraints.NotBlank;
|
||||||
|
import jakarta.validation.constraints.Size;
|
||||||
|
|
||||||
|
public class UserDto {
|
||||||
|
private Long id;
|
||||||
|
@NotBlank
|
||||||
|
@Size(min = 3, max = 20)
|
||||||
|
private String login;
|
||||||
|
private String role;
|
||||||
|
|
||||||
|
public Long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(Long id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getLogin() {
|
||||||
|
return login;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLogin(String login) {
|
||||||
|
this.login = login;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getRole() {
|
||||||
|
return role;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRole(String role) {
|
||||||
|
this.role = role;
|
||||||
|
}
|
||||||
|
}
|
31
src/main/java/com/example/demo/users/api/UserFilmsDto.java
Normal file
31
src/main/java/com/example/demo/users/api/UserFilmsDto.java
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
package com.example.demo.users.api;
|
||||||
|
|
||||||
|
public class UserFilmsDto {
|
||||||
|
private Long id;
|
||||||
|
private String name;
|
||||||
|
private boolean active;
|
||||||
|
|
||||||
|
public Long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(Long id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isActive() {
|
||||||
|
return active;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setActive(boolean active) {
|
||||||
|
this.active = active;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,50 @@
|
|||||||
|
package com.example.demo.users.api;
|
||||||
|
|
||||||
|
import org.modelmapper.ModelMapper;
|
||||||
|
import org.springframework.security.core.annotation.AuthenticationPrincipal;
|
||||||
|
import org.springframework.stereotype.Controller;
|
||||||
|
import org.springframework.ui.Model;
|
||||||
|
import org.springframework.validation.BindingResult;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
|
|
||||||
|
import com.example.demo.core.configuration.Constants;
|
||||||
|
import com.example.demo.core.security.UserPrincipal;
|
||||||
|
import com.example.demo.users.service.UserService;
|
||||||
|
|
||||||
|
import jakarta.validation.Valid;
|
||||||
|
|
||||||
|
@Controller
|
||||||
|
public class UserProfileController {
|
||||||
|
private static final String PROFILE_VIEW = "profile";
|
||||||
|
|
||||||
|
private static final String PAGE_ATTRIBUTE = "page";
|
||||||
|
private static final String PROFILE_ATTRIBUTE = "profile";
|
||||||
|
|
||||||
|
public UserProfileController(
|
||||||
|
UserService userService,
|
||||||
|
ModelMapper modelMapper) {
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping
|
||||||
|
public String getProfile(
|
||||||
|
@RequestParam(name = PAGE_ATTRIBUTE, defaultValue = "0") int page,
|
||||||
|
Model model,
|
||||||
|
@AuthenticationPrincipal UserPrincipal principal) {
|
||||||
|
return PROFILE_VIEW;
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping
|
||||||
|
public String saveProfile(
|
||||||
|
@ModelAttribute(name = PROFILE_ATTRIBUTE) @Valid UserProfileDto profile,
|
||||||
|
BindingResult bindResult,
|
||||||
|
Model model,
|
||||||
|
@AuthenticationPrincipal UserPrincipal principal) {
|
||||||
|
if (bindResult.hasErrors()) {
|
||||||
|
return PROFILE_VIEW;
|
||||||
|
}
|
||||||
|
return Constants.REDIRECT_VIEW + "/";
|
||||||
|
}
|
||||||
|
}
|
25
src/main/java/com/example/demo/users/api/UserProfileDto.java
Normal file
25
src/main/java/com/example/demo/users/api/UserProfileDto.java
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
package com.example.demo.users.api;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import com.example.demo.films.api.FilmDto;
|
||||||
|
|
||||||
|
public class UserProfileDto {
|
||||||
|
private List<FilmDto> films = new ArrayList<>();
|
||||||
|
|
||||||
|
public UserProfileDto() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public UserProfileDto(List<FilmDto> films) {
|
||||||
|
this.films = films;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<FilmDto> getFilms() {
|
||||||
|
return films;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSubscriptions(List<FilmDto> films) {
|
||||||
|
this.films = films;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,65 @@
|
|||||||
|
package com.example.demo.users.api;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
import org.modelmapper.ModelMapper;
|
||||||
|
import org.springframework.stereotype.Controller;
|
||||||
|
import org.springframework.ui.Model;
|
||||||
|
import org.springframework.validation.BindingResult;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
|
||||||
|
import com.example.demo.core.configuration.Constants;
|
||||||
|
import com.example.demo.users.model.UserEntity;
|
||||||
|
import com.example.demo.users.service.UserService;
|
||||||
|
|
||||||
|
import jakarta.validation.Valid;
|
||||||
|
|
||||||
|
@Controller
|
||||||
|
@RequestMapping(UserSignupController.URL)
|
||||||
|
public class UserSignupController {
|
||||||
|
public static final String URL = "/signup";
|
||||||
|
|
||||||
|
private static final String SIGNUP_VIEW = "signup";
|
||||||
|
private static final String USER_ATTRIBUTE = "user";
|
||||||
|
|
||||||
|
private final UserService userService;
|
||||||
|
private final ModelMapper modelMapper;
|
||||||
|
|
||||||
|
public UserSignupController(
|
||||||
|
UserService userService,
|
||||||
|
ModelMapper modelMapper) {
|
||||||
|
this.userService = userService;
|
||||||
|
this.modelMapper = modelMapper;
|
||||||
|
}
|
||||||
|
|
||||||
|
private UserEntity toEntity(UserSignupDto dto) {
|
||||||
|
return modelMapper.map(dto, UserEntity.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping
|
||||||
|
public String getSignup(Model model) {
|
||||||
|
model.addAttribute(USER_ATTRIBUTE, new UserSignupDto());
|
||||||
|
return SIGNUP_VIEW;
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping
|
||||||
|
public String signup(
|
||||||
|
@ModelAttribute(name = USER_ATTRIBUTE) @Valid UserSignupDto user,
|
||||||
|
BindingResult bindingResult,
|
||||||
|
Model model) {
|
||||||
|
if (bindingResult.hasErrors()) {
|
||||||
|
return SIGNUP_VIEW;
|
||||||
|
}
|
||||||
|
if (!Objects.equals(user.getPassword(), user.getPasswordConfirm())) {
|
||||||
|
bindingResult.rejectValue("password", "signup:passwords", "Пароли не совпадают.");
|
||||||
|
model.addAttribute(USER_ATTRIBUTE, user);
|
||||||
|
return SIGNUP_VIEW;
|
||||||
|
}
|
||||||
|
userService.create(toEntity(user));
|
||||||
|
return Constants.REDIRECT_VIEW + Constants.LOGIN_URL + "?signup";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
40
src/main/java/com/example/demo/users/api/UserSignupDto.java
Normal file
40
src/main/java/com/example/demo/users/api/UserSignupDto.java
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
package com.example.demo.users.api;
|
||||||
|
|
||||||
|
import jakarta.validation.constraints.NotBlank;
|
||||||
|
import jakarta.validation.constraints.Size;
|
||||||
|
|
||||||
|
public class UserSignupDto {
|
||||||
|
@NotBlank
|
||||||
|
@Size(min = 3, max = 20)
|
||||||
|
private String login;
|
||||||
|
@NotBlank
|
||||||
|
@Size(min = 3, max = 20)
|
||||||
|
private String password;
|
||||||
|
@NotBlank
|
||||||
|
@Size(min = 3, max = 20)
|
||||||
|
private String passwordConfirm;
|
||||||
|
|
||||||
|
public String getLogin() {
|
||||||
|
return login;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLogin(String login) {
|
||||||
|
this.login = login;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPassword() {
|
||||||
|
return password;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPassword(String password) {
|
||||||
|
this.password = password;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPasswordConfirm() {
|
||||||
|
return passwordConfirm;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPasswordConfirm(String passwordConfirm) {
|
||||||
|
this.passwordConfirm = passwordConfirm;
|
||||||
|
}
|
||||||
|
}
|
92
src/main/java/com/example/demo/users/model/UserEntity.java
Normal file
92
src/main/java/com/example/demo/users/model/UserEntity.java
Normal file
@ -0,0 +1,92 @@
|
|||||||
|
package com.example.demo.users.model;
|
||||||
|
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Objects;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import com.example.demo.core.model.BaseEntity;
|
||||||
|
import com.example.demo.films.model.FilmEntity;
|
||||||
|
|
||||||
|
import jakarta.persistence.Column;
|
||||||
|
import jakarta.persistence.Entity;
|
||||||
|
import jakarta.persistence.JoinColumn;
|
||||||
|
import jakarta.persistence.JoinTable;
|
||||||
|
import jakarta.persistence.ManyToMany;
|
||||||
|
import jakarta.persistence.Table;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
@Table(name = "users")
|
||||||
|
public class UserEntity extends BaseEntity {
|
||||||
|
@Column(nullable = false, unique = true, length = 20)
|
||||||
|
private String login;
|
||||||
|
@Column(nullable = false, length = 60)
|
||||||
|
private String password;
|
||||||
|
private UserRole role;
|
||||||
|
@ManyToMany()
|
||||||
|
@JoinTable(name = "users_films", joinColumns = @JoinColumn(name = "user_id"), inverseJoinColumns = @JoinColumn(name = "film_id"))
|
||||||
|
private Set<FilmEntity> userFilms = new HashSet<>();
|
||||||
|
|
||||||
|
public UserEntity() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public UserEntity(String login, String password) {
|
||||||
|
this.login = login;
|
||||||
|
this.password = password;
|
||||||
|
this.role = UserRole.USER;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getLogin() {
|
||||||
|
return login;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLogin(String login) {
|
||||||
|
this.login = login;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPassword() {
|
||||||
|
return password;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPassword(String password) {
|
||||||
|
this.password = password;
|
||||||
|
}
|
||||||
|
|
||||||
|
public UserRole getRole() {
|
||||||
|
return role;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRole(UserRole role) {
|
||||||
|
this.role = role;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Set<FilmEntity> getUserFilms() {
|
||||||
|
return userFilms;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addFilm(FilmEntity userFilm) {
|
||||||
|
userFilms.add(userFilm);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void deleteFilm(FilmEntity userFilm) {
|
||||||
|
userFilms.remove(userFilm);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return Objects.hash(id, login, password, role, userFilms);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object obj) {
|
||||||
|
if (this == obj)
|
||||||
|
return true;
|
||||||
|
if (obj == null || getClass() != obj.getClass())
|
||||||
|
return false;
|
||||||
|
UserEntity other = (UserEntity) obj;
|
||||||
|
return Objects.equals(other.getId(), id)
|
||||||
|
&& Objects.equals(other.getLogin(), login)
|
||||||
|
&& Objects.equals(other.getPassword(), password)
|
||||||
|
&& Objects.equals(other.getRole(), role)
|
||||||
|
&& Objects.equals(other.getUserFilms(), userFilms);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,60 @@
|
|||||||
|
package com.example.demo.users.model;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
import com.example.demo.films.model.FilmEntity;
|
||||||
|
|
||||||
|
public class UserFilmWithStatus {
|
||||||
|
private Long id;
|
||||||
|
private String name;
|
||||||
|
private boolean active;
|
||||||
|
|
||||||
|
public UserFilmWithStatus() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public UserFilmWithStatus(FilmEntity entity, boolean active) {
|
||||||
|
this.id = entity.getId();
|
||||||
|
this.name = entity.getName();
|
||||||
|
this.active = active;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(Long id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isActive() {
|
||||||
|
return active;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setActive(boolean active) {
|
||||||
|
this.active = active;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return Objects.hash(id, name, active);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object obj) {
|
||||||
|
if (this == obj)
|
||||||
|
return true;
|
||||||
|
if (obj == null || getClass() != obj.getClass())
|
||||||
|
return false;
|
||||||
|
UserFilmWithStatus other = (UserFilmWithStatus) obj;
|
||||||
|
return Objects.equals(id, other.id) && Objects.equals(name, other.name) && active == other.active;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
15
src/main/java/com/example/demo/users/model/UserRole.java
Normal file
15
src/main/java/com/example/demo/users/model/UserRole.java
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
package com.example.demo.users.model;
|
||||||
|
|
||||||
|
import org.springframework.security.core.GrantedAuthority;
|
||||||
|
|
||||||
|
public enum UserRole implements GrantedAuthority {
|
||||||
|
ADMIN,
|
||||||
|
USER;
|
||||||
|
|
||||||
|
private static final String PREFIX = "ROLE_";
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getAuthority() {
|
||||||
|
return PREFIX + this.name();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,20 @@
|
|||||||
|
package com.example.demo.users.repository;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
import org.springframework.data.jpa.repository.Query;
|
||||||
|
import org.springframework.data.repository.CrudRepository;
|
||||||
|
import org.springframework.data.repository.PagingAndSortingRepository;
|
||||||
|
import org.springframework.data.repository.query.Param;
|
||||||
|
|
||||||
|
import com.example.demo.users.model.UserEntity;
|
||||||
|
|
||||||
|
public interface UserRepository
|
||||||
|
extends CrudRepository<UserEntity, Long>, PagingAndSortingRepository<UserEntity, Long> {
|
||||||
|
Optional<UserEntity> findByLoginIgnoreCase(String login);
|
||||||
|
|
||||||
|
@Query("SELECT CASE WHEN COUNT(uf) > 0 THEN true ELSE false END " +
|
||||||
|
"FROM UserEntity u JOIN u.userFilms uf " +
|
||||||
|
"WHERE u.id = :userId AND uf.id = :filmId")
|
||||||
|
boolean existsByUserIdAndFilmId(@Param("userId") Long userId, @Param("filmId") Long filmId);
|
||||||
|
}
|
187
src/main/java/com/example/demo/users/service/UserService.java
Normal file
187
src/main/java/com/example/demo/users/service/UserService.java
Normal file
@ -0,0 +1,187 @@
|
|||||||
|
package com.example.demo.users.service;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.stream.StreamSupport;
|
||||||
|
|
||||||
|
import org.springframework.data.domain.Page;
|
||||||
|
import org.springframework.data.domain.PageRequest;
|
||||||
|
import org.springframework.data.domain.Sort;
|
||||||
|
import org.springframework.security.core.Authentication;
|
||||||
|
import org.springframework.security.core.context.SecurityContextHolder;
|
||||||
|
import org.springframework.security.core.userdetails.UserDetails;
|
||||||
|
import org.springframework.security.core.userdetails.UserDetailsService;
|
||||||
|
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
||||||
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
|
import com.example.demo.core.configuration.Constants;
|
||||||
|
import com.example.demo.core.error.NotFoundException;
|
||||||
|
import com.example.demo.core.security.UserPrincipal;
|
||||||
|
import com.example.demo.films.model.FilmEntity;
|
||||||
|
import com.example.demo.films.repository.FilmRepository;
|
||||||
|
import com.example.demo.users.model.UserEntity;
|
||||||
|
import com.example.demo.users.model.UserRole;
|
||||||
|
import com.example.demo.users.repository.UserRepository;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class UserService implements UserDetailsService {
|
||||||
|
private final UserRepository repository;
|
||||||
|
private final FilmRepository filmRepository;
|
||||||
|
private final PasswordEncoder passwordEncoder;
|
||||||
|
|
||||||
|
public UserService(
|
||||||
|
UserRepository repository,
|
||||||
|
PasswordEncoder passwordEncoder,
|
||||||
|
FilmRepository filmRepository) {
|
||||||
|
this.repository = repository;
|
||||||
|
this.passwordEncoder = passwordEncoder;
|
||||||
|
this.filmRepository = filmRepository;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void checkLogin(Long id, String login) {
|
||||||
|
final Optional<UserEntity> existsUser = repository.findByLoginIgnoreCase(login);
|
||||||
|
if (existsUser.isPresent() && !existsUser.get().getId().equals(id)) {
|
||||||
|
throw new IllegalArgumentException(
|
||||||
|
String.format("User with login %s is already exists", login));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional(readOnly = true)
|
||||||
|
public List<UserEntity> getAll() {
|
||||||
|
List<UserEntity> list = StreamSupport.stream(repository.findAll().spliterator(), false).toList();
|
||||||
|
list.forEach(film -> film.getUserFilms().size());
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional(readOnly = true)
|
||||||
|
public Page<UserEntity> getAll(int page, int size) {
|
||||||
|
Page<UserEntity> list = repository.findAll(PageRequest.of(page, size, Sort.by("id")));
|
||||||
|
list.forEach(film -> film.getUserFilms().size());
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional(readOnly = true)
|
||||||
|
public UserEntity get(long id) {
|
||||||
|
UserEntity user = repository.findById(id)
|
||||||
|
.orElseThrow(() -> new NotFoundException(UserEntity.class, id));
|
||||||
|
user.getUserFilms().size();
|
||||||
|
return user;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional(readOnly = true)
|
||||||
|
public UserEntity getByLogin(String login) {
|
||||||
|
UserEntity user = repository.findByLoginIgnoreCase(login)
|
||||||
|
.orElseThrow(() -> new IllegalArgumentException("Invalid login"));
|
||||||
|
user.getUserFilms().size();
|
||||||
|
return user;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public UserEntity addFilm(long userId, Long filmId) {
|
||||||
|
UserEntity us = repository.findById(userId)
|
||||||
|
.orElseThrow(() -> new NotFoundException(UserEntity.class, userId));
|
||||||
|
|
||||||
|
FilmEntity fm = filmRepository.findById(filmId)
|
||||||
|
.orElseThrow(() -> new NotFoundException(FilmEntity.class, filmId));
|
||||||
|
us.addFilm(fm);
|
||||||
|
return repository.save(us);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public UserEntity deleteFilm(long userId, Long filmId) {
|
||||||
|
UserEntity us = repository.findById(userId)
|
||||||
|
.orElseThrow(() -> new NotFoundException(UserEntity.class, userId));
|
||||||
|
|
||||||
|
FilmEntity fm = filmRepository.findById(filmId)
|
||||||
|
.orElseThrow(() -> new NotFoundException(FilmEntity.class, filmId));
|
||||||
|
us.deleteFilm(fm);
|
||||||
|
return repository.save(us);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public UserEntity create(UserEntity entity) {
|
||||||
|
if (entity == null) {
|
||||||
|
throw new IllegalArgumentException("Entity is null");
|
||||||
|
}
|
||||||
|
checkLogin(null, entity.getLogin());
|
||||||
|
final String password = Optional.ofNullable(entity.getPassword()).orElse("");
|
||||||
|
entity.setPassword(
|
||||||
|
passwordEncoder.encode(
|
||||||
|
StringUtils.hasText(password.strip()) ? password : Constants.DEFAULT_PASSWORD));
|
||||||
|
entity.setRole(Optional.ofNullable(entity.getRole()).orElse(UserRole.USER));
|
||||||
|
repository.save(entity);
|
||||||
|
return repository.save(entity);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public UserEntity update(long id, UserEntity entity) {
|
||||||
|
final UserEntity existsEntity = get(id);
|
||||||
|
checkLogin(id, entity.getLogin());
|
||||||
|
existsEntity.setLogin(entity.getLogin());
|
||||||
|
repository.save(existsEntity);
|
||||||
|
return existsEntity;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public UserEntity delete(long id) {
|
||||||
|
final UserEntity existsEntity = get(id);
|
||||||
|
repository.delete(existsEntity);
|
||||||
|
return existsEntity;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional(readOnly = true)
|
||||||
|
public List<FilmEntity> getUserFilms(long id) {
|
||||||
|
UserEntity user = get(id);
|
||||||
|
List<FilmEntity> list = user.getUserFilms().stream().toList();
|
||||||
|
list.forEach(film -> film.getFilmGenres().size());
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public List<FilmEntity> deleteUserFilms(long id, List<Long> filmsIds) {
|
||||||
|
final UserEntity existsUser = get(id);
|
||||||
|
final List<FilmEntity> films = existsUser.getUserFilms().stream()
|
||||||
|
.filter(film -> filmsIds.contains(film.getId()))
|
||||||
|
.toList();
|
||||||
|
films.forEach(existsUser::deleteFilm);
|
||||||
|
repository.save(existsUser);
|
||||||
|
return films.stream()
|
||||||
|
.toList();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public List<FilmEntity> deleteAllUserFilms(long id) {
|
||||||
|
final UserEntity existsUser = get(id);
|
||||||
|
final List<FilmEntity> films = existsUser.getUserFilms().stream()
|
||||||
|
.toList();
|
||||||
|
for (FilmEntity filmEntity : films) {
|
||||||
|
existsUser.getUserFilms().remove(filmEntity);
|
||||||
|
}
|
||||||
|
repository.save(existsUser);
|
||||||
|
return films.stream()
|
||||||
|
.toList();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@Transactional(readOnly = true)
|
||||||
|
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
|
||||||
|
final UserEntity existsUser = getByLogin(username);
|
||||||
|
return new UserPrincipal(existsUser);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long getUserIdLong() {
|
||||||
|
Authentication aut = SecurityContextHolder.getContext().getAuthentication();
|
||||||
|
if (aut != null && aut.getPrincipal() instanceof UserDetails) {
|
||||||
|
return ((UserPrincipal) aut.getPrincipal()).getId();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean existByUserIdAndFilmId(Long userId, Long filmId) {
|
||||||
|
return repository.existsByUserIdAndFilmId(userId, filmId);
|
||||||
|
}
|
||||||
|
}
|
20
src/main/resources/application.properties
Normal file
20
src/main/resources/application.properties
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
# Server
|
||||||
|
spring.main.banner-mode=off
|
||||||
|
server.port=8080
|
||||||
|
|
||||||
|
# Logger settings
|
||||||
|
# Available levels are: TRACE, DEBUG, INFO, WARN, ERROR, FATAL, OFF
|
||||||
|
logging.level.com.example.demo=DEBUG
|
||||||
|
|
||||||
|
# JPA Settings
|
||||||
|
spring.datasource.url=jdbc:h2:file:./data
|
||||||
|
spring.datasource.username=sa
|
||||||
|
spring.datasource.password=password
|
||||||
|
spring.datasource.driver-class-name=org.h2.Driver
|
||||||
|
spring.jpa.hibernate.ddl-auto=create
|
||||||
|
spring.jpa.open-in-view=false
|
||||||
|
# spring.jpa.show-sql=true
|
||||||
|
# spring.jpa.properties.hibernate.format_sql=true
|
||||||
|
|
||||||
|
# H2 console
|
||||||
|
spring.h2.console.enabled=true
|
80
src/main/resources/public/css/style.css
Normal file
80
src/main/resources/public/css/style.css
Normal file
@ -0,0 +1,80 @@
|
|||||||
|
html,
|
||||||
|
body {
|
||||||
|
height: 100%;
|
||||||
|
background-color: #f0f0f0;
|
||||||
|
}
|
||||||
|
|
||||||
|
h1 {
|
||||||
|
font-size: 1.5em;
|
||||||
|
}
|
||||||
|
|
||||||
|
label {
|
||||||
|
color: #333;
|
||||||
|
}
|
||||||
|
|
||||||
|
h2 {
|
||||||
|
font-size: 1.25em;
|
||||||
|
}
|
||||||
|
|
||||||
|
h3 {
|
||||||
|
font-size: 1.1em;
|
||||||
|
}
|
||||||
|
|
||||||
|
td form {
|
||||||
|
margin: 0;
|
||||||
|
padding: 0;
|
||||||
|
margin-top: -.25em;
|
||||||
|
}
|
||||||
|
|
||||||
|
.button-fixed-width {
|
||||||
|
width: 150px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.button-link {
|
||||||
|
padding: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.invalid-feedback {
|
||||||
|
display: block;
|
||||||
|
}
|
||||||
|
|
||||||
|
.w-10 {
|
||||||
|
width: 10% !important;
|
||||||
|
}
|
||||||
|
|
||||||
|
.my-navbar {
|
||||||
|
background-color: #3c3c3c !important;
|
||||||
|
}
|
||||||
|
|
||||||
|
.my-navbar .link a:hover {
|
||||||
|
text-decoration: underline;
|
||||||
|
}
|
||||||
|
|
||||||
|
.my-navbar .logo {
|
||||||
|
width: 26px;
|
||||||
|
height: 26px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.my-footer {
|
||||||
|
background-color: #2c2c2c;
|
||||||
|
height: 32px;
|
||||||
|
color: rgba(255, 255, 255, 0.5);
|
||||||
|
}
|
||||||
|
|
||||||
|
.cart-image {
|
||||||
|
width: 3.1rem;
|
||||||
|
padding: 0.25rem;
|
||||||
|
border-radius: 0.5rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.cart-item {
|
||||||
|
height: auto;
|
||||||
|
}
|
||||||
|
|
||||||
|
.btn-primary {
|
||||||
|
background-color: #007bff;
|
||||||
|
}
|
||||||
|
|
||||||
|
.btn-primary:hover {
|
||||||
|
background-color: #0056b3;
|
||||||
|
}
|
5
src/main/resources/public/favicon.svg
Normal file
5
src/main/resources/public/favicon.svg
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor"
|
||||||
|
class="bi bi-camera-video" viewBox="0 0 16 16">
|
||||||
|
<path fill-rule="evenodd"
|
||||||
|
d="M0 5a2 2 0 0 1 2-2h7.5a2 2 0 0 1 1.983 1.738l3.11-1.382A1 1 0 0 1 16 4.269v7.462a1 1 0 0 1-1.406.913l-3.111-1.382A2 2 0 0 1 9.5 13H2a2 2 0 0 1-2-2zm11.5 5.175 3.5 1.556V4.269l-3.5 1.556zM2 4a1 1 0 0 0-1 1v6a1 1 0 0 0 1 1h7.5a1 1 0 0 0 1-1V5a1 1 0 0 0-1-1z" />
|
||||||
|
</svg>
|
After Width: | Height: | Size: 442 B |
BIN
src/main/resources/public/photo.jpg
Normal file
BIN
src/main/resources/public/photo.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 268 KiB |
68
src/main/resources/templates/default.html
Normal file
68
src/main/resources/templates/default.html
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="ru" data-bs-theme="dark" xmlns:th="http://www.thymeleaf.org"
|
||||||
|
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
|
||||||
|
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity6">
|
||||||
|
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8" />
|
||||||
|
<link rel="icon" type="image/svg+xml" href="/favicon.svg" />
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||||
|
<title layout:title-pattern="$LAYOUT_TITLE - $CONTENT_TITLE">Онлайн кинотеатр</title>
|
||||||
|
<script type="text/javascript" src="/webjars/bootstrap/5.3.3/dist/js/bootstrap.bundle.min.js"></script>
|
||||||
|
<link rel="stylesheet" href="/webjars/bootstrap/5.3.3/dist/css/bootstrap.min.css" />
|
||||||
|
<link rel="stylesheet" href="/webjars/bootstrap-icons/1.11.3/font/bootstrap-icons.min.css" />
|
||||||
|
<link rel="stylesheet" href="/css/style.css" />
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body class="h-100 d-flex flex-column">
|
||||||
|
<nav class="navbar navbar-expand-md my-navbar" data-bs-theme="dark">
|
||||||
|
<div class="container-fluid">
|
||||||
|
<a class="navbar-brand" href="/">
|
||||||
|
<i class=" d-inline-block align-top me-1 bi bi-camera-video"></i>
|
||||||
|
Онлайн кинотеатр
|
||||||
|
</a>
|
||||||
|
<th:block sec:authorize="isAuthenticated()" th:with="userName=${#authentication.name}">
|
||||||
|
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#main-navbar"
|
||||||
|
aria-controls="main-navbar" aria-expanded="false" aria-label="Toggle navigation">
|
||||||
|
<span class="navbar-toggler-icon"></span>
|
||||||
|
</button>
|
||||||
|
<div class="collapse navbar-collapse" id="main-navbar">
|
||||||
|
<ul class="navbar-nav me-auto link" th:with="activeLink=${#objects.nullSafe(servletPath, '')}">
|
||||||
|
<th:block sec:authorize="hasRole('ADMIN')">
|
||||||
|
<a class="nav-link" href="/admin/user"
|
||||||
|
th:classappend="${activeLink.startsWith('/admin/user') ? 'active' : ''}">
|
||||||
|
Пользователи
|
||||||
|
</a>
|
||||||
|
<a class="nav-link" href="/admin/lang"
|
||||||
|
th:classappend="${activeLink.startsWith('/admin/lang') ? 'active' : ''}">
|
||||||
|
Языки
|
||||||
|
</a>
|
||||||
|
<a class="nav-link" href="/admin/genre"
|
||||||
|
th:classappend="${activeLink.startsWith('/admin/genre') ? 'active' : ''}">
|
||||||
|
Жанры
|
||||||
|
</a>
|
||||||
|
<a class="nav-link" href="/admin/film"
|
||||||
|
th:classappend="${activeLink.startsWith('/admin/film') ? 'active' : ''}">
|
||||||
|
Фильмы
|
||||||
|
</a>
|
||||||
|
</th:block>
|
||||||
|
</ul>
|
||||||
|
<ul class="navbar-nav" th:if="${not #strings.isEmpty(userName)}">
|
||||||
|
<form th:action="@{/logout}" method="post">
|
||||||
|
<button type="submit" class="navbar-brand nav-link" onclick="return confirm('Вы уверены?')">
|
||||||
|
Выход ([[${userName}]])
|
||||||
|
</button>
|
||||||
|
</form>
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
</th:block>
|
||||||
|
</div>
|
||||||
|
</nav>
|
||||||
|
<main class="container-fluid p-2" layout:fragment="content">
|
||||||
|
</main>
|
||||||
|
<footer class="my-footer mt-auto d-flex flex-shrink-0 justify-content-center align-items-center">
|
||||||
|
Автор, [[${#dates.year(#dates.createNow())}]]
|
||||||
|
</footer>
|
||||||
|
</body>
|
||||||
|
|
||||||
|
</html>
|
37
src/main/resources/templates/error.html
Normal file
37
src/main/resources/templates/error.html
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="ru" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" layout:decorate="~{default}">
|
||||||
|
|
||||||
|
<head>
|
||||||
|
<title>Ошибка</title>
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
<main layout:fragment="content">
|
||||||
|
<ul class="list-group mb-2">
|
||||||
|
<th:block th:if="${#strings.isEmpty(message)}">
|
||||||
|
<li class="list-group-item">
|
||||||
|
Неизвестная ошибка
|
||||||
|
</li>
|
||||||
|
</th:block>
|
||||||
|
<th:block th:if="${not #strings.isEmpty(message)}">
|
||||||
|
<li class="list-group-item">
|
||||||
|
<strong>Ошибка:</strong> [[${message}]]
|
||||||
|
</li>
|
||||||
|
</th:block>
|
||||||
|
<th:block th:if="${not #strings.isEmpty(url)}">
|
||||||
|
<li class="list-group-item">
|
||||||
|
<strong>Адрес:</strong> [[${url}]]
|
||||||
|
</li>
|
||||||
|
<li class="list-group-item">
|
||||||
|
<strong>Класс исключения:</strong> [[${exception}]]
|
||||||
|
</li>
|
||||||
|
<li class="list-group-item">
|
||||||
|
[[${method}]] ([[${file}]]:[[${line}]])
|
||||||
|
</li>
|
||||||
|
</th:block>
|
||||||
|
</ul>
|
||||||
|
<a class="btn btn-primary button-fixed-width" href="/">На главную</a>
|
||||||
|
</main>
|
||||||
|
</body>
|
||||||
|
|
||||||
|
</html>
|
58
src/main/resources/templates/film-admin.html
Normal file
58
src/main/resources/templates/film-admin.html
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="ru" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" layout:decorate="~{default}">
|
||||||
|
|
||||||
|
<head>
|
||||||
|
<title>Список фильмов</title>
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
<main layout:fragment="content">
|
||||||
|
<th:block th:switch="${items.size()}">
|
||||||
|
<h2 th:case="0">Данные отсутствуют</h2>
|
||||||
|
<th:block th:case="*">
|
||||||
|
<div>
|
||||||
|
<a href="/admin/film/edit/" class="btn btn-primary">Добавить фильм</a>
|
||||||
|
</div>
|
||||||
|
<table class="table">
|
||||||
|
<caption></caption>
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th scope="col" class="w-10">ID</th>
|
||||||
|
<th scope="col" class="w-auto">Список фильмов</th>
|
||||||
|
<th:block sec:authorize="hasRole('ADMIN')">
|
||||||
|
<th scope="col" class="w-10"></th>
|
||||||
|
<th scope="col" class="w-10"></th>
|
||||||
|
</th:block>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
<tr th:each="film : ${items}">
|
||||||
|
<th scope="row" th:text="${film.id}"></th>
|
||||||
|
<td th:text="${film.name}"></td>
|
||||||
|
<th:block sec:authorize="hasRole('ADMIN')">
|
||||||
|
<td>
|
||||||
|
<form th:action="@{/admin/film/edit/{id}(id=${film.id})}" method="get">
|
||||||
|
<button type="submit" class="btn btn-link button-link">Редактировать</button>
|
||||||
|
</form>
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
<form th:action="@{/admin/film/delete/{id}(id=${film.id})}"
|
||||||
|
method="post">
|
||||||
|
<button type="submit" class="btn btn-link button-link"
|
||||||
|
onclick="return confirm('Вы уверены?')">Удалить</button>
|
||||||
|
</form>
|
||||||
|
</td>
|
||||||
|
</th:block>
|
||||||
|
</tr>
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</th:block>
|
||||||
|
</th:block>
|
||||||
|
<th:block th:replace="~{ pagination :: pagination (
|
||||||
|
url=${'admin/film'},
|
||||||
|
totalPages=${totalPages},
|
||||||
|
currentPage=${currentPage}) }" />
|
||||||
|
</main>
|
||||||
|
</body>
|
||||||
|
|
||||||
|
</html>
|
51
src/main/resources/templates/film-edit.html
Normal file
51
src/main/resources/templates/film-edit.html
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="ru" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" layout:decorate="~{default}">
|
||||||
|
|
||||||
|
<head>
|
||||||
|
<title>Редакторовать списки фильмов</title>
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
<main layout:fragment="content">
|
||||||
|
<form action="#" th:action="@{/admin/film/edit/{id}(id=${film.id})}" th:object="${film}"
|
||||||
|
method="post">
|
||||||
|
<div class="mb-3">
|
||||||
|
<label for="id" class="form-label">ID</label>
|
||||||
|
<input type="text" th:value="*{id}" id="id" class="form-control" readonly disabled>
|
||||||
|
</div>
|
||||||
|
<div class="mb-3">
|
||||||
|
<label for="name" class="form-label">Название</label>
|
||||||
|
<input lang="text" th:field="*{name}" id="name" class="form-control">
|
||||||
|
<div th:if="${#fields.hasErrors('name')}" th:errors="*{name}" class="invalid-feedback"></div>
|
||||||
|
</div>
|
||||||
|
<div class="mb-3">
|
||||||
|
<label for="description" class="form-label">Описание</label>
|
||||||
|
<input lang="text" th:field="*{description}" id="description" class="form-control">
|
||||||
|
<div th:if="${#fields.hasErrors('description')}" th:errors="*{description}" class="invalid-feedback"></div>
|
||||||
|
</div>
|
||||||
|
<div class="mb-2">
|
||||||
|
<label for="langId" class="form-label">Язык</label>
|
||||||
|
<select th:field="*{langId}" id="langId" class="form-select">
|
||||||
|
<option selected value="">Укажите язык</option>
|
||||||
|
<option th:each="lang : ${langs}" th:value="${lang.id}">[[${lang.name}]]</option>
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
<div class="mb-2">
|
||||||
|
<label class="form-label">Жанр</label>
|
||||||
|
<div class="form-check" th:each="genre : ${genresCheck}">
|
||||||
|
<input class="form-check-input" th:field="*{genres}" th:value="${genre.id}" type="checkbox"
|
||||||
|
th:for="${id}">
|
||||||
|
<label class="form-check-label" th:field="*{genres}" for="" th:for="${id}">
|
||||||
|
[[${genre.name}]]
|
||||||
|
</label>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="mb-3 d-flex flex-row">
|
||||||
|
<button class="btn btn-primary me-2 button-fixed-width" type="submit">Сохранить</button>
|
||||||
|
<a class="btn btn-secondary button-fixed-width" href="/admin/film">Отмена</a>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</main>
|
||||||
|
</body>
|
||||||
|
|
||||||
|
</html>
|
36
src/main/resources/templates/film-like.html
Normal file
36
src/main/resources/templates/film-like.html
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="ru" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" layout:decorate="~{default}">
|
||||||
|
|
||||||
|
<head>
|
||||||
|
<title>Понравившиеся</title>
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
<main layout:fragment="content">
|
||||||
|
<th:block th:switch="${items.size()}">
|
||||||
|
<h2 th:case="0">Данные отсутствуют</h2>
|
||||||
|
<th:block th:case="*">
|
||||||
|
<div style="display: flex; flex-wrap: wrap; justify-content: center; gap: 20px; padding: 20px; background-color: #f0f0f0;">
|
||||||
|
<div th:each="like : ${items}" th:if="${like.isLiked}" style="padding: 20px; background-color: white; border-radius: 15px; box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1); width: 300px; display: flex; flex-direction: column; align-items: center; text-align: center;">
|
||||||
|
<div th:text="${like.name}" style="color: #333; font-size: 1.2em; font-weight: bold; margin-bottom: 10px;"></div>
|
||||||
|
<div>
|
||||||
|
<img src="/photo.jpg" alt="phoo" width="100%" style="border-radius: 15px; margin-bottom: 10px;" />
|
||||||
|
<div th:text="${languages.containsKey(like.langId)} ? ${languages[like.langId]} : 'Неизвестный язык'" style="color: #666; margin-bottom: 10px;"></div>
|
||||||
|
<div style="display: flex; justify-content: center;">
|
||||||
|
<form th:action="@{/film/dislike2/{id}(id=${like.id}, page=${page})}" method="post">
|
||||||
|
<button type="submit" class="btn btn-dark" style="padding: 10px 20px; border: none; border-radius: 5px; background-color: #dc3545; color: white; cursor: pointer;" onclick="return confirm('Вы уверены?')">Удалить</button>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</th:block>
|
||||||
|
<th:block th:replace="~{ pagination :: pagination (
|
||||||
|
url=${'film/like'},
|
||||||
|
totalPages=${totalPages},
|
||||||
|
currentPage=${currentPage}) }" />
|
||||||
|
</th:block>
|
||||||
|
</main>
|
||||||
|
</body>
|
||||||
|
|
||||||
|
</html>
|
78
src/main/resources/templates/film.html
Normal file
78
src/main/resources/templates/film.html
Normal file
@ -0,0 +1,78 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="ru" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" layout:decorate="~{default}">
|
||||||
|
|
||||||
|
<head>
|
||||||
|
<title>Список фильмов</title>
|
||||||
|
<style>
|
||||||
|
.search-form {
|
||||||
|
display: flex;
|
||||||
|
justify-content: center;
|
||||||
|
margin-bottom: 20px;
|
||||||
|
}
|
||||||
|
.search-form input {
|
||||||
|
padding: 10px;
|
||||||
|
font-size: 1em;
|
||||||
|
border: 1px solid #ccc;
|
||||||
|
border-radius: 5px;
|
||||||
|
margin-right: 10px;
|
||||||
|
}
|
||||||
|
.search-form button {
|
||||||
|
padding: 10px 20px;
|
||||||
|
font-size: 1em;
|
||||||
|
border: none;
|
||||||
|
border-radius: 5px;
|
||||||
|
background-color: #007bff;
|
||||||
|
color: white;
|
||||||
|
cursor: pointer;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
<main layout:fragment="content">
|
||||||
|
<form class="search-form" th:action="@{/list/film}" method="get">
|
||||||
|
<input type="text" style="width:50%;" name="query" th:value="${query}" placeholder="Поиск по названию фильма..." />
|
||||||
|
<button type="submit">Поиск</button>
|
||||||
|
</form>
|
||||||
|
<th:block th:switch="${items.size()}">
|
||||||
|
<h2 th:case="0" style="color: #333; text-align:center;">Данные отсутствуют</h2>
|
||||||
|
<th:block th:case="*">
|
||||||
|
<div style="display: flex; flex-wrap: wrap; justify-content: center; gap: 20px; padding: 20px; background-color: #f0f0f0;">
|
||||||
|
<div th:each="film : ${items}" style="padding: 20px; background-color: white; border-radius: 15px; box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1); width: 300px; display: flex; flex-direction: column; align-items: center; text-align: center;">
|
||||||
|
<div th:text="${film.name}" style="color: #333; font-size: 1.2em; font-weight: bold; margin-bottom: 10px;"></div>
|
||||||
|
<div>
|
||||||
|
<img src="/photo.jpg" alt="phoo" width="100%" style="border-radius: 15px; margin-bottom: 10px;" />
|
||||||
|
<div th:text="${languages.containsKey(film.langId)} ? ${languages[film.langId]} : 'Неизвестный язык'" style="color: #666; margin-bottom: 10px;"></div>
|
||||||
|
<div style="display: flex; justify-content: center;">
|
||||||
|
<form th:action="${film.isLiked} ? @{/film/dislike1/{id}(id=${film.id}, page=${page})} : @{/film/like/{filmId}(filmId=${film.id}, page=${page})}" method="post">
|
||||||
|
<button type="submit" class="btn btn-dark text-big" style="padding: 10px 20px; border: none; border-radius: 5px; background-color: #007bff; color: white; cursor: pointer; font-size: 1em;">
|
||||||
|
<span th:if="${film.isLiked}">
|
||||||
|
<i class="bi bi-heart-fill"></i>
|
||||||
|
</span>
|
||||||
|
<span th:unless="${film.isLiked}">
|
||||||
|
<i class="bi bi-heart"></i>
|
||||||
|
</span>
|
||||||
|
</button>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div sec:authorize="hasRole('ADMIN')" style="margin-top: 10px;">
|
||||||
|
<form th:action="@{/admin/film/edit/{id}(id=${film.id}, page=${page})}" method="get" style="display: flex; justify-content: center; margin-bottom: 10px;">
|
||||||
|
<button type="submit" class="btn btn-dark" style="padding: 10px 20px; border: none; border-radius: 5px; background-color: #28a745; color: white; cursor: pointer;">Редактировать</button>
|
||||||
|
</form>
|
||||||
|
<form th:action="@{/admin/film/delete/{id}(id=${film.id}, page=${page})}" method="post" style="display: flex; justify-content: center;">
|
||||||
|
<button type="submit" class="btn btn-dark" style="padding: 10px 20px; border: none; border-radius: 5px; background-color: #dc3545; color: white; cursor: pointer;" onclick="return confirm('Вы уверены?')">Удалить</button>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</th:block>
|
||||||
|
</th:block>
|
||||||
|
<th:block th:replace="~{ pagination :: pagination (
|
||||||
|
url=${'list/film'},
|
||||||
|
totalPages=${totalPages},
|
||||||
|
currentPage=${currentPage}) }" />
|
||||||
|
</main>
|
||||||
|
</body>
|
||||||
|
|
||||||
|
</html>
|
28
src/main/resources/templates/genre-edit.html
Normal file
28
src/main/resources/templates/genre-edit.html
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="ru" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" layout:decorate="~{default}">
|
||||||
|
|
||||||
|
<head>
|
||||||
|
<title>Редакторовать жанр</title>
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
<main layout:fragment="content">
|
||||||
|
<form action="#" th:action="@{/admin/genre/edit/{id}(id=${genre.id})}" th:object="${genre}" method="post">
|
||||||
|
<div class="mb-3">
|
||||||
|
<label for="id" class="form-label">ID</label>
|
||||||
|
<input genre="text" th:value="*{id}" id="id" class="form-control" readonly disabled>
|
||||||
|
</div>
|
||||||
|
<div class="mb-3">
|
||||||
|
<label for="name" class="form-label">Жанр</label>
|
||||||
|
<input genre="text" th:field="*{name}" id="name" class="form-control">
|
||||||
|
<div th:if="${#fields.hasErrors('name')}" th:errors="*{name}" class="invalid-feedback"></div>
|
||||||
|
</div>
|
||||||
|
<div class="mb-3 d-flex flex-row">
|
||||||
|
<button class="btn btn-primary me-2 button-fixed-width" genre="submit">Сохранить</button>
|
||||||
|
<a class="btn btn-secondary button-fixed-width" href="/admin/genre">Отмена</a>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</main>
|
||||||
|
</body>
|
||||||
|
|
||||||
|
</html>
|
53
src/main/resources/templates/genre.html
Normal file
53
src/main/resources/templates/genre.html
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="ru" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" layout:decorate="~{default}">
|
||||||
|
|
||||||
|
<head>
|
||||||
|
<title>Жанры фильмов</title>
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
<main layout:fragment="content">
|
||||||
|
<th:block th:switch="${items.size()}">
|
||||||
|
<h2 th:case="0">Данные отсутствуют</h2>
|
||||||
|
<th:block th:case="*">
|
||||||
|
<div>
|
||||||
|
<a href="/admin/genre/edit/" class="btn btn-primary">Добавить жанр</a>
|
||||||
|
</div>
|
||||||
|
<table class="table">
|
||||||
|
<caption></caption>
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th scope="col" class="w-10">ID</th>
|
||||||
|
<th scope="col" class="w-auto">Жанр</th>
|
||||||
|
<th scope="col" class="w-10"></th>
|
||||||
|
<th scope="col" class="w-10"></th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
<tr th:each="genre : ${items}">
|
||||||
|
<th scope="row" th:text="${genre.id}"></th>
|
||||||
|
<td th:text="${genre.name}"></td>
|
||||||
|
<td>
|
||||||
|
<form th:action="@{/admin/genre/edit/{id}(id=${genre.id})}" method="get">
|
||||||
|
<button genre="submit" class="btn btn-link button-link">Редактировать</button>
|
||||||
|
</form>
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
<form th:action="@{/admin/genre/delete/{id}(id=${genre.id})}" method="post">
|
||||||
|
<button genre="submit" class="btn btn-link button-link"
|
||||||
|
onclick="return confirm('Вы уверены?')">Удалить</button>
|
||||||
|
</form>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</th:block>
|
||||||
|
</th:block>
|
||||||
|
<th:block th:replace="~{ pagination :: pagination (
|
||||||
|
url=${'admin/genre'},
|
||||||
|
totalPages=${totalPages},
|
||||||
|
currentPage=${currentPage}) }" />
|
||||||
|
</main>
|
||||||
|
</body>
|
||||||
|
|
||||||
|
</html>
|
19
src/main/resources/templates/index.html
Normal file
19
src/main/resources/templates/index.html
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="ru" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" layout:decorate="~{default}">
|
||||||
|
|
||||||
|
<head>
|
||||||
|
<title>Главная страница</title>
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
<main layout:fragment="content">
|
||||||
|
<div>
|
||||||
|
<a th:href="@{/list/film}" class="btn btn-primary">Все фильмы</a>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<a th:href="@{/film/like}" class="btn btn-primary">Понравившиеся фильмы</a>
|
||||||
|
</div>
|
||||||
|
</main>
|
||||||
|
</body>
|
||||||
|
|
||||||
|
</html>
|
28
src/main/resources/templates/lang-edit.html
Normal file
28
src/main/resources/templates/lang-edit.html
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="ru" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" layout:decorate="~{default}">
|
||||||
|
|
||||||
|
<head>
|
||||||
|
<title>Редакторовать язык</title>
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
<main layout:fragment="content">
|
||||||
|
<form action="#" th:action="@{/admin/lang/edit/{id}(id=${lang.id})}" th:object="${lang}" method="post">
|
||||||
|
<div class="mb-3">
|
||||||
|
<label for="id" class="form-label">ID</label>
|
||||||
|
<input lang="text" th:value="*{id}" id="id" class="form-control" readonly disabled>
|
||||||
|
</div>
|
||||||
|
<div class="mb-3">
|
||||||
|
<label for="name" class="form-label">Язык</label>
|
||||||
|
<input lang="text" th:field="*{name}" id="name" class="form-control">
|
||||||
|
<div th:if="${#fields.hasErrors('name')}" th:errors="*{name}" class="invalid-feedback"></div>
|
||||||
|
</div>
|
||||||
|
<div class="mb-3 d-flex flex-row">
|
||||||
|
<button class="btn btn-primary me-2 button-fixed-width" lang="submit">Сохранить</button>
|
||||||
|
<a class="btn btn-secondary button-fixed-width" href="/admin/lang">Отмена</a>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</main>
|
||||||
|
</body>
|
||||||
|
|
||||||
|
</html>
|
53
src/main/resources/templates/lang.html
Normal file
53
src/main/resources/templates/lang.html
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="ru" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" layout:decorate="~{default}">
|
||||||
|
|
||||||
|
<head>
|
||||||
|
<title>Языки фильмов</title>
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
<main layout:fragment="content">
|
||||||
|
<th:block th:switch="${items.size()}">
|
||||||
|
<h2 th:case="0">Данные отсутствуют</h2>
|
||||||
|
<th:block th:case="*">
|
||||||
|
<div>
|
||||||
|
<a href="/admin/lang/edit/" class="btn btn-primary">Добавить язык</a>
|
||||||
|
</div>
|
||||||
|
<table class="table">
|
||||||
|
<caption></caption>
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th scope="col" class="w-10">ID</th>
|
||||||
|
<th scope="col" class="w-auto">Язык</th>
|
||||||
|
<th scope="col" class="w-10"></th>
|
||||||
|
<th scope="col" class="w-10"></th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
<tr th:each="lang : ${items}">
|
||||||
|
<th scope="row" th:text="${lang.id}"></th>
|
||||||
|
<td th:text="${lang.name}"></td>
|
||||||
|
<td>
|
||||||
|
<form th:action="@{/admin/lang/edit/{id}(id=${lang.id})}" method="get">
|
||||||
|
<button lang="submit" class="btn btn-link button-link">Редактировать</button>
|
||||||
|
</form>
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
<form th:action="@{/admin/lang/delete/{id}(id=${lang.id})}" method="post">
|
||||||
|
<button lang="submit" class="btn btn-link button-link"
|
||||||
|
onclick="return confirm('Вы уверены?')">Удалить</button>
|
||||||
|
</form>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</th:block>
|
||||||
|
</th:block>
|
||||||
|
<th:block th:replace="~{ pagination :: pagination (
|
||||||
|
url=${'admin/lang'},
|
||||||
|
totalPages=${totalPages},
|
||||||
|
currentPage=${currentPage}) }" />
|
||||||
|
</main>
|
||||||
|
</body>
|
||||||
|
|
||||||
|
</html>
|
44
src/main/resources/templates/login.html
Normal file
44
src/main/resources/templates/login.html
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="ru" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" layout:decorate="~{default}">
|
||||||
|
|
||||||
|
<head>
|
||||||
|
<title>Вход</title>
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
<main layout:fragment="content">
|
||||||
|
<form action="#" th:action="@{/login}" method="post">
|
||||||
|
<div th:if="${param.error}" class="alert alert-danger">
|
||||||
|
Неверный логин или пароль
|
||||||
|
</div>
|
||||||
|
<div th:if="${param.logout}" class="alert alert-success">
|
||||||
|
Выход успешно произведен
|
||||||
|
</div>
|
||||||
|
<div th:if="${param.signup}" class="alert alert-success">
|
||||||
|
Пользователь успешно создан
|
||||||
|
</div>
|
||||||
|
<div class="mb-3">
|
||||||
|
<label for="username" class="form-label">Имя пользователя</label>
|
||||||
|
<input type="text" id="username" name="username" class="form-control" required minlength="3"
|
||||||
|
maxlength="20">
|
||||||
|
</div>
|
||||||
|
<div class="mb-3">
|
||||||
|
<label for="password" class="form-label">Пароль</label>
|
||||||
|
<input type="password" id="password" name="password" class="form-control" required minlength="3"
|
||||||
|
maxlength="20">
|
||||||
|
</div>
|
||||||
|
<div class="form-check mb-3">
|
||||||
|
<input class="form-check-input" type="checkbox" id="remember-me" name="remember-me" checked>
|
||||||
|
<label class="form-check-label" for="remember-me">Запомнить меня</label>
|
||||||
|
</div>
|
||||||
|
<div class="mb-3 d-flex flex-row">
|
||||||
|
<button class="btn btn-primary me-2 button-fixed-width" type="submit">Войти</button>
|
||||||
|
<a class="btn btn-secondary button-fixed-width" href="/signup">Регистрация</a>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</main>
|
||||||
|
</body>
|
||||||
|
|
||||||
|
</html>
|
||||||
|
|
||||||
|
</html>
|
51
src/main/resources/templates/pagination.html
Normal file
51
src/main/resources/templates/pagination.html
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html xmlns:th="http://www.thymeleaf.org">
|
||||||
|
|
||||||
|
<body>
|
||||||
|
|
||||||
|
<th:block th:fragment="pagination (url, totalPages, currentPage)">
|
||||||
|
<nav th:if="${totalPages > 1}" th:with="
|
||||||
|
maxPage=2,
|
||||||
|
currentPage=${currentPage + 1}">
|
||||||
|
<ul class="pagination justify-content-center"
|
||||||
|
th:with="
|
||||||
|
seqFrom=${currentPage - maxPage < 1 ? 1 : currentPage - maxPage},
|
||||||
|
seqTo=${currentPage + maxPage > totalPages ? totalPages : currentPage + maxPage}">
|
||||||
|
<th:block th:if="${currentPage > maxPage + 1}">
|
||||||
|
<li class="page-item">
|
||||||
|
<a class="page-link" aria-label="Previous" th:href="@{/{url}?page=0(url=${url})}">
|
||||||
|
<span aria-hidden="true">«</span>
|
||||||
|
</a>
|
||||||
|
</li>
|
||||||
|
<li class="page-item disabled">
|
||||||
|
<span class="page-link" aria-label="Previous">
|
||||||
|
<span aria-hidden="true">…</span>
|
||||||
|
</span>
|
||||||
|
</li>
|
||||||
|
</th:block>
|
||||||
|
<li class="page-item" th:each="page : ${#numbers.sequence(seqFrom, seqTo)}"
|
||||||
|
th:classappend="${page == currentPage} ? 'active' : ''">
|
||||||
|
<a class=" page-link" th:href="@{/{url}?page={page}(url=${url},page=${page - 1})}">
|
||||||
|
<span th:text="${page}" />
|
||||||
|
</a>
|
||||||
|
</li>
|
||||||
|
<th:block th:if="${currentPage < totalPages - maxPage}">
|
||||||
|
<li class="page-item disabled">
|
||||||
|
<span class="page-link" aria-label="Previous">
|
||||||
|
<span aria-hidden="true">…</span>
|
||||||
|
</span>
|
||||||
|
</li>
|
||||||
|
<li class="page-item">
|
||||||
|
<a class="page-link" aria-label="Next"
|
||||||
|
th:href="@{/{url}?page={page}(url=${url},page=${totalPages - 1})}">
|
||||||
|
<span aria-hidden="true">»</span>
|
||||||
|
</a>
|
||||||
|
</li>
|
||||||
|
</th:block>
|
||||||
|
</ul>
|
||||||
|
</nav>
|
||||||
|
</th:block>
|
||||||
|
|
||||||
|
</body>
|
||||||
|
|
||||||
|
</html>
|
57
src/main/resources/templates/profile.html
Normal file
57
src/main/resources/templates/profile.html
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="ru" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" layout:decorate="~{default}">
|
||||||
|
|
||||||
|
<head>
|
||||||
|
<title>Главная страница</title>
|
||||||
|
<style>
|
||||||
|
body {
|
||||||
|
background-color: #f0f0f0;
|
||||||
|
margin: 0;
|
||||||
|
padding: 0;
|
||||||
|
display: flex;
|
||||||
|
}
|
||||||
|
main {
|
||||||
|
background-color: white;
|
||||||
|
padding: 40px;
|
||||||
|
border-radius: 15px;
|
||||||
|
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
|
||||||
|
text-align: center;
|
||||||
|
}
|
||||||
|
.btn {
|
||||||
|
display: inline-block;
|
||||||
|
padding: 15px 30px;
|
||||||
|
margin: 10px;
|
||||||
|
font-size: 1.2em;
|
||||||
|
border: none;
|
||||||
|
border-radius: 5px;
|
||||||
|
color: white;
|
||||||
|
cursor: pointer;
|
||||||
|
text-decoration: none;
|
||||||
|
}
|
||||||
|
h1 {
|
||||||
|
color: #333;
|
||||||
|
font-size: 2em;
|
||||||
|
margin-bottom: 20px;
|
||||||
|
}
|
||||||
|
p {
|
||||||
|
color: #555;
|
||||||
|
font-size: 1.2em;
|
||||||
|
margin-bottom: 30px;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
<main layout:fragment="content">
|
||||||
|
<h1>Лучший сервис поиска фильмов в стране!</h1>
|
||||||
|
<p>Просмотрите наши фильмы и найдите свои любимые.</p>
|
||||||
|
<div>
|
||||||
|
<a th:href="@{/list/film}" class="btn btn-primary">Все фильмы</a>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<a th:href="@{/film/like}" class="btn btn-primary">Понравившиеся фильмы</a>
|
||||||
|
</div>
|
||||||
|
</main>
|
||||||
|
</body>
|
||||||
|
|
||||||
|
</html>
|
37
src/main/resources/templates/signup.html
Normal file
37
src/main/resources/templates/signup.html
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="ru" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" layout:decorate="~{default}">
|
||||||
|
|
||||||
|
<head>
|
||||||
|
<title>Вход</title>
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
<main layout:fragment="content">
|
||||||
|
<form action="#" th:action="@{/signup}" th:object="${user}" method="post">
|
||||||
|
<div class="mb-3">
|
||||||
|
<label for="login" class="form-label">Имя пользователя</label>
|
||||||
|
<input type="text" th:field="*{login}" id="login" class="form-control">
|
||||||
|
<div th:if="${#fields.hasErrors('login')}" th:errors="*{login}" class="invalid-feedback"></div>
|
||||||
|
</div>
|
||||||
|
<div class="mb-3">
|
||||||
|
<label for="password" class="form-label">Пароль</label>
|
||||||
|
<input type="password" th:field="*{password}" id="password" class="form-control">
|
||||||
|
<div th:if="${#fields.hasErrors('password')}" th:errors="*{password}" class="invalid-feedback"></div>
|
||||||
|
</div>
|
||||||
|
<div class="mb-3">
|
||||||
|
<label for="passwordConfirm" class="form-label">Пароль (подтверждение)</label>
|
||||||
|
<input type="password" th:field="*{passwordConfirm}" id="passwordConfirm" class="form-control">
|
||||||
|
<div th:if="${#fields.hasErrors('passwordConfirm')}" th:errors="*{passwordConfirm}"
|
||||||
|
class="invalid-feedback"></div>
|
||||||
|
</div>
|
||||||
|
<div class="mb-3 d-flex flex-row">
|
||||||
|
<button class="btn btn-primary me-2 button-fixed-width" type="submit">Регистрация</button>
|
||||||
|
<a class="btn btn-secondary button-fixed-width" href="/">Отмена</a>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</main>
|
||||||
|
</body>
|
||||||
|
|
||||||
|
</html>
|
||||||
|
|
||||||
|
</html>
|
29
src/main/resources/templates/user-edit.html
Normal file
29
src/main/resources/templates/user-edit.html
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="ru" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" layout:decorate="~{default}">
|
||||||
|
|
||||||
|
<head>
|
||||||
|
<title>Редакторовать пользователя</title>
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
<main layout:fragment="content">
|
||||||
|
<form action="#" th:action="@{/admin/user/edit/{id}(id=${user.id},page=${page})}" th:object="${user}"
|
||||||
|
method="post">
|
||||||
|
<div class="mb-3">
|
||||||
|
<label for="id" class="form-label">ID</label>
|
||||||
|
<input type="text" th:value="*{id}" id="id" class="form-control" readonly disabled>
|
||||||
|
</div>
|
||||||
|
<div class="mb-3">
|
||||||
|
<label for="login" class="form-label">Имя пользователя</label>
|
||||||
|
<input type="text" th:field="*{login}" id="login" class="form-control">
|
||||||
|
<div th:if="${#fields.hasErrors('login')}" th:errors="*{login}" class="invalid-feedback"></div>
|
||||||
|
</div>
|
||||||
|
<div class="mb-3 d-flex flex-row">
|
||||||
|
<button class="btn btn-primary me-2 button-fixed-width" type="submit">Сохранить</button>
|
||||||
|
<a class="btn btn-secondary button-fixed-width" th:href="@{/admin/user(page=${page})}">Отмена</a>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</main>
|
||||||
|
</body>
|
||||||
|
|
||||||
|
</html>
|
55
src/main/resources/templates/user.html
Normal file
55
src/main/resources/templates/user.html
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="ru" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" layout:decorate="~{default}">
|
||||||
|
|
||||||
|
<head>
|
||||||
|
<title>Пользователи</title>
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
<main layout:fragment="content">
|
||||||
|
<th:block th:switch="${items.size()}">
|
||||||
|
<h2 th:case="0">Данные отсутствуют</h2>
|
||||||
|
<th:block th:case="*">
|
||||||
|
<div>
|
||||||
|
<a th:href="@{/admin/user/edit/(page=${page})}" class="btn btn-primary">Добавить пользователя</a>
|
||||||
|
</div>
|
||||||
|
<table class="table">
|
||||||
|
<caption></caption>
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th scope="col" class="w-10">ID</th>
|
||||||
|
<th scope="col" class="w-auto">Имя пользователя</th>
|
||||||
|
<th scope="col" class="w-10"></th>
|
||||||
|
<th scope="col" class="w-10"></th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
<tr th:each="user : ${items}">
|
||||||
|
<th scope="row" th:text="${user.id}"></th>
|
||||||
|
<td th:text="${user.login}"></td>
|
||||||
|
<td>
|
||||||
|
<form th:action="@{/admin/user/edit/{id}(id=${user.id})}" method="get">
|
||||||
|
<input type="hidden" th:name="page" th:value="${page}">
|
||||||
|
<button type="submit" class="btn btn-link button-link">Редактировать</button>
|
||||||
|
</form>
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
<form th:action="@{/admin/user/delete/{id}(id=${user.id})}" method="post">
|
||||||
|
<input type="hidden" th:name="page" th:value="${page}">
|
||||||
|
<button type="submit" class="btn btn-link button-link"
|
||||||
|
onclick="return confirm('Вы уверены?')">Удалить</button>
|
||||||
|
</form>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</th:block>
|
||||||
|
<th:block th:replace="~{ pagination :: pagination (
|
||||||
|
url=${'admin/user'},
|
||||||
|
totalPages=${totalPages},
|
||||||
|
currentPage=${currentPage}) }" />
|
||||||
|
</th:block>
|
||||||
|
</main>
|
||||||
|
</body>
|
||||||
|
|
||||||
|
</html>
|
121
src/test/java/com/example/demo/FilmGenreServiceTests.java
Normal file
121
src/test/java/com/example/demo/FilmGenreServiceTests.java
Normal file
@ -0,0 +1,121 @@
|
|||||||
|
package com.example.demo;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.AfterEach;
|
||||||
|
import org.junit.jupiter.api.Assertions;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
|
||||||
|
import org.junit.jupiter.api.Order;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.TestMethodOrder;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.dao.DataIntegrityViolationException;
|
||||||
|
|
||||||
|
import com.example.demo.films.model.FilmEntity;
|
||||||
|
import com.example.demo.films.service.FilmService;
|
||||||
|
import com.example.demo.genres.model.GenreEntity;
|
||||||
|
import com.example.demo.genres.service.GenreService;
|
||||||
|
import com.example.demo.langs.model.LangEntity;
|
||||||
|
import com.example.demo.langs.service.LangService;
|
||||||
|
import com.example.demo.users.service.UserService;
|
||||||
|
|
||||||
|
@SpringBootTest
|
||||||
|
@TestMethodOrder(OrderAnnotation.class)
|
||||||
|
class FilmGenreServiceTests {
|
||||||
|
@Autowired
|
||||||
|
private GenreService genreService;
|
||||||
|
@Autowired
|
||||||
|
private LangService langService;
|
||||||
|
@Autowired
|
||||||
|
private FilmService filmService;
|
||||||
|
@Autowired
|
||||||
|
private UserService userService;
|
||||||
|
|
||||||
|
private GenreEntity genre1;
|
||||||
|
private GenreEntity genre2;
|
||||||
|
private GenreEntity genre3;
|
||||||
|
private GenreEntity genre4;
|
||||||
|
|
||||||
|
private LangEntity lang1;
|
||||||
|
private LangEntity lang2;
|
||||||
|
|
||||||
|
private FilmEntity film1;
|
||||||
|
private FilmEntity film2;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void createData() {
|
||||||
|
removeData();
|
||||||
|
|
||||||
|
genre1 = genreService.create(new GenreEntity("ТРЫЛЛЕР"));
|
||||||
|
genre2 = genreService.create(new GenreEntity("ХоРРоР"));
|
||||||
|
genre3 = genreService.create(new GenreEntity("Камедия"));
|
||||||
|
genre4 = genreService.create(new GenreEntity("Камедeeeeeeeия"));
|
||||||
|
|
||||||
|
lang1 = langService.create(new LangEntity("Русссский"));
|
||||||
|
lang2 = langService.create(new LangEntity("Не руссссский"));
|
||||||
|
|
||||||
|
film1 = filmService.create(new FilmEntity("Гарри", "ПОоООООООООООтер", lang1));
|
||||||
|
film2 = filmService.create(new FilmEntity("Алиса в стране", "Какой-то", lang2));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterEach
|
||||||
|
void removeData() {
|
||||||
|
userService.getAll().forEach(item -> userService.delete(item.getId()));
|
||||||
|
filmService.getAll().forEach(item -> filmService.delete(item.getId()));
|
||||||
|
genreService.getAll().forEach(item -> genreService.delete(item.getId()));
|
||||||
|
langService.getAll().forEach(item -> langService.delete(item.getId()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Order(1)
|
||||||
|
void createNullableFilmTest() {
|
||||||
|
final FilmEntity nullableType = new FilmEntity(null, null, null);
|
||||||
|
Assertions.assertThrows(DataIntegrityViolationException.class, () -> filmService.create(nullableType));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Order(2)
|
||||||
|
void createNullableGenreTest() {
|
||||||
|
final GenreEntity nullableType = new GenreEntity(null);
|
||||||
|
Assertions.assertThrows(DataIntegrityViolationException.class, () -> genreService.create(nullableType));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Order(3)
|
||||||
|
void connectFilmAndGenreTest() {
|
||||||
|
Assertions.assertEquals(0, filmService.get(film1.getId()).getFilmGenres().size());
|
||||||
|
Assertions.assertEquals(0, filmService.get(film2.getId()).getFilmGenres().size());
|
||||||
|
filmService.addFilmGenres(film1.getId(), genre1.getId());
|
||||||
|
filmService.addFilmGenres(film1.getId(), genre2.getId());
|
||||||
|
filmService.addFilmGenres(film1.getId(), genre3.getId());
|
||||||
|
filmService.addFilmGenres(film1.getId(), genre4.getId());
|
||||||
|
filmService.addFilmGenres(film2.getId(), genre3.getId());
|
||||||
|
Assertions.assertEquals(4, filmService.get(film1.getId()).getFilmGenres().size());
|
||||||
|
Assertions.assertEquals(1, filmService.get(film2.getId()).getFilmGenres().size());
|
||||||
|
filmService.addFilmGenres(film1.getId(), genre2.getId());
|
||||||
|
Assertions.assertEquals(4, filmService.get(film1.getId()).getFilmGenres().size());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Order(4)
|
||||||
|
void updateFilmTest() {
|
||||||
|
Assertions.assertNotEquals(filmService.get(film1.getId()).getDescription(),
|
||||||
|
filmService.get(film2.getId()).getDescription());
|
||||||
|
filmService.update(film1.getId(), film2);
|
||||||
|
Assertions.assertEquals(filmService.get(film1.getId()).getDescription(),
|
||||||
|
filmService.get(film2.getId()).getDescription());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Order(5)
|
||||||
|
void deleteFilmGenreTest() {
|
||||||
|
Assertions.assertEquals(0, filmService.getFilmGenres(film1.getId()).size());
|
||||||
|
genreService.getAll()
|
||||||
|
.forEach(genre -> filmService.addFilmGenres(film1.getId(), genre.getId()));
|
||||||
|
Assertions.assertEquals(4, filmService.getFilmGenres(film1.getId()).size());
|
||||||
|
genreService.getAll().forEach(item -> genreService.delete(item.getId()));
|
||||||
|
Assertions.assertEquals(0, filmService.getFilmGenres(film1.getId()).size());
|
||||||
|
}
|
||||||
|
}
|
79
src/test/java/com/example/demo/LangServiceTests.java
Normal file
79
src/test/java/com/example/demo/LangServiceTests.java
Normal file
@ -0,0 +1,79 @@
|
|||||||
|
package com.example.demo;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.AfterEach;
|
||||||
|
import org.junit.jupiter.api.Assertions;
|
||||||
|
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 org.springframework.dao.DataIntegrityViolationException;
|
||||||
|
|
||||||
|
import com.example.demo.core.error.NotFoundException;
|
||||||
|
import com.example.demo.langs.model.LangEntity;
|
||||||
|
import com.example.demo.langs.service.LangService;
|
||||||
|
|
||||||
|
@SpringBootTest
|
||||||
|
class LangServiceTests {
|
||||||
|
@Autowired
|
||||||
|
private LangService langService;
|
||||||
|
|
||||||
|
private LangEntity lang;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void createData() {
|
||||||
|
removeData();
|
||||||
|
|
||||||
|
lang = langService.create(new LangEntity("Русский"));
|
||||||
|
langService.create(new LangEntity("Китайский"));
|
||||||
|
langService.create(new LangEntity("Английский"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterEach
|
||||||
|
void removeData() {
|
||||||
|
langService.getAll().forEach(item -> langService.delete(item.getId()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void getTest() {
|
||||||
|
Assertions.assertThrows(NotFoundException.class, () -> langService.get(0L));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void createTest() {
|
||||||
|
Assertions.assertEquals(3, langService.getAll().size());
|
||||||
|
Assertions.assertEquals(lang, langService.get(lang.getId()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void createNotUniqueTest() {
|
||||||
|
final LangEntity nonUniqueLang = new LangEntity("Русский");
|
||||||
|
Assertions.assertThrows(IllegalArgumentException.class, () -> langService.create(nonUniqueLang));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void createNullableTest() {
|
||||||
|
final LangEntity nullableLang = new LangEntity(null);
|
||||||
|
Assertions.assertThrows(DataIntegrityViolationException.class, () -> langService.create(nullableLang));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void updateTest() {
|
||||||
|
final String test = "TEST";
|
||||||
|
final String oldName = lang.getName();
|
||||||
|
final LangEntity newEntity = langService.update(lang.getId(), new LangEntity(test));
|
||||||
|
Assertions.assertEquals(3, langService.getAll().size());
|
||||||
|
Assertions.assertEquals(newEntity, langService.get(lang.getId()));
|
||||||
|
Assertions.assertEquals(test, newEntity.getName());
|
||||||
|
Assertions.assertNotEquals(oldName, newEntity.getName());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void deleteTest() {
|
||||||
|
langService.delete(lang.getId());
|
||||||
|
Assertions.assertEquals(2, langService.getAll().size());
|
||||||
|
|
||||||
|
final LangEntity newEntity = langService.create(new LangEntity(lang.getName()));
|
||||||
|
Assertions.assertEquals(3, langService.getAll().size());
|
||||||
|
Assertions.assertNotEquals(lang.getId(), newEntity.getId());
|
||||||
|
}
|
||||||
|
}
|
108
src/test/java/com/example/demo/UserfilmServiceTests.java
Normal file
108
src/test/java/com/example/demo/UserfilmServiceTests.java
Normal file
@ -0,0 +1,108 @@
|
|||||||
|
package com.example.demo;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.AfterEach;
|
||||||
|
import org.junit.jupiter.api.Assertions;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
|
||||||
|
import org.junit.jupiter.api.Order;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.TestMethodOrder;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.dao.DataIntegrityViolationException;
|
||||||
|
|
||||||
|
import com.example.demo.films.model.FilmEntity;
|
||||||
|
import com.example.demo.films.service.FilmService;
|
||||||
|
import com.example.demo.users.model.UserEntity;
|
||||||
|
import com.example.demo.users.service.UserService;
|
||||||
|
import com.example.demo.langs.model.LangEntity;
|
||||||
|
import com.example.demo.langs.service.LangService;
|
||||||
|
|
||||||
|
@SpringBootTest
|
||||||
|
@TestMethodOrder(OrderAnnotation.class)
|
||||||
|
class UserfilmServiceTests {
|
||||||
|
@Autowired
|
||||||
|
private UserService userService;
|
||||||
|
@Autowired
|
||||||
|
private LangService langService;
|
||||||
|
@Autowired
|
||||||
|
private FilmService filmService;
|
||||||
|
|
||||||
|
private UserEntity user1;
|
||||||
|
private UserEntity user2;
|
||||||
|
|
||||||
|
private LangEntity lang1;
|
||||||
|
private LangEntity lang2;
|
||||||
|
|
||||||
|
private FilmEntity film1;
|
||||||
|
private FilmEntity film2;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void createData() {
|
||||||
|
removeData();
|
||||||
|
|
||||||
|
user1 = userService.create(new UserEntity("user1", "123456"));
|
||||||
|
user2 = userService.create(new UserEntity("user2", "1234567"));
|
||||||
|
|
||||||
|
lang1 = langService.create(new LangEntity("Русссский"));
|
||||||
|
lang2 = langService.create(new LangEntity("Не руссссский"));
|
||||||
|
|
||||||
|
film1 = filmService.create(new FilmEntity("Гарри", "ПОоООООООООООтер", lang1));
|
||||||
|
film2 = filmService.create(new FilmEntity("Алиса в стране", "Какой-то", lang2));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterEach
|
||||||
|
void removeData() {
|
||||||
|
userService.getAll().forEach(item -> userService.delete(item.getId()));
|
||||||
|
filmService.getAll().forEach(item -> filmService.delete(item.getId()));
|
||||||
|
userService.getAll().forEach(item -> userService.delete(item.getId()));
|
||||||
|
langService.getAll().forEach(item -> langService.delete(item.getId()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Order(1)
|
||||||
|
void createNullableFilmTest() {
|
||||||
|
final FilmEntity nullableType = new FilmEntity(null, null, null);
|
||||||
|
Assertions.assertThrows(DataIntegrityViolationException.class, () -> filmService.create(nullableType));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Order(2)
|
||||||
|
void createNullableUserTest() {
|
||||||
|
final UserEntity nullableType = new UserEntity(null, null);
|
||||||
|
Assertions.assertThrows(DataIntegrityViolationException.class, () -> userService.create(nullableType));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Order(3)
|
||||||
|
void connectFilmAndUserTest() {
|
||||||
|
Assertions.assertEquals(0, userService.get(user1.getId()).getUserFilms().size());
|
||||||
|
Assertions.assertEquals(0, userService.get(user2.getId()).getUserFilms().size());
|
||||||
|
userService.addFilm(user1.getId(), film1.getId());
|
||||||
|
userService.addFilm(user1.getId(), film2.getId());
|
||||||
|
userService.addFilm(user2.getId(), film1.getId());
|
||||||
|
Assertions.assertEquals(2, userService.get(user1.getId()).getUserFilms().size());
|
||||||
|
Assertions.assertEquals(1, userService.get(user2.getId()).getUserFilms().size());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Order(4)
|
||||||
|
void updateUserTest() {
|
||||||
|
UserEntity test = new UserEntity("test", "test");
|
||||||
|
Assertions.assertNotEquals(test.getLogin(), userService.get(user1.getId()).getLogin());
|
||||||
|
userService.update(user1.getId(), test);
|
||||||
|
Assertions.assertEquals(test.getLogin(), userService.get(user1.getId()).getLogin());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Order(5)
|
||||||
|
void deleteUserFilmTest() {
|
||||||
|
Assertions.assertEquals(0, userService.getUserFilms(user1.getId()).size());
|
||||||
|
filmService.getAll()
|
||||||
|
.forEach(film -> userService.addFilm(user1.getId(), film.getId()));
|
||||||
|
Assertions.assertEquals(2, userService.get(user1.getId()).getUserFilms().size());
|
||||||
|
userService.deleteAllUserFilms(user1.getId());
|
||||||
|
Assertions.assertEquals(0, userService.get(user1.getId()).getUserFilms().size());
|
||||||
|
}
|
||||||
|
}
|
14
src/test/resources/application.properties
Normal file
14
src/test/resources/application.properties
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
# Server
|
||||||
|
spring.main.banner-mode=off
|
||||||
|
|
||||||
|
# Logger settings
|
||||||
|
# Available levels are: TRACE, DEBUG, INFO, WARN, ERROR, FATAL, OFF
|
||||||
|
logging.level.com.example.demo=DEBUG
|
||||||
|
|
||||||
|
# JPA Settings
|
||||||
|
spring.datasource.url=jdbc:h2:mem:testdb
|
||||||
|
spring.datasource.username=sa
|
||||||
|
spring.datasource.password=password
|
||||||
|
spring.datasource.driver-class-name=org.h2.Driver
|
||||||
|
spring.jpa.hibernate.ddl-auto=create
|
||||||
|
spring.jpa.open-in-view=false
|
Loading…
Reference in New Issue
Block a user