Compare commits

...

10 Commits

33 changed files with 1125 additions and 47 deletions

3
.idea/.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
# Default ignored files
/shelf/
/workspace.xml

10
.idea/compiler.xml Normal file
View File

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<bytecodeTargetLevel>
<module name="spring_online_calculator" target="17" />
<module name="spring_online_calculator.main" target="17" />
<module name="spring_online_calculator.test" target="17" />
</bytecodeTargetLevel>
</component>
</project>

8
.idea/discord.xml Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="DiscordProjectSettings">
<option name="show" value="ASK" />
<option name="nameOverrideEnabled" value="true" />
<option name="description" value="" />
</component>
</project>

19
.idea/gradle.xml Normal file
View File

@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="GradleMigrationSettings" migrationVersion="1" />
<component name="GradleSettings">
<option name="linkedExternalProjectsSettings">
<GradleProjectSettings>
<option name="delegatedBuild" value="true" />
<option name="testRunner" value="GRADLE" />
<option name="distributionType" value="DEFAULT_WRAPPED" />
<option name="externalProjectPath" value="$PROJECT_DIR$/spring_online_calculator" />
<option name="modules">
<set>
<option value="$PROJECT_DIR$/spring_online_calculator" />
</set>
</option>
</GradleProjectSettings>
</option>
</component>
</project>

20
.idea/jarRepositories.xml Normal file
View File

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RemoteRepositoriesConfiguration">
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Maven Central repository" />
<option name="url" value="https://repo1.maven.org/maven2" />
</remote-repository>
<remote-repository>
<option name="id" value="jboss.community" />
<option name="name" value="JBoss Community repository" />
<option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" />
</remote-repository>
<remote-repository>
<option name="id" value="MavenRepo" />
<option name="name" value="MavenRepo" />
<option name="url" value="https://repo.maven.apache.org/maven2/" />
</remote-repository>
</component>
</project>

6
.idea/jpa-buddy.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="JpaBuddyIdeaProjectConfig">
<option name="defaultUnitInitialized" value="true" />
</component>
</project>

8
.idea/misc.xml Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" project-jdk-name="temurin-17" project-jdk-type="JavaSDK" />
<component name="ProjectType">
<option name="id" value="jpab" />
</component>
</project>

8
.idea/modules.xml Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/С репозитория.iml" filepath="$PROJECT_DIR$/.idea/С репозитория.iml" />
</modules>
</component>
</project>

124
.idea/uiDesigner.xml Normal file
View File

@ -0,0 +1,124 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Palette2">
<group name="Swing">
<item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" />
</item>
<item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" />
</item>
<item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.png" removable="false" auto-create-binding="false" can-attach-label="true">
<default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" />
</item>
<item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" />
<initial-values>
<property name="text" value="Button" />
</initial-values>
</item>
<item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="RadioButton" />
</initial-values>
</item>
<item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="CheckBox" />
</initial-values>
</item>
<item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" />
<initial-values>
<property name="text" value="Label" />
</initial-values>
</item>
<item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" />
</item>
<item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" />
</item>
<item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1">
<preferred-size width="-1" height="20" />
</default-constraints>
</item>
<item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" />
</item>
<item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" />
</item>
</group>
</component>
</project>

6
.idea/vcs.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>

View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

View File

@ -35,3 +35,5 @@ out/
### VS Code ### ### VS Code ###
.vscode/ .vscode/
*.db

View File

@ -1,10 +1,10 @@
plugins { plugins {
id 'java' id 'org.springframework.boot' version '2.6.3'
id 'org.springframework.boot' version '2.7.8'
id 'io.spring.dependency-management' version '1.0.15.RELEASE' id 'io.spring.dependency-management' version '1.0.15.RELEASE'
id 'java'
} }
group = 'com.example' group = 'ru.ulstu.is'
version = '0.0.1-SNAPSHOT' version = '0.0.1-SNAPSHOT'
sourceCompatibility = '17' sourceCompatibility = '17'
@ -14,7 +14,12 @@ repositories {
dependencies { dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.boot:spring-boot-starter-web'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'com.h2database:h2:2.1.210'
implementation group: 'org.springdoc', name: 'springdoc-openapi-ui', version: '1.6.5'
testImplementation 'org.springframework.boot:spring-boot-starter-test' testImplementation 'org.springframework.boot:spring-boot-starter-test'
} }

View File

@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.6-bin.zip distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.3-bin.zip
zipStoreBase=GRADLE_USER_HOME zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists zipStorePath=wrapper/dists

View File

@ -205,12 +205,6 @@ set -- \
org.gradle.wrapper.GradleWrapperMain \ 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. # Use "xargs" to parse quoted args.
# #
# With -n1 it outputs one arg per line, with the quotes and backslashes removed. # With -n1 it outputs one arg per line, with the quotes and backslashes removed.

View File

@ -40,7 +40,7 @@ if defined JAVA_HOME goto findJavaFromJavaHome
set JAVA_EXE=java.exe set JAVA_EXE=java.exe
%JAVA_EXE% -version >NUL 2>&1 %JAVA_EXE% -version >NUL 2>&1
if %ERRORLEVEL% equ 0 goto execute if "%ERRORLEVEL%" == "0" goto execute
echo. echo.
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
@ -75,15 +75,13 @@ set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
:end :end
@rem End local scope for the variables with windows NT shell @rem End local scope for the variables with windows NT shell
if %ERRORLEVEL% equ 0 goto mainEnd if "%ERRORLEVEL%"=="0" goto mainEnd
:fail :fail
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
rem the _cmd.exe /c_ return code! rem the _cmd.exe /c_ return code!
set EXIT_CODE=%ERRORLEVEL% if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
if %EXIT_CODE% equ 0 set EXIT_CODE=1 exit /b 1
if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE%
exit /b %EXIT_CODE%
:mainEnd :mainEnd
if "%OS%"=="Windows_NT" endlocal if "%OS%"=="Windows_NT" endlocal

View File

@ -1 +1 @@
rootProject.name = 'spring_online_calculator' rootProject.name = 'premium_store'

View File

@ -1,13 +0,0 @@
package com.example.spring_online_calculator;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringOnlineCalculatorApplication {
public static void main(String[] args) {
SpringApplication.run(SpringOnlineCalculatorApplication.class, args);
}
}

View File

@ -0,0 +1,11 @@
package premium_store;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class PremiumStoreApplication {
public static void main(String[] args) {
SpringApplication.run(PremiumStoreApplication.class, args);
}
}

View File

@ -0,0 +1,100 @@
package premium_store.model;
import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
@Entity
public class Client {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(nullable = false, length = 255)
private String nickName;
@Column(nullable = false, length = 255)
private String email;
private Integer balance;
@OneToMany(fetch= FetchType.EAGER)
private List<Tank> tanks = new ArrayList<>();
public Client(){ }
public Client(String nickName, String email, Integer balance){
this.nickName = nickName;
this.email = email;
this.balance = balance;
}
public Long getId(){
return id;
}
public String getNickName(){
return nickName;
}
public void setNickName(String nickName){
this.nickName = nickName;
}
public String getEmail(){
return email;
}
public void setEmail(String email){
this.email = email;
}
public Integer getBalance(){
return balance;
}
public void setBalance(Integer balance){
this.balance = balance;
}
public List<Tank> getTanks(){
return tanks;
}
public void setTanks(List<Tank> tanks){
this.tanks = tanks;
}
//метод для сравнения
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
Client client = (Client) o;
return Objects.equals(id, client.id);
}
@Override
public int hashCode() {
return Objects.hash(id);
}
//преобразование данных по объекту в строчку
@Override
public String toString() {
return "Client{" +
"id=" + id +
", nickName='" + nickName + '\'' +
", email='" + email + '\'' +
", balance='" + balance + '\'' +
", tanks='" + tanks.stream().map(Object::toString).collect(Collectors.joining(", ")) + '\'' +
'}';
}
}

View File

@ -0,0 +1,62 @@
package premium_store.model;
import javax.persistence.*;
import java.util.Objects;
@Entity
public class Level {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(nullable = false)
private int level;
public Level() {
}
public Level(int level) {
this.level = level;
}
//возвращает id
public Long getId() {
return id;
}
//возвращает нацию
public int getLevel() {
return level;
}
public void setLevel(int level) {
this.level = level;
}
//метод для сравнения
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
Level level = (Level) o;
return Objects.equals(id, level.id);
}
@Override
public int hashCode() {
return Objects.hash(id);
}
//преобразование данных по объекту в строчку
@Override
public String toString() {
return "Level{" +
"id=" + id +
", level='" + level + '}';
}
}

View File

@ -0,0 +1,77 @@
package premium_store.model;
import javax.persistence.*;
import java.util.*;
import java.util.stream.Collectors;
@Entity
public class Nation {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(nullable = false)
private String nation;
@OneToMany(mappedBy = "nation", fetch = FetchType.EAGER, cascade = CascadeType.MERGE)
private List<Tank> tanksOfThisNation;
public Nation() {
}
public Nation(String nation) {
this.nation = nation;
tanksOfThisNation = new ArrayList<>();
}
//возвращает id
public Long getId() {
return id;
}
//возвращает нацию
public String getNation() {
return nation;
}
public void setNation(String nation) {
this.nation = nation;
}
public List<Tank> getTanksOfThisNation(){
return tanksOfThisNation;
}
public void setTankOfThisNation(List<Tank> newTank){
this.tanksOfThisNation = newTank;
}
//метод для сравнения
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
Nation nation = (Nation) o;
return Objects.equals(id, nation.id);
}
@Override
public int hashCode() {
return Objects.hash(id);
}
//преобразование данных по объекту в строчку
@Override
public String toString() {
return "Nation{" +
"id=" + id +
", nation='" + nation + '\'' +
", tanksOfThisNation='" + tanksOfThisNation.stream().map(Object::toString).collect(Collectors.joining(", ")) + '\'' +
'}';
}
}

View File

@ -0,0 +1,99 @@
package premium_store.model;
import javax.persistence.*;
import java.util.Objects;
@Entity
public class Tank {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(nullable = false)
private String name;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "nation")
private Nation nation;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "level")
private Level level;
@Column(nullable = false)
private int cost;
public Tank() {
}
public Tank(String name, Nation nation, Level level, int cost) {
this.name = name;
this.nation = nation;
this.level = level;
this.cost = cost;
}
public Long getId(){
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getCost() {
return cost;
}
public void setCost(int cost) {
this.cost = cost;
}
public Level getLevel() {
return level;
}
public void setLevel(Level level) {
this.level = level;
}
public Nation getNation() {
return nation;
}
public void setNation(Nation nation) {
this.nation = nation;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Tank tank)) return false;
return Objects.equals(getId(), tank.getId()) && Objects.equals(getName(), tank.getName())
&& Objects.equals(getCost(), tank.getCost())
&& Objects.equals(getLevel(), tank.getLevel());
}
@Override
public int hashCode() {
return Objects.hash(id);
}
//преобразование данных по объекту в строчку
@Override
public String toString() {
return "Tank{" +
"id=" + id +
", name='" + name + '\'' +
", nation='" + nation.getId() + '\'' +
", level='" + level + '\'' +
", cost='" + cost + '\'' +
'}';
}
}

View File

@ -0,0 +1,88 @@
package premium_store.service;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import premium_store.model.Client;
import premium_store.model.Tank;
import javax.persistence.EntityManager;
import javax.persistence.EntityNotFoundException;
import javax.persistence.PersistenceContext;
import java.util.List;
@Service
public class ClientService {
@PersistenceContext
private EntityManager em;
@Transactional
public Client addClient(String newNickName, String newEmail, Integer newBallance) {
if (!StringUtils.hasText(newNickName) || !StringUtils.hasText(newEmail)) {
throw new IllegalArgumentException("NickName or newEmail is zero or empty");
}
final Client client = new Client(newNickName, newEmail, newBallance);
em.persist(client);
return client;
}
@Transactional(readOnly = true)
public Client findClient(Long id) {
final Client Client = em.find(Client.class, id);
if (Client == null) {
throw new EntityNotFoundException(String.format("Client with id [%s] is not found", id));
}
return Client;
}
@Transactional(readOnly = true)
public List<Client> findAllClients() {
return em.createQuery("select c from Client c", Client.class)
.getResultList();
}
@Transactional
public Client updateClient(Long id, String newNickName, String newEmail, Integer newBalance, List<Tank> newTanks) {
if (id <= 0) {
throw new IllegalArgumentException("Invalid id");
}
final Client currentClient = findClient(id);
if (StringUtils.hasText(newNickName)){
currentClient.setNickName(newNickName);
}
if(StringUtils.hasText(newEmail)){
currentClient.setEmail(newEmail);
}
if(newBalance != null){
currentClient.setBalance(newBalance);
}
if(newTanks != null){
currentClient.setTanks(newTanks);
}
return em.merge(currentClient);
}
@Transactional
public Client deleteClient(Long id) {
final Client currentClient = findClient(id);
em.remove(currentClient);
return currentClient;
}
//прямой sql-запрос на удаление всех записей в таблице
@Transactional
public void deleteAllClients() {
em.createQuery("Delete from Client").executeUpdate();
}
}

View File

@ -0,0 +1,70 @@
package premium_store.service;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import premium_store.model.Level;
import javax.persistence.EntityManager;
import javax.persistence.EntityNotFoundException;
import javax.persistence.PersistenceContext;
import java.util.List;
@Service
public class LevelService {
@PersistenceContext
private EntityManager em;
@Transactional
public Level addLevel(int newLevel) {
if (newLevel <= 0) {
throw new IllegalArgumentException("Level name is zero or less");
}
final Level level = new Level(newLevel);
em.persist(level);
return level;
}
@Transactional(readOnly = true)
public Level findLevel(Long id) {
final Level level = em.find(Level.class, id);
if (level == null) {
throw new EntityNotFoundException(String.format("Level with id [%s] is not found", id));
}
return level;
}
@Transactional(readOnly = true)
public List<Level> findAllLevels() {
return em.createQuery("select s from Level s", Level.class)
.getResultList();
}
@Transactional
public Level updateLevel(Long id, int newLevel) {
if (newLevel <= 0) {
throw new IllegalArgumentException("Level name is zero or less");
}
final Level currentLevel = findLevel(id);
currentLevel.setLevel(newLevel);
return em.merge(currentLevel);
}
@Transactional
public Level deleteLevel(Long id) {
final Level currentLevel = findLevel(id);
em.remove(currentLevel);
return currentLevel;
}
@Transactional
public void deleteAllLevels() {
em.createQuery("Delete from Level").executeUpdate();
}
}

View File

@ -0,0 +1,79 @@
package premium_store.service;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import premium_store.model.Nation;
import premium_store.model.Tank;
import javax.persistence.EntityManager;
import javax.persistence.EntityNotFoundException;
import javax.persistence.PersistenceContext;
import java.util.List;
@Service
public class NationService {
@PersistenceContext
private EntityManager em;
@Transactional
public Nation addNation(String nameNation){
if(!StringUtils.hasText(nameNation)){
throw new IllegalArgumentException("The name of the nation is null or empty");
}
final Nation nation = new Nation(nameNation);
em.persist(nation);
return nation;
}
@Transactional(readOnly = true)
public Nation findNation(Long id){
final Nation nation = em.find(Nation.class, id);
if(nation == null){
throw new EntityNotFoundException(String.format("Nation with id [%s] is not found", id));
}
return nation;
}
@Transactional(readOnly = true)
public List<Nation> findAllNations() {
return em.createQuery("select n from Nation n", Nation.class)
.getResultList();
}
@Transactional
public Nation updateNation(Long id, String newNation, List<Tank> newTank) {
if(id <= 0){
throw new IllegalArgumentException(String.format("Incorrect id: [%s]", id));
}
final Nation currentNation = findNation(id);
if (StringUtils.hasText(newNation)) {
currentNation.setNation(newNation);
}
if(newTank.size() > 0){
currentNation.setTankOfThisNation(newTank);
}
return em.merge(currentNation);
}
@Transactional
public Nation deleteNAtion(Long id) {
final Nation currentNation = findNation(id);
em.remove(currentNation);
return currentNation;
}
@Transactional
public void deleteAllNations() {
em.createQuery("delete from Nation").executeUpdate();
}
}

View File

@ -0,0 +1,99 @@
package premium_store.service;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import premium_store.model.Level;
import premium_store.model.Nation;
import premium_store.model.Tank;
import javax.persistence.EntityManager;
import javax.persistence.EntityNotFoundException;
import javax.persistence.PersistenceContext;
import java.util.List;
@Service
public class TankService {
@PersistenceContext
private EntityManager em;
@Transactional
public Tank addTank(String newName, Nation newNation, Level newLevel, int newCost) {
if (!StringUtils.hasText(newName) || newNation == null || newLevel == null || newCost <= 0) {
throw new IllegalArgumentException("New Name, new Nation, new Level or newCost is zero or empty");
}
final Tank tank = new Tank(newName, newNation, newLevel, newCost);
em.persist(tank);
return tank;
}
@Transactional(readOnly = true)
public Tank findTank(Long id) {
final Tank Tank = em.find(Tank.class, id);
if (Tank == null) {
throw new EntityNotFoundException(String.format("Tank with id [%s] is not found", id));
}
return Tank;
}
@Transactional(readOnly = true)
public List<Tank> findAllTanks() {
return em.createQuery("select t from Tank t", Tank.class)
.getResultList();
}
@Transactional
public Tank updateTank(Long id, String newName, Nation newNation,
Level newLevel, int newCost) {
if (id <= 0) {
throw new IllegalArgumentException("Invalid id");
}
final Tank currentTank = findTank(id);
if (StringUtils.hasText(newName)){
currentTank.setName(newName);
}
if(newNation != null){
currentTank.setNation(newNation);
}
if(newLevel != null){
currentTank.setLevel(newLevel);
}
if(newCost > 0){
currentTank.setCost(newCost);
}
return em.merge(currentTank);
}
@Transactional
public Tank deleteTank(Long id) {
final Tank currentTank = findTank(id);
em.remove(currentTank);
return currentTank;
}
//прямой sql-запрос на удаление всех записей в таблице
@Transactional
public void deleteAllTanks() {
em.createQuery("Delete from Tank").executeUpdate();
}
@Transactional(readOnly = true)
public List<Tank> findTank(Level level, Nation nation) {
return em.createQuery("select t from Tank t where t.nation.nation = :Nid and t.level.level BETWEEN :Llevel AND :L2level", Tank.class)
.setParameter("Nid", nation.getNation())
.setParameter("Llevel", level.getLevel())
.setParameter("L2level", level.getLevel() + 1)
.getResultList();
}
}

View File

@ -1 +1,11 @@
spring.main.banner-mode=off
#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

View File

@ -1,13 +0,0 @@
package com.example.spring_online_calculator;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class SpringOnlineCalculatorApplicationTests {
@Test
void contextLoads() {
}
}

View File

@ -0,0 +1,183 @@
package premium_store;
import premium_store.model.Client;
import premium_store.model.Level;
import premium_store.model.Nation;
import premium_store.model.Tank;
import premium_store.service.ClientService;
import premium_store.service.LevelService;
import premium_store.service.NationService;
import premium_store.service.TankService;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import javax.persistence.EntityNotFoundException;
import java.util.*;
@SpringBootTest
class PremiumStoreApplicationTests {
private static final Logger log = LoggerFactory.getLogger(PremiumStoreApplicationTests.class);
@Autowired
TankService tankService;
@Autowired
ClientService clientService;
@Autowired
LevelService levelService;
@Autowired
NationService nationService;
@Test
void testClientRead() {
clientService.deleteAllClients();
Client client = clientService.addClient("3tankista73", "fff@mail.ru", 3400);
log.info(client.toString());
Client findClient = clientService.findClient(client.getId());
log.info(findClient.toString());
Assertions.assertEquals(client, findClient);
}
@Test
void testNationRead() {
nationService.deleteAllNations();
tankService.deleteAllTanks();
levelService.deleteAllLevels();
Nation nation = nationService.addNation("Германия");
Level firstLevel = levelService.addLevel(8);
Level secondLevel = levelService.addLevel(9);
Tank firstTank = tankService.addTank("ИС-3", nation, firstLevel, 3700000);
Tank secondTank = tankService.addTank("E-75", nation, secondLevel, 5600000);
List<Tank> tanks = new ArrayList<>(Arrays.asList(firstTank, secondTank));
nation = nationService.updateNation(nation.getId(), "СССР", tanks);
log.info(nation.toString());
Nation findNation = nationService.findNation(nation.getId());
log.info(findNation.toString());
Assertions.assertEquals(nation, findNation);
}
@Test
void testClientReadNotFound() {
clientService.deleteAllClients();
Assertions.assertThrows(EntityNotFoundException.class, () -> clientService.findClient(-1L));
}
@Test
void testLevelRead() {
tankService.deleteAllTanks();
levelService.deleteAllLevels();
Level level = levelService.addLevel(8);
log.info(level.toString());
Level secondLevel = levelService.addLevel(9);
log.info(secondLevel.toString());
Assertions.assertEquals(levelService.findAllLevels().size(), 2);
}
@Test
void testLevelReadAllEmpty() {
tankService.deleteAllTanks();
levelService.deleteAllLevels();
List<Level> levels = levelService.findAllLevels();
log.info(levels.toString());
Assertions.assertEquals(levels.size(), 0);
}
@Test
void testTankReadAll() {
tankService.deleteAllTanks();
levelService.deleteAllLevels();
Level firstLevel = levelService.addLevel(8);
Level secondLevel = levelService.addLevel(9);
tankService.addTank("ИС-3", nationService.addNation("СССР"), firstLevel, 3700000);
tankService.addTank("E-75", nationService.addNation("Германия"), secondLevel, 5600000);
List<Tank> tanks = tankService.findAllTanks();
log.info(tanks.toString());
Assertions.assertEquals(tanks.size(), 2);
}
@Test
void testClientCreate() {
clientService.deleteAllClients();
Client firstClient = clientService.addClient("Barbarian", "dsfg@gmail.com", 56000);
log.info(firstClient.toString());
Client secondClient = clientService.addClient("KorbenDallas", "tankoviyGeniy@mail.ru", 37000);
log.info(secondClient.toString());
Assertions.assertEquals(clientService.findAllClients().size(), 2);
}
@Test
void testUpdateTank(){
tankService.deleteAllTanks();
levelService.deleteAllLevels();
clientService.deleteAllClients();
Level firstLevel = levelService.addLevel(8);
Level secondLevel = levelService.addLevel(9);
Tank firstTank = tankService.addTank("ИС-3", nationService.addNation("СССР"), firstLevel, 3700000);
Tank secondTank = tankService.addTank("E-75", nationService.addNation("Германия"), secondLevel, 5600000);
Tank tank = tankService.findTank(firstTank.getId());
log.info(tank.toString());
tank.setName("ИСУ-152");
tank.setLevel(secondLevel);
tank.setCost(4100000);
log.info(tank.toString());
}
@Test
void testNewTest() {
nationService.deleteAllNations();
tankService.deleteAllTanks();
levelService.deleteAllLevels();
Nation nation = nationService.addNation("Германия");
Nation newNation = nationService.addNation("CCCР");
Level firstLevel = levelService.addLevel(8);
Level secondLevel = levelService.addLevel(9);
Tank firstTank = tankService.addTank("ИС-3", nation, firstLevel, 3700000);
Tank secondTank = tankService.addTank("E-75", nation, secondLevel, 5600000);
List<Tank> tanks = new ArrayList<>(Arrays.asList(firstTank, secondTank));
nation = nationService.updateNation(nation.getId(), "СССР", tanks);
tanks = tankService.findTank(firstLevel, nation);
log.info("AAAAA" + tanks.toString());
}
}

View 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