From b79d35d1af62323a7dc06484155d3a537fb9cbf2 Mon Sep 17 00:00:00 2001 From: Programmist73 Date: Thu, 6 Apr 2023 21:31:26 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9F=D1=80=D0=BE=D0=BC=D0=B5=D0=B6=D1=83?= =?UTF-8?q?=D1=82=D0=BE=D1=87=D0=BD=D0=BE=D0=B5=20=D1=81=D0=BE=D1=85=D1=80?= =?UTF-8?q?=D0=B0=D0=BD=D0=B5=D0=BD=D0=B8=D0=B5.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../labWork/premium_store/model/Client.java | 9 +++ .../labWork/premium_store/model/Tank.java | 22 ++++-- .../premium_store/service/ClientService.java | 76 +++++++++++++++++++ .../premium_store/service/LevelService.java | 67 ++++++++++++++++ .../premium_store/service/NationService.java | 71 +++++++++++++++++ 5 files changed, 240 insertions(+), 5 deletions(-) create mode 100644 spring_online_calculator/src/main/java/labWork/premium_store/service/ClientService.java create mode 100644 spring_online_calculator/src/main/java/labWork/premium_store/service/LevelService.java create mode 100644 spring_online_calculator/src/main/java/labWork/premium_store/service/NationService.java 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(); + } +}