Сданная 3-я лаба.
This commit is contained in:
parent
4d88e5c6c3
commit
22a63747ac
@ -4,6 +4,7 @@ import javax.persistence.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Entity
|
||||
public class Client {
|
||||
@ -19,8 +20,8 @@ public class Client {
|
||||
|
||||
private Integer balance;
|
||||
|
||||
@ManyToMany(mappedBy = "clients", fetch= FetchType.EAGER)
|
||||
private List<Tank> tanks;
|
||||
@OneToMany(fetch= FetchType.EAGER)
|
||||
private List<Tank> tanks = new ArrayList<>();
|
||||
|
||||
public Client(){ }
|
||||
|
||||
@ -28,7 +29,6 @@ public class Client {
|
||||
this.nickName = nickName;
|
||||
this.email = email;
|
||||
this.balance = balance;
|
||||
tanks = new ArrayList<>();
|
||||
}
|
||||
|
||||
public Long getId(){
|
||||
@ -94,7 +94,7 @@ public class Client {
|
||||
", nickName='" + nickName + '\'' +
|
||||
", email='" + email + '\'' +
|
||||
", balance='" + balance + '\'' +
|
||||
", tanks count='" + tanks.size() + '\'' +
|
||||
", tanks='" + tanks.stream().map(Object::toString).collect(Collectors.joining(", ")) + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
|
@ -1,11 +1,8 @@
|
||||
package premium_store.model;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import java.util.Objects;
|
||||
import javax.persistence.*;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Entity
|
||||
public class Nation {
|
||||
@ -16,11 +13,15 @@ public class Nation {
|
||||
@Column(nullable = false)
|
||||
private String nation;
|
||||
|
||||
@OneToMany(mappedBy = "nation", fetch = FetchType.EAGER, cascade = CascadeType.MERGE)
|
||||
private List<Tank> tanksOfThisNation;
|
||||
|
||||
public Nation() {
|
||||
}
|
||||
|
||||
public Nation(String nation) {
|
||||
this.nation = nation;
|
||||
tanksOfThisNation = new ArrayList<>();
|
||||
}
|
||||
|
||||
//возвращает id
|
||||
@ -37,6 +38,14 @@ public class Nation {
|
||||
this.nation = nation;
|
||||
}
|
||||
|
||||
public List<Tank> getTanksOfThisNation(){
|
||||
return tanksOfThisNation;
|
||||
}
|
||||
|
||||
public void setTankOfThisNation(List<Tank> newTank){
|
||||
this.tanksOfThisNation = newTank;
|
||||
}
|
||||
|
||||
//метод для сравнения
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
@ -61,6 +70,8 @@ public class Nation {
|
||||
public String toString() {
|
||||
return "Nation{" +
|
||||
"id=" + id +
|
||||
", nation='" + nation + '}';
|
||||
", nation='" + nation + '\'' +
|
||||
", tanksOfThisNation='" + tanksOfThisNation.stream().map(Object::toString).collect(Collectors.joining(", ")) + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
|
@ -2,9 +2,6 @@ package premium_store.model;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.util.Objects;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Entity
|
||||
public class Tank {
|
||||
@ -15,24 +12,17 @@ public class Tank {
|
||||
@Column(nullable = false)
|
||||
private String name;
|
||||
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "nation_id")
|
||||
@ManyToOne(fetch = FetchType.EAGER)
|
||||
@JoinColumn(name = "nation")
|
||||
private Nation nation;
|
||||
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "level_id")
|
||||
@ManyToOne(fetch = FetchType.EAGER)
|
||||
@JoinColumn(name = "level")
|
||||
private Level level;
|
||||
|
||||
@Column(nullable = false)
|
||||
private int cost;
|
||||
|
||||
//реализация двунаправленной связи мнокие-ко-многим
|
||||
@ManyToMany(fetch= FetchType.EAGER)
|
||||
@JoinTable(name = "tanks_of_clients",
|
||||
joinColumns = @JoinColumn(name = "tank_id"),
|
||||
inverseJoinColumns = @JoinColumn(name = "client_id"))
|
||||
private List<Client> clients;
|
||||
|
||||
public Tank() {
|
||||
}
|
||||
|
||||
@ -41,7 +31,6 @@ public class Tank {
|
||||
this.nation = nation;
|
||||
this.level = level;
|
||||
this.cost = cost;
|
||||
clients = new ArrayList<>();
|
||||
}
|
||||
|
||||
public Long getId(){
|
||||
@ -80,28 +69,6 @@ public class Tank {
|
||||
this.nation = nation;
|
||||
}
|
||||
|
||||
public List<Client> getClients() {
|
||||
return clients;
|
||||
}
|
||||
|
||||
public void setClients(List<Client> clients) {
|
||||
this.clients = clients;
|
||||
}
|
||||
|
||||
public void addClient(Client newClient){
|
||||
if (clients == null){
|
||||
this.clients = new ArrayList<>();
|
||||
}
|
||||
|
||||
if (!clients.contains(newClient))
|
||||
this.clients.add(newClient);
|
||||
}
|
||||
|
||||
public void removeClient(Client remClient){
|
||||
if (clients.contains(remClient))
|
||||
this.clients.remove(remClient);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
@ -109,7 +76,7 @@ public class Tank {
|
||||
if (!(o instanceof Tank tank)) return false;
|
||||
|
||||
return Objects.equals(getId(), tank.getId()) && Objects.equals(getName(), tank.getName())
|
||||
&& Objects.equals(getCost(), tank.getCost()) && Objects.equals(getClients(), tank.getClients())
|
||||
&& Objects.equals(getCost(), tank.getCost())
|
||||
&& Objects.equals(getLevel(), tank.getLevel());
|
||||
}
|
||||
|
||||
@ -124,10 +91,9 @@ public class Tank {
|
||||
return "Tank{" +
|
||||
"id=" + id +
|
||||
", name='" + name + '\'' +
|
||||
", nation='" + nation + '\'' +
|
||||
", nation='" + nation.getId() + '\'' +
|
||||
", level='" + level + '\'' +
|
||||
", cost='" + cost + '\'' +
|
||||
", clients='" + clients.stream().map(Object::toString).collect(Collectors.joining(", ")) + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
|
@ -4,6 +4,8 @@ import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.StringUtils;
|
||||
import premium_store.model.Client;
|
||||
import premium_store.model.Level;
|
||||
import premium_store.model.Nation;
|
||||
import premium_store.model.Tank;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
|
@ -4,6 +4,7 @@ import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.StringUtils;
|
||||
import premium_store.model.Nation;
|
||||
import premium_store.model.Tank;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.EntityNotFoundException;
|
||||
@ -45,13 +46,20 @@ public class NationService {
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Nation updateNation(Long id, String newNation) {
|
||||
if (!StringUtils.hasText(newNation)) {
|
||||
throw new IllegalArgumentException("Student name is null or empty");
|
||||
public Nation updateNation(Long id, String newNation, List<Tank> newTank) {
|
||||
if(id <= 0){
|
||||
throw new IllegalArgumentException(String.format("Incorrect id: [%s]", id));
|
||||
}
|
||||
|
||||
final Nation currentNation = findNation(id);
|
||||
currentNation.setNation(newNation);
|
||||
|
||||
if (StringUtils.hasText(newNation)) {
|
||||
currentNation.setNation(newNation);
|
||||
}
|
||||
|
||||
if(newTank.size() > 0){
|
||||
currentNation.setTankOfThisNation(newTank);
|
||||
}
|
||||
|
||||
return em.merge(currentNation);
|
||||
}
|
||||
|
@ -49,7 +49,7 @@ public class TankService {
|
||||
|
||||
@Transactional
|
||||
public Tank updateTank(Long id, String newName, Nation newNation,
|
||||
Level newLevel, int newCost, List<Client> newClients) {
|
||||
Level newLevel, int newCost) {
|
||||
if (id <= 0) {
|
||||
throw new IllegalArgumentException("Invalid id");
|
||||
}
|
||||
@ -72,10 +72,6 @@ public class TankService {
|
||||
currentTank.setCost(newCost);
|
||||
}
|
||||
|
||||
if(newClients != null){
|
||||
currentTank.setClients(newClients);
|
||||
}
|
||||
|
||||
return em.merge(currentTank);
|
||||
}
|
||||
|
||||
@ -92,4 +88,15 @@ public class TankService {
|
||||
public void deleteAllTanks() {
|
||||
em.createQuery("Delete from Tank").executeUpdate();
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<Tank> findTank(Level level, Nation nation) {
|
||||
return em.createQuery("select t from Tank t where t.nation.nation = :Nid and t.level.level BETWEEN :Llevel AND :L2level", Tank.class)
|
||||
.setParameter("Nid", nation.getNation())
|
||||
.setParameter("Llevel", level.getLevel())
|
||||
.setParameter("L2level", level.getLevel() + 1)
|
||||
.getResultList();
|
||||
}
|
||||
}
|
||||
|
@ -51,8 +51,21 @@ class PremiumStoreApplicationTests {
|
||||
@Test
|
||||
void testNationRead() {
|
||||
nationService.deleteAllNations();
|
||||
tankService.deleteAllTanks();
|
||||
levelService.deleteAllLevels();
|
||||
|
||||
Nation nation = nationService.addNation("Германия");
|
||||
|
||||
Level firstLevel = levelService.addLevel(8);
|
||||
Level secondLevel = levelService.addLevel(9);
|
||||
|
||||
Tank firstTank = tankService.addTank("ИС-3", nation, firstLevel, 3700000);
|
||||
Tank secondTank = tankService.addTank("E-75", nation, secondLevel, 5600000);
|
||||
|
||||
List<Tank> tanks = new ArrayList<>(Arrays.asList(firstTank, secondTank));
|
||||
|
||||
nation = nationService.updateNation(nation.getId(), "СССР", tanks);
|
||||
|
||||
Nation nation = nationService.addNation("СССР");
|
||||
log.info(nation.toString());
|
||||
|
||||
Nation findNation = nationService.findNation(nation.getId());
|
||||
@ -128,9 +141,6 @@ class PremiumStoreApplicationTests {
|
||||
levelService.deleteAllLevels();
|
||||
clientService.deleteAllClients();
|
||||
|
||||
Client firstClient = clientService.addClient("Barbarian", "dsfg@gmail.com", 56000);
|
||||
Client secondClient = clientService.addClient("KorbenDallas", "tankoviyGeniy@mail.ru", 37000);
|
||||
|
||||
Level firstLevel = levelService.addLevel(8);
|
||||
Level secondLevel = levelService.addLevel(9);
|
||||
|
||||
@ -140,14 +150,34 @@ class PremiumStoreApplicationTests {
|
||||
Tank tank = tankService.findTank(firstTank.getId());
|
||||
log.info(tank.toString());
|
||||
|
||||
List<Client> newClient = new ArrayList<>();
|
||||
Collections.addAll(newClient, firstClient, secondClient);
|
||||
|
||||
tank.setName("ИСУ-152");
|
||||
tank.setLevel(secondLevel);
|
||||
tank.setCost(4100000);
|
||||
tank.setClients(newClient);
|
||||
|
||||
log.info(tank.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testNewTest() {
|
||||
nationService.deleteAllNations();
|
||||
tankService.deleteAllTanks();
|
||||
levelService.deleteAllLevels();
|
||||
|
||||
Nation nation = nationService.addNation("Германия");
|
||||
Nation newNation = nationService.addNation("CCCР");
|
||||
|
||||
Level firstLevel = levelService.addLevel(8);
|
||||
Level secondLevel = levelService.addLevel(9);
|
||||
|
||||
Tank firstTank = tankService.addTank("ИС-3", nation, firstLevel, 3700000);
|
||||
Tank secondTank = tankService.addTank("E-75", nation, secondLevel, 5600000);
|
||||
|
||||
List<Tank> tanks = new ArrayList<>(Arrays.asList(firstTank, secondTank));
|
||||
|
||||
nation = nationService.updateNation(nation.getId(), "СССР", tanks);
|
||||
|
||||
tanks = tankService.findTank(firstLevel, nation);
|
||||
|
||||
log.info("AAAAA" + tanks.toString());
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user