Вроде как рабочий проект для 3-й лабы.

This commit is contained in:
Programmist73 2023-04-08 10:29:58 +04:00
parent 8fa5b5f8c4
commit c4e3593fa4
24 changed files with 242 additions and 84 deletions

10
.idea/compiler.xml generated 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>

2
.idea/misc.xml generated
View File

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

View File

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

View File

@ -1,10 +0,0 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
</body>
</html>

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 = 'labWork' group = 'ru.ulstu.is'
version = '0.0.1-SNAPSHOT' version = '0.0.1-SNAPSHOT'
sourceCompatibility = '17' sourceCompatibility = '17'
@ -14,11 +14,13 @@ repositories {
dependencies { dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa' implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'com.h2database:h2:2.1.210' implementation 'com.h2database:h2:2.1.210'
implementation group: 'org.springdoc', name: 'springdoc-openapi-ui', version: '1.6.5' 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'
implementation 'org.springframework.boot:spring-boot-starter-validation'
} }
tasks.named('test') { tasks.named('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

@ -1,6 +1,7 @@
package labWork.premium_store.model; package premium_store.model;
import javax.persistence.*; import javax.persistence.*;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
@ -18,7 +19,7 @@ public class Client {
private Integer balance; private Integer balance;
@ManyToMany(mappedBy = "clients") @ManyToMany(mappedBy = "clients", fetch= FetchType.EAGER)
private List<Tank> tanks; private List<Tank> tanks;
public Client(){ } public Client(){ }
@ -27,6 +28,7 @@ public class Client {
this.nickName = nickName; this.nickName = nickName;
this.email = email; this.email = email;
this.balance = balance; this.balance = balance;
tanks = new ArrayList<>();
} }
public Long getId(){ public Long getId(){
@ -87,7 +89,7 @@ public class Client {
//преобразование данных по объекту в строчку //преобразование данных по объекту в строчку
@Override @Override
public String toString() { public String toString() {
return "Student{" + return "Client{" +
"id=" + id + "id=" + id +
", nickName='" + nickName + '\'' + ", nickName='" + nickName + '\'' +
", email='" + email + '\'' + ", email='" + email + '\'' +

View File

@ -1,4 +1,4 @@
package labWork.premium_store.model; package premium_store.model;
import javax.persistence.*; import javax.persistence.*;
import java.util.Objects; import java.util.Objects;
@ -55,7 +55,7 @@ public class Level {
//преобразование данных по объекту в строчку //преобразование данных по объекту в строчку
@Override @Override
public String toString() { public String toString() {
return "Nation{" + return "Level{" +
"id=" + id + "id=" + id +
", level='" + level + '}'; ", level='" + level + '}';
} }

View File

@ -1,6 +1,10 @@
package labWork.premium_store.model; package premium_store.model;
import javax.persistence.*; import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import java.util.Objects; import java.util.Objects;
@Entity @Entity

View File

@ -1,9 +1,10 @@
package labWork.premium_store.model; package premium_store.model;
import javax.persistence.*; import javax.persistence.*;
import java.util.Objects;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.stream.Collectors;
@Entity @Entity
public class Tank { public class Tank {
@ -26,10 +27,10 @@ public class Tank {
private int cost; private int cost;
//реализация двунаправленной связи мнокие-ко-многим //реализация двунаправленной связи мнокие-ко-многим
@ManyToMany @ManyToMany(fetch= FetchType.EAGER)
@JoinTable(name = "tanks_of_clients", @JoinTable(name = "tanks_of_clients",
joinColumns = @JoinColumn(name = "tank_fk"), joinColumns = @JoinColumn(name = "tank_id"),
inverseJoinColumns = @JoinColumn(name = "client_fk")) inverseJoinColumns = @JoinColumn(name = "client_id"))
private List<Client> clients; private List<Client> clients;
public Tank() { public Tank() {
@ -40,6 +41,7 @@ public class Tank {
this.nation = nation; this.nation = nation;
this.level = level; this.level = level;
this.cost = cost; this.cost = cost;
clients = new ArrayList<>();
} }
public Long getId(){ public Long getId(){
@ -119,12 +121,13 @@ public class Tank {
//преобразование данных по объекту в строчку //преобразование данных по объекту в строчку
@Override @Override
public String toString() { public String toString() {
return "Student{" + return "Tank{" +
"id=" + id + "id=" + id +
", name='" + name + '\'' + ", name='" + name + '\'' +
", nation='" + nation + '\'' + ", nation='" + nation + '\'' +
", level='" + level + '\'' + ", level='" + level + '\'' +
", cost='" + cost + '\'' + ", cost='" + cost + '\'' +
", clients='" + clients.stream().map(Object::toString).collect(Collectors.joining(", ")) + '\'' +
'}'; '}';
} }
} }

View File

@ -1,10 +1,10 @@
package labWork.premium_store.service; package premium_store.service;
import labWork.premium_store.model.Client;
import labWork.premium_store.model.Tank;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import premium_store.model.Client;
import premium_store.model.Tank;
import javax.persistence.EntityManager; import javax.persistence.EntityManager;
import javax.persistence.EntityNotFoundException; import javax.persistence.EntityNotFoundException;

View File

@ -1,8 +1,8 @@
package labWork.premium_store.service; package premium_store.service;
import labWork.premium_store.model.Level;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import premium_store.model.Level;
import javax.persistence.EntityManager; import javax.persistence.EntityManager;
import javax.persistence.EntityNotFoundException; import javax.persistence.EntityNotFoundException;
@ -19,17 +19,21 @@ public class LevelService {
if (newLevel <= 0) { if (newLevel <= 0) {
throw new IllegalArgumentException("Level name is zero or less"); throw new IllegalArgumentException("Level name is zero or less");
} }
final Level level = new Level(newLevel); final Level level = new Level(newLevel);
em.persist(level); em.persist(level);
return level; return level;
} }
@Transactional(readOnly = true) @Transactional(readOnly = true)
public Level findLevel(Long id) { public Level findLevel(Long id) {
final Level level = em.find(Level.class, id); final Level level = em.find(Level.class, id);
if (level == null) { if (level == null) {
throw new EntityNotFoundException(String.format("Level with id [%s] is not found", id)); throw new EntityNotFoundException(String.format("Level with id [%s] is not found", id));
} }
return level; return level;
} }

View File

@ -1,9 +1,9 @@
package labWork.premium_store.service; package premium_store.service;
import labWork.premium_store.model.Nation;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import premium_store.model.Nation;
import javax.persistence.EntityManager; import javax.persistence.EntityManager;
import javax.persistence.EntityNotFoundException; import javax.persistence.EntityNotFoundException;

View File

@ -1,12 +1,12 @@
package labWork.premium_store.service; package premium_store.service;
import labWork.premium_store.model.Client;
import labWork.premium_store.model.Level;
import labWork.premium_store.model.Nation;
import labWork.premium_store.model.Tank;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import premium_store.model.Client;
import premium_store.model.Level;
import premium_store.model.Nation;
import premium_store.model.Tank;
import javax.persistence.EntityManager; import javax.persistence.EntityManager;
import javax.persistence.EntityNotFoundException; import javax.persistence.EntityNotFoundException;

View File

@ -1,6 +1,11 @@
spring.datasource.url=jdbc:h2:mem:testdb spring.main.banner-mode=off
#server.port=8080
spring.datasource.url=jdbc:h2:file:./data
spring.datasource.driverClassName=org.h2.Driver spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa spring.datasource.username=sa
spring.datasource.password=password spring.datasource.password=password
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.jpa.hibernate.ddl-auto=create-drop 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,153 @@
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();
Nation nation = nationService.addNation("СССР");
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();
Client firstClient = clientService.addClient("Barbarian", "dsfg@gmail.com", 56000);
Client secondClient = clientService.addClient("KorbenDallas", "tankoviyGeniy@mail.ru", 37000);
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());
List<Client> newClient = new ArrayList<>();
Collections.addAll(newClient, firstClient, secondClient);
tank.setName("ИСУ-152");
tank.setLevel(secondLevel);
tank.setCost(4100000);
tank.setClients(newClient);
log.info(tank.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