Добавление всех ревисов.

This commit is contained in:
Programmist73 2023-04-07 11:00:17 +04:00
parent b79d35d1af
commit 663fd6ad72
6 changed files with 123 additions and 8 deletions

View File

@ -10,11 +10,15 @@ public class Client {
@Id @Id
@GeneratedValue(strategy = GenerationType.AUTO) @GeneratedValue(strategy = GenerationType.AUTO)
private Long id; private Long id;
@Column(nullable = false, length = 255) @Column(nullable = false, length = 255)
private String nickName; private String nickName;
@Column(nullable = false, length = 255) @Column(nullable = false, length = 255)
private String email; private String email;
private Integer balance; private Integer balance;
@ManyToMany(mappedBy = "clients") @ManyToMany(mappedBy = "clients")
private List<Tank> tanks; private List<Tank> tanks;

View File

@ -8,6 +8,7 @@ public class Level {
@Id @Id
@GeneratedValue(strategy = GenerationType.AUTO) @GeneratedValue(strategy = GenerationType.AUTO)
private Long id; private Long id;
@Column(nullable = false) @Column(nullable = false)
private int level; private int level;

View File

@ -8,6 +8,7 @@ public class Nation {
@Id @Id
@GeneratedValue(strategy = GenerationType.AUTO) @GeneratedValue(strategy = GenerationType.AUTO)
private Long id; private Long id;
@Column(nullable = false) @Column(nullable = false)
private String nation; private String nation;

View File

@ -35,8 +35,10 @@ public class Tank {
public Tank() { public Tank() {
} }
public Tank(String name, int cost) { public Tank(String name, Nation nation, Level level, int cost) {
this.name = name; this.name = name;
this.nation = nation;
this.level = level;
this.cost = cost; this.cost = cost;
} }

View File

@ -46,17 +46,28 @@ public class ClientService {
} }
@Transactional @Transactional
public Client updateClient(Long id, String newNickName, String newEmail, Integer newBallance, List<Tank> newTanks) { public Client updateClient(Long id, String newNickName, String newEmail, Integer newBalance, List<Tank> newTanks) {
if (!StringUtils.hasText(newNickName) || !StringUtils.hasText(newEmail)) { if (id <= 0) {
throw new IllegalArgumentException("NickName or newEmail is zero or empty"); throw new IllegalArgumentException("Invalid id");
} }
final Client currentClient = findClient(id); final Client currentClient = findClient(id);
currentClient.setNickName(newNickName); if (StringUtils.hasText(newNickName)){
currentClient.setNickName(newNickName); currentClient.setNickName(newNickName);
currentClient.setBalance(newBallance); }
currentClient.setTanks(newTanks);
if(StringUtils.hasText(newEmail)){
currentClient.setEmail(newEmail);
}
if(newBalance != null){
currentClient.setBalance(newBalance);
}
if(newTanks != null){
currentClient.setTanks(newTanks);
}
return em.merge(currentClient); return em.merge(currentClient);
} }
@ -69,6 +80,7 @@ public class ClientService {
return currentClient; return currentClient;
} }
//прямой sql-запрос на удаление всех записей в таблице
@Transactional @Transactional
public void deleteAllClients() { public void deleteAllClients() {
em.createQuery("Delete from Client").executeUpdate(); em.createQuery("Delete from Client").executeUpdate();

View File

@ -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();
}
}