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

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

View File

@ -1,8 +1,9 @@
package premium_store.controller.controller;
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.TankService;
import java.util.List;
@ -10,41 +11,46 @@ import java.util.List;
//здесь происходит внедрение зависимости нашего сервиса
//так же здесь прописываем вызовы методов CRUD в привязке к URL
@RestController
@CrossOrigin
@RequestMapping("/nation")
public class NationController {
private final NationService nationService;
private final TankService tankService;
public NationController(NationService nationService){
public NationController(NationService nationService, TankService tankService){
this.nationService = nationService;
this.tankService = tankService;
}
//аннотация PathVariable связывает значения id из URL и Long id
@GetMapping("/{id}")
public Nation getNation(@PathVariable Long id) {
return nationService.findNation(id);
public FullNationDTO getNation(@PathVariable Long id) {
return new FullNationDTO(nationService.findNation(id));
}
//с помощью Java Stream преобразуем набор пришедших данных в объекты StudentDto
@GetMapping("/")
public List<Nation> getNations() {
public List<FullNationDTO> getNations() {
return nationService.findAllNations().stream()
.map(FullNationDTO::new)
.toList();
}
@PostMapping("/")
public Nation createNation(@RequestParam("nation") String nation) {
return nationService.addNation(nation);
public FullNationDTO createNation(@RequestParam("nation") String nation) {
return new FullNationDTO(nationService.addNation(nation));
}
@PutMapping("/{id}")
public Nation updateNation(@PathVariable Long id,
@RequestParam("nation") String nation) {
return nationService.updateNation(id, nation);
public FullNationDTO updateNation(@PathVariable Long id,
@RequestParam("nation") String nation,
@RequestParam(value = "tankId", required = false) Long tankId ) {
return new FullNationDTO(nationService.updateNation(id, nation, (tankId > 0) ? tankService.findTank(tankId) : null));
}
@DeleteMapping("/{id}")
public Nation deleteNation(@PathVariable Long id) {
return nationService.deleteNation(id);
public FullNationDTO deleteNation(@PathVariable Long 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 premium_store.controller.DTO.TankDTO;
import premium_store.model.GameClient;
import premium_store.model.TankLevel;
import premium_store.model.Nation;
import premium_store.service.NationService;
import premium_store.service.TankLevelService;
import premium_store.service.TankService;
import java.util.List;
@RestController
@CrossOrigin
@RequestMapping("/tank")
public class TankController {
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.tankLevelService = tankLevelService;
this.nationService = nationService;
}
//аннотация PathVariable связывает значения id из URL и Long id
@ -32,22 +36,32 @@ public class TankController {
.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("/")
public TankDTO createTank(@RequestParam("firstName") String name,
@RequestParam("nation") Nation nation,
@RequestParam("level") TankLevel tankLevel,
@RequestParam("cost") int cost) {
return new TankDTO(tankService.addTank(name, nation, tankLevel, cost));
@RequestParam("nationId") Long nationId,
@RequestParam("levelId") Long tankLevelId,
@RequestParam("cost") int cost
) {
return new TankDTO(tankService.addTank(name, nationService.findNation(nationId), tankLevelService.findLevel(tankLevelId), cost));
}
@PutMapping("/{id}")
public TankDTO updateTank(@PathVariable Long id,
@RequestParam("firstName") String name,
@RequestParam("nation") Nation nation,
@RequestParam("level") TankLevel tankLevel,
@RequestParam("cost") int cost,
@RequestParam("clients")List<GameClient> gameClients) {
return new TankDTO(tankService.updateTank(id, name, nation, tankLevel, cost, gameClients));
@RequestParam("nationId") Long nationId,
@RequestParam("levelId") Long tankLevelId,
@RequestParam("cost") int cost
) {
return new TankDTO(tankService.updateTank(id, name, nationService.findNation(nationId), tankLevelService.findLevel(tankLevelId), cost));
}
@DeleteMapping("/{id}")

View File

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

View File

@ -1,5 +1,6 @@
spring.main.banner-mode=off
server.port=8080
server.tomcat.relaxed-query-chars=|,{,},[,]
spring.datasource.url=jdbc:h2:file:./data
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa

View File

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