Промежуточное сохранение.
This commit is contained in:
parent
9297fb06ae
commit
b79d35d1af
@ -54,6 +54,14 @@ public class Client {
|
||||
this.balance = balance;
|
||||
}
|
||||
|
||||
public List<Tank> getTanks(){
|
||||
return tanks;
|
||||
}
|
||||
|
||||
public void setTanks(List<Tank> 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() + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
|
@ -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 + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
|
@ -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<Client> 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<Tank> 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();
|
||||
}
|
||||
}
|
@ -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<Level> 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();
|
||||
}
|
||||
}
|
@ -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<Nation> 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();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user