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 88b3f07..de82941 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 @@ -54,6 +54,14 @@ public class Client { this.balance = balance; } + public List getTanks(){ + return tanks; + } + + public void setTanks(List tanks){ + this.tanks = tanks; + } + //метод для сравнения @Override public boolean equals(Object o) { @@ -81,6 +89,7 @@ public class Client { ", nickName='" + nickName + '\'' + ", email='" + email + '\'' + ", balance='" + balance + '\'' + + ", tanks count='" + tanks.size() + '\'' + '}'; } } 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 f3b2ae1..f223c6f 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,15 +35,15 @@ public class Tank { public Tank() { } - public Long getId(){ - return id; - } - public Tank(String name, int cost) { this.name = name; this.cost = cost; } + public Long getId(){ + return id; + } + public String getName() { return name; } @@ -111,6 +111,18 @@ public class Tank { @Override public int hashCode() { - return Objects.hash(getId(), getName(), getLevel(), getClients(), getCost()); + return Objects.hash(id); + } + + //преобразование данных по объекту в строчку + @Override + public String toString() { + return "Student{" + + "id=" + id + + ", name='" + name + '\'' + + ", nation='" + nation + '\'' + + ", level='" + level + '\'' + + ", 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 new file mode 100644 index 0000000..87a7994 --- /dev/null +++ b/spring_online_calculator/src/main/java/labWork/premium_store/service/ClientService.java @@ -0,0 +1,76 @@ +package labWork.premium_store.service; + +import labWork.premium_store.model.Client; +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 ClientService { + @PersistenceContext + private EntityManager em; + + @Transactional + public Client addClient(String newNickName, String newEmail, Integer newBallance) { + if (!StringUtils.hasText(newNickName) || !StringUtils.hasText(newEmail)) { + throw new IllegalArgumentException("NickName or newEmail is zero or empty"); + } + + final Client client = new Client(newNickName, newEmail, newBallance); + em.persist(client); + + return client; + } + + @Transactional(readOnly = true) + public Client findClient(Long id) { + final Client Client = em.find(Client.class, id); + + if (Client == null) { + throw new EntityNotFoundException(String.format("Client with id [%s] is not found", id)); + } + + return Client; + } + + @Transactional(readOnly = true) + public List findAllClients() { + return em.createQuery("select c from Client c", Client.class) + .getResultList(); + } + + @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"); + } + + final Client currentClient = findClient(id); + + currentClient.setNickName(newNickName); + currentClient.setNickName(newNickName); + currentClient.setBalance(newBallance); + currentClient.setTanks(newTanks); + + return em.merge(currentClient); + } + + @Transactional + public Client deleteClient(Long id) { + final Client currentClient = findClient(id); + em.remove(currentClient); + + return currentClient; + } + + @Transactional + public void deleteAllClients() { + em.createQuery("Delete from Client").executeUpdate(); + } +} diff --git a/spring_online_calculator/src/main/java/labWork/premium_store/service/LevelService.java b/spring_online_calculator/src/main/java/labWork/premium_store/service/LevelService.java new file mode 100644 index 0000000..c90b13f --- /dev/null +++ b/spring_online_calculator/src/main/java/labWork/premium_store/service/LevelService.java @@ -0,0 +1,67 @@ +package labWork.premium_store.service; + +import labWork.premium_store.model.Level; +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 LevelService { + @PersistenceContext + private EntityManager em; + + @Transactional + public Level addLevel(int newLevel) { + if (newLevel <= 0) { + throw new IllegalArgumentException("Level name is zero or less"); + } + final Level level = new Level(newLevel); + em.persist(level); + return level; + } + + @Transactional(readOnly = true) + public Level findLevel(Long id) { + final Level level = em.find(Level.class, id); + if (level == null) { + throw new EntityNotFoundException(String.format("Level with id [%s] is not found", id)); + } + return level; + } + + @Transactional(readOnly = true) + public List findAllLevels() { + return em.createQuery("select s from Level s", Level.class) + .getResultList(); + } + + @Transactional + public Level updateLevel(Long id, int newLevel) { + if (newLevel <= 0) { + throw new IllegalArgumentException("Level name is zero or less"); + } + + final Level currentLevel = findLevel(id); + currentLevel.setLevel(newLevel); + + return em.merge(currentLevel); + } + + @Transactional + public Level deleteLevel(Long id) { + final Level currentLevel = findLevel(id); + em.remove(currentLevel); + + return currentLevel; + } + + @Transactional + public void deleteAllLevels() { + em.createQuery("Delete from Level").executeUpdate(); + } +} diff --git a/spring_online_calculator/src/main/java/labWork/premium_store/service/NationService.java b/spring_online_calculator/src/main/java/labWork/premium_store/service/NationService.java new file mode 100644 index 0000000..263e783 --- /dev/null +++ b/spring_online_calculator/src/main/java/labWork/premium_store/service/NationService.java @@ -0,0 +1,71 @@ +package labWork.premium_store.service; + +import labWork.premium_store.model.Nation; +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 NationService { + @PersistenceContext + private EntityManager em; + + @Transactional + public Nation addNation(String nameNation){ + if(!StringUtils.hasText(nameNation)){ + throw new IllegalArgumentException("The name of the nation is null or empty"); + } + + final Nation nation = new Nation(nameNation); + em.persist(nation); + + return nation; + } + + @Transactional(readOnly = true) + public Nation findNation(Long id){ + final Nation nation = em.find(Nation.class, id); + + if(nation == null){ + throw new EntityNotFoundException(String.format("Nation with id [%s] is not found", id)); + } + + return nation; + } + + @Transactional(readOnly = true) + public List findAllNations() { + return em.createQuery("select n from Nation n", Nation.class) + .getResultList(); + } + + @Transactional + public Nation updateNation(Long id, String newNation) { + if (!StringUtils.hasText(newNation)) { + throw new IllegalArgumentException("Student name is null or empty"); + } + + final Nation currentNation = findNation(id); + currentNation.setNation(newNation); + + return em.merge(currentNation); + } + + @Transactional + public Nation deleteNAtion(Long id) { + final Nation currentNation = findNation(id); + em.remove(currentNation); + + return currentNation; + } + + @Transactional + public void deleteAllNations() { + em.createQuery("delete from Nation").executeUpdate(); + } +}