Compare commits
No commits in common. "master" and "Lab5" have entirely different histories.
37
build.gradle
37
build.gradle
@ -1,22 +1,43 @@
|
||||
plugins {
|
||||
id 'java'
|
||||
id 'org.springframework.boot' version '3.0.2'
|
||||
id 'io.spring.dependency-management' version '1.1.0'
|
||||
id 'java'
|
||||
id 'org.springframework.boot' version '3.0.4'
|
||||
id 'io.spring.dependency-management' version '1.1.0'
|
||||
}
|
||||
|
||||
group = 'com.example'
|
||||
version = '0.0.1-SNAPSHOT'
|
||||
sourceCompatibility = '17'
|
||||
sourceCompatibility = '19'
|
||||
|
||||
repositories {
|
||||
mavenCentral()
|
||||
mavenCentral()
|
||||
}
|
||||
|
||||
jar {
|
||||
enabled = false
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation 'org.springframework.boot:spring-boot-starter-web'
|
||||
testImplementation 'org.springframework.boot:spring-boot-starter-test'
|
||||
implementation 'org.springframework.boot:spring-boot-starter-web'
|
||||
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
|
||||
implementation 'org.springframework.boot:spring-boot-devtools'
|
||||
implementation 'nz.net.ultraq.thymeleaf:thymeleaf-layout-dialect'
|
||||
|
||||
implementation 'org.webjars.npm:bootstrap:5.3.0-alpha2'
|
||||
implementation 'org.webjars:jquery:3.6.0'
|
||||
implementation 'org.webjars:font-awesome:6.1.0'
|
||||
|
||||
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
|
||||
implementation 'com.h2database:h2:2.1.210'
|
||||
|
||||
implementation 'org.hibernate.validator:hibernate-validator'
|
||||
|
||||
implementation 'org.springdoc:springdoc-openapi-ui:1.6.5'
|
||||
implementation 'org.projectlombok:lombok:1.18.22'
|
||||
|
||||
testImplementation 'org.springframework.boot:spring-boot-starter-test'
|
||||
|
||||
}
|
||||
|
||||
tasks.named('test') {
|
||||
useJUnitPlatform()
|
||||
useJUnitPlatform()
|
||||
}
|
||||
|
2
gradle/wrapper/gradle-wrapper.properties
vendored
2
gradle/wrapper/gradle-wrapper.properties
vendored
@ -1,5 +1,5 @@
|
||||
distributionBase=GRADLE_USER_HOME
|
||||
distributionPath=wrapper/dists
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-7.6-bin.zip
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-7.6.1-bin.zip
|
||||
zipStoreBase=GRADLE_USER_HOME
|
||||
zipStorePath=wrapper/dists
|
||||
|
182
gradlew.bat
vendored
182
gradlew.bat
vendored
@ -1,91 +1,91 @@
|
||||
@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=.
|
||||
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
|
||||
@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=.
|
||||
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
|
||||
|
@ -1 +1,2 @@
|
||||
rootProject.name = 'demo1'
|
||||
rootProject.name = 'demo'
|
||||
include 'front'
|
@ -1,18 +0,0 @@
|
||||
package com.example.demo;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@SpringBootApplication
|
||||
@RestController
|
||||
public class Demo1Application {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Demo1Application.class, args);
|
||||
}
|
||||
@GetMapping("/")
|
||||
public String hello(@RequestParam(value = "name", defaultValue = "World") String name) {
|
||||
return String.format("Hello %s!", name);
|
||||
}
|
||||
}
|
15
src/main/java/com/example/demo/DemoApplication.java
Normal file
15
src/main/java/com/example/demo/DemoApplication.java
Normal file
@ -0,0 +1,15 @@
|
||||
package com.example.demo;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@SpringBootApplication
|
||||
@RestController
|
||||
public class DemoApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(DemoApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
34
src/main/java/com/example/demo/WebConfiguration.java
Normal file
34
src/main/java/com/example/demo/WebConfiguration.java
Normal file
@ -0,0 +1,34 @@
|
||||
package com.example.demo;
|
||||
|
||||
import org.springframework.boot.web.server.ErrorPage;
|
||||
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
|
||||
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.web.servlet.config.annotation.CorsRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.ViewControllerRegistration;
|
||||
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
@Configuration
|
||||
public class WebConfiguration implements WebMvcConfigurer {
|
||||
public static final String REST_API = "/api";
|
||||
@Override
|
||||
public void addCorsMappings(CorsRegistry registry) {
|
||||
registry.addMapping("/**").allowedMethods("*");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addViewControllers(ViewControllerRegistry registry) {
|
||||
ViewControllerRegistration registration = registry.addViewController("/notFound");
|
||||
registration.setViewName("forward:/index.html");
|
||||
registration.setStatusCode(HttpStatus.OK);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> containerCustomizer() {
|
||||
return container -> {
|
||||
container.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/notFound"));
|
||||
};
|
||||
}
|
||||
}
|
87
src/main/java/com/example/demo/master/Master.java
Normal file
87
src/main/java/com/example/demo/master/Master.java
Normal file
@ -0,0 +1,87 @@
|
||||
package com.example.demo.master;
|
||||
|
||||
import com.example.demo.order.Order;
|
||||
import jakarta.persistence.*;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
@Entity
|
||||
|
||||
public class Master {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
@Column(name="_id")
|
||||
private Long id;
|
||||
@Column()
|
||||
private String firstName;
|
||||
private String lastName;
|
||||
private String email;
|
||||
private String password;
|
||||
|
||||
public Master() {
|
||||
}
|
||||
|
||||
public Master(String firstName, String lastName, String email, String password) {
|
||||
this.firstName = firstName;
|
||||
this.lastName = lastName;
|
||||
this.email = email;
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public String getEmail() {return email; }
|
||||
|
||||
public String getPassword() { return password; }
|
||||
|
||||
public void setFirstName(String firstName) {
|
||||
this.firstName = firstName;
|
||||
}
|
||||
|
||||
public void setLastName(String lastName) {
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
public void setEmail(String email) {
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
Master master = (Master) o;
|
||||
return Objects.equals(id, master.id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Master{" +
|
||||
"id=" + id +
|
||||
", firstName='" + firstName + '\'' +
|
||||
", lastName='" + lastName + '\'' +
|
||||
", email='" + email + '\'' +
|
||||
", password='" + password + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
75
src/main/java/com/example/demo/master/MasterController.java
Normal file
75
src/main/java/com/example/demo/master/MasterController.java
Normal file
@ -0,0 +1,75 @@
|
||||
package com.example.demo.master;
|
||||
|
||||
import com.example.demo.WebConfiguration;
|
||||
import com.example.demo.order.OrderService;
|
||||
import com.example.demo.product.ProductDto;
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PatchMapping;
|
||||
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.bind.annotation.RestController;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping(WebConfiguration.REST_API + "/master")
|
||||
public class MasterController {
|
||||
private final MasterService masterService;
|
||||
private final OrderService orderService;
|
||||
|
||||
|
||||
public MasterController(MasterService masterService, OrderService orderService) {
|
||||
this.masterService = masterService;
|
||||
this.orderService = orderService;
|
||||
}
|
||||
|
||||
@GetMapping("/")
|
||||
public MasterDto getCurrentMaster() {
|
||||
return new MasterDto(masterService.findMaster(masterService.getCurrentMasterId()));
|
||||
|
||||
}
|
||||
@GetMapping("/{email}/{password}")
|
||||
public MasterDto getCurrentMaster(@PathVariable("email") String email,
|
||||
@PathVariable("password") String password) {
|
||||
var master = new MasterDto(masterService.findMaster(email, password));
|
||||
masterService.setCurrentMasterId(master.getId());
|
||||
return master;
|
||||
}
|
||||
@PostMapping("/")
|
||||
public MasterDto createMaster(@RequestParam("firstName") String firstName,
|
||||
@RequestParam("lastName") String lastName,
|
||||
@RequestParam("email") String email,
|
||||
@RequestParam("password") String password) {
|
||||
MasterDto master = new MasterDto(masterService.addMaster(firstName, lastName, email, password));
|
||||
masterService.setCurrentMasterId(master.getId());
|
||||
orderService.addOrder(master.getId());
|
||||
return master;
|
||||
}
|
||||
|
||||
@PatchMapping("/")
|
||||
public MasterDto updateMaster(@RequestParam("firstName") String firstName,
|
||||
@RequestParam("lastName") String lastName,
|
||||
@RequestParam("email") String email,
|
||||
@RequestParam("password") String password) {
|
||||
return new MasterDto(masterService.updateMaster(masterService.getCurrentMasterId(),
|
||||
firstName, lastName, email, password));
|
||||
}
|
||||
|
||||
@DeleteMapping("/")
|
||||
public MasterDto deleteMaster(@PathVariable Long id) {
|
||||
return new MasterDto(masterService.deleteMaster(masterService.getCurrentMasterId()));
|
||||
}
|
||||
|
||||
@PostMapping("/log_out")
|
||||
public void logOut() {
|
||||
masterService.setCurrentMasterId(0L);
|
||||
}
|
||||
|
||||
@GetMapping("/all")
|
||||
public List<MasterDto> GetMasters(){
|
||||
return masterService.findAllMasters().stream().map(MasterDto::new).toList();
|
||||
}
|
||||
}
|
44
src/main/java/com/example/demo/master/MasterDto.java
Normal file
44
src/main/java/com/example/demo/master/MasterDto.java
Normal file
@ -0,0 +1,44 @@
|
||||
package com.example.demo.master;
|
||||
|
||||
import com.example.demo.order.Order;
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class MasterDto {
|
||||
private final Long id;
|
||||
@NotBlank
|
||||
private final String firstName;
|
||||
@NotBlank
|
||||
private final String lastName;
|
||||
|
||||
@NotBlank
|
||||
private final String email;
|
||||
|
||||
@NotBlank
|
||||
private final String password;
|
||||
|
||||
public MasterDto(Master master) {
|
||||
id = master.getId();
|
||||
firstName = master.getFirstName();
|
||||
lastName = master.getLastName();
|
||||
email = master.getEmail();
|
||||
password = master.getPassword();
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public String getEmail() {return email; }
|
||||
|
||||
public String getPassword() { return password; }
|
||||
}
|
@ -0,0 +1,84 @@
|
||||
package com.example.demo.master;
|
||||
|
||||
import com.example.demo.order.OrderService;
|
||||
import com.example.demo.product.ProductDto;
|
||||
import com.example.demo.product.ProductService;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@Controller
|
||||
@RequestMapping("/master")
|
||||
public class MasterMvcController {
|
||||
private final MasterService masterService;
|
||||
private final OrderService orderService;
|
||||
|
||||
public MasterMvcController(MasterService masterService, OrderService orderService) {
|
||||
this.masterService = masterService;
|
||||
this.orderService = orderService;
|
||||
}
|
||||
|
||||
@GetMapping("/login")
|
||||
public String loginPage(Model model) {
|
||||
if (masterService.getCurrentMasterId() != 0) {
|
||||
return "redirect:/product";
|
||||
}
|
||||
model.addAttribute("user", new Master());
|
||||
return "Login";
|
||||
}
|
||||
|
||||
@PostMapping("/login")
|
||||
public String login(@ModelAttribute Master user) {
|
||||
var master = new MasterDto(masterService.findMaster(user.getEmail(), user.getPassword()));
|
||||
masterService.setCurrentMasterId(master.getId());
|
||||
return "redirect:/product/my_products";
|
||||
}
|
||||
|
||||
@GetMapping("")
|
||||
public String getUserPage(Model model) {
|
||||
if (masterService.getCurrentMasterId() != 0) {
|
||||
model.addAttribute("user", masterService.findMaster(masterService.getCurrentMasterId()));
|
||||
} else {
|
||||
return "redirect:/master/register";
|
||||
}
|
||||
return "UserPage";
|
||||
}
|
||||
|
||||
@PostMapping(value = "", params = "action=update")
|
||||
public String updateUserPage(@ModelAttribute Master user) {
|
||||
if (masterService.getCurrentMasterId() == 0) {
|
||||
return "redirect:/master";
|
||||
}
|
||||
masterService.updateMaster(
|
||||
masterService.getCurrentMasterId(),
|
||||
user.getFirstName(),
|
||||
user.getLastName(),
|
||||
user.getEmail(),
|
||||
user.getPassword());
|
||||
return "redirect:/product";
|
||||
}
|
||||
|
||||
@PostMapping(value = "", params = "action=log_out")
|
||||
public String logOut() {
|
||||
masterService.setCurrentMasterId(0L);
|
||||
return "redirect:/product";
|
||||
}
|
||||
|
||||
@GetMapping("/register")
|
||||
public String registerPage(Model model) {
|
||||
model.addAttribute("user", new Master());
|
||||
return "UserPage";
|
||||
}
|
||||
|
||||
@PostMapping(value = "/register", params = "action=register")
|
||||
public String register(@ModelAttribute Master user) {
|
||||
MasterDto master = new MasterDto(masterService.addMaster(
|
||||
user.getFirstName(),
|
||||
user.getLastName(),
|
||||
user.getEmail(),
|
||||
user.getPassword()));
|
||||
masterService.setCurrentMasterId(master.getId());
|
||||
orderService.addOrder(master.getId());
|
||||
return "redirect:/product/my_products";
|
||||
}
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
package com.example.demo.master;
|
||||
|
||||
public class MasterNotFoundException extends RuntimeException {
|
||||
public MasterNotFoundException(Long id) {
|
||||
super(String.format("Master with id [%s] is not found", id));
|
||||
}
|
||||
public MasterNotFoundException(String email) {
|
||||
super(String.format("Master with email [%s] is not found", email));
|
||||
}
|
||||
}
|
10
src/main/java/com/example/demo/master/MasterRepository.java
Normal file
10
src/main/java/com/example/demo/master/MasterRepository.java
Normal file
@ -0,0 +1,10 @@
|
||||
package com.example.demo.master;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public interface MasterRepository extends JpaRepository<Master, Long> {
|
||||
Optional<Master> findByEmail(String email);
|
||||
}
|
89
src/main/java/com/example/demo/master/MasterService.java
Normal file
89
src/main/java/com/example/demo/master/MasterService.java
Normal file
@ -0,0 +1,89 @@
|
||||
package com.example.demo.master;
|
||||
|
||||
import com.example.demo.order.Order;
|
||||
import com.example.demo.order.OrderController;
|
||||
import com.example.demo.order.OrderService;
|
||||
import com.example.demo.util.validation.ValidatorUtil;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
|
||||
@Service
|
||||
public class MasterService {
|
||||
private final MasterRepository masterRepository;
|
||||
|
||||
private final ValidatorUtil validatorUtil;
|
||||
|
||||
private Long currentMasterId = 0L;
|
||||
|
||||
public MasterService(MasterRepository masterRepository, ValidatorUtil validatorUtil) {
|
||||
this.masterRepository = masterRepository;
|
||||
this.validatorUtil = validatorUtil;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Master addMaster(String firstName, String lastName, String email, String password) {
|
||||
final Master master = new Master(firstName, lastName, email, password);
|
||||
validatorUtil.validate(master);
|
||||
return masterRepository.save(master);
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public Master findMaster(Long id) {
|
||||
final Optional<Master> mater = masterRepository.findById(id);
|
||||
return mater.orElseThrow(() -> new MasterNotFoundException(id));
|
||||
}
|
||||
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public Master findMaster(String email, String password) {
|
||||
final Optional<Master> master = masterRepository.findByEmail(email);
|
||||
Master realMaster = master.orElseThrow(() -> new MasterNotFoundException(email));
|
||||
if (!Objects.equals(realMaster.getPassword(), password)) {
|
||||
throw new MasterNotFoundException(email);
|
||||
}
|
||||
return realMaster;
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<Master> findAllMasters() {
|
||||
return masterRepository.findAll();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Master updateMaster(Long id, String firstName, String lastName, String email, String password) {
|
||||
final Master master = findMaster(id);
|
||||
master.setFirstName(firstName);
|
||||
master.setLastName(lastName);
|
||||
master.setEmail(email);
|
||||
master.setPassword(password);
|
||||
validatorUtil.validate(master);
|
||||
return masterRepository.save(master);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Master deleteMaster(Long id) {
|
||||
final Master master = findMaster(id);
|
||||
masterRepository.delete(master);
|
||||
return master;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void deleteAllMasters() {
|
||||
masterRepository.deleteAll();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Long getCurrentMasterId (){
|
||||
return currentMasterId;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void setCurrentMasterId(Long masterId) {
|
||||
currentMasterId = masterId;
|
||||
}
|
||||
|
||||
}
|
97
src/main/java/com/example/demo/order/Order.java
Normal file
97
src/main/java/com/example/demo/order/Order.java
Normal file
@ -0,0 +1,97 @@
|
||||
package com.example.demo.order;
|
||||
|
||||
import com.example.demo.master.Master;
|
||||
import com.example.demo.product.Product;
|
||||
import jakarta.persistence.*;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
@Entity
|
||||
@Table(name = "Master_order")
|
||||
public class Order {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
@Column(name="_id")
|
||||
private Long id;
|
||||
|
||||
|
||||
@ManyToOne(fetch = FetchType.EAGER)
|
||||
private Master master;
|
||||
@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
|
||||
@JoinTable(name = "order_products", joinColumns = {@JoinColumn(name = "order_id")}, inverseJoinColumns = {@JoinColumn(name = "product_id")})
|
||||
private List<Product> products;
|
||||
|
||||
private OrderStatus status;
|
||||
|
||||
public Order() {
|
||||
status = OrderStatus.Open;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void buyProducts() {
|
||||
for (var item: products) {
|
||||
removeProduct(item);
|
||||
}
|
||||
}
|
||||
|
||||
public void addProduct(Product product) {
|
||||
if (products == null)
|
||||
products = new ArrayList<>();
|
||||
products.add(product);
|
||||
}
|
||||
|
||||
public void removeProduct(Product product) {
|
||||
if (products == null)
|
||||
products = new ArrayList<>();
|
||||
products.remove(product);
|
||||
}
|
||||
|
||||
public Master getMaster() {
|
||||
return master;
|
||||
}
|
||||
|
||||
public void setMaster(Master master) {
|
||||
this.master = master;
|
||||
}
|
||||
|
||||
public List<Product> getProducts() {
|
||||
return products;
|
||||
}
|
||||
public OrderStatus getStatus() {return status;}
|
||||
|
||||
public void setStatus(OrderStatus status) {
|
||||
this.status = status;
|
||||
}
|
||||
|
||||
public void setProducts(List<Product> products) {
|
||||
this.products = products;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
Order order = (Order) o;
|
||||
return Objects.equals(id, order.id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Order {" +
|
||||
"id=" + id +
|
||||
", master='" + master.toString() + '\'' +
|
||||
", products='" + products.toString() + '\'';
|
||||
}
|
||||
}
|
54
src/main/java/com/example/demo/order/OrderController.java
Normal file
54
src/main/java/com/example/demo/order/OrderController.java
Normal file
@ -0,0 +1,54 @@
|
||||
package com.example.demo.order;
|
||||
|
||||
import com.example.demo.WebConfiguration;
|
||||
import com.example.demo.master.MasterService;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping(WebConfiguration.REST_API + "/order")
|
||||
public class OrderController {
|
||||
private final OrderService orderService;
|
||||
private final MasterService masterService;
|
||||
|
||||
public OrderController(OrderService orderService, MasterService masterService) {
|
||||
this.orderService = orderService;
|
||||
this.masterService = masterService;
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public OrderDto getOrder(@PathVariable Long id) {
|
||||
return new OrderDto(orderService.findOrder(id));
|
||||
}
|
||||
|
||||
@DeleteMapping("/")
|
||||
public void buyProducts() {
|
||||
orderService.buyProducts(masterService.getCurrentMasterId());
|
||||
}
|
||||
|
||||
@GetMapping("/")
|
||||
public List<OrderDto> getOrder() {
|
||||
return orderService.findAllOrders().stream().map(OrderDto::new).toList();
|
||||
}
|
||||
|
||||
@PostMapping("/")
|
||||
public OrderDto createOrder(@RequestParam("master") Long masterId) {
|
||||
return new OrderDto(orderService.addOrder(masterId));
|
||||
}
|
||||
|
||||
@PostMapping("/{product}")
|
||||
public void addProduct(@PathVariable("product") Long productId) {
|
||||
orderService.addProduct(masterService.getCurrentMasterId(), productId);
|
||||
}
|
||||
|
||||
@DeleteMapping("/{product}")
|
||||
public void deleteProduct(@PathVariable("product") Long productId) {
|
||||
orderService.deleteProduct(masterService.getCurrentMasterId(), productId);
|
||||
}
|
||||
|
||||
@GetMapping("/findOrders/{masterId}")
|
||||
public List<OrderDto> findOrders(@PathVariable("masterId") Long masterId) {
|
||||
return orderService.findMastersOrders(masterId).stream().map(OrderDto::new).toList();
|
||||
}
|
||||
}
|
51
src/main/java/com/example/demo/order/OrderDto.java
Normal file
51
src/main/java/com/example/demo/order/OrderDto.java
Normal file
@ -0,0 +1,51 @@
|
||||
package com.example.demo.order;
|
||||
|
||||
import com.example.demo.master.Master;
|
||||
import com.example.demo.product.Product;
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
public class OrderDto {
|
||||
private final Long id;
|
||||
@NotBlank
|
||||
private final Master master;
|
||||
private final List<Product> products;
|
||||
|
||||
private final OrderStatus status;
|
||||
|
||||
private final int cost;
|
||||
|
||||
public OrderDto(Order order) {
|
||||
id = order.getId();
|
||||
master = order.getMaster();
|
||||
products = order.getProducts();
|
||||
status = order.getStatus();
|
||||
int cost1 = 0;
|
||||
for (var item : products) {
|
||||
cost1 += item.getCost();
|
||||
}
|
||||
cost = cost1;
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public Master getMaster() {
|
||||
return master;
|
||||
}
|
||||
|
||||
public List<Product> getProducts() {
|
||||
return products;
|
||||
}
|
||||
|
||||
public OrderStatus getStatus() {
|
||||
return status;
|
||||
}
|
||||
public int getCost() {
|
||||
return cost;
|
||||
}
|
||||
}
|
66
src/main/java/com/example/demo/order/OrderMvcController.java
Normal file
66
src/main/java/com/example/demo/order/OrderMvcController.java
Normal file
@ -0,0 +1,66 @@
|
||||
package com.example.demo.order;
|
||||
|
||||
import com.example.demo.master.MasterService;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
@Controller
|
||||
@RequestMapping("/order")
|
||||
public class OrderMvcController {
|
||||
private final MasterService masterService;
|
||||
private final OrderService orderService;
|
||||
|
||||
public OrderMvcController(MasterService masterService, OrderService orderService) {
|
||||
this.masterService = masterService;
|
||||
this.orderService = orderService;
|
||||
}
|
||||
|
||||
@GetMapping("")
|
||||
public String getOrder(Model model) {
|
||||
if (masterService.getCurrentMasterId() == 0) {
|
||||
return "redirect:/master/login";
|
||||
}
|
||||
model.addAttribute("user", masterService.findMaster(masterService.getCurrentMasterId()));
|
||||
model.addAttribute("order", orderService.findOrder(masterService.getCurrentMasterId()));
|
||||
AtomicInteger fullCost = new AtomicInteger();
|
||||
orderService.findOrder(masterService.getCurrentMasterId()).getProducts().forEach(
|
||||
item -> fullCost.addAndGet(item.getCost()));
|
||||
model.addAttribute("fullCost", fullCost);
|
||||
return "OrderPage";
|
||||
}
|
||||
|
||||
@PostMapping(value = "", params = "action=delete")
|
||||
public String deleteProduct(@RequestParam(value = "id", required = true) Long id) {
|
||||
orderService.deleteProduct(masterService.getCurrentMasterId(), id);
|
||||
return "redirect:/order";
|
||||
}
|
||||
|
||||
@PostMapping(value = "", params = "action=buy")
|
||||
public String buyProducts() {
|
||||
orderService.buyProducts(masterService.getCurrentMasterId());
|
||||
return "redirect:/product";
|
||||
}
|
||||
|
||||
@GetMapping("/masters_order")
|
||||
public String MastersOrders(Model model, @RequestParam(value = "master_id", defaultValue = "-1") String masterId) {
|
||||
model.addAttribute("user", masterService.findMaster(masterService.getCurrentMasterId()));
|
||||
model.addAttribute("masters", masterService.findAllMasters());
|
||||
|
||||
if (!Objects.equals(masterId, "-1")) {
|
||||
model.addAttribute("orders", orderService.findMastersOrders(Long.valueOf(masterId)).
|
||||
stream().map(OrderDto::new).toList());
|
||||
}
|
||||
else {
|
||||
model.addAttribute("orders", new ArrayList<OrderDto>());
|
||||
}
|
||||
return "MastersOrders";
|
||||
}
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package com.example.demo.order;
|
||||
|
||||
public class OrderNotFoundException extends RuntimeException {
|
||||
public OrderNotFoundException(Long id) {
|
||||
super(String.format("Order with id [%s] is not found", id));
|
||||
}
|
||||
}
|
13
src/main/java/com/example/demo/order/OrderRepository.java
Normal file
13
src/main/java/com/example/demo/order/OrderRepository.java
Normal file
@ -0,0 +1,13 @@
|
||||
package com.example.demo.order;
|
||||
|
||||
import com.example.demo.master.Master;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public interface OrderRepository extends JpaRepository<Order, Long> {
|
||||
Optional<Order> findByMasterAndStatus(Master master, OrderStatus status);
|
||||
|
||||
List<Order> findByMaster(Master master);
|
||||
}
|
95
src/main/java/com/example/demo/order/OrderService.java
Normal file
95
src/main/java/com/example/demo/order/OrderService.java
Normal file
@ -0,0 +1,95 @@
|
||||
package com.example.demo.order;
|
||||
|
||||
import com.example.demo.master.MasterService;
|
||||
import com.example.demo.product.*;
|
||||
import com.example.demo.util.validation.ValidatorUtil;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class OrderService {
|
||||
private final OrderRepository orderRepository;
|
||||
private final ProductService productService;
|
||||
private final MasterService masterService;
|
||||
private final ValidatorUtil validatorUtil;
|
||||
|
||||
public OrderService(OrderRepository orderRepository, ProductService productService,
|
||||
MasterService masterService, ValidatorUtil validatorUtil) {
|
||||
this.orderRepository = orderRepository;
|
||||
this.productService = productService;
|
||||
this.masterService = masterService;
|
||||
this.validatorUtil = validatorUtil;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Order addOrder(Long masterId) {
|
||||
final Order order = new Order();
|
||||
order.setMaster(masterService.findMaster(masterId));
|
||||
validatorUtil.validate(order);
|
||||
return orderRepository.save(order);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void addProduct(Long masterId, Long productId) {
|
||||
final Order order = findOrder(masterId);
|
||||
final Product product = productService.findProduct(productId);
|
||||
order.addProduct(product); var products = new ArrayList<Product>();
|
||||
product.setOrder(order);
|
||||
product.setAvailable(Status.Ordered);
|
||||
orderRepository.save(order);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void buyProducts(Long masterId) {
|
||||
final Order order = findOrder(masterId);
|
||||
for (var item: order.getProducts()) {
|
||||
item.setAvailable(Status.Bought);
|
||||
}
|
||||
order.setStatus(OrderStatus.Closed);
|
||||
orderRepository.save(order);
|
||||
addOrder(masterId);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void deleteProduct(Long masterId, Long productId) {
|
||||
final Order order = findOrder(masterId);
|
||||
final Product product = productService.findProduct(productId);
|
||||
order.removeProduct(product);
|
||||
product.removeOrder(order);
|
||||
product.setAvailable(Status.Availible);
|
||||
orderRepository.save(order);
|
||||
}
|
||||
|
||||
@Transactional()
|
||||
public Order findOrder(Long masterId) {
|
||||
return orderRepository.findByMasterAndStatus(masterService.findMaster(masterId), OrderStatus.Open).orElseThrow(
|
||||
() -> new OrderNotFoundException(masterId));
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<Order> findAllOrders() {
|
||||
return orderRepository.findAll();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Order deleteOrder(Long id) {
|
||||
final Order order = findOrder(id);
|
||||
order.setStatus(OrderStatus.Closed);
|
||||
orderRepository.save(order);
|
||||
return addOrder(id);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void deleteAllOrders() {
|
||||
orderRepository.deleteAll();
|
||||
}
|
||||
|
||||
|
||||
@Transactional
|
||||
public List<Order> findMastersOrders(Long masterId) {
|
||||
return orderRepository.findByMaster(masterService.findMaster(masterId));
|
||||
}
|
||||
}
|
6
src/main/java/com/example/demo/order/OrderStatus.java
Normal file
6
src/main/java/com/example/demo/order/OrderStatus.java
Normal file
@ -0,0 +1,6 @@
|
||||
package com.example.demo.order;
|
||||
|
||||
public enum OrderStatus {
|
||||
Open,
|
||||
Closed
|
||||
}
|
111
src/main/java/com/example/demo/product/Product.java
Normal file
111
src/main/java/com/example/demo/product/Product.java
Normal file
@ -0,0 +1,111 @@
|
||||
package com.example.demo.product;
|
||||
|
||||
import com.example.demo.master.Master;
|
||||
import com.example.demo.order.Order;
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import jakarta.persistence.*;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
@Entity
|
||||
public class Product {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
@Column(name="_id")
|
||||
private Long id;
|
||||
@Column()
|
||||
private String name;
|
||||
private Integer cost;
|
||||
|
||||
@ManyToMany(fetch = FetchType.LAZY)
|
||||
@JsonIgnore
|
||||
private List<Order> mastersOrders;
|
||||
|
||||
@ManyToOne(fetch = FetchType.EAGER)
|
||||
private Master master;
|
||||
|
||||
private Status status;
|
||||
|
||||
public Product() {
|
||||
}
|
||||
|
||||
public Product(String name, Integer cost) {
|
||||
this.name = name;
|
||||
this.cost = cost;
|
||||
this.status = Status.Availible;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String _name) {
|
||||
this.name = _name;
|
||||
}
|
||||
|
||||
public Integer getCost() {
|
||||
return cost;
|
||||
}
|
||||
|
||||
public void setCost(Integer cost) {
|
||||
this.cost = cost;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
Product product = (Product) o;
|
||||
return Objects.equals(id, product.id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Product {" +
|
||||
"id=" + id +
|
||||
", name='" + name + '\'' +
|
||||
", cost='" + cost.toString() + '\'' +
|
||||
", master='" + master.toString() + '\'' +
|
||||
'}';
|
||||
}
|
||||
|
||||
|
||||
public Master getMaster() {
|
||||
return master;
|
||||
}
|
||||
|
||||
public void setMaster(Master master) {
|
||||
this.master = master;
|
||||
}
|
||||
public List<Order> getOrders() {
|
||||
return mastersOrders;
|
||||
}
|
||||
public void setOrder(Order order) {
|
||||
if (!order.getProducts().contains(this)) {
|
||||
order.getProducts().add(this);
|
||||
}
|
||||
}
|
||||
|
||||
public void removeOrder(Order order) {
|
||||
order.getProducts().remove(this);
|
||||
}
|
||||
|
||||
public Status getAvailable() {
|
||||
return this.status;
|
||||
}
|
||||
|
||||
public void setAvailable(Status status) {
|
||||
this.status = status;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,59 @@
|
||||
package com.example.demo.product;
|
||||
|
||||
import com.example.demo.WebConfiguration;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PatchMapping;
|
||||
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.bind.annotation.RestController;
|
||||
|
||||
import java.util.List;
|
||||
@RestController
|
||||
@RequestMapping(WebConfiguration.REST_API + "/product")
|
||||
public class ProductController {
|
||||
private final ProductService productService;
|
||||
|
||||
|
||||
public ProductController(ProductService productService) {
|
||||
this.productService = productService;
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public ProductDto getProduct(@PathVariable Long id) {
|
||||
return new ProductDto(productService.findProduct(id));
|
||||
}
|
||||
|
||||
@GetMapping("/")
|
||||
public List<ProductDto> getProduct() {
|
||||
return productService.findAllProducts().stream().map(ProductDto::new).toList();
|
||||
}
|
||||
|
||||
@GetMapping("/master/{id}")
|
||||
public List<ProductDto> getMasterProduct(@PathVariable("id") Long id) {
|
||||
return productService.findProducts(id).stream().map(ProductDto::new).toList();
|
||||
}
|
||||
|
||||
@PostMapping("/{name}/{cost}/{masterId}")
|
||||
public ProductDto createProduct(@PathVariable("name") String name,
|
||||
@PathVariable("cost") Integer cost,
|
||||
@PathVariable("masterId") Long masterId) {
|
||||
return new ProductDto(productService.addProduct(name, cost, masterId));
|
||||
}
|
||||
|
||||
@PatchMapping("/{id}")
|
||||
public ProductDto updateProduct(@PathVariable Long id,
|
||||
@RequestParam("name") String name,
|
||||
@RequestParam("cost") Integer cost) {
|
||||
return new ProductDto(productService.updateProduct(id, name, cost));
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
public ProductDto deleteProduct(@PathVariable Long id) {
|
||||
return new ProductDto(productService.deleteProduct(id));
|
||||
}
|
||||
|
||||
}
|
54
src/main/java/com/example/demo/product/ProductDto.java
Normal file
54
src/main/java/com/example/demo/product/ProductDto.java
Normal file
@ -0,0 +1,54 @@
|
||||
package com.example.demo.product;
|
||||
|
||||
import com.example.demo.master.Master;
|
||||
import com.example.demo.order.Order;
|
||||
import jakarta.validation.constraints.Min;
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class ProductDto {
|
||||
private final long id;
|
||||
@NotBlank
|
||||
private final String name;
|
||||
@Min(1)
|
||||
private final int cost;
|
||||
private final List<Order> orders;
|
||||
@NotBlank
|
||||
private final Master master;
|
||||
@NotBlank
|
||||
private final Status status;
|
||||
|
||||
public ProductDto(Product product) {
|
||||
id = product.getId();
|
||||
name = product.getName();
|
||||
cost = product.getCost();
|
||||
orders = product.getOrders();
|
||||
master = product.getMaster();
|
||||
status = product.getAvailable();
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public Integer getCost() {
|
||||
return cost;
|
||||
}
|
||||
|
||||
public List<Order> getOrders() {
|
||||
return orders;
|
||||
}
|
||||
|
||||
public Master getMaster() {
|
||||
return master;
|
||||
}
|
||||
|
||||
public Status getStatus() {
|
||||
return status;
|
||||
}
|
||||
}
|
102
src/main/java/com/example/demo/product/ProductMvcController.java
Normal file
102
src/main/java/com/example/demo/product/ProductMvcController.java
Normal file
@ -0,0 +1,102 @@
|
||||
package com.example.demo.product;
|
||||
|
||||
import com.example.demo.master.Master;
|
||||
import com.example.demo.master.MasterService;
|
||||
import com.example.demo.order.OrderService;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
|
||||
@Controller
|
||||
@RequestMapping("/product")
|
||||
public class ProductMvcController {
|
||||
private final ProductService productService;
|
||||
private final MasterService masterService;
|
||||
private final OrderService orderService;
|
||||
public ProductMvcController(MasterService masterService,
|
||||
ProductService productService,
|
||||
OrderService orderService) {
|
||||
this.masterService = masterService;
|
||||
this.productService = productService;
|
||||
this.orderService = orderService;
|
||||
}
|
||||
|
||||
@GetMapping("")
|
||||
public String getProducts(Model model) {
|
||||
if (masterService.getCurrentMasterId() != 0) {
|
||||
Master user = masterService.findMaster(masterService.getCurrentMasterId());
|
||||
model.addAttribute("user", user);
|
||||
}
|
||||
else {
|
||||
model.addAttribute("user", new Master());
|
||||
}
|
||||
model.addAttribute("products", productService.findAllProducts()
|
||||
.stream().map(ProductDto::new).toList());
|
||||
return "Products";
|
||||
}
|
||||
|
||||
@PostMapping("")
|
||||
public String addProductToOrder(@RequestParam(value = "id", required = true) Long id) {
|
||||
if (masterService.getCurrentMasterId() == 0) {
|
||||
return "redirect:/master/login";
|
||||
}
|
||||
orderService.addProduct(masterService.getCurrentMasterId(), id);
|
||||
return "redirect:/product";
|
||||
}
|
||||
|
||||
|
||||
@GetMapping("/my_products")
|
||||
public String getMasterProduct(Model model) {
|
||||
if (masterService.getCurrentMasterId() == 0) {
|
||||
return "redirect:/product";
|
||||
}
|
||||
model.addAttribute("user",
|
||||
masterService.findMaster(masterService.getCurrentMasterId()));
|
||||
model.addAttribute("products",
|
||||
productService.findProducts(masterService.getCurrentMasterId()).stream().map(ProductDto::new).toList());
|
||||
return "UserProducts";
|
||||
}
|
||||
|
||||
@GetMapping("/create_product")
|
||||
public String createProductPage(Model model) {
|
||||
if (masterService.getCurrentMasterId() == 0) {
|
||||
return "redirect:/product";
|
||||
}
|
||||
model.addAttribute("user", masterService.findMaster(masterService.getCurrentMasterId()));
|
||||
model.addAttribute("product", new Product());
|
||||
model.addAttribute("buttonText", "Create");
|
||||
return "ProductCreate";
|
||||
}
|
||||
|
||||
@PostMapping("/create_product")
|
||||
public String createProduct(@ModelAttribute Product product) {
|
||||
productService.addProduct(
|
||||
product.getName(),
|
||||
product.getCost(),
|
||||
masterService.getCurrentMasterId()
|
||||
);
|
||||
return "redirect:/product/my_products";
|
||||
}
|
||||
|
||||
@GetMapping("/update_product/{id}")
|
||||
public String updateProductPage(Model model, @PathVariable("id") Long id) {
|
||||
if (masterService.getCurrentMasterId() == 0) {
|
||||
return "redirect:/product";
|
||||
}
|
||||
model.addAttribute("user", masterService.findMaster(masterService.getCurrentMasterId()));
|
||||
model.addAttribute("product", productService.findProduct(id));
|
||||
model.addAttribute("buttonText", "Update");
|
||||
return "ProductCreate";
|
||||
}
|
||||
|
||||
@PostMapping("/update_product/{id}")
|
||||
public String createProduct(@ModelAttribute Product product, @PathVariable("id") Long id) {
|
||||
productService.updateProduct(
|
||||
id,
|
||||
product.getName(),
|
||||
product.getCost()
|
||||
);
|
||||
return "redirect:/product/my_products";
|
||||
}
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package com.example.demo.product;
|
||||
|
||||
public class ProductNotFoundException extends RuntimeException {
|
||||
public ProductNotFoundException(Long id) {
|
||||
super(String.format("Product with id [%s] is not found", id));
|
||||
}
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
package com.example.demo.product;
|
||||
|
||||
import com.example.demo.master.Master;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public interface ProductRepository extends JpaRepository<Product, Long> {
|
||||
List<Product> findByMaster(Master master);
|
||||
|
||||
List<Product> findByStatus(Status status);
|
||||
}
|
68
src/main/java/com/example/demo/product/ProductService.java
Normal file
68
src/main/java/com/example/demo/product/ProductService.java
Normal file
@ -0,0 +1,68 @@
|
||||
package com.example.demo.product;
|
||||
|
||||
import com.example.demo.master.*;
|
||||
import com.example.demo.util.validation.ValidatorUtil;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@Service
|
||||
public class ProductService {
|
||||
private final ProductRepository productRepository;
|
||||
private final ValidatorUtil validatorUtil;
|
||||
private final MasterService masterService;
|
||||
|
||||
public ProductService(ProductRepository productRepository, MasterService masterService, ValidatorUtil validatorUtil) {
|
||||
this.productRepository = productRepository;
|
||||
this.validatorUtil = validatorUtil;
|
||||
this.masterService = masterService;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Product addProduct(String name, Integer cost, Long masterId) {
|
||||
final Product product = new Product(name, cost);
|
||||
validatorUtil.validate(product);
|
||||
product.setMaster(masterService.findMaster(masterId));
|
||||
return productRepository.save(product);
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public Product findProduct(Long id) {
|
||||
final Optional<Product> product = productRepository.findById(id);
|
||||
return product.orElseThrow(() -> new ProductNotFoundException(id));
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<Product> findProducts(Long masterId) {
|
||||
List<Product> products = productRepository.findByMaster(masterService.findMaster(masterId));
|
||||
return products.stream().filter((item) -> item.getAvailable() != Status.Bought).toList();
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<Product> findAllProducts() {
|
||||
return productRepository.findByStatus(Status.Availible);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Product updateProduct(Long id, String name, Integer cost) {
|
||||
final Product product = findProduct(id);
|
||||
product.setName(name);
|
||||
product.setCost(cost);
|
||||
validatorUtil.validate(product);
|
||||
return productRepository.save(product);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Product deleteProduct(Long id) {
|
||||
final Product product = findProduct(id);
|
||||
productRepository.delete(product);
|
||||
return product;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void deleteAllProducts() {
|
||||
productRepository.deleteAll();
|
||||
}
|
||||
}
|
7
src/main/java/com/example/demo/product/Status.java
Normal file
7
src/main/java/com/example/demo/product/Status.java
Normal file
@ -0,0 +1,7 @@
|
||||
package com.example.demo.product;
|
||||
|
||||
public enum Status {
|
||||
Availible,
|
||||
Ordered,
|
||||
Bought
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
package com.example.demo.util.error;
|
||||
|
||||
import com.example.demo.master.MasterNotFoundException;
|
||||
import com.example.demo.order.Order;
|
||||
import com.example.demo.order.OrderNotFoundException;
|
||||
import com.example.demo.product.ProductNotFoundException;
|
||||
import com.example.demo.util.validation.ValidationException;
|
||||
import org.springframework.context.support.DefaultMessageSourceResolvable;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.MethodArgumentNotValidException;
|
||||
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
|
||||
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@ControllerAdvice
|
||||
public class AdviceController {
|
||||
@ExceptionHandler({
|
||||
MasterNotFoundException.class,
|
||||
ProductNotFoundException.class,
|
||||
OrderNotFoundException.class,
|
||||
ValidationException.class,
|
||||
})
|
||||
public ResponseEntity<Object> handleException(Throwable e) {
|
||||
return new ResponseEntity<>(e.getMessage(), HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
|
||||
@ExceptionHandler(MethodArgumentNotValidException.class)
|
||||
public ResponseEntity<Object> handleBindException(MethodArgumentNotValidException e) {
|
||||
final ValidationException validationException = new ValidationException(
|
||||
e.getBindingResult().getAllErrors().stream()
|
||||
.map(DefaultMessageSourceResolvable::getDefaultMessage)
|
||||
.collect(Collectors.toSet()));
|
||||
return handleException(validationException);
|
||||
}
|
||||
|
||||
@ExceptionHandler(Exception.class)
|
||||
public ResponseEntity<Object> handleUnknownException(Throwable e) {
|
||||
e.printStackTrace();
|
||||
return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
package com.example.demo.util.validation;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
public class ValidationException extends RuntimeException {
|
||||
public ValidationException(Set<String> errors) {
|
||||
super(String.join("\n", errors));
|
||||
}
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
package com.example.demo.util.validation;
|
||||
|
||||
import jakarta.validation.ConstraintViolation;
|
||||
import jakarta.validation.Validation;
|
||||
import jakarta.validation.Validator;
|
||||
import jakarta.validation.ValidatorFactory;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Component
|
||||
public class ValidatorUtil {
|
||||
private final Validator validator;
|
||||
|
||||
public ValidatorUtil() {
|
||||
try (ValidatorFactory factory = Validation.buildDefaultValidatorFactory()) {
|
||||
this.validator = factory.getValidator();
|
||||
}
|
||||
}
|
||||
|
||||
public <T> void validate(T object) {
|
||||
final Set<ConstraintViolation<T>> errors = validator.validate(object);
|
||||
if (!errors.isEmpty()) {
|
||||
throw new ValidationException(errors.stream()
|
||||
.map(ConstraintViolation::getMessage)
|
||||
.collect(Collectors.toSet()));
|
||||
}
|
||||
}
|
||||
}
|
@ -1 +1,11 @@
|
||||
|
||||
server.port=8080
|
||||
spring.datasource.url=jdbc:h2:file:./data
|
||||
spring.datasource.driverClassName=org.h2.Driver
|
||||
spring.datasource.username=sa
|
||||
spring.datasource.password=password
|
||||
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
|
||||
spring.jpa.hibernate.ddl-auto=update
|
||||
spring.h2.console.enabled=true
|
||||
spring.h2.console.settings.trace=false
|
||||
spring.h2.console.settings.web-allow-others=false
|
||||
|
25
src/main/resources/static/style.css
Normal file
25
src/main/resources/static/style.css
Normal file
@ -0,0 +1,25 @@
|
||||
body {
|
||||
background: #f54d9a;
|
||||
}
|
||||
|
||||
.logo {
|
||||
background: #FF9CCE;
|
||||
}
|
||||
|
||||
main {
|
||||
padding: 2%;
|
||||
margin: 10% 5%;
|
||||
}
|
||||
|
||||
main img {
|
||||
width: 100%;
|
||||
object-fit: cover;
|
||||
}
|
||||
|
||||
form {
|
||||
padding: 1%;
|
||||
}
|
||||
|
||||
.product-div {
|
||||
background: #e874ac;
|
||||
}
|
38
src/main/resources/templates/Header.html
Normal file
38
src/main/resources/templates/Header.html
Normal file
@ -0,0 +1,38 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Title</title>
|
||||
<link rel="stylesheet" href="/style.css"/>
|
||||
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css"
|
||||
rel="stylesheet" integrity="sha384-KK94CHFLLe+nY2dmCWGMq91rCGa5gtU4mk92HdvYe+M/SXH301p5ILy+dN9+nJOZ"
|
||||
crossorigin="anonymous">
|
||||
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.11.7/dist/umd/popper.min.js"
|
||||
integrity="sha384-zYPOMqeu1DAVkHiLqWBUTcbYfZ8osu1Nd6Z89ify25QV9guujx43ITvfi12/QExE"
|
||||
crossorigin="anonymous"></script>
|
||||
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/js/bootstrap.min.js"
|
||||
integrity="sha384-Y4oOpwW3duJdCWv5ly8SCFYWqFDsfob/3GkgExXKV4idmbt98QcxXYs9UoXAB7BZ"
|
||||
crossorigin="anonymous"></script>
|
||||
</head>
|
||||
<body>
|
||||
<header class="logo d-flex flex-wrap align-items-center justify-content-center
|
||||
justify-content-md-between py-3 mb-4 border-bottom" th:fragment="header">
|
||||
<a href="/" class="d-flex align-items-center col-md-3 mb-2 mb-md-0 text-dark text-decoration-none">
|
||||
<svg class="bi me-2" width="40" height="32" role="img" aria-label="Bootstrap"><use xlink:href="#bootstrap"></use></svg>
|
||||
</a>
|
||||
<ul class="nav col-12 col-md-auto mb-2 justify-content-center mb-md-0">
|
||||
<li th:if="${user.firstName != null}"><a href="/product/my_products" class="nav-link px-2 link-dark">My Products</a></li>
|
||||
<li><a href="/product" class="nav-link px-2 link-dark">Products</a></li>
|
||||
<li th:if="${user.firstName != null}"><a href="/order/masters_order" class="nav-link px-2 link-dark">Orders</a></li>
|
||||
</ul>
|
||||
<div th:if="${user.firstName != null}" class="col-md-3 text-end">
|
||||
<a href="/order" class="btn btn-outline-primary me-2">Shop List</a>
|
||||
<a href="/master" class="nav-link px-2 link-dark" th:text="${user.firstName + ' ' + user.lastName}">User Name</a>
|
||||
</div>
|
||||
<div th:unless="${user.firstName != null}" class="col-md-3 text-end">
|
||||
<a href="/master/login" class="btn btn-outline-primary me-2">Login</a>
|
||||
<a href="/master" class="btn btn-primary">Register</a>
|
||||
</div>
|
||||
</header>
|
||||
</body>
|
||||
</html>
|
38
src/main/resources/templates/Login.html
Normal file
38
src/main/resources/templates/Login.html
Normal file
@ -0,0 +1,38 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Login</title>
|
||||
<link rel="stylesheet" href="/style.css"/>
|
||||
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css"
|
||||
rel="stylesheet" integrity="sha384-KK94CHFLLe+nY2dmCWGMq91rCGa5gtU4mk92HdvYe+M/SXH301p5ILy+dN9+nJOZ"
|
||||
crossorigin="anonymous">
|
||||
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.11.7/dist/umd/popper.min.js"
|
||||
integrity="sha384-zYPOMqeu1DAVkHiLqWBUTcbYfZ8osu1Nd6Z89ify25QV9guujx43ITvfi12/QExE"
|
||||
crossorigin="anonymous"></script>
|
||||
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/js/bootstrap.min.js"
|
||||
integrity="sha384-Y4oOpwW3duJdCWv5ly8SCFYWqFDsfob/3GkgExXKV4idmbt98QcxXYs9UoXAB7BZ"
|
||||
crossorigin="anonymous"></script>
|
||||
</head>
|
||||
<body style="background: #f54d9a">
|
||||
<header th:insert="~{Header :: header}"></header>
|
||||
<form method="post" th:object="${user}">
|
||||
<div class="container">
|
||||
<div class="row gy-5 p-2">
|
||||
<input type="email" name="email" class="form-control"
|
||||
id="email" placeholder="Email" th:field="*{email}">
|
||||
</div>
|
||||
<div class="row gy-5 p-2">
|
||||
<input type="password" name="password" class="form-control"
|
||||
id="password" placeholder="Password" th:field="*{password}">
|
||||
</div>
|
||||
<div class="row gy-5">
|
||||
<button type="submit" class="btn btn-primary">Sing In</button>
|
||||
</div>
|
||||
<div class="row gy-5 ">
|
||||
<a href="/master/register" class="btn btn-primary">Register</a>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
</body>
|
||||
</html>
|
51
src/main/resources/templates/MastersOrders.html
Normal file
51
src/main/resources/templates/MastersOrders.html
Normal file
@ -0,0 +1,51 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Login</title>
|
||||
<link rel="stylesheet" href="/style.css"/>
|
||||
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css"
|
||||
rel="stylesheet" integrity="sha384-KK94CHFLLe+nY2dmCWGMq91rCGa5gtU4mk92HdvYe+M/SXH301p5ILy+dN9+nJOZ"
|
||||
crossorigin="anonymous">
|
||||
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.11.7/dist/umd/popper.min.js"
|
||||
integrity="sha384-zYPOMqeu1DAVkHiLqWBUTcbYfZ8osu1Nd6Z89ify25QV9guujx43ITvfi12/QExE"
|
||||
crossorigin="anonymous"></script>
|
||||
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/js/bootstrap.min.js"
|
||||
integrity="sha384-Y4oOpwW3duJdCWv5ly8SCFYWqFDsfob/3GkgExXKV4idmbt98QcxXYs9UoXAB7BZ"
|
||||
crossorigin="anonymous"></script>
|
||||
</head>
|
||||
<body style="background: #f54d9a">
|
||||
<header th:insert="~{Header :: header}"></header>
|
||||
<div>
|
||||
<h1 class="text-center">Masters</h1>
|
||||
|
||||
<div class="container">
|
||||
<div class="row gy-5 p-2">
|
||||
<form th:action="@{/order/masters_order}" method="get">
|
||||
<select th:each="master : ${masters}" th:name="master_id" class="form-select" aria-label="Default select example">
|
||||
<option th:id="master_id" th:value="${master.id}" >
|
||||
<span th:text="${master.firstName + ' ' + master.lastName }"></span>
|
||||
</option>
|
||||
</select>
|
||||
<button class="btn btn-primary w-100">Check</button>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<table class="table table-dark">
|
||||
<thead>
|
||||
<tr>
|
||||
<th scope="col">#</th>
|
||||
<th scope="col">Cost</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr th:each="order : ${orders}">
|
||||
<th scope="row" th:text="${order.id}"></th>
|
||||
<td th:text="${order.cost}"></td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
48
src/main/resources/templates/OrderPage.html
Normal file
48
src/main/resources/templates/OrderPage.html
Normal file
@ -0,0 +1,48 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Login</title>
|
||||
<link rel="stylesheet" href="/style.css"/>
|
||||
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css"
|
||||
rel="stylesheet" integrity="sha384-KK94CHFLLe+nY2dmCWGMq91rCGa5gtU4mk92HdvYe+M/SXH301p5ILy+dN9+nJOZ"
|
||||
crossorigin="anonymous">
|
||||
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.11.7/dist/umd/popper.min.js"
|
||||
integrity="sha384-zYPOMqeu1DAVkHiLqWBUTcbYfZ8osu1Nd6Z89ify25QV9guujx43ITvfi12/QExE"
|
||||
crossorigin="anonymous"></script>
|
||||
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/js/bootstrap.min.js"
|
||||
integrity="sha384-Y4oOpwW3duJdCWv5ly8SCFYWqFDsfob/3GkgExXKV4idmbt98QcxXYs9UoXAB7BZ"
|
||||
crossorigin="anonymous"></script>
|
||||
</head>
|
||||
<body style="background: #f54d9a">
|
||||
<header th:insert="~{Header :: header}"></header>
|
||||
<div>
|
||||
<h1 class="text-center">Order</h1>
|
||||
<form method="post" th:object="${order}">
|
||||
<div class="container">
|
||||
<div th:each="product : ${order.products}" class="row product-div p-2 m-2">
|
||||
<input type="hidden" name="id_item" th:name="id" th:value="${product.id}">
|
||||
<div class="col">
|
||||
<h2 th:text="${product.name}"></h2>
|
||||
</div>
|
||||
<div class="col">
|
||||
<h3 th:text="${product.cost} + '$'"></h3>
|
||||
</div>
|
||||
<div class="col">
|
||||
<button name="action" value="delete" class="btn btn-primary w-100 h-100 text-lg-center">Delete</button>
|
||||
</div>
|
||||
</div>
|
||||
<div class="row">
|
||||
<div class="col">
|
||||
<h2 th:text="${fullCost} + '$'"></h2>
|
||||
</div>
|
||||
<div class="col">
|
||||
<h3><button name="action" value="buy" class="btn btn-primary w-100">Buy</button>
|
||||
</h3>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
27
src/main/resources/templates/ProductCreate.html
Normal file
27
src/main/resources/templates/ProductCreate.html
Normal file
@ -0,0 +1,27 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Login</title>
|
||||
<link rel="stylesheet" href="/style.css"/>
|
||||
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css"
|
||||
rel="stylesheet" integrity="sha384-KK94CHFLLe+nY2dmCWGMq91rCGa5gtU4mk92HdvYe+M/SXH301p5ILy+dN9+nJOZ"
|
||||
crossorigin="anonymous">
|
||||
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.11.7/dist/umd/popper.min.js"
|
||||
integrity="sha384-zYPOMqeu1DAVkHiLqWBUTcbYfZ8osu1Nd6Z89ify25QV9guujx43ITvfi12/QExE"
|
||||
crossorigin="anonymous"></script>
|
||||
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/js/bootstrap.min.js"
|
||||
integrity="sha384-Y4oOpwW3duJdCWv5ly8SCFYWqFDsfob/3GkgExXKV4idmbt98QcxXYs9UoXAB7BZ"
|
||||
crossorigin="anonymous"></script>
|
||||
</head>
|
||||
<body style="background: #f54d9a">
|
||||
<header th:insert="~{Header :: header}"></header>
|
||||
<div>
|
||||
<form method="post" th:object="${product}">
|
||||
<input type="text" th:field="*{name}" th:value="${product.getName()}" name="name" class="form-control" id="name">
|
||||
<input type="number" th:field="*{cost}" th:value="${product.getCost()}" name="cost" class="form-control" id="cost">
|
||||
<button type="submit" class="btn btn-primary" th:text="${buttonText}">Add</button>
|
||||
</form>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
38
src/main/resources/templates/Products.html
Normal file
38
src/main/resources/templates/Products.html
Normal file
@ -0,0 +1,38 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Title</title>
|
||||
<link rel="stylesheet" href="/style.css"/>
|
||||
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css"
|
||||
rel="stylesheet" integrity="sha384-KK94CHFLLe+nY2dmCWGMq91rCGa5gtU4mk92HdvYe+M/SXH301p5ILy+dN9+nJOZ"
|
||||
crossorigin="anonymous">
|
||||
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.11.7/dist/umd/popper.min.js"
|
||||
integrity="sha384-zYPOMqeu1DAVkHiLqWBUTcbYfZ8osu1Nd6Z89ify25QV9guujx43ITvfi12/QExE"
|
||||
crossorigin="anonymous"></script>
|
||||
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/js/bootstrap.min.js"
|
||||
integrity="sha384-Y4oOpwW3duJdCWv5ly8SCFYWqFDsfob/3GkgExXKV4idmbt98QcxXYs9UoXAB7BZ"
|
||||
crossorigin="anonymous"></script>
|
||||
</head>
|
||||
<body style="background: #f54d9a">
|
||||
<header th:insert="~{Header :: header}"></header>
|
||||
|
||||
<div class="container">
|
||||
<div th:each="item : ${products}" class="row product-div p-2 m-2">
|
||||
<form method="post" th:object="${item}" >
|
||||
<input type="hidden" th:name="id" th:value="${item.getId()}">
|
||||
<div class="col">
|
||||
<h2 th:text="${item.getName()}" th:name="*{name}"></h2>
|
||||
</div>
|
||||
<div class="col">
|
||||
<h3 th:text="${item.getCost()} + '$'" th:name="*{cost}"></h3>
|
||||
</div>
|
||||
<div class="col">
|
||||
<button type="submit" class="btn btn-primary w-100">Buy</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</body>
|
||||
</html>
|
55
src/main/resources/templates/UserPage.html
Normal file
55
src/main/resources/templates/UserPage.html
Normal file
@ -0,0 +1,55 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Title</title>
|
||||
<link rel="stylesheet" href="/style.css"/>
|
||||
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css"
|
||||
rel="stylesheet" integrity="sha384-KK94CHFLLe+nY2dmCWGMq91rCGa5gtU4mk92HdvYe+M/SXH301p5ILy+dN9+nJOZ"
|
||||
crossorigin="anonymous">
|
||||
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.11.7/dist/umd/popper.min.js"
|
||||
integrity="sha384-zYPOMqeu1DAVkHiLqWBUTcbYfZ8osu1Nd6Z89ify25QV9guujx43ITvfi12/QExE"
|
||||
crossorigin="anonymous"></script>
|
||||
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/js/bootstrap.min.js"
|
||||
integrity="sha384-Y4oOpwW3duJdCWv5ly8SCFYWqFDsfob/3GkgExXKV4idmbt98QcxXYs9UoXAB7BZ"
|
||||
crossorigin="anonymous"></script>
|
||||
</head>
|
||||
<body style="background: #f54d9a">
|
||||
<header th:insert="~{Header :: header}"></header>
|
||||
<form method="post" th:object="${user}">
|
||||
<div class="container">
|
||||
<div class="row gy-5 p-2">
|
||||
<input type="text" name="name" class="form-control"
|
||||
id="name" placeholder="Name" th:value="${user.firstName}" th:field="*{firstName}">
|
||||
</div>
|
||||
<div class="row gy-5 p-2">
|
||||
<input type="text" name="surname" class="form-control"
|
||||
id="surname" placeholder="Surname" th:value="${user.lastName}" th:field="*{lastName}">
|
||||
</div>
|
||||
<div class="row gy-5 p-2">
|
||||
<input type="email" name="email" class="form-control"
|
||||
id="email" placeholder="Email" th:value="${user.email}" th:field="*{email}">
|
||||
</div>
|
||||
<div class="row gy-5 p-2">
|
||||
<input type="password" name="password" class="form-control"
|
||||
id="password" placeholder="Password" th:value="${user.password}" th:field="*{password}">
|
||||
</div>
|
||||
<div th:if='${user.firstName != null}' class="row align-items-start">
|
||||
<div class="col">
|
||||
<button name="action" class="btn btn-primary w-100" value="update">Update</button>
|
||||
</div>
|
||||
<div class="col">
|
||||
<button name="action" th:onclick="/master/log_out"
|
||||
class="btn btn-primary w-100" value="log_out">Log Out</button>
|
||||
</div>
|
||||
</div>
|
||||
<div th:unless='${user.firstName != null}' class="row gy-5">
|
||||
<button name="action" value="register" class="btn btn-primary w-100">Register</button>
|
||||
</div>
|
||||
<div th:unless='${user.firstName != null}' class="row align-items-start">
|
||||
<a href="/sing_in" class="btn btn-primary">Sing In</a>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
</body>
|
||||
</html>
|
37
src/main/resources/templates/UserProducts.html
Normal file
37
src/main/resources/templates/UserProducts.html
Normal file
@ -0,0 +1,37 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Title</title>
|
||||
<link rel="stylesheet" href="/style.css"/>
|
||||
<a></a>
|
||||
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css"
|
||||
rel="stylesheet" integrity="sha384-KK94CHFLLe+nY2dmCWGMq91rCGa5gtU4mk92HdvYe+M/SXH301p5ILy+dN9+nJOZ"
|
||||
crossorigin="anonymous">
|
||||
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.11.7/dist/umd/popper.min.js"
|
||||
integrity="sha384-zYPOMqeu1DAVkHiLqWBUTcbYfZ8osu1Nd6Z89ify25QV9guujx43ITvfi12/QExE"
|
||||
crossorigin="anonymous"></script>
|
||||
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/js/bootstrap.min.js"
|
||||
integrity="sha384-Y4oOpwW3duJdCWv5ly8SCFYWqFDsfob/3GkgExXKV4idmbt98QcxXYs9UoXAB7BZ"
|
||||
crossorigin="anonymous"></script>
|
||||
</head>
|
||||
<body style="background: #f54d9a">
|
||||
<header th:insert="~{Header :: header}"></header>
|
||||
<div class="container">
|
||||
<div class="row align-content-center">
|
||||
<a href="/product/create_product" class="btn btn-primary w-100 h-100 text-lg-center">Create</a>
|
||||
</div>
|
||||
<div th:each="item : ${products}" class="row product-div p-2 m-2">
|
||||
<div class="col">
|
||||
<h2 th:text="${item.name}"></h2>
|
||||
</div>
|
||||
<div class="col">
|
||||
<h3 th:text="${item.cost} + '$'"></h3>
|
||||
</div>
|
||||
<div class="col">
|
||||
<a th:href="'/product/update_product/' + ${{item.id}}" class="btn btn-primary w-100 h-100 text-lg-center">Edit</a>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
@ -1,13 +0,0 @@
|
||||
package com.example.demo;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
@SpringBootTest
|
||||
class Demo1ApplicationTests {
|
||||
|
||||
@Test
|
||||
void contextLoads() {
|
||||
}
|
||||
|
||||
}
|
133
src/test/java/com/example/demo/DemoApplicationTests.java
Normal file
133
src/test/java/com/example/demo/DemoApplicationTests.java
Normal file
@ -0,0 +1,133 @@
|
||||
//package com.example.demo;
|
||||
//
|
||||
//import jakarta.persistence.EntityNotFoundException;
|
||||
//import org.junit.jupiter.api.Assertions;
|
||||
//import org.junit.jupiter.api.Test;
|
||||
//import org.springframework.beans.factory.annotation.Autowired;
|
||||
//import org.springframework.boot.test.context.SpringBootTest;
|
||||
//import com.example.demo.order.*;
|
||||
//import com.example.demo.master.*;
|
||||
//import com.example.demo.product.*;
|
||||
//
|
||||
//import java.util.List;
|
||||
//
|
||||
//@SpringBootTest
|
||||
//class DemoApplicationTests {
|
||||
//
|
||||
// @Autowired
|
||||
// private MasterService masterService;
|
||||
// @Autowired
|
||||
// private ProductService productService;
|
||||
// @Autowired
|
||||
// private OrderService orderService;
|
||||
//
|
||||
// @Test
|
||||
// void testOrder() {
|
||||
// productService.deleteAllProducts();
|
||||
// orderService.deleteAllOrders();
|
||||
// masterService.deleteAllMasters();
|
||||
//
|
||||
// final Product product1 = productService.addProduct("Машинка", 300);
|
||||
// final Product product2 = productService.addProduct("Ключ", 200);
|
||||
//
|
||||
// final Master master1 = masterService.addMaster("Кирилл", "Петрович");
|
||||
//
|
||||
// final Order order0 = orderService.addOrder(masterService.findMaster(master1.getId()));
|
||||
// final Order order1 = orderService.findOrder(order0.getId());
|
||||
// Assertions.assertEquals(order0, order1);
|
||||
//
|
||||
// Assertions.assertEquals(masterService
|
||||
// .findMaster(master1.getId()).getOrders().size(), 1);
|
||||
// orderService.deleteAllOrders();
|
||||
// Assertions.assertThrows(EntityNotFoundException.class, () -> orderService.findOrder(-1L));
|
||||
// final Master customer2 = masterService.addMaster("Александр", "Игоревич");
|
||||
// final Order order2 = orderService
|
||||
// .addOrder(masterService.findMaster(customer2.getId()));
|
||||
// orderService.addProduct(order2.getId(), product1);
|
||||
// orderService.addProduct(order2.getId(), product1);
|
||||
// orderService.deleteProductsInOrder(order2.getId(), product1);
|
||||
//
|
||||
// Assertions.assertEquals(orderService
|
||||
// .findOrder(order2.getId()).getProducts().size(), 1);
|
||||
// masterService.deleteMaster(customer2.getId());
|
||||
// Assertions.assertThrows(EntityNotFoundException.class, () -> masterService.findMaster(customer2.getId()));
|
||||
// Assertions.assertThrows(EntityNotFoundException.class, () -> orderService.findOrder(order2.getId()));
|
||||
// Assertions.assertEquals(orderService.findAllOrders().size(), 0);
|
||||
// }
|
||||
//
|
||||
// @Test()
|
||||
// void testMaster() {
|
||||
// masterService.deleteAllMasters();
|
||||
// final Master customer = masterService.addMaster("Иван", "Иванов");
|
||||
// Assertions.assertNotNull(customer.getId());
|
||||
//
|
||||
// masterService.deleteAllMasters();
|
||||
// final Master customer1 = masterService.addMaster("Иван", "Иванов");
|
||||
// final Master findMaster = masterService.findMaster(customer1.getId());
|
||||
// Assertions.assertEquals(customer1, findMaster);
|
||||
//
|
||||
// masterService.deleteAllMasters();
|
||||
// Assertions.assertThrows(EntityNotFoundException.class, () -> masterService.findMaster(-1L));
|
||||
//
|
||||
// masterService.deleteAllMasters();
|
||||
// masterService.addMaster("Иван", "Иванов");
|
||||
// masterService.addMaster("Петр", "Петров");
|
||||
// final List<Master> customers1 = masterService.findAllMasters();
|
||||
// Assertions.assertEquals(customers1.size(), 2);
|
||||
//
|
||||
// masterService.deleteAllMasters();
|
||||
// final List<Master> customers2 = masterService.findAllMasters();
|
||||
//
|
||||
// Assertions.assertEquals(customers2.size(), 0);
|
||||
// }
|
||||
//
|
||||
// @Test
|
||||
// void testProduct() {
|
||||
// productService.deleteAllProducts();
|
||||
// final Product ticket1 = productService.addProduct("Руно", 100);
|
||||
// Assertions.assertNotNull(ticket1.getId());
|
||||
//
|
||||
// productService.deleteAllProducts();
|
||||
// final Product ticket2 = productService.addProduct("Шоколад", 100);
|
||||
// final Product findProduct = productService.findProduct(ticket2.getId());
|
||||
// Assertions.assertEquals(ticket2, findProduct);
|
||||
//
|
||||
// productService.deleteAllProducts();
|
||||
// Assertions.assertThrows(EntityNotFoundException.class, () -> productService.findProduct(-1L));
|
||||
//
|
||||
// productService.deleteAllProducts();
|
||||
// productService.addProduct("Автомобиль", 100);
|
||||
// productService.addProduct("Мячик", 100);
|
||||
// final List<Product> tickets1 = productService.findAllProducts();
|
||||
// Assertions.assertEquals(tickets1.size(), 2);
|
||||
//
|
||||
// productService.deleteAllProducts();
|
||||
// final List<Product> tickets2 = productService.findAllProducts();
|
||||
// Assertions.assertEquals(tickets2.size(), 0);
|
||||
// }
|
||||
//
|
||||
// @Test
|
||||
// void testCommand() {
|
||||
// productService.deleteAllProducts();
|
||||
// orderService.deleteAllOrders();
|
||||
// masterService.deleteAllMasters();
|
||||
//
|
||||
// final Product product1 = productService.addProduct("Машинка", 300);
|
||||
// final Product product2 = productService.addProduct("Ключ", 200);
|
||||
//
|
||||
// final Master master1 = masterService.addMaster("Кирилл", "Петрович");
|
||||
// final Master master2 = masterService.addMaster("Александр", "Камугович");
|
||||
//
|
||||
// final Order order0 = orderService.addOrder(masterService.findMaster(master1.getId()));
|
||||
// final Order order1 = orderService.addOrder(masterService.findMaster(master2.getId()));
|
||||
// final Order order2 = orderService.addOrder(masterService.findMaster(master2.getId()));
|
||||
// orderService.addProduct(order0.getId(), product1);
|
||||
// orderService.addProduct(order1.getId(), product1);
|
||||
// orderService.addProduct(order2.getId(), product1);
|
||||
//
|
||||
// Assertions.assertTrue(orderService.findOrdersWithProduct(master1.getId(), product1).contains(order0));
|
||||
// Assertions.assertTrue(orderService.findOrdersWithProduct(master1.getId(), product2).isEmpty());
|
||||
// Assertions.assertTrue(orderService.findOrdersWithProduct(master2.getId(), product1).contains(order1)
|
||||
// && orderService.findOrdersWithProduct(master2.getId(), product1).contains(order2));
|
||||
// }
|
||||
//}
|
6
src/test/resources/application.properties
Normal file
6
src/test/resources/application.properties
Normal file
@ -0,0 +1,6 @@
|
||||
spring.datasource.url=jdbc:h2:mem:testdb
|
||||
spring.datasource.driverClassName=org.h2.Driver
|
||||
spring.datasource.username=sa
|
||||
spring.datasource.password=password
|
||||
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
|
||||
spring.jpa.hibernate.ddl-auto=create-drop
|
Loading…
Reference in New Issue
Block a user