Добавление всех ревисов.
This commit is contained in:
parent
b79d35d1af
commit
663fd6ad72
@ -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<Tank> tanks;
|
||||
|
||||
|
@ -8,6 +8,7 @@ public class Level {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
private Long id;
|
||||
|
||||
@Column(nullable = false)
|
||||
private int level;
|
||||
|
||||
|
@ -8,6 +8,7 @@ public class Nation {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
private Long id;
|
||||
|
||||
@Column(nullable = false)
|
||||
private String nation;
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
@ -46,17 +46,28 @@ public class ClientService {
|
||||
}
|
||||
|
||||
@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");
|
||||
public Client updateClient(Long id, String newNickName, String newEmail, Integer newBalance, List<Tank> 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();
|
||||
|
@ -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<Tank> 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<Client> 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();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user