Промежуточное сохранение.
This commit is contained in:
parent
e2d99cec86
commit
43cc05427d
@ -3,29 +3,29 @@ package premium_store.controller.controller;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import premium_store.controller.DTO.ClientDTO;
|
||||
import premium_store.model.Tank;
|
||||
import premium_store.service.ClientService;
|
||||
import premium_store.service.GameClientService;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/client")
|
||||
public class ClientController {
|
||||
private final ClientService clientService;
|
||||
public class GameClientController {
|
||||
private final GameClientService gameClientService;
|
||||
|
||||
public ClientController(ClientService clientService){
|
||||
this.clientService = clientService;
|
||||
public GameClientController(GameClientService gameClientService){
|
||||
this.gameClientService = gameClientService;
|
||||
}
|
||||
|
||||
//аннотация PathVariable связывает значения id из URL и Long id
|
||||
@GetMapping("/{id}")
|
||||
public ClientDTO getClient(@PathVariable Long id) {
|
||||
return new ClientDTO(clientService.findClient(id));
|
||||
return new ClientDTO(gameClientService.findClient(id));
|
||||
}
|
||||
|
||||
//с помощью Java Stream преобразуем набор пришедших данных в объекты StudentDto
|
||||
@GetMapping("/")
|
||||
public List<ClientDTO> getClients() {
|
||||
return clientService.findAllClients().stream()
|
||||
return gameClientService.findAllClients().stream()
|
||||
.map(ClientDTO::new)
|
||||
.toList();
|
||||
}
|
||||
@ -34,7 +34,7 @@ public class ClientController {
|
||||
public ClientDTO createClient(@RequestParam("nickName") String nickName,
|
||||
@RequestParam("email") String email,
|
||||
@RequestParam("balance") Integer balance) {
|
||||
return new ClientDTO(clientService.addClient(nickName, email, balance));
|
||||
return new ClientDTO(gameClientService.addClient(nickName, email, balance));
|
||||
}
|
||||
|
||||
@PutMapping("/{id}")
|
||||
@ -43,11 +43,11 @@ public class ClientController {
|
||||
@RequestParam("email") String email,
|
||||
@RequestParam("balance") Integer balance,
|
||||
@RequestParam("tanks") List<Tank> tanks) {
|
||||
return new ClientDTO(clientService.updateClient(id, nickName, email, balance, tanks));
|
||||
return new ClientDTO(gameClientService.updateClient(id, nickName, email, balance, tanks));
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
public ClientDTO deleteClient(@PathVariable Long id) {
|
||||
return new ClientDTO(clientService.deleteClient(id));
|
||||
return new ClientDTO(gameClientService.deleteClient(id));
|
||||
}
|
||||
}
|
@ -2,7 +2,7 @@ package premium_store.controller.controller;
|
||||
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import premium_store.model.TankLevel;
|
||||
import premium_store.service.LevelService;
|
||||
import premium_store.service.TankLevelService;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@ -11,39 +11,39 @@ import java.util.List;
|
||||
//так же здесь прописываем вызовы методов CRUD в привязке к URL
|
||||
@RestController
|
||||
@RequestMapping("/level")
|
||||
public class LevelController {
|
||||
private final LevelService levelService;
|
||||
public class TankLevelController {
|
||||
private final TankLevelService tankLevelService;
|
||||
|
||||
public LevelController(LevelService levelService){
|
||||
this.levelService = levelService;
|
||||
public TankLevelController(TankLevelService tankLevelService){
|
||||
this.tankLevelService = tankLevelService;
|
||||
}
|
||||
|
||||
//аннотация PathVariable связывает значения id из URL и Long id
|
||||
@GetMapping("/{id}")
|
||||
public TankLevel getLevel(@PathVariable Long id) {
|
||||
return levelService.findLevel(id);
|
||||
return tankLevelService.findLevel(id);
|
||||
}
|
||||
|
||||
//с помощью Java Stream преобразуем набор пришедших данных в объекты StudentDto
|
||||
@GetMapping("/")
|
||||
public List<TankLevel> getLevels() {
|
||||
return levelService.findAllLevels().stream()
|
||||
return tankLevelService.findAllLevels().stream()
|
||||
.toList();
|
||||
}
|
||||
|
||||
@PostMapping("/")
|
||||
public TankLevel createLevel(@RequestParam("Level") int level) {
|
||||
return levelService.addLevel(level);
|
||||
return tankLevelService.addLevel(level);
|
||||
}
|
||||
|
||||
@PutMapping("/{id}")
|
||||
public TankLevel updateLevel(@PathVariable Long id,
|
||||
@RequestParam("Level") int level) {
|
||||
return levelService.updateLevel(id, level);
|
||||
return tankLevelService.updateLevel(id, level);
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
public TankLevel deleteLevel(@PathVariable Long id) {
|
||||
return levelService.deleteLevel(id);
|
||||
return tankLevelService.deleteLevel(id);
|
||||
}
|
||||
}
|
@ -6,6 +6,7 @@ import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
@Entity
|
||||
@Table(name = "GAMECLIENTS")
|
||||
public class GameClient {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
|
@ -1,12 +1,11 @@
|
||||
package premium_store.model;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.*;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
|
||||
@Entity
|
||||
@Table(name = "NATIONS")
|
||||
public class Nation {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
|
@ -7,6 +7,7 @@ import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Entity
|
||||
@Table(name = "TANKS")
|
||||
public class Tank {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
@ -15,20 +16,20 @@ public class Tank {
|
||||
private String name;
|
||||
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "nation_id")
|
||||
@JoinColumn(name = "nation")
|
||||
private Nation nation;
|
||||
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "level_id")
|
||||
@JoinColumn(name = "tankLevel")
|
||||
private TankLevel tankLevel;
|
||||
|
||||
private int cost;
|
||||
|
||||
//реализация двунаправленной связи многие-ко-многим
|
||||
@ManyToMany(fetch= FetchType.EAGER)
|
||||
@JoinTable(name = "tanks_of_clients",
|
||||
joinColumns = @JoinColumn(name = "tank_id"),
|
||||
inverseJoinColumns = @JoinColumn(name = "client_id"))
|
||||
@ManyToMany(fetch = FetchType.EAGER)
|
||||
@JoinTable(name = "tanks_clients",
|
||||
joinColumns = @JoinColumn(name = "tank_fk"),
|
||||
inverseJoinColumns = @JoinColumn(name = "client_fk"))
|
||||
private List<GameClient> gameClients;
|
||||
|
||||
public Tank() {
|
||||
|
@ -2,8 +2,10 @@ package premium_store.model;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
|
||||
@Entity
|
||||
@Table(name = "TANKLEVELS")
|
||||
public class TankLevel {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
|
@ -3,5 +3,5 @@ package premium_store.repository;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import premium_store.model.GameClient;
|
||||
|
||||
public interface ClientRepository extends JpaRepository<GameClient, Long> {
|
||||
public interface GameClientRepository extends JpaRepository<GameClient, Long> {
|
||||
}
|
@ -5,5 +5,5 @@ import premium_store.model.TankLevel;
|
||||
|
||||
//класс для взаимодействия с БД вместо низкоуровневого EntityManager
|
||||
//передаём тип класса и тип id его элементов
|
||||
public interface LevelRepository extends JpaRepository<TankLevel, Long> {
|
||||
public interface TankLevelRepository extends JpaRepository<TankLevel, Long> {
|
||||
}
|
@ -4,7 +4,7 @@ import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import premium_store.model.GameClient;
|
||||
import premium_store.model.Tank;
|
||||
import premium_store.repository.ClientRepository;
|
||||
import premium_store.repository.GameClientRepository;
|
||||
import premium_store.service.exception.ClientNotFoundException;
|
||||
import premium_store.util.validation.ValidatorUtil;
|
||||
|
||||
@ -12,12 +12,12 @@ import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@Service
|
||||
public class ClientService {
|
||||
private final ClientRepository clientRepository;
|
||||
public class GameClientService {
|
||||
private final GameClientRepository gameClientRepository;
|
||||
private final ValidatorUtil validatorUtil;
|
||||
|
||||
public ClientService(ClientRepository clientRepository, ValidatorUtil validatorUtil){
|
||||
this.clientRepository = clientRepository;
|
||||
public GameClientService(GameClientRepository gameClientRepository, ValidatorUtil validatorUtil){
|
||||
this.gameClientRepository = gameClientRepository;
|
||||
this.validatorUtil = validatorUtil;
|
||||
}
|
||||
|
||||
@ -26,19 +26,19 @@ public class ClientService {
|
||||
final GameClient gameClient = new GameClient(newNickName, newEmail, newBallance);
|
||||
validatorUtil.validate(gameClient);
|
||||
|
||||
return clientRepository.save(gameClient);
|
||||
return gameClientRepository.save(gameClient);
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public GameClient findClient(Long id) {
|
||||
final Optional<GameClient> client = clientRepository.findById(id);
|
||||
final Optional<GameClient> client = gameClientRepository.findById(id);
|
||||
|
||||
return client.orElseThrow(() -> new ClientNotFoundException(id));
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<GameClient> findAllClients() {
|
||||
return clientRepository.findAll();
|
||||
return gameClientRepository.findAll();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
@ -50,13 +50,13 @@ public class ClientService {
|
||||
currentGameClient.setTanks(newTanks);
|
||||
validatorUtil.validate(currentGameClient);
|
||||
|
||||
return clientRepository.save(currentGameClient);
|
||||
return gameClientRepository.save(currentGameClient);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public GameClient deleteClient(Long id) {
|
||||
final GameClient currentGameClient = findClient(id);
|
||||
clientRepository.delete(currentGameClient);
|
||||
gameClientRepository.delete(currentGameClient);
|
||||
|
||||
return currentGameClient;
|
||||
}
|
||||
@ -64,6 +64,6 @@ public class ClientService {
|
||||
//прямой sql-запрос на удаление всех записей в таблице
|
||||
@Transactional
|
||||
public void deleteAllClients() {
|
||||
clientRepository.deleteAll();
|
||||
gameClientRepository.deleteAll();
|
||||
}
|
||||
}
|
@ -3,7 +3,7 @@ package premium_store.service;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import premium_store.model.TankLevel;
|
||||
import premium_store.repository.LevelRepository;
|
||||
import premium_store.repository.TankLevelRepository;
|
||||
import premium_store.service.exception.LevelNotFoundException;
|
||||
import premium_store.util.validation.ValidatorUtil;
|
||||
|
||||
@ -12,12 +12,12 @@ import java.util.Optional;
|
||||
|
||||
//сервис после удаления EntityManager и добавления нашего репозитория. То есть у него уже все методы работы с полями прописаны за нас?
|
||||
@Service
|
||||
public class LevelService {
|
||||
private final LevelRepository levelRepository;
|
||||
public class TankLevelService {
|
||||
private final TankLevelRepository tankLevelRepository;
|
||||
private final ValidatorUtil validatorUtil;
|
||||
|
||||
public LevelService(LevelRepository levelRepository, ValidatorUtil validatorUtil){
|
||||
this.levelRepository = levelRepository;
|
||||
public TankLevelService(TankLevelRepository tankLevelRepository, ValidatorUtil validatorUtil){
|
||||
this.tankLevelRepository = tankLevelRepository;
|
||||
this.validatorUtil = validatorUtil;
|
||||
}
|
||||
|
||||
@ -26,13 +26,13 @@ public class LevelService {
|
||||
final TankLevel tankLevel = new TankLevel(newLevel);
|
||||
validatorUtil.validate(tankLevel);
|
||||
|
||||
return levelRepository.save(tankLevel);
|
||||
return tankLevelRepository.save(tankLevel);
|
||||
}
|
||||
|
||||
//здесь используем Optional - спец. тип данных, позволяющий определять, вернулось ли что-то при вызове метода, или вернулся null
|
||||
@Transactional(readOnly = true)
|
||||
public TankLevel findLevel(Long id) {
|
||||
final Optional<TankLevel> level = levelRepository.findById(id);
|
||||
final Optional<TankLevel> level = tankLevelRepository.findById(id);
|
||||
|
||||
//благодаря Optional можем вызвать orElseThrow, который в случае null сделает проброс кастомного исключения
|
||||
return level.orElseThrow(() -> new LevelNotFoundException(id));
|
||||
@ -40,7 +40,7 @@ public class LevelService {
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<TankLevel> findAllLevels() {
|
||||
return levelRepository.findAll();
|
||||
return tankLevelRepository.findAll();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
@ -49,19 +49,19 @@ public class LevelService {
|
||||
currentTankLevel.setLevel(newLevel);
|
||||
validatorUtil.validate(currentTankLevel);
|
||||
|
||||
return levelRepository.save(currentTankLevel);
|
||||
return tankLevelRepository.save(currentTankLevel);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public TankLevel deleteLevel(Long id) {
|
||||
final TankLevel currentTankLevel = findLevel(id);
|
||||
levelRepository.delete(currentTankLevel);
|
||||
tankLevelRepository.delete(currentTankLevel);
|
||||
|
||||
return currentTankLevel;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void deleteAllLevels() {
|
||||
levelRepository.deleteAll();
|
||||
tankLevelRepository.deleteAll();
|
||||
}
|
||||
}
|
@ -4,8 +4,8 @@ import premium_store.model.GameClient;
|
||||
import premium_store.model.TankLevel;
|
||||
import premium_store.model.Nation;
|
||||
import premium_store.model.Tank;
|
||||
import premium_store.service.ClientService;
|
||||
import premium_store.service.LevelService;
|
||||
import premium_store.service.GameClientService;
|
||||
import premium_store.service.TankLevelService;
|
||||
import premium_store.service.NationService;
|
||||
import premium_store.service.TankService;
|
||||
|
||||
@ -27,22 +27,22 @@ class PremiumStoreApplicationTests {
|
||||
TankService tankService;
|
||||
|
||||
@Autowired
|
||||
ClientService clientService;
|
||||
GameClientService gameClientService;
|
||||
|
||||
@Autowired
|
||||
LevelService levelService;
|
||||
TankLevelService tankLevelService;
|
||||
|
||||
@Autowired
|
||||
NationService nationService;
|
||||
|
||||
@Test
|
||||
void testClientRead() {
|
||||
clientService.deleteAllClients();
|
||||
gameClientService.deleteAllClients();
|
||||
|
||||
GameClient gameClient = clientService.addClient("3tankista73", "fff@mail.ru", 3400);
|
||||
GameClient gameClient = gameClientService.addClient("3tankista73", "fff@mail.ru", 3400);
|
||||
log.info(gameClient.toString());
|
||||
|
||||
GameClient findGameClient = clientService.findClient(gameClient.getId());
|
||||
GameClient findGameClient = gameClientService.findClient(gameClient.getId());
|
||||
log.info(findGameClient.toString());
|
||||
|
||||
Assertions.assertEquals(gameClient, findGameClient);
|
||||
@ -63,30 +63,30 @@ class PremiumStoreApplicationTests {
|
||||
|
||||
@Test
|
||||
void testClientReadNotFound() {
|
||||
clientService.deleteAllClients();
|
||||
Assertions.assertThrows(ClientNotFoundException.class, () -> clientService.findClient(-1L));
|
||||
gameClientService.deleteAllClients();
|
||||
Assertions.assertThrows(ClientNotFoundException.class, () -> gameClientService.findClient(-1L));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testLevelRead() {
|
||||
tankService.deleteAllTanks();
|
||||
levelService.deleteAllLevels();
|
||||
tankLevelService.deleteAllLevels();
|
||||
|
||||
TankLevel tankLevel = levelService.addLevel(8);
|
||||
TankLevel tankLevel = tankLevelService.addLevel(8);
|
||||
log.info(tankLevel.toString());
|
||||
|
||||
TankLevel secondTankLevel = levelService.addLevel(9);
|
||||
TankLevel secondTankLevel = tankLevelService.addLevel(9);
|
||||
log.info(secondTankLevel.toString());
|
||||
|
||||
Assertions.assertEquals(levelService.findAllLevels().size(), 2);
|
||||
Assertions.assertEquals(tankLevelService.findAllLevels().size(), 2);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testLevelReadAllEmpty() {
|
||||
tankService.deleteAllTanks();
|
||||
levelService.deleteAllLevels();
|
||||
tankLevelService.deleteAllLevels();
|
||||
|
||||
List<TankLevel> tankLevels = levelService.findAllLevels();
|
||||
List<TankLevel> tankLevels = tankLevelService.findAllLevels();
|
||||
log.info(tankLevels.toString());
|
||||
|
||||
Assertions.assertEquals(tankLevels.size(), 0);
|
||||
@ -95,10 +95,10 @@ class PremiumStoreApplicationTests {
|
||||
@Test
|
||||
void testTankReadAll() {
|
||||
tankService.deleteAllTanks();
|
||||
levelService.deleteAllLevels();
|
||||
tankLevelService.deleteAllLevels();
|
||||
|
||||
TankLevel firstTankLevel = levelService.addLevel(8);
|
||||
TankLevel secondTankLevel = levelService.addLevel(9);
|
||||
TankLevel firstTankLevel = tankLevelService.addLevel(8);
|
||||
TankLevel secondTankLevel = tankLevelService.addLevel(9);
|
||||
|
||||
tankService.addTank("ИС-3", nationService.addNation("СССР"), firstTankLevel, 3700000);
|
||||
tankService.addTank("E-75", nationService.addNation("Германия"), secondTankLevel, 5600000);
|
||||
@ -111,28 +111,28 @@ class PremiumStoreApplicationTests {
|
||||
|
||||
@Test
|
||||
void testClientCreate() {
|
||||
clientService.deleteAllClients();
|
||||
gameClientService.deleteAllClients();
|
||||
|
||||
GameClient firstGameClient = clientService.addClient("Barbarian", "dsfg@gmail.com", 56000);
|
||||
GameClient firstGameClient = gameClientService.addClient("Barbarian", "dsfg@gmail.com", 56000);
|
||||
log.info(firstGameClient.toString());
|
||||
|
||||
GameClient secondGameClient = clientService.addClient("KorbenDallas", "tankoviyGeniy@mail.ru", 37000);
|
||||
GameClient secondGameClient = gameClientService.addClient("KorbenDallas", "tankoviyGeniy@mail.ru", 37000);
|
||||
log.info(secondGameClient.toString());
|
||||
|
||||
Assertions.assertEquals(clientService.findAllClients().size(), 2);
|
||||
Assertions.assertEquals(gameClientService.findAllClients().size(), 2);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testUpdateTank(){
|
||||
tankService.deleteAllTanks();
|
||||
levelService.deleteAllLevels();
|
||||
clientService.deleteAllClients();
|
||||
tankLevelService.deleteAllLevels();
|
||||
gameClientService.deleteAllClients();
|
||||
|
||||
GameClient firstGameClient = clientService.addClient("Barbarian", "dsfg@gmail.com", 56000);
|
||||
GameClient secondGameClient = clientService.addClient("KorbenDallas", "tankoviyGeniy@mail.ru", 37000);
|
||||
GameClient firstGameClient = gameClientService.addClient("Barbarian", "dsfg@gmail.com", 56000);
|
||||
GameClient secondGameClient = gameClientService.addClient("KorbenDallas", "tankoviyGeniy@mail.ru", 37000);
|
||||
|
||||
TankLevel firstTankLevel = levelService.addLevel(8);
|
||||
TankLevel secondTankLevel = levelService.addLevel(9);
|
||||
TankLevel firstTankLevel = tankLevelService.addLevel(8);
|
||||
TankLevel secondTankLevel = tankLevelService.addLevel(9);
|
||||
|
||||
Tank firstTank = tankService.addTank("ИС-3", nationService.addNation("СССР"), firstTankLevel, 3700000);
|
||||
Tank secondTank = tankService.addTank("E-75", nationService.addNation("Германия"), secondTankLevel, 5600000);
|
||||
|
Loading…
x
Reference in New Issue
Block a user