Добавление DTO. Исправление сервисов и некоторых контроллеров.
This commit is contained in:
parent
973e2c6a16
commit
a0ff253478
@ -1,25 +1,44 @@
|
||||
package premium_store.controller.DTO;
|
||||
|
||||
import premium_store.model.GameClient;
|
||||
import premium_store.model.Tank;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
//класс, который соединяет танки клиента в одну строчку (нам так захотелось)
|
||||
public class ClientDTO {
|
||||
private final long id;
|
||||
private final List<Tank> tanks;
|
||||
private final String nickName;
|
||||
private final String email;
|
||||
private final Integer balance;
|
||||
private final List<TankDTO> tanks;
|
||||
|
||||
public ClientDTO(GameClient gameClient){
|
||||
this.id = gameClient.getId();
|
||||
this.tanks = gameClient.getTanks();
|
||||
this.nickName = gameClient.getNickName();
|
||||
this.email = gameClient.getEmail();
|
||||
this.balance = gameClient.getBalance();
|
||||
this.tanks = gameClient.getTanks().stream()
|
||||
.map(TankDTO::new)
|
||||
.toList();
|
||||
}
|
||||
|
||||
public long getId(){
|
||||
return id;
|
||||
}
|
||||
|
||||
public List<Tank> getTanks(){
|
||||
public String getNickName(){
|
||||
return nickName;
|
||||
}
|
||||
|
||||
public String getEmail(){
|
||||
return email;
|
||||
}
|
||||
|
||||
public Integer getBalance(){
|
||||
return balance;
|
||||
}
|
||||
|
||||
public List<TankDTO> getTanks(){
|
||||
return tanks;
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,31 @@
|
||||
package premium_store.controller.DTO;
|
||||
|
||||
import premium_store.model.Nation;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class FullNationDTO {
|
||||
public final Long id;
|
||||
public final String nation;
|
||||
public final List<TankDTO> tanks;
|
||||
|
||||
public FullNationDTO(Nation nation){
|
||||
this.id = nation.getId();
|
||||
this.nation = nation.getNation();
|
||||
this.tanks = nation.getTanksOfThisNation().stream()
|
||||
.map(TankDTO::new)
|
||||
.toList();
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getNation() {
|
||||
return nation;
|
||||
}
|
||||
|
||||
public List<TankDTO> getTanksOfThisNation() {
|
||||
return tanks;
|
||||
}
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
package premium_store.controller.DTO;
|
||||
|
||||
import premium_store.model.TankLevel;
|
||||
|
||||
public class LevelDTO {
|
||||
private final Long id;
|
||||
private final int level;
|
||||
|
||||
public LevelDTO(TankLevel level){
|
||||
this.id = level.getId();
|
||||
this.level = level.getLevel();
|
||||
}
|
||||
|
||||
public Long getId(){
|
||||
return id;
|
||||
}
|
||||
|
||||
public int getLevel(){
|
||||
return level;
|
||||
}
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
package premium_store.controller.DTO;
|
||||
|
||||
import premium_store.model.Nation;
|
||||
|
||||
public class SimpleNationDTO {
|
||||
private final Long id;
|
||||
private final String nation;
|
||||
|
||||
public SimpleNationDTO(Nation nation){
|
||||
this.id = nation.getId();
|
||||
this.nation = nation.getNation();
|
||||
}
|
||||
|
||||
public Long getId(){
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getNation(){
|
||||
return nation;
|
||||
}
|
||||
}
|
@ -1,24 +1,39 @@
|
||||
package premium_store.controller.DTO;
|
||||
|
||||
import premium_store.model.GameClient;
|
||||
import premium_store.model.Tank;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class TankDTO {
|
||||
private final long id;
|
||||
private final List<GameClient> gameClients;
|
||||
private final String name;
|
||||
private final SimpleNationDTO nation;
|
||||
private final LevelDTO level;
|
||||
private final int cost;
|
||||
|
||||
public TankDTO(Tank tank){
|
||||
this.id = tank.getId();
|
||||
this.gameClients = tank.getClients();
|
||||
this.nation = new SimpleNationDTO(tank.getNation());
|
||||
this.level = new LevelDTO(tank.getLevel());
|
||||
this.name = tank.getName();
|
||||
this.cost = tank.getCost();
|
||||
}
|
||||
|
||||
public long getId(){
|
||||
return id;
|
||||
}
|
||||
|
||||
public List<GameClient> getClients(){
|
||||
return gameClients;
|
||||
public String getName(){
|
||||
return name;
|
||||
}
|
||||
|
||||
public SimpleNationDTO getNation(){
|
||||
return nation;
|
||||
}
|
||||
|
||||
public LevelDTO getLevel(){
|
||||
return level;
|
||||
}
|
||||
|
||||
public int getCost(){
|
||||
return cost;
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,17 @@
|
||||
package premium_store.repository;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
import premium_store.model.Tank;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface TankRepository extends JpaRepository<Tank, Long> {
|
||||
@Query("SELECT t FROM Tank t WHERE t.nation.nation = :nation AND t.tankLevel.level BETWEEN :llevelid AND :l2levelid")
|
||||
List<Tank> checkNationAndLevel(
|
||||
@Param("nation") String nation,
|
||||
@Param("llevelid") int levelOne,
|
||||
@Param("l2levelid") int levelTwo);
|
||||
}
|
||||
|
||||
|
@ -2,6 +2,7 @@ package premium_store.service;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.StringUtils;
|
||||
import premium_store.model.GameClient;
|
||||
import premium_store.model.Tank;
|
||||
import premium_store.repository.GameClientRepository;
|
||||
@ -42,13 +43,28 @@ public class GameClientService {
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public GameClient updateClient(Long id, String newNickName, String newEmail, Integer newBalance, List<Tank> newTanks) {
|
||||
public GameClient updateClient(Long id, String newNickName, String newEmail, Integer newBalance, Tank newTank) {
|
||||
if (id <= 0) {
|
||||
throw new IllegalArgumentException("Invalid id");
|
||||
}
|
||||
|
||||
final GameClient currentGameClient = findClient(id);
|
||||
currentGameClient.setNickName(newNickName);
|
||||
currentGameClient.setEmail(newEmail);
|
||||
currentGameClient.setBalance(newBalance);
|
||||
currentGameClient.setTanks(newTanks);
|
||||
validatorUtil.validate(currentGameClient);
|
||||
|
||||
if (StringUtils.hasText(newNickName)){
|
||||
currentGameClient.setNickName(newNickName);
|
||||
}
|
||||
|
||||
if(StringUtils.hasText(newEmail)){
|
||||
currentGameClient.setEmail(newEmail);
|
||||
}
|
||||
|
||||
if(newBalance != null){
|
||||
currentGameClient.setBalance(newBalance);
|
||||
}
|
||||
|
||||
if(newTank != null){
|
||||
currentGameClient.setTanks(newTank);
|
||||
}
|
||||
|
||||
return gameClientRepository.save(currentGameClient);
|
||||
}
|
||||
|
@ -2,7 +2,9 @@ package premium_store.service;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.StringUtils;
|
||||
import premium_store.model.Nation;
|
||||
import premium_store.model.Tank;
|
||||
import premium_store.repository.NationRepository;
|
||||
import premium_store.service.exception.NationNotFoundException;
|
||||
import premium_store.util.validation.ValidatorUtil;
|
||||
@ -24,7 +26,7 @@ public class NationService {
|
||||
public Nation addNation(String nameNation){
|
||||
final Nation nation = new Nation(nameNation);
|
||||
validatorUtil.validate(nation);
|
||||
|
||||
|
||||
return nationRepository.save(nation);
|
||||
}
|
||||
|
||||
@ -43,10 +45,20 @@ public class NationService {
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Nation updateNation(Long id, String newNation) {
|
||||
public Nation updateNation(Long id, String newNation, Tank newTank) {
|
||||
if(id <= 0){
|
||||
throw new IllegalArgumentException(String.format("Incorrect id: [%s]", id));
|
||||
}
|
||||
|
||||
final Nation currentNation = findNation(id);
|
||||
currentNation.setNation(newNation);
|
||||
validatorUtil.validate(currentNation);
|
||||
|
||||
if (StringUtils.hasText(newNation)) {
|
||||
currentNation.setNation(newNation);
|
||||
}
|
||||
|
||||
if(newTank != null){
|
||||
currentNation.setTanksOfThisNation(newTank);
|
||||
}
|
||||
|
||||
return nationRepository.save(currentNation);
|
||||
}
|
||||
|
@ -65,3 +65,4 @@ public class TankLevelService {
|
||||
tankLevelRepository.deleteAll();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -2,7 +2,7 @@ package premium_store.service;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import premium_store.model.GameClient;
|
||||
import org.springframework.util.StringUtils;
|
||||
import premium_store.model.TankLevel;
|
||||
import premium_store.model.Nation;
|
||||
import premium_store.model.Tank;
|
||||
@ -45,14 +45,28 @@ public class TankService {
|
||||
|
||||
@Transactional
|
||||
public Tank updateTank(Long id, String newName, Nation newNation,
|
||||
TankLevel newTankLevel, int newCost, List<GameClient> newGameClients) {
|
||||
TankLevel newLevel, int newCost) {
|
||||
if (id <= 0) {
|
||||
throw new IllegalArgumentException("Invalid id");
|
||||
}
|
||||
|
||||
final Tank currentTank = findTank(id);
|
||||
currentTank.setName(newName);
|
||||
currentTank.setNation(newNation);
|
||||
currentTank.setLevel(newTankLevel);
|
||||
currentTank.setCost(newCost);
|
||||
currentTank.setClients(newGameClients);
|
||||
validatorUtil.validate(currentTank);
|
||||
|
||||
if (StringUtils.hasText(newName)){
|
||||
currentTank.setName(newName);
|
||||
}
|
||||
|
||||
if(newNation != null){
|
||||
currentTank.setNation(newNation);
|
||||
}
|
||||
|
||||
if(newLevel != null){
|
||||
currentTank.setLevel(newLevel);
|
||||
}
|
||||
|
||||
if(newCost > 0){
|
||||
currentTank.setCost(newCost);
|
||||
}
|
||||
|
||||
return tankRepository.save(currentTank);
|
||||
}
|
||||
@ -70,4 +84,10 @@ public class TankService {
|
||||
public void deleteAllTanks() {
|
||||
tankRepository.deleteAll();
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<Tank> findListTank(String nationName, int levelFirst, int levelSecond) {
|
||||
return tankRepository.checkNationAndLevel(nationName, levelFirst, levelSecond);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -2,6 +2,6 @@ package premium_store.service.exception;
|
||||
|
||||
public class ClientNotFoundException extends RuntimeException {
|
||||
public ClientNotFoundException(Long id) {
|
||||
super(String.format("Student with id [%s] is not found", id));
|
||||
super(String.format("Client with id [%s] is not found", id));
|
||||
}
|
||||
}
|
@ -2,6 +2,6 @@ package premium_store.service.exception;
|
||||
|
||||
public class NationNotFoundException extends RuntimeException {
|
||||
public NationNotFoundException(Long id) {
|
||||
super(String.format("Student with id [%s] is not found", id));
|
||||
super(String.format("Nation with id [%s] is not found", id));
|
||||
}
|
||||
}
|
||||
|
@ -2,6 +2,6 @@ package premium_store.service.exception;
|
||||
|
||||
public class TankNotFoundException extends RuntimeException {
|
||||
public TankNotFoundException(Long id) {
|
||||
super(String.format("Student with id [%s] is not found", id));
|
||||
super(String.format("Tank with id [%s] is not found", id));
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user