This commit is contained in:
root 2023-04-10 20:58:47 +04:00
parent 951bf3001f
commit 7eff23080c
12 changed files with 296 additions and 52 deletions

1
.gitignore vendored
View File

@ -4,6 +4,7 @@ build/
!gradle/wrapper/gradle-wrapper.jar
!**/src/main/**/build/
!**/src/test/**/build/
data.mv.db
### STS ###
.apt_generated

Binary file not shown.

View File

@ -1,13 +1,9 @@
package com.kalyshev.yan.cabinet.model;
import com.kalyshev.yan.computer.model.Computer;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.ManyToMany;
import jakarta.persistence.JoinTable;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
@ -17,7 +13,7 @@ public class Cabinet {
@GeneratedValue
private Long id;
private String number;
@ManyToMany
@ManyToMany(cascade = { CascadeType.MERGE }, fetch = FetchType.EAGER)
@JoinTable(name = "cabinets_computers",
joinColumns = @JoinColumn(name = "cabinet_fk"),
inverseJoinColumns = @JoinColumn(name = "computer_fk"))
@ -37,18 +33,23 @@ public class Cabinet {
public void setnumber(String number) {
this.number = number;
}
public List<Computer> getComputers() { return computers; }
public List<Computer> getComputers() {
return computers;
}
public void addComputer(Computer computer) {
boolean added = computers.add(computer);
if (added) {
computer.getCabinets().add(this);
if (computers == null){
this.computers = new ArrayList<>();
}
if (!computers.contains(computer)) {
this.computers.add(computer);
computer.addCabinet(this);
}
}
public void removeComputer(Computer computer) {
boolean added = computers.add(computer);
if (added) {
computer.getCabinets().remove(this);
if (computers.contains(computer)) {
this.computers.remove(computer);
computer.removeCabinet(this);
}
}

View File

@ -54,6 +54,13 @@ public class CabinetService {
@Transactional
public Cabinet deleteCabinet(Long id) {
final Cabinet currentCabinet = findCabinet(id);
int size = currentCabinet.getComputers().size();
for (int i = 0; i < size; i++) {
Computer temp = currentCabinet.getComputers().get(i);
temp.removeCabinet(currentCabinet);
currentCabinet.removeComputer(temp);
em.remove(temp);
}
em.remove(currentCabinet);
return currentCabinet;
}

View File

@ -40,8 +40,9 @@ public class ComputerController {
@PostMapping("/")
public Computer createComputer(@RequestParam("modelName") String modelName,
@RequestParam("serialNum") String serialNum,
@RequestParam("monitorId") Long monitorId) {
return computerService.addComputer(modelName, monitorId);
return computerService.addComputer(modelName, serialNum, monitorId);
}
@PostMapping("/cabinet")
@ -52,8 +53,9 @@ public class ComputerController {
@PatchMapping("/{id}")
public Computer updateComputer(@PathVariable Long id,
@RequestParam("modelName") String modelName) {
return computerService.updateComputer(id, modelName);
@RequestParam("modelName") String modelName,
@RequestParam("serialNum") String serialNum) {
return computerService.updateComputer(id, modelName, serialNum);
}
@DeleteMapping("/{id}")

View File

@ -4,6 +4,7 @@ import com.kalyshev.yan.cabinet.model.Cabinet;
import com.kalyshev.yan.monitor.model.Monitor;
import jakarta.persistence.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
@ -11,21 +12,21 @@ import java.util.Objects;
@Table(name = "computer")
public class Computer {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
@GeneratedValue
private Long id;
private String modelName;
@ManyToMany(mappedBy = "computers")
private String serialNum;
@ManyToMany(mappedBy = "computers", cascade = CascadeType.MERGE, fetch = FetchType.EAGER)
private List<Cabinet> cabinets;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "monitor_fk", nullable = false, referencedColumnName = "id")
@OneToOne(cascade = {CascadeType.ALL})
@JoinColumn(name = "monitor_id")
private Monitor monitor;
public Computer() {
}
public Computer(String modelName, Monitor monitor) {
public Computer(String modelName, String serialNum) {
this.modelName = modelName;
this.monitor = monitor;
this.serialNum = serialNum;
}
public Long getId() {
@ -37,21 +38,40 @@ public class Computer {
public void setmodelName(String modelName) {
this.modelName = modelName;
}
public String getserialNum() {
return serialNum;
}
public void setserialNum(String serialNum) { this.serialNum = serialNum; }
public List<Cabinet> getCabinets() { return cabinets; }
public void addCabinet(Cabinet cabinet) {
boolean added = cabinets.add(cabinet);
if (added) {
cabinet.getComputers().add(this);
if (cabinets == null){
this.cabinets = new ArrayList<>();
}
if (!cabinets.contains(cabinet)) {
this.cabinets.add(cabinet);
cabinet.addComputer(this);
}
}
public void removeCabinet(Cabinet cabinet) {
boolean removed = cabinets.remove(cabinet);
if (removed) {
cabinet.getComputers().remove(this);
if (cabinets.contains(cabinet)) {
this.cabinets.remove(cabinet);
cabinet.removeComputer(this);
}
}
public Monitor getMonitor() {
return monitor;
}
public void setMonitor(Monitor monitor) {
this.monitor = monitor;
}
public Monitor removeMonitor() {
Monitor temp = this.monitor;
this.monitor = null;
return temp;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;

View File

@ -2,6 +2,8 @@ package com.kalyshev.yan.computer.service;
import com.kalyshev.yan.cabinet.model.Cabinet;
import com.kalyshev.yan.monitor.model.Monitor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
@ -14,19 +16,27 @@ import java.util.List;
@Service
public class ComputerService {
private static final Logger log = LoggerFactory.getLogger(ComputerService.class);
@PersistenceContext
private EntityManager em;
@Transactional
public Computer addComputer(String modelName, Long monitorId) {
public Computer addComputer(String modelName, String serialNum, Long monitorId) {
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);
final Computer computer = new Computer(modelName, monitor);
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);
em.persist(computer);
return computer;
}
@ -35,7 +45,7 @@ public class ComputerService {
public Computer findComputer(Long id) {
final Computer computer = em.find(Computer.class, id);
if (computer == null) {
throw new EntityNotFoundException(String.format("Computer with id [%s] is not found", id));
throw new EntityNotFoundException(String.format("Computer with id [%s] not found", id));
}
return computer;
}
@ -47,25 +57,39 @@ public class ComputerService {
}
@Transactional
public Computer updateComputer(Long id, String modelName) {
if (!StringUtils.hasText(modelName)) {
throw new IllegalArgumentException("Computer model name is null or empty");
public Computer updateComputer(Long id, String modelName, String serialNum) {
if (!(StringUtils.hasText(modelName) || StringUtils.hasText(serialNum))) {
throw new IllegalArgumentException("Need at least one argument");
}
final Computer currentComputer = findComputer(id);
currentComputer.setmodelName(modelName);
if (modelName != null) {
currentComputer.setmodelName(modelName);
}
if (serialNum != null) {
currentComputer.setserialNum(serialNum);
}
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);
}
em.remove(currentComputer);
return currentComputer;
}
@Transactional
public void deleteAllComputers() {
em.createQuery("delete from Computer").executeUpdate();
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());
}
}
@Transactional

View File

@ -10,11 +10,10 @@ import java.util.Objects;
@Table(name = "monitor")
public class Monitor {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
@GeneratedValue
private Long id;
private String modelName;
@OneToOne(mappedBy = "computer")
@OneToOne(mappedBy = "monitor")
private Computer computer;
public Monitor() {
@ -32,6 +31,8 @@ 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

@ -20,18 +20,18 @@ public class MonitorService {
if (!StringUtils.hasText(modelName)) {
throw new IllegalArgumentException("Monitor model name is null or empty");
}
final Monitor computer = new Monitor(modelName);
em.persist(computer);
return computer;
final Monitor monitor = new Monitor(modelName);
em.persist(monitor);
return monitor;
}
@Transactional(readOnly = true)
public Monitor findMonitor(Long id) {
final Monitor computer = em.find(Monitor.class, id);
if (computer == null) {
final Monitor monitor = em.find(Monitor.class, id);
if (monitor == null) {
throw new EntityNotFoundException(String.format("Monitor with id [%s] is not found", id));
}
return computer;
return monitor;
}
@Transactional(readOnly = true)
@ -53,12 +53,16 @@ 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() {
em.createQuery("delete from Monitor").executeUpdate();
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());
}
}
}

View File

@ -0,0 +1,66 @@
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;
import com.kalyshev.yan.monitor.model.Monitor;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
@SpringBootTest
public class JpaCabinetTests {
private static final Logger log = LoggerFactory.getLogger(JpaCabinetTests.class);
@Autowired
private ComputerService computerService;
@Autowired
private MonitorService monitorService;
@Autowired
private CabinetService cabinetService;
@Test
void testCabinetCreate() {
cabinetService.deleteAllCabinets();
final Cabinet cabinet = cabinetService.addCabinet("18");
log.info(cabinet.toString());
Assertions.assertNotNull(cabinet.getId());
}
@Test
void testCabinetRead() {
cabinetService.deleteAllCabinets();
final Cabinet cabinet = cabinetService.addCabinet("18");
log.info(cabinet.toString());
final Cabinet findCabinet = cabinetService.findCabinet(cabinet.getId());
log.info(findCabinet.toString());
Assertions.assertEquals(cabinet, findCabinet);
}
@Test
void testCabinetReadNotFound() {
cabinetService.deleteAllCabinets();
Assertions.assertThrows(EntityNotFoundException.class, () -> cabinetService.findCabinet(-1L));
}
@Test
void testCabinetReadAll() {
cabinetService.deleteAllCabinets();
cabinetService.addCabinet("18");
cabinetService.addCabinet("19");
final List<Cabinet> cabinets = cabinetService.findAllCabinets();
log.info(cabinets.toString());
Assertions.assertEquals(cabinets.size(), 2);
}
@Test
void testComputerReadAllEmpty() {
cabinetService.deleteAllCabinets();
final List<Cabinet> cabinets = cabinetService.findAllCabinets();
log.info(cabinets.toString());
Assertions.assertEquals(cabinets.size(), 0);
}
}

View File

@ -0,0 +1,85 @@
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;
import com.kalyshev.yan.monitor.model.Monitor;
import com.kalyshev.yan.monitor.service.MonitorService;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
public class JpaComputerCabinetTests {
private static final Logger log = LoggerFactory.getLogger(JpaCabinetTests.class);
@Autowired
private ComputerService computerService;
@Autowired
private MonitorService monitorService;
@Autowired
private CabinetService cabinetService;
@Test
void testCabinetAddManyToMany() {
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());
cabinet.addComputer(computer);
log.info(cabinet.toString());
log.info(computer.toString());
Assertions.assertEquals(cabinet.getComputers().get(0).getserialNum(), computer.getserialNum());
}
@Test
void testCabinetDeleteManyToMany() {
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());
cabinet.addComputer(computer);
log.info(cabinet.toString());
log.info(computer.toString());
Assertions.assertEquals(cabinet.getComputers().get(0).getserialNum(), computer.getserialNum());
cabinet.removeComputer(computer);
Assertions.assertEquals(cabinet.getComputers().size(), 0);
}
@Test
void testComputerAddManyToMany() {
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());
computer.addCabinet(cabinet);
log.info(cabinet.toString());
log.info(computer.toString());
Assertions.assertEquals(computer.getCabinets().get(0).getnumber(), cabinet.getnumber());
}
@Test
void testComputerDeleteManyToMany() {
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());
computer.addCabinet(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);
}
}

View File

@ -1,9 +1,11 @@
package com.kalyshev.yan;
import com.kalyshev.yan.cabinet.service.CabinetService;
import com.kalyshev.yan.computer.model.Computer;
import com.kalyshev.yan.computer.service.ComputerService;
import com.kalyshev.yan.monitor.model.Monitor;
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.slf4j.Logger;
@ -11,6 +13,8 @@ import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
@SpringBootTest
public class JpaComputerTests {
private static final Logger log = LoggerFactory.getLogger(JpaComputerTests.class);
@ -19,15 +23,44 @@ public class JpaComputerTests {
@Autowired
private MonitorService monitorService;
@Test
void testComputerCreate() {
monitorService.deleteAllMonitors();
computerService.deleteAllComputers();
final Monitor monitor = monitorService.addMonitor("Asus");
log.info(monitor.toString());
final Computer computer = computerService.addComputer("Computer", monitor.getId());
final Computer computer = computerService.addComputer("Computer", "w7894572", monitor.getId());
log.info(computer.toString());
Assertions.assertNotNull(computer.getId());
}
@Test
void testComputerRead() {
computerService.deleteAllComputers();
final Monitor monitor = monitorService.addMonitor("Asus");
final Computer computer = computerService.addComputer("Computer", "w7894572", monitor.getId());
log.info(computer.toString());
final Computer findComputer = computerService.findComputer(computer.getId());
log.info(findComputer.toString());
Assertions.assertEquals(computer, findComputer);
}
@Test
void testComputerReadNotFound() {
computerService.deleteAllComputers();
Assertions.assertThrows(EntityNotFoundException.class, () -> computerService.findComputer(-1L));
}
@Test
void testComputerReadAll() {
computerService.deleteAllComputers();
final Monitor monitor = monitorService.addMonitor("Asus");
computerService.addComputer("Computer", "w7894572", monitor.getId());
computerService.addComputer("Another comp", "3453s", monitor.getId());
final List<Computer> computers = computerService.findAllComputers();
log.info(computers.toString());
Assertions.assertEquals(computers.size(), 2);
}
@Test
void testComputerReadAllEmpty() {
computerService.deleteAllComputers();
final List<Computer> computers = computerService.findAllComputers();
log.info(computers.toString());
Assertions.assertEquals(computers.size(), 0);
}
}