some done

This commit is contained in:
Калышев Ян 2023-04-11 11:37:31 +04:00
parent 13a162f744
commit e620af3165
11 changed files with 137 additions and 110 deletions

1
.gitignore vendored
View File

@ -5,6 +5,7 @@ build/
!**/src/main/**/build/
!**/src/test/**/build/
data.mv.db
data.trace.db
### STS ###
.apt_generated

View File

@ -8,15 +8,13 @@ import java.util.List;
import java.util.Objects;
@Entity
@Table(name = "cabinet")
public class Cabinet {
@Id
@GeneratedValue
private Long id;
private String number;
@ManyToMany(cascade = { CascadeType.MERGE }, fetch = FetchType.EAGER)
@JoinTable(name = "cabinets_computers",
joinColumns = @JoinColumn(name = "cabinet_fk"),
inverseJoinColumns = @JoinColumn(name = "computer_fk"))
@OneToMany(cascade = {CascadeType.MERGE})
private List<Computer> computers;
public Cabinet() {
@ -36,21 +34,18 @@ public class Cabinet {
public List<Computer> getComputers() {
return computers;
}
public void addComputer(Computer computer) {
if (computers == null){
public void addComputer(Computer computer){
if (computers == null){
this.computers = new ArrayList<>();
}
if (!computers.contains(computer)) {
this.computers.add(computer);
computer.addCabinet(this);
computer.setCabinet(this);
}
}
public void removeComputer(Computer computer) {
if (computers.contains(computer)) {
public void removeComputer(Computer computer){
if (computers.contains(computer))
this.computers.remove(computer);
computer.removeCabinet(this);
}
}
@Override

View File

@ -57,7 +57,7 @@ public class CabinetService {
int size = currentCabinet.getComputers().size();
for (int i = 0; i < size; i++) {
Computer temp = currentCabinet.getComputers().get(i);
temp.removeCabinet(currentCabinet);
temp.setCabinet(null);
currentCabinet.removeComputer(temp);
em.remove(temp);
}

View File

@ -34,38 +34,35 @@ public class ComputerController {
}
@GetMapping("/{id}/computers")
public List<Cabinet> getCabinetComputers(@PathVariable Long id) {
return computerService.listCabinets(id);
public Cabinet getComputersCabinet(@PathVariable Long id) {
return computerService.getCabinet(id);
}
@PostMapping("/")
public Computer createComputer(@RequestParam("modelName") String modelName,
@RequestParam("serialNum") String serialNum,
@RequestParam("monitorId") Long monitorId) {
return computerService.addComputer(modelName, serialNum, monitorId);
@RequestParam("monitorId") Long monitorId,
@RequestParam("cabinetId") Long cabinetId) {
return computerService.addComputer(modelName, serialNum, monitorId, cabinetId);
}
@PostMapping("/cabinet")
public Cabinet createCabinetComputer(@RequestParam("computerId") Long computerId,
public Cabinet setCabinetComputer(@RequestParam("computerId") Long computerId,
@RequestParam("cabinetId") Long cabinetId) {
return computerService.addCabinet(cabinetId, computerId);
return computerService.setCabinet(cabinetId, computerId);
}
@PatchMapping("/{id}")
public Computer updateComputer(@PathVariable Long id,
@RequestParam("modelName") String modelName,
@RequestParam("serialNum") String serialNum) {
return computerService.updateComputer(id, modelName, serialNum);
@RequestParam("serialNum") String serialNum,
@RequestParam("monitorId") Long monitorId,
@RequestParam("cabinetId") Long cabinetId) {
return computerService.updateComputer(id, modelName, serialNum, monitorId, cabinetId);
}
@DeleteMapping("/{id}")
public Computer deleteComputer(@PathVariable Long id) {
return computerService.deleteComputer(id);
}
@DeleteMapping("/{computerId}/cabinet")
public Cabinet deleteCabinetComputer(@PathVariable Long computerId,
@RequestParam("cabinetId") Long cabinetId) {
return computerService.deleteCabinet(cabinetId, computerId);
}
}

View File

@ -16,9 +16,10 @@ public class Computer {
private Long id;
private String modelName;
private String serialNum;
@ManyToMany(mappedBy = "computers", cascade = CascadeType.MERGE, fetch = FetchType.EAGER)
private List<Cabinet> cabinets;
@OneToOne(cascade = {CascadeType.ALL})
@ManyToOne( cascade = {CascadeType.MERGE}, fetch = FetchType.EAGER)
@JoinColumn(name = "cabinets", nullable = true)
private Cabinet cabinet;
@OneToOne()
@JoinColumn(name = "monitor_id")
private Monitor monitor;
@ -42,23 +43,8 @@ public class Computer {
return serialNum;
}
public void setserialNum(String serialNum) { this.serialNum = serialNum; }
public List<Cabinet> getCabinets() { return cabinets; }
public void addCabinet(Cabinet cabinet) {
if (cabinets == null){
this.cabinets = new ArrayList<>();
}
if (!cabinets.contains(cabinet)) {
this.cabinets.add(cabinet);
cabinet.addComputer(this);
}
}
public void removeCabinet(Cabinet cabinet) {
if (cabinets.contains(cabinet)) {
this.cabinets.remove(cabinet);
cabinet.removeComputer(this);
}
}
public Cabinet getCabinet() { return cabinet; }
public void setCabinet(Cabinet cabinet) { this.cabinet = cabinet; }
public Monitor getMonitor() {
return monitor;

View File

@ -21,22 +21,28 @@ public class ComputerService {
private EntityManager em;
@Transactional
public Computer addComputer(String modelName, String serialNum, Long monitorId) {
public Computer addComputer(String modelName, String serialNum, Long monitorId, Long cabinetId) {
if (!StringUtils.hasText(modelName)) {
throw new IllegalArgumentException("Computer model name is null or empty");
}
if (!StringUtils.hasText(serialNum)) {
throw new IllegalArgumentException("Computer serial number is null or empty");
}
if ((Object)monitorId == null) {
throw new IllegalArgumentException("Monitor id is null or empty");
}
final Monitor monitor = em.find(Monitor.class, monitorId);
if (monitor == null) {
throw new IllegalArgumentException(String.format("Monitor with id [%s] not found", monitorId));
}
final Computer computer = new Computer(modelName, serialNum);
computer.setMonitor(monitor);
if (monitorId != null) {
final Monitor monitor = em.find(Monitor.class, monitorId);
if (monitor == null) {
throw new IllegalArgumentException(String.format("Monitor with id [%s] not found", monitorId));
}
computer.setMonitor(monitor);
}
if (cabinetId != null) {
final Cabinet cabinet = em.find(Cabinet.class, cabinetId);
if (cabinet == null) {
throw new IllegalArgumentException(String.format("Cabinet with id [%s] not found", cabinetId));
}
computer.setCabinet(cabinet);
}
em.persist(computer);
return computer;
}
@ -57,7 +63,7 @@ public class ComputerService {
}
@Transactional
public Computer updateComputer(Long id, String modelName, String serialNum) {
public Computer updateComputer(Long id, String modelName, String serialNum, Long monitorId, Long cabinetId) {
if (!(StringUtils.hasText(modelName) || StringUtils.hasText(serialNum))) {
throw new IllegalArgumentException("Need at least one argument");
}
@ -68,17 +74,23 @@ public class ComputerService {
if (serialNum != null) {
currentComputer.setserialNum(serialNum);
}
if (monitorId != null) {
Monitor monitor = em.find(Monitor.class, monitorId);
currentComputer.setMonitor(monitor);
}
if (cabinetId != null) {
Cabinet cabinet = em.find(Cabinet.class, cabinetId);
currentComputer.setCabinet(cabinet);
}
return em.merge(currentComputer);
}
@Transactional
public Computer deleteComputer(Long id) {
final Computer currentComputer = findComputer(id);
int size = currentComputer.getCabinets().size();
for (int i = 0; i < size; i++) {
Cabinet temp = currentComputer.getCabinets().get(i);
temp.removeComputer(currentComputer);
currentComputer.removeCabinet(temp);
Cabinet currentComputerCabinet = currentComputer.getCabinet();
if (currentComputerCabinet != null) {
currentComputerCabinet.removeComputer(currentComputer);
}
em.remove(currentComputer);
return currentComputer;
@ -86,23 +98,20 @@ public class ComputerService {
@Transactional
public void deleteAllComputers() {
List<Computer> list = em.createQuery("select s from Computer s", Computer.class).getResultList();
for(int i = 0; i < list.size(); i++){
deleteComputer(list.get(i).getId());
}
em.createQuery("delete from Computer").executeUpdate();
}
@Transactional
public List<Cabinet> listCabinets(Long id) {
public Cabinet getCabinet(Long id) {
if ((Object)id == null) {
throw new IllegalArgumentException("Computer id is null or empty");
}
final Computer computer = em.find(Computer.class, id);
return computer.getCabinets();
return computer.getCabinet();
}
@Transactional
public Cabinet addCabinet(Long cabinetId, Long computerId) {
public Cabinet setCabinet(Long cabinetId, Long computerId) {
if ((Object)computerId == null) {
throw new IllegalArgumentException("Computer id is null or empty");
}
@ -111,21 +120,7 @@ public class ComputerService {
}
final Computer computer = em.find(Computer.class, computerId);
final Cabinet cabinet = em.find(Cabinet.class, cabinetId);
computer.addCabinet(cabinet);
return cabinet;
}
@Transactional
public Cabinet deleteCabinet(Long cabinetId, Long computerId) {
if ((Object)computerId == null) {
throw new IllegalArgumentException("Computer id is null or empty");
}
if ((Object)cabinetId == null) {
throw new IllegalArgumentException("Cabinet id is null or empty");
}
final Computer computer = em.find(Computer.class, computerId);
final Cabinet cabinet = em.find(Cabinet.class, cabinetId);
computer.removeCabinet(cabinet);
computer.setCabinet(cabinet);
return cabinet;
}
}

View File

@ -13,8 +13,6 @@ public class Monitor {
@GeneratedValue
private Long id;
private String modelName;
@OneToOne(mappedBy = "monitor")
private Computer computer;
public Monitor() {
}
@ -31,8 +29,6 @@ public class Monitor {
public void setmodelName(String modelName) {
this.modelName = modelName;
}
public Computer getComputer() { return computer; }
public void setComputer(Computer computer) { this.computer = computer; }
@Override
public boolean equals(Object o) {

View File

@ -1,5 +1,6 @@
package com.kalyshev.yan.monitor.service;
import com.kalyshev.yan.computer.model.Computer;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
@ -53,16 +54,30 @@ public class MonitorService {
@Transactional
public Monitor deleteMonitor(Long id) {
final Monitor currentMonitor = findMonitor(id);
if(currentMonitor.getComputer() != null) currentMonitor.getComputer().removeMonitor();
em.remove(currentMonitor);
return currentMonitor;
}
@Transactional
public void deleteAllMonitors() {
List<Monitor> list = em.createQuery("select s from Monitor s", Monitor.class).getResultList();
for(int i = 0; i < list.size(); i++){
deleteMonitor(list.get(i).getId());
List<Computer> listComps = em.createQuery("select s from Computer s", Computer.class).getResultList();
for (int i = 0; i < listComps.size(); i++){
listComps.get(i).removeMonitor();
}
List<Monitor> listMons = em.createQuery("select s from Monitor s", Monitor.class).getResultList();
for (int i = 0; i < listMons.size(); i++){
deleteMonitor(listMons.get(i).getId());
}
}
@Transactional
public Computer getComputer(Long id) {
List<Computer> computers = em.createQuery("select s from Computer").getResultList();
for (int i = 0; i < computers.size(); i++){
if (computers.get(i).getMonitor().getId() == id) {
return computers.get(i);
}
}
return null;
}
}

View File

@ -24,13 +24,13 @@ public class JpaComputerCabinetTests {
private CabinetService cabinetService;
@Test
void testCabinetAddManyToMany() {
void testCabinetAddOneToMany() {
cabinetService.deleteAllCabinets();
computerService.deleteAllComputers();
monitorService.deleteAllMonitors();
final Cabinet cabinet = cabinetService.addCabinet("18");
final Monitor monitor = monitorService.addMonitor("Asus");
final Computer computer = computerService.addComputer("PC", "78457h", monitor.getId());
final Computer computer = computerService.addComputer("PC", "78457h", monitor.getId(), null);
cabinet.addComputer(computer);
log.info(cabinet.toString());
log.info(computer.toString());
@ -38,13 +38,13 @@ public class JpaComputerCabinetTests {
}
@Test
void testCabinetDeleteManyToMany() {
void testCabinetDeleteOneToMany() {
cabinetService.deleteAllCabinets();
computerService.deleteAllComputers();
monitorService.deleteAllMonitors();
final Cabinet cabinet = cabinetService.addCabinet("18");
final Monitor monitor = monitorService.addMonitor("Asus");
final Computer computer = computerService.addComputer("PC", "78457h", monitor.getId());
final Computer computer = computerService.addComputer("PC", "78457h", monitor.getId(), null);
cabinet.addComputer(computer);
log.info(cabinet.toString());
log.info(computer.toString());
@ -60,11 +60,11 @@ public class JpaComputerCabinetTests {
monitorService.deleteAllMonitors();
final Cabinet cabinet = cabinetService.addCabinet("18");
final Monitor monitor = monitorService.addMonitor("Asus");
final Computer computer = computerService.addComputer("PC", "78457h", monitor.getId());
computer.addCabinet(cabinet);
final Computer computer = computerService.addComputer("PC", "78457h", monitor.getId(), null);
computer.setCabinet(cabinet);
log.info(cabinet.toString());
log.info(computer.toString());
Assertions.assertEquals(computer.getCabinets().get(0).getnumber(), cabinet.getnumber());
Assertions.assertEquals(computer.getCabinet().getnumber(), cabinet.getnumber());
}
@Test
@ -74,12 +74,12 @@ public class JpaComputerCabinetTests {
monitorService.deleteAllMonitors();
final Cabinet cabinet = cabinetService.addCabinet("18");
final Monitor monitor = monitorService.addMonitor("Asus");
final Computer computer = computerService.addComputer("PC", "78457h", monitor.getId());
computer.addCabinet(cabinet);
final Computer computer = computerService.addComputer("PC", "78457h", monitor.getId(), null);
computer.setCabinet(cabinet);
log.info(cabinet.toString());
log.info(computer.toString());
Assertions.assertEquals(computer.getCabinets().get(0).getnumber(), cabinet.getnumber());
computer.removeCabinet(cabinet);
Assertions.assertEquals(computer.getCabinets().size(), 0);
Assertions.assertEquals(computer.getCabinet().getnumber(), cabinet.getnumber());
computer.setCabinet(null);
Assertions.assertNull(computer.getCabinet());
}
}

View File

@ -1,5 +1,6 @@
package com.kalyshev.yan;
import com.kalyshev.yan.cabinet.model.Cabinet;
import com.kalyshev.yan.cabinet.service.CabinetService;
import com.kalyshev.yan.computer.model.Computer;
import com.kalyshev.yan.computer.service.ComputerService;
@ -22,20 +23,41 @@ public class JpaComputerTests {
private ComputerService computerService;
@Autowired
private MonitorService monitorService;
@Autowired
private CabinetService cabinetService;
@Test
void testComputerCreate() {
void testComputerPartialCreate() {
computerService.deleteAllComputers();
final Monitor monitor = monitorService.addMonitor("Asus");
final Computer computer = computerService.addComputer("Computer", "w7894572", monitor.getId());
final Computer computer = computerService.addComputer("Computer", "w7894572", monitor.getId(), null);
log.info(computer.toString());
Assertions.assertNotNull(computer.getId());
Assertions.assertNull(computer.getCabinet());
}
@Test
void testComputerFullCreate() {
computerService.deleteAllComputers();
final Monitor monitor = monitorService.addMonitor("Asus");
final Cabinet cabinet = cabinetService.addCabinet("18a");
final Computer computer = computerService.addComputer("Computer", "w7894572", monitor.getId(), cabinet.getId());
log.info(computer.toString());
Assertions.assertNotNull(computer.getId());
Assertions.assertNull(computer.getCabinet());
}
@Test
void testComputerDelete() {
computerService.deleteAllComputers();
final Monitor monitor = monitorService.addMonitor("Asus");
final Computer computer = computerService.addComputer("Computer", "w7894572", null, null);
log.info(computer.toString());
computerService.deleteComputer(computer.getId());
Assertions.assertThrows(EntityNotFoundException.class, () -> computerService.findComputer(computer.getId()));
}
@Test
void testComputerRead() {
computerService.deleteAllComputers();
final Monitor monitor = monitorService.addMonitor("Asus");
final Computer computer = computerService.addComputer("Computer", "w7894572", monitor.getId());
final Computer computer = computerService.addComputer("Computer", "w7894572", null, null);
log.info(computer.toString());
final Computer findComputer = computerService.findComputer(computer.getId());
log.info(findComputer.toString());
@ -50,8 +72,8 @@ public class JpaComputerTests {
void testComputerReadAll() {
computerService.deleteAllComputers();
final Monitor monitor = monitorService.addMonitor("Asus");
computerService.addComputer("Computer", "w7894572", monitor.getId());
computerService.addComputer("Another comp", "3453s", monitor.getId());
computerService.addComputer("Computer", "w7894572", null, null);
computerService.addComputer("Another comp", "3453s", null, null);
final List<Computer> computers = computerService.findAllComputers();
log.info(computers.toString());
Assertions.assertEquals(computers.size(), 2);

View File

@ -8,6 +8,7 @@ import com.kalyshev.yan.monitor.service.MonitorService;
import jakarta.persistence.EntityNotFoundException;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
@ -19,6 +20,8 @@ public class JpaMonitorTests {
private static final Logger log = LoggerFactory.getLogger(JpaMonitorTests.class);
@Autowired
private MonitorService monitorService;
@Autowired
private ComputerService computerService;
@Test
void testMonitorCreate() {
monitorService.deleteAllMonitors();
@ -36,6 +39,14 @@ public class JpaMonitorTests {
Assertions.assertEquals(monitor, findMonitor);
}
@Test
void testMonitorDelete() {
monitorService.deleteAllMonitors();
final Monitor monitor = monitorService.addMonitor("Asus");
log.info(monitor.toString());
monitorService.deleteMonitor(monitor.getId());
Assertions.assertThrows(EntityNotFoundException.class, () -> monitorService.findMonitor(monitor.getId()));
}
@Test
void testMonitorReadNotFound() {
monitorService.deleteAllMonitors();
Assertions.assertThrows(EntityNotFoundException.class, () -> monitorService.findMonitor(-1L));
@ -56,4 +67,13 @@ public class JpaMonitorTests {
log.info(monitors.toString());
Assertions.assertEquals(monitors.size(), 0);
}
@Test
void testMonitorGetComputer() {
monitorService.deleteAllMonitors();
computerService.deleteAllComputers();
Monitor monitor = monitorService.addMonitor("Asus");
Computer computer = computerService.addComputer("Model", "6sfv4", monitor.getId(), null);
Computer fetchedComputer = monitorService.getComputer(monitor.getId());
Assertions.assertEquals(computer, fetchedComputer);
}
}