Точечные правки. Всё работает.

This commit is contained in:
Programmist73 2023-04-19 19:57:09 +04:00
parent a0ff253478
commit 01470af3bb
7 changed files with 142 additions and 84 deletions

View File

@ -12,6 +12,10 @@ repositories {
mavenCentral() mavenCentral()
} }
jar {
enabled = false
}
dependencies { dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.boot:spring-boot-starter-web'

View File

@ -2,8 +2,8 @@ package premium_store.controller.controller;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import premium_store.controller.DTO.ClientDTO; import premium_store.controller.DTO.ClientDTO;
import premium_store.model.Tank;
import premium_store.service.GameClientService; import premium_store.service.GameClientService;
import premium_store.service.TankService;
import java.util.List; import java.util.List;
@ -11,9 +11,11 @@ import java.util.List;
@RequestMapping("/client") @RequestMapping("/client")
public class GameClientController { public class GameClientController {
private final GameClientService gameClientService; private final GameClientService gameClientService;
private final TankService tankService;
public GameClientController(GameClientService gameClientService){ public GameClientController(GameClientService gameClientService, TankService tankService){
this.gameClientService = gameClientService; this.gameClientService = gameClientService;
this.tankService = tankService;
} }
//аннотация PathVariable связывает значения id из URL и Long id //аннотация PathVariable связывает значения id из URL и Long id
@ -42,8 +44,8 @@ public class GameClientController {
@RequestParam("nickName") String nickName, @RequestParam("nickName") String nickName,
@RequestParam("email") String email, @RequestParam("email") String email,
@RequestParam("balance") Integer balance, @RequestParam("balance") Integer balance,
@RequestParam("tanks") List<Tank> tanks) { @RequestParam("tankId") Long tankId) {
return new ClientDTO(gameClientService.updateClient(id, nickName, email, balance, tanks)); return new ClientDTO(gameClientService.updateClient(id, nickName, email, balance, tankService.findTank(tankId)));
} }
@DeleteMapping("/{id}") @DeleteMapping("/{id}")

View File

@ -1,8 +1,9 @@
package premium_store.controller.controller; package premium_store.controller.controller;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import premium_store.model.Nation; import premium_store.controller.DTO.FullNationDTO;
import premium_store.service.NationService; import premium_store.service.NationService;
import premium_store.service.TankService;
import java.util.List; import java.util.List;
@ -10,41 +11,46 @@ import java.util.List;
//здесь происходит внедрение зависимости нашего сервиса //здесь происходит внедрение зависимости нашего сервиса
//так же здесь прописываем вызовы методов CRUD в привязке к URL //так же здесь прописываем вызовы методов CRUD в привязке к URL
@RestController @RestController
@CrossOrigin
@RequestMapping("/nation") @RequestMapping("/nation")
public class NationController { public class NationController {
private final NationService nationService; private final NationService nationService;
private final TankService tankService;
public NationController(NationService nationService){ public NationController(NationService nationService, TankService tankService){
this.nationService = nationService; this.nationService = nationService;
this.tankService = tankService;
} }
//аннотация PathVariable связывает значения id из URL и Long id //аннотация PathVariable связывает значения id из URL и Long id
@GetMapping("/{id}") @GetMapping("/{id}")
public Nation getNation(@PathVariable Long id) { public FullNationDTO getNation(@PathVariable Long id) {
return nationService.findNation(id); return new FullNationDTO(nationService.findNation(id));
} }
//с помощью Java Stream преобразуем набор пришедших данных в объекты StudentDto //с помощью Java Stream преобразуем набор пришедших данных в объекты StudentDto
@GetMapping("/") @GetMapping("/")
public List<Nation> getNations() { public List<FullNationDTO> getNations() {
return nationService.findAllNations().stream() return nationService.findAllNations().stream()
.map(FullNationDTO::new)
.toList(); .toList();
} }
@PostMapping("/") @PostMapping("/")
public Nation createNation(@RequestParam("nation") String nation) { public FullNationDTO createNation(@RequestParam("nation") String nation) {
return nationService.addNation(nation); return new FullNationDTO(nationService.addNation(nation));
} }
@PutMapping("/{id}") @PutMapping("/{id}")
public Nation updateNation(@PathVariable Long id, public FullNationDTO updateNation(@PathVariable Long id,
@RequestParam("nation") String nation) { @RequestParam("nation") String nation,
return nationService.updateNation(id, nation); @RequestParam(value = "tankId", required = false) Long tankId ) {
return new FullNationDTO(nationService.updateNation(id, nation, (tankId > 0) ? tankService.findTank(tankId) : null));
} }
@DeleteMapping("/{id}") @DeleteMapping("/{id}")
public Nation deleteNation(@PathVariable Long id) { public FullNationDTO deleteNation(@PathVariable Long id) {
return nationService.deleteNation(id); return new FullNationDTO(nationService.deleteNation(id));
} }
} }

View File

@ -2,20 +2,24 @@ package premium_store.controller.controller;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import premium_store.controller.DTO.TankDTO; import premium_store.controller.DTO.TankDTO;
import premium_store.model.GameClient; import premium_store.service.NationService;
import premium_store.model.TankLevel; import premium_store.service.TankLevelService;
import premium_store.model.Nation;
import premium_store.service.TankService; import premium_store.service.TankService;
import java.util.List; import java.util.List;
@RestController @RestController
@CrossOrigin
@RequestMapping("/tank") @RequestMapping("/tank")
public class TankController { public class TankController {
private final TankService tankService; private final TankService tankService;
private final TankLevelService tankLevelService;
private final NationService nationService;
public TankController(TankService tankService){ public TankController(TankService tankService, TankLevelService tankLevelService, NationService nationService){
this.tankService = tankService; this.tankService = tankService;
this.tankLevelService = tankLevelService;
this.nationService = nationService;
} }
//аннотация PathVariable связывает значения id из URL и Long id //аннотация PathVariable связывает значения id из URL и Long id
@ -32,22 +36,32 @@ public class TankController {
.toList(); .toList();
} }
@GetMapping("/filteredList/")
public List<TankDTO> getFilteredTanks(@RequestParam("nation") String nation,
@RequestParam("firstLevel") int firstLevel,
@RequestParam("secondLevel") int secondLevel) {
return tankService.findListTank(nation, firstLevel, secondLevel).stream()
.map(TankDTO::new)
.toList();
}
@PostMapping("/") @PostMapping("/")
public TankDTO createTank(@RequestParam("firstName") String name, public TankDTO createTank(@RequestParam("firstName") String name,
@RequestParam("nation") Nation nation, @RequestParam("nationId") Long nationId,
@RequestParam("level") TankLevel tankLevel, @RequestParam("levelId") Long tankLevelId,
@RequestParam("cost") int cost) { @RequestParam("cost") int cost
return new TankDTO(tankService.addTank(name, nation, tankLevel, cost)); ) {
return new TankDTO(tankService.addTank(name, nationService.findNation(nationId), tankLevelService.findLevel(tankLevelId), cost));
} }
@PutMapping("/{id}") @PutMapping("/{id}")
public TankDTO updateTank(@PathVariable Long id, public TankDTO updateTank(@PathVariable Long id,
@RequestParam("firstName") String name, @RequestParam("firstName") String name,
@RequestParam("nation") Nation nation, @RequestParam("nationId") Long nationId,
@RequestParam("level") TankLevel tankLevel, @RequestParam("levelId") Long tankLevelId,
@RequestParam("cost") int cost, @RequestParam("cost") int cost
@RequestParam("clients")List<GameClient> gameClients) { ) {
return new TankDTO(tankService.updateTank(id, name, nation, tankLevel, cost, gameClients)); return new TankDTO(tankService.updateTank(id, name, nationService.findNation(nationId), tankLevelService.findLevel(tankLevelId), cost));
} }
@DeleteMapping("/{id}") @DeleteMapping("/{id}")

View File

@ -1,7 +1,7 @@
package premium_store.controller.controller; package premium_store.controller.controller;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import premium_store.model.TankLevel; import premium_store.controller.DTO.LevelDTO;
import premium_store.service.TankLevelService; import premium_store.service.TankLevelService;
import java.util.List; import java.util.List;
@ -20,30 +20,31 @@ public class TankLevelController {
//аннотация PathVariable связывает значения id из URL и Long id //аннотация PathVariable связывает значения id из URL и Long id
@GetMapping("/{id}") @GetMapping("/{id}")
public TankLevel getLevel(@PathVariable Long id) { public LevelDTO getLevel(@PathVariable Long id) {
return tankLevelService.findLevel(id); return new LevelDTO(tankLevelService.findLevel(id));
} }
//с помощью Java Stream преобразуем набор пришедших данных в объекты StudentDto //с помощью Java Stream преобразуем набор пришедших данных в объекты StudentDto
@GetMapping("/") @GetMapping("/")
public List<TankLevel> getLevels() { public List<LevelDTO> getLevels() {
return tankLevelService.findAllLevels().stream() return tankLevelService.findAllLevels().stream()
.map(LevelDTO::new)
.toList(); .toList();
} }
@PostMapping("/") @PostMapping("/")
public TankLevel createLevel(@RequestParam("Level") int level) { public LevelDTO createLevel(@RequestParam("Level") int level) {
return tankLevelService.addLevel(level); return new LevelDTO(tankLevelService.addLevel(level));
} }
@PutMapping("/{id}") @PutMapping("/{id}")
public TankLevel updateLevel(@PathVariable Long id, public LevelDTO updateLevel(@PathVariable Long id,
@RequestParam("Level") int level) { @RequestParam("Level") int level) {
return tankLevelService.updateLevel(id, level); return new LevelDTO(tankLevelService.updateLevel(id, level));
} }
@DeleteMapping("/{id}") @DeleteMapping("/{id}")
public TankLevel deleteLevel(@PathVariable Long id) { public LevelDTO deleteLevel(@PathVariable Long id) {
return tankLevelService.deleteLevel(id); return new LevelDTO(tankLevelService.deleteLevel(id));
} }
} }

View File

@ -1,5 +1,6 @@
spring.main.banner-mode=off spring.main.banner-mode=off
server.port=8080 server.port=8080
server.tomcat.relaxed-query-chars=|,{,},[,]
spring.datasource.url=jdbc:h2:file:./data 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

View File

@ -21,38 +21,51 @@ import java.util.*;
@SpringBootTest @SpringBootTest
class PremiumStoreApplicationTests { class PremiumStoreApplicationTests {
private static final Logger log = LoggerFactory.getLogger(PremiumStoreApplicationTests.class); private static final Logger log = LoggerFactory.getLogger(PremiumStoreApplicationTests.class);
@Autowired @Autowired
TankService tankService; TankService tankService;
@Autowired @Autowired
GameClientService gameClientService; GameClientService gameClientService;
@Autowired @Autowired
TankLevelService tankLevelService; TankLevelService tankLevelService;
@Autowired @Autowired
NationService nationService; NationService nationService;
@Test @Test
void testClientRead() { void testClientRead() {
gameClientService.deleteAllClients(); gameClientService.deleteAllClients();
GameClient gameClient = gameClientService.addClient("3tankista73", "fff@mail.ru", 3400); GameClient client = gameClientService.addClient("3tankista73", "fff@mail.ru", 3400);
log.info(gameClient.toString()); log.info(client.toString());
GameClient findGameClient = gameClientService.findClient(gameClient.getId()); GameClient findClient = gameClientService.findClient(client.getId());
log.info(findGameClient.toString()); log.info(findClient.toString());
Assertions.assertEquals(gameClient, findGameClient); Assertions.assertEquals(client, findClient);
} }
@Test @Test
void testNationRead() { void testNationRead() {
nationService.deleteAllNations(); nationService.deleteAllNations();
tankService.deleteAllTanks();
tankLevelService.deleteAllLevels();
Nation nation = nationService.addNation("Германия");
TankLevel firstLevel = tankLevelService.addLevel(8);
TankLevel secondLevel = tankLevelService.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(), "СССР", null);
Nation nation = nationService.addNation("СССР");
log.info(nation.toString()); log.info(nation.toString());
Nation findNation = nationService.findNation(nation.getId()); Nation findNation = nationService.findNation(nation.getId());
@ -72,11 +85,11 @@ class PremiumStoreApplicationTests {
tankService.deleteAllTanks(); tankService.deleteAllTanks();
tankLevelService.deleteAllLevels(); tankLevelService.deleteAllLevels();
TankLevel tankLevel = tankLevelService.addLevel(8); TankLevel level = tankLevelService.addLevel(8);
log.info(tankLevel.toString()); log.info(level.toString());
TankLevel secondTankLevel = tankLevelService.addLevel(9); TankLevel secondLevel = tankLevelService.addLevel(9);
log.info(secondTankLevel.toString()); log.info(secondLevel.toString());
Assertions.assertEquals(tankLevelService.findAllLevels().size(), 2); Assertions.assertEquals(tankLevelService.findAllLevels().size(), 2);
} }
@ -86,22 +99,22 @@ class PremiumStoreApplicationTests {
tankService.deleteAllTanks(); tankService.deleteAllTanks();
tankLevelService.deleteAllLevels(); tankLevelService.deleteAllLevels();
List<TankLevel> tankLevels = tankLevelService.findAllLevels(); List<TankLevel> levels = tankLevelService.findAllLevels();
log.info(tankLevels.toString()); log.info(levels.toString());
Assertions.assertEquals(tankLevels.size(), 0); Assertions.assertEquals(levels.size(), 0);
} }
@Test @Test
void testTankReadAll() { void testTankReadAll() {
tankService.deleteAllTanks(); tankService.deleteAllTanks();
tankLevelService.deleteAllLevels(); tankLevelService.deleteAllLevels();
TankLevel firstTankLevel = tankLevelService.addLevel(8); TankLevel firstLevel = tankLevelService.addLevel(8);
TankLevel secondTankLevel = tankLevelService.addLevel(9); TankLevel secondLevel = tankLevelService.addLevel(9);
tankService.addTank("ИС-3", nationService.addNation("СССР"), firstTankLevel, 3700000); tankService.addTank("ИС-3", nationService.addNation("СССР"), firstLevel, 3700000);
tankService.addTank("E-75", nationService.addNation("Германия"), secondTankLevel, 5600000); tankService.addTank("E-75", nationService.addNation("Германия"), secondLevel, 5600000);
List<Tank> tanks = tankService.findAllTanks(); List<Tank> tanks = tankService.findAllTanks();
log.info(tanks.toString()); log.info(tanks.toString());
@ -113,41 +126,58 @@ class PremiumStoreApplicationTests {
void testClientCreate() { void testClientCreate() {
gameClientService.deleteAllClients(); gameClientService.deleteAllClients();
GameClient firstGameClient = gameClientService.addClient("Barbarian", "dsfg@gmail.com", 56000); GameClient firstClient = gameClientService.addClient("Barbarian", "dsfg@gmail.com", 56000);
log.info(firstGameClient.toString()); log.info(firstClient.toString());
GameClient secondGameClient = gameClientService.addClient("KorbenDallas", "tankoviyGeniy@mail.ru", 37000); GameClient secondClient = gameClientService.addClient("KorbenDallas", "tankoviyGeniy@mail.ru", 37000);
log.info(secondGameClient.toString()); log.info(secondClient.toString());
Assertions.assertEquals(gameClientService.findAllClients().size(), 2); Assertions.assertEquals(gameClientService.findAllClients().size(), 2);
} }
@Test @Test
void testUpdateTank(){ void testUpdateTank(){
tankService.deleteAllTanks(); tankService.deleteAllTanks();
tankLevelService.deleteAllLevels(); tankLevelService.deleteAllLevels();
gameClientService.deleteAllClients(); gameClientService.deleteAllClients();
GameClient firstGameClient = gameClientService.addClient("Barbarian", "dsfg@gmail.com", 56000); TankLevel firstLevel = tankLevelService.addLevel(8);
GameClient secondGameClient = gameClientService.addClient("KorbenDallas", "tankoviyGeniy@mail.ru", 37000); TankLevel secondLevel = tankLevelService.addLevel(9);
TankLevel firstTankLevel = tankLevelService.addLevel(8); Tank firstTank = tankService.addTank("ИС-3", nationService.addNation("СССР"), firstLevel, 3700000);
TankLevel secondTankLevel = tankLevelService.addLevel(9); Tank secondTank = tankService.addTank("E-75", nationService.addNation("Германия"), secondLevel, 5600000);
Tank firstTank = tankService.addTank("ИС-3", nationService.addNation("СССР"), firstTankLevel, 3700000);
Tank secondTank = tankService.addTank("E-75", nationService.addNation("Германия"), secondTankLevel, 5600000);
Tank tank = tankService.findTank(firstTank.getId()); Tank tank = tankService.findTank(firstTank.getId());
log.info(tank.toString()); log.info(tank.toString());
List<GameClient> newGameClient = new ArrayList<>();
Collections.addAll(newGameClient, firstGameClient, secondGameClient);
tank.setName("ИСУ-152"); tank.setName("ИСУ-152");
tank.setLevel(secondTankLevel); tank.setLevel(secondLevel);
tank.setCost(4100000); tank.setCost(4100000);
tank.setClients(newGameClient);
log.info(tank.toString()); log.info(tank.toString());
} }
@Test
void testNewTest() {
nationService.deleteAllNations();
tankService.deleteAllTanks();
tankLevelService.deleteAllLevels();
Nation nation = nationService.addNation("Германия");
Nation newNation = nationService.addNation("CCCР");
TankLevel firstLevel = tankLevelService.addLevel(8);
TankLevel secondLevel = tankLevelService.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(), "СССР", null);
//tanks = tankService.findListTank(firstLevel, nation);
log.info("AAAAA" + tanks.toString());
}
} }