Вроде как рабочий проект для 3-й лабы.
This commit is contained in:
parent
8fa5b5f8c4
commit
c4e3593fa4
10
.idea/compiler.xml
generated
Normal file
10
.idea/compiler.xml
generated
Normal 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
2
.idea/misc.xml
generated
@ -1,7 +1,7 @@
|
||||
<?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="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">
|
||||
<option name="id" value="jpab" />
|
||||
</component>
|
||||
|
2
spring_online_calculator/.gitignore
vendored
2
spring_online_calculator/.gitignore
vendored
@ -35,3 +35,5 @@ out/
|
||||
|
||||
### VS Code ###
|
||||
.vscode/
|
||||
|
||||
*.db
|
@ -1,10 +0,0 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Title</title>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
</body>
|
||||
</html>
|
@ -1,10 +1,10 @@
|
||||
plugins {
|
||||
id 'java'
|
||||
id 'org.springframework.boot' version '2.7.8'
|
||||
id 'org.springframework.boot' version '2.6.3'
|
||||
id 'io.spring.dependency-management' version '1.0.15.RELEASE'
|
||||
id 'java'
|
||||
}
|
||||
|
||||
group = 'labWork'
|
||||
group = 'ru.ulstu.is'
|
||||
version = '0.0.1-SNAPSHOT'
|
||||
sourceCompatibility = '17'
|
||||
|
||||
@ -14,11 +14,13 @@ repositories {
|
||||
|
||||
dependencies {
|
||||
implementation 'org.springframework.boot:spring-boot-starter-web'
|
||||
|
||||
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'
|
||||
implementation 'org.springframework.boot:spring-boot-starter-validation'
|
||||
}
|
||||
|
||||
tasks.named('test') {
|
||||
|
Binary file not shown.
@ -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.3.3-bin.zip
|
||||
zipStoreBase=GRADLE_USER_HOME
|
||||
zipStorePath=wrapper/dists
|
||||
|
6
spring_online_calculator/gradlew
vendored
6
spring_online_calculator/gradlew
vendored
@ -205,12 +205,6 @@ set -- \
|
||||
org.gradle.wrapper.GradleWrapperMain \
|
||||
"$@"
|
||||
|
||||
# Stop when "xargs" is not available.
|
||||
if ! command -v xargs >/dev/null 2>&1
|
||||
then
|
||||
die "xargs is not available"
|
||||
fi
|
||||
|
||||
# Use "xargs" to parse quoted args.
|
||||
#
|
||||
# With -n1 it outputs one arg per line, with the quotes and backslashes removed.
|
||||
|
10
spring_online_calculator/gradlew.bat
vendored
10
spring_online_calculator/gradlew.bat
vendored
@ -40,7 +40,7 @@ if defined JAVA_HOME goto findJavaFromJavaHome
|
||||
|
||||
set JAVA_EXE=java.exe
|
||||
%JAVA_EXE% -version >NUL 2>&1
|
||||
if %ERRORLEVEL% equ 0 goto execute
|
||||
if "%ERRORLEVEL%" == "0" goto execute
|
||||
|
||||
echo.
|
||||
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
|
||||
@rem End local scope for the variables with windows NT shell
|
||||
if %ERRORLEVEL% equ 0 goto mainEnd
|
||||
if "%ERRORLEVEL%"=="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%
|
||||
if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
|
||||
exit /b 1
|
||||
|
||||
:mainEnd
|
||||
if "%OS%"=="Windows_NT" endlocal
|
||||
|
@ -1 +1 @@
|
||||
rootProject.name = 'spring_online_calculator'
|
||||
rootProject.name = 'premium_store'
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
package labWork.premium_store.model;
|
||||
package premium_store.model;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
@ -18,7 +19,7 @@ public class Client {
|
||||
|
||||
private Integer balance;
|
||||
|
||||
@ManyToMany(mappedBy = "clients")
|
||||
@ManyToMany(mappedBy = "clients", fetch= FetchType.EAGER)
|
||||
private List<Tank> tanks;
|
||||
|
||||
public Client(){ }
|
||||
@ -27,6 +28,7 @@ public class Client {
|
||||
this.nickName = nickName;
|
||||
this.email = email;
|
||||
this.balance = balance;
|
||||
tanks = new ArrayList<>();
|
||||
}
|
||||
|
||||
public Long getId(){
|
||||
@ -87,7 +89,7 @@ public class Client {
|
||||
//преобразование данных по объекту в строчку
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Student{" +
|
||||
return "Client{" +
|
||||
"id=" + id +
|
||||
", nickName='" + nickName + '\'' +
|
||||
", email='" + email + '\'' +
|
@ -1,4 +1,4 @@
|
||||
package labWork.premium_store.model;
|
||||
package premium_store.model;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.util.Objects;
|
||||
@ -55,7 +55,7 @@ public class Level {
|
||||
//преобразование данных по объекту в строчку
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Nation{" +
|
||||
return "Level{" +
|
||||
"id=" + id +
|
||||
", level='" + level + '}';
|
||||
}
|
@ -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;
|
||||
|
||||
@Entity
|
@ -1,9 +1,10 @@
|
||||
package labWork.premium_store.model;
|
||||
package premium_store.model;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.util.Objects;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Entity
|
||||
public class Tank {
|
||||
@ -26,10 +27,10 @@ public class Tank {
|
||||
private int cost;
|
||||
|
||||
//реализация двунаправленной связи мнокие-ко-многим
|
||||
@ManyToMany
|
||||
@ManyToMany(fetch= FetchType.EAGER)
|
||||
@JoinTable(name = "tanks_of_clients",
|
||||
joinColumns = @JoinColumn(name = "tank_fk"),
|
||||
inverseJoinColumns = @JoinColumn(name = "client_fk"))
|
||||
joinColumns = @JoinColumn(name = "tank_id"),
|
||||
inverseJoinColumns = @JoinColumn(name = "client_id"))
|
||||
private List<Client> clients;
|
||||
|
||||
public Tank() {
|
||||
@ -40,6 +41,7 @@ public class Tank {
|
||||
this.nation = nation;
|
||||
this.level = level;
|
||||
this.cost = cost;
|
||||
clients = new ArrayList<>();
|
||||
}
|
||||
|
||||
public Long getId(){
|
||||
@ -119,12 +121,13 @@ public class Tank {
|
||||
//преобразование данных по объекту в строчку
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Student{" +
|
||||
return "Tank{" +
|
||||
"id=" + id +
|
||||
", name='" + name + '\'' +
|
||||
", nation='" + nation + '\'' +
|
||||
", level='" + level + '\'' +
|
||||
", cost='" + cost + '\'' +
|
||||
", clients='" + clients.stream().map(Object::toString).collect(Collectors.joining(", ")) + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
@ -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.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;
|
@ -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.transaction.annotation.Transactional;
|
||||
import premium_store.model.Level;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.EntityNotFoundException;
|
||||
@ -19,17 +19,21 @@ public class LevelService {
|
||||
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;
|
||||
}
|
||||
|
@ -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.transaction.annotation.Transactional;
|
||||
import org.springframework.util.StringUtils;
|
||||
import premium_store.model.Nation;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.EntityNotFoundException;
|
@ -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.transaction.annotation.Transactional;
|
||||
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.EntityNotFoundException;
|
@ -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.username=sa
|
||||
spring.datasource.password=password
|
||||
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
|
@ -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() {
|
||||
}
|
||||
|
||||
}
|
@ -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());
|
||||
}
|
||||
}
|
@ -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…
x
Reference in New Issue
Block a user