diff --git a/spring_online_calculator/src/main/java/labWork/premium_store/model/Client.java b/spring_online_calculator/src/main/java/labWork/premium_store/model/Client.java index de82941..c2aeda0 100644 --- a/spring_online_calculator/src/main/java/labWork/premium_store/model/Client.java +++ b/spring_online_calculator/src/main/java/labWork/premium_store/model/Client.java @@ -10,11 +10,15 @@ public class Client { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; + @Column(nullable = false, length = 255) private String nickName; + @Column(nullable = false, length = 255) private String email; + private Integer balance; + @ManyToMany(mappedBy = "clients") private List tanks; diff --git a/spring_online_calculator/src/main/java/labWork/premium_store/model/Level.java b/spring_online_calculator/src/main/java/labWork/premium_store/model/Level.java index 2b11cf2..1e8ba6f 100644 --- a/spring_online_calculator/src/main/java/labWork/premium_store/model/Level.java +++ b/spring_online_calculator/src/main/java/labWork/premium_store/model/Level.java @@ -8,6 +8,7 @@ public class Level { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; + @Column(nullable = false) private int level; diff --git a/spring_online_calculator/src/main/java/labWork/premium_store/model/Nation.java b/spring_online_calculator/src/main/java/labWork/premium_store/model/Nation.java index 2b2b19d..3ba19eb 100644 --- a/spring_online_calculator/src/main/java/labWork/premium_store/model/Nation.java +++ b/spring_online_calculator/src/main/java/labWork/premium_store/model/Nation.java @@ -8,6 +8,7 @@ public class Nation { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; + @Column(nullable = false) private String nation; diff --git a/spring_online_calculator/src/main/java/labWork/premium_store/model/Tank.java b/spring_online_calculator/src/main/java/labWork/premium_store/model/Tank.java index f223c6f..4dd0e4d 100644 --- a/spring_online_calculator/src/main/java/labWork/premium_store/model/Tank.java +++ b/spring_online_calculator/src/main/java/labWork/premium_store/model/Tank.java @@ -35,8 +35,10 @@ public class Tank { public Tank() { } - public Tank(String name, int cost) { + public Tank(String name, Nation nation, Level level, int cost) { this.name = name; + this.nation = nation; + this.level = level; this.cost = cost; } diff --git a/spring_online_calculator/src/main/java/labWork/premium_store/service/ClientService.java b/spring_online_calculator/src/main/java/labWork/premium_store/service/ClientService.java index 87a7994..5f688ae 100644 --- a/spring_online_calculator/src/main/java/labWork/premium_store/service/ClientService.java +++ b/spring_online_calculator/src/main/java/labWork/premium_store/service/ClientService.java @@ -46,17 +46,28 @@ public class ClientService { } @Transactional - public Client updateClient(Long id, String newNickName, String newEmail, Integer newBallance, List newTanks) { - if (!StringUtils.hasText(newNickName) || !StringUtils.hasText(newEmail)) { - throw new IllegalArgumentException("NickName or newEmail is zero or empty"); + public Client updateClient(Long id, String newNickName, String newEmail, Integer newBalance, List newTanks) { + if (id <= 0) { + throw new IllegalArgumentException("Invalid id"); } final Client currentClient = findClient(id); - currentClient.setNickName(newNickName); - currentClient.setNickName(newNickName); - currentClient.setBalance(newBallance); - currentClient.setTanks(newTanks); + if (StringUtils.hasText(newNickName)){ + currentClient.setNickName(newNickName); + } + + if(StringUtils.hasText(newEmail)){ + currentClient.setEmail(newEmail); + } + + if(newBalance != null){ + currentClient.setBalance(newBalance); + } + + if(newTanks != null){ + currentClient.setTanks(newTanks); + } return em.merge(currentClient); } @@ -69,6 +80,7 @@ public class ClientService { return currentClient; } + //прямой sql-запрос на удаление всех записей в таблице @Transactional public void deleteAllClients() { em.createQuery("Delete from Client").executeUpdate(); diff --git a/spring_online_calculator/src/main/java/labWork/premium_store/service/TankService.java b/spring_online_calculator/src/main/java/labWork/premium_store/service/TankService.java new file mode 100644 index 0000000..02a959f --- /dev/null +++ b/spring_online_calculator/src/main/java/labWork/premium_store/service/TankService.java @@ -0,0 +1,95 @@ +package labWork.premium_store.service; + +import labWork.premium_store.model.Client; +import labWork.premium_store.model.Level; +import labWork.premium_store.model.Nation; +import labWork.premium_store.model.Tank; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.util.StringUtils; + +import javax.persistence.EntityManager; +import javax.persistence.EntityNotFoundException; +import javax.persistence.PersistenceContext; +import java.util.List; + +@Service +public class TankService { + @PersistenceContext + private EntityManager em; + + @Transactional + public Tank addTank(String newName, Nation newNation, Level newLevel, int newCost) { + if (!StringUtils.hasText(newName) || newNation == null || newLevel == null || newCost <= 0) { + throw new IllegalArgumentException("New Name, new Nation, new Level or newCost is zero or empty"); + } + + final Tank tank = new Tank(newName, newNation, newLevel, newCost); + em.persist(tank); + + return tank; + } + + @Transactional(readOnly = true) + public Tank findTank(Long id) { + final Tank Tank = em.find(Tank.class, id); + + if (Tank == null) { + throw new EntityNotFoundException(String.format("Tank with id [%s] is not found", id)); + } + + return Tank; + } + + @Transactional(readOnly = true) + public List findAllTanks() { + return em.createQuery("select t from Tank t", Tank.class) + .getResultList(); + } + + @Transactional + public Tank updateTank(Long id, String newName, Nation newNation, + Level newLevel, int newCost, List newClients) { + if (id <= 0) { + throw new IllegalArgumentException("Invalid id"); + } + + final Tank currentTank = findTank(id); + + 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); + } + + if(newClients != null){ + currentTank.setClients(newClients); + } + + return em.merge(currentTank); + } + + @Transactional + public Tank deleteTank(Long id) { + final Tank currentTank = findTank(id); + em.remove(currentTank); + + return currentTank; + } + + //прямой sql-запрос на удаление всех записей в таблице + @Transactional + public void deleteAllTanks() { + em.createQuery("Delete from Tank").executeUpdate(); + } +}