done
This commit is contained in:
parent
e620af3165
commit
a73685f3b3
@ -76,6 +76,7 @@ public class Computer {
|
||||
return "Computer{" +
|
||||
"id=" + id +
|
||||
", modelName='" + modelName + '\'' +
|
||||
", serialNum='" + serialNum + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,9 @@
|
||||
package com.kalyshev.yan.monitor.service;
|
||||
|
||||
import com.kalyshev.yan.computer.model.Computer;
|
||||
import jakarta.persistence.TypedQuery;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.StringUtils;
|
||||
@ -10,9 +13,11 @@ import jakarta.persistence.EntityManager;
|
||||
import jakarta.persistence.EntityNotFoundException;
|
||||
import jakarta.persistence.PersistenceContext;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
@Service
|
||||
public class MonitorService {
|
||||
private static final Logger log = LoggerFactory.getLogger(MonitorService.class);
|
||||
@PersistenceContext
|
||||
private EntityManager em;
|
||||
|
||||
@ -72,12 +77,9 @@ public class MonitorService {
|
||||
|
||||
@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;
|
||||
Computer computer = em.createQuery("select s from Computer s where s.monitor.id = :id", Computer.class)
|
||||
.setParameter("id", id)
|
||||
.getSingleResult();
|
||||
return computer;
|
||||
}
|
||||
}
|
@ -43,7 +43,7 @@ public class JpaComputerTests {
|
||||
final Computer computer = computerService.addComputer("Computer", "w7894572", monitor.getId(), cabinet.getId());
|
||||
log.info(computer.toString());
|
||||
Assertions.assertNotNull(computer.getId());
|
||||
Assertions.assertNull(computer.getCabinet());
|
||||
Assertions.assertEquals(cabinet, computer.getCabinet());
|
||||
}
|
||||
@Test
|
||||
void testComputerDelete() {
|
||||
|
@ -73,6 +73,7 @@ public class JpaMonitorTests {
|
||||
computerService.deleteAllComputers();
|
||||
Monitor monitor = monitorService.addMonitor("Asus");
|
||||
Computer computer = computerService.addComputer("Model", "6sfv4", monitor.getId(), null);
|
||||
log.info(computer.toString());
|
||||
Computer fetchedComputer = monitorService.getComputer(monitor.getId());
|
||||
Assertions.assertEquals(computer, fetchedComputer);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user