diff --git a/spring_online_calculator/src/main/java/premium_store/controller/controller/ClientController.java b/spring_online_calculator/src/main/java/premium_store/controller/controller/GameClientController.java similarity index 69% rename from spring_online_calculator/src/main/java/premium_store/controller/controller/ClientController.java rename to spring_online_calculator/src/main/java/premium_store/controller/controller/GameClientController.java index afb46d8..3bcfdc9 100644 --- a/spring_online_calculator/src/main/java/premium_store/controller/controller/ClientController.java +++ b/spring_online_calculator/src/main/java/premium_store/controller/controller/GameClientController.java @@ -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 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 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)); } } diff --git a/spring_online_calculator/src/main/java/premium_store/controller/controller/LevelController.java b/spring_online_calculator/src/main/java/premium_store/controller/controller/TankLevelController.java similarity index 72% rename from spring_online_calculator/src/main/java/premium_store/controller/controller/LevelController.java rename to spring_online_calculator/src/main/java/premium_store/controller/controller/TankLevelController.java index 561c5d9..2ea9050 100644 --- a/spring_online_calculator/src/main/java/premium_store/controller/controller/LevelController.java +++ b/spring_online_calculator/src/main/java/premium_store/controller/controller/TankLevelController.java @@ -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 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); } } diff --git a/spring_online_calculator/src/main/java/premium_store/model/GameClient.java b/spring_online_calculator/src/main/java/premium_store/model/GameClient.java index 8236d65..c2c686f 100644 --- a/spring_online_calculator/src/main/java/premium_store/model/GameClient.java +++ b/spring_online_calculator/src/main/java/premium_store/model/GameClient.java @@ -6,6 +6,7 @@ import java.util.List; import java.util.Objects; @Entity +@Table(name = "GAMECLIENTS") public class GameClient { @Id @GeneratedValue(strategy = GenerationType.AUTO) diff --git a/spring_online_calculator/src/main/java/premium_store/model/Nation.java b/spring_online_calculator/src/main/java/premium_store/model/Nation.java index 63472e7..ee58588 100644 --- a/spring_online_calculator/src/main/java/premium_store/model/Nation.java +++ b/spring_online_calculator/src/main/java/premium_store/model/Nation.java @@ -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) diff --git a/spring_online_calculator/src/main/java/premium_store/model/Tank.java b/spring_online_calculator/src/main/java/premium_store/model/Tank.java index 31a1f08..7163fe2 100644 --- a/spring_online_calculator/src/main/java/premium_store/model/Tank.java +++ b/spring_online_calculator/src/main/java/premium_store/model/Tank.java @@ -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 gameClients; public Tank() { diff --git a/spring_online_calculator/src/main/java/premium_store/model/TankLevel.java b/spring_online_calculator/src/main/java/premium_store/model/TankLevel.java index 9cfd625..20266e7 100644 --- a/spring_online_calculator/src/main/java/premium_store/model/TankLevel.java +++ b/spring_online_calculator/src/main/java/premium_store/model/TankLevel.java @@ -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) diff --git a/spring_online_calculator/src/main/java/premium_store/repository/ClientRepository.java b/spring_online_calculator/src/main/java/premium_store/repository/GameClientRepository.java similarity index 63% rename from spring_online_calculator/src/main/java/premium_store/repository/ClientRepository.java rename to spring_online_calculator/src/main/java/premium_store/repository/GameClientRepository.java index e02d11f..f8efe4c 100644 --- a/spring_online_calculator/src/main/java/premium_store/repository/ClientRepository.java +++ b/spring_online_calculator/src/main/java/premium_store/repository/GameClientRepository.java @@ -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 { +public interface GameClientRepository extends JpaRepository { } diff --git a/spring_online_calculator/src/main/java/premium_store/repository/LevelRepository.java b/spring_online_calculator/src/main/java/premium_store/repository/TankLevelRepository.java similarity index 80% rename from spring_online_calculator/src/main/java/premium_store/repository/LevelRepository.java rename to spring_online_calculator/src/main/java/premium_store/repository/TankLevelRepository.java index 134449c..88b0128 100644 --- a/spring_online_calculator/src/main/java/premium_store/repository/LevelRepository.java +++ b/spring_online_calculator/src/main/java/premium_store/repository/TankLevelRepository.java @@ -5,5 +5,5 @@ import premium_store.model.TankLevel; //класс для взаимодействия с БД вместо низкоуровневого EntityManager //передаём тип класса и тип id его элементов -public interface LevelRepository extends JpaRepository { +public interface TankLevelRepository extends JpaRepository { } diff --git a/spring_online_calculator/src/main/java/premium_store/service/ClientService.java b/spring_online_calculator/src/main/java/premium_store/service/GameClientService.java similarity index 73% rename from spring_online_calculator/src/main/java/premium_store/service/ClientService.java rename to spring_online_calculator/src/main/java/premium_store/service/GameClientService.java index 1d5844e..b433b36 100644 --- a/spring_online_calculator/src/main/java/premium_store/service/ClientService.java +++ b/spring_online_calculator/src/main/java/premium_store/service/GameClientService.java @@ -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 client = clientRepository.findById(id); + final Optional client = gameClientRepository.findById(id); return client.orElseThrow(() -> new ClientNotFoundException(id)); } @Transactional(readOnly = true) public List 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(); } } diff --git a/spring_online_calculator/src/main/java/premium_store/service/LevelService.java b/spring_online_calculator/src/main/java/premium_store/service/TankLevelService.java similarity index 75% rename from spring_online_calculator/src/main/java/premium_store/service/LevelService.java rename to spring_online_calculator/src/main/java/premium_store/service/TankLevelService.java index c9c3e70..49fd28e 100644 --- a/spring_online_calculator/src/main/java/premium_store/service/LevelService.java +++ b/spring_online_calculator/src/main/java/premium_store/service/TankLevelService.java @@ -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 level = levelRepository.findById(id); + final Optional 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 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(); } } diff --git a/spring_online_calculator/src/test/java/premium_store/PremiumStoreApplicationTests.java b/spring_online_calculator/src/test/java/premium_store/PremiumStoreApplicationTests.java index e2ffaf1..3446a64 100644 --- a/spring_online_calculator/src/test/java/premium_store/PremiumStoreApplicationTests.java +++ b/spring_online_calculator/src/test/java/premium_store/PremiumStoreApplicationTests.java @@ -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 tankLevels = levelService.findAllLevels(); + List 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);