done
This commit is contained in:
parent
1faa719f92
commit
e007f51d04
@ -81,7 +81,6 @@
|
||||
addCabinetToComputer(computerId) {
|
||||
let cabinetId = document.getElementById('cabinets').value;
|
||||
let response = axios.post(`http://localhost:8080/api/computer/${computerId}/cabinet?cabinetId=${cabinetId}`);
|
||||
console.log(response);
|
||||
},
|
||||
itemsComps(computers) {
|
||||
if (typeof computers === 'undefined') {
|
||||
@ -94,9 +93,11 @@
|
||||
}
|
||||
});
|
||||
this.cabinets.forEach(cabinet => {
|
||||
if (computer.cabinetId === cabinet.id) {
|
||||
computer.cabinet = cabinet;
|
||||
}
|
||||
cabinet.computerIds.forEach(computerId => {
|
||||
if (computer.id === computerId) {
|
||||
computer.cabinet = cabinet;
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
return computers;
|
||||
@ -161,7 +162,7 @@
|
||||
</option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<!-- <div class="mb-3">
|
||||
<label for="cabinet" class="form-label">Номер кабинета</label>
|
||||
<select class="form-select" id="cabinet" v-model="data.cabinetId">
|
||||
<option disabled value="">Выберите номер кабинета</option>
|
||||
@ -171,6 +172,6 @@
|
||||
{{ cabinet.number }}
|
||||
</option>
|
||||
</select>
|
||||
</div>
|
||||
</div> -->
|
||||
</Modal>
|
||||
</template>
|
@ -82,7 +82,7 @@ const CatalogMixin = {
|
||||
const promises = [];
|
||||
const self = this;
|
||||
this.selectedItems.forEach(item => {
|
||||
promises.push(DataService.delete(this.dataUrl + item));
|
||||
promises.push(DataService.delete(this.dataUrl + "/" + item));
|
||||
});
|
||||
Promise.all(promises).then((results) => {
|
||||
results.forEach(function (id) {
|
||||
|
@ -6,7 +6,6 @@ export default class Computer {
|
||||
this._monitor = data?.monitor;
|
||||
this._monitorId = data?.monitorId;
|
||||
this._cabinet = data?.cabinet;
|
||||
this._cabinetId = data?.cabinetId;
|
||||
}
|
||||
|
||||
get id() {
|
||||
@ -49,14 +48,6 @@ export default class Computer {
|
||||
return this._monitor?.modelName;
|
||||
}
|
||||
|
||||
get cabinetId() {
|
||||
return this._cabinetId;
|
||||
}
|
||||
|
||||
set cabinetId(value) {
|
||||
this._cabinetId = value;
|
||||
}
|
||||
|
||||
get cabinet() {
|
||||
return this._cabinet;
|
||||
}
|
||||
|
@ -2,7 +2,6 @@ package com.kalyshev.yan;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@SpringBootApplication
|
||||
public class YanApplication {
|
||||
|
@ -1,15 +1,11 @@
|
||||
package com.kalyshev.yan.cabinet.controller;
|
||||
|
||||
import com.kalyshev.yan.WebConfiguration;
|
||||
import com.kalyshev.yan.cabinet.service.CabinetService;
|
||||
import com.kalyshev.yan.computer.controller.ComputerDto;
|
||||
import com.kalyshev.yan.computer.model.Computer;
|
||||
import com.kalyshev.yan.monitor.controller.MonitorDto;
|
||||
import jakarta.validation.Valid;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import com.kalyshev.yan.cabinet.model.Cabinet;
|
||||
import com.kalyshev.yan.cabinet.service.CabinetService;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@ -31,7 +27,7 @@ public class CabinetController {
|
||||
}
|
||||
@GetMapping("/{id}/computers")
|
||||
public List<ComputerDto> getCabinetComputers(@PathVariable Long id) {
|
||||
return cabinetService.listComputers(id).stream()
|
||||
return cabinetService.listComputersFromCabinet(id).stream()
|
||||
.map(ComputerDto::new)
|
||||
.toList();
|
||||
}
|
||||
@ -47,9 +43,9 @@ public class CabinetController {
|
||||
return new CabinetDto(cabinetService.addCabinet(cabinetDto.getNumber()));
|
||||
}
|
||||
@PostMapping("/{id}/computer")
|
||||
public ComputerDto createCabinetComputer(@PathVariable Long id,
|
||||
public void createCabinetComputer(@PathVariable Long id,
|
||||
@RequestParam("computerId") Long computerId) {
|
||||
return new ComputerDto(cabinetService.addComputer(computerId, id));
|
||||
cabinetService.addComputerToCabinet(computerId, id);
|
||||
}
|
||||
@PutMapping("/{id}")
|
||||
public CabinetDto updateCabinet(@PathVariable Long id,
|
||||
@ -61,8 +57,8 @@ public class CabinetController {
|
||||
return new CabinetDto(cabinetService.deleteCabinet(id));
|
||||
}
|
||||
@DeleteMapping("/{cabinetId}/computer")
|
||||
public ComputerDto deleteCabinetComputer(@PathVariable Long cabinetId,
|
||||
public void deleteCabinetComputer(@PathVariable Long cabinetId,
|
||||
@RequestParam("computerId") Long computerId) {
|
||||
return new ComputerDto(cabinetService.deleteComputer(computerId, cabinetId));
|
||||
cabinetService.deleteComputerFromCabinet(computerId, cabinetId);
|
||||
}
|
||||
}
|
@ -50,10 +50,12 @@ public class Cabinet {
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
if (this == o)
|
||||
return true;
|
||||
if (!(o instanceof Cabinet))
|
||||
return false;
|
||||
Cabinet cabinet = (Cabinet) o;
|
||||
return Objects.equals(id, cabinet.id);
|
||||
return Objects.equals(id, cabinet.id) && Objects.equals(this.number, cabinet.number);
|
||||
}
|
||||
@Override
|
||||
public int hashCode() {
|
||||
|
@ -1,7 +1,15 @@
|
||||
package com.kalyshev.yan.cabinet.repository;
|
||||
|
||||
import com.kalyshev.yan.cabinet.model.Cabinet;
|
||||
import com.kalyshev.yan.monitor.model.Monitor;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface CabinetRepository extends JpaRepository<Cabinet, Long> {
|
||||
@Query(value = "select s from Cabinet s where (s.id = :id or :id is Null) and (s.number = :number or :number is Null)")
|
||||
public List<Cabinet> findFilteredCabinets(@Param("id") Long id,
|
||||
@Param("number") String number);
|
||||
}
|
@ -1,32 +1,30 @@
|
||||
package com.kalyshev.yan.cabinet.service;
|
||||
|
||||
import com.kalyshev.yan.cabinet.model.Cabinet;
|
||||
import com.kalyshev.yan.cabinet.repository.CabinetNotFoundException;
|
||||
import com.kalyshev.yan.cabinet.repository.CabinetRepository;
|
||||
import com.kalyshev.yan.computer.model.Computer;
|
||||
import com.kalyshev.yan.computer.repository.ComputerNotFoundException;
|
||||
import com.kalyshev.yan.computer.repository.ComputerRepository;
|
||||
import com.kalyshev.yan.computer.service.ComputerService;
|
||||
import com.kalyshev.yan.util.validation.ValidatorUtil;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.StringUtils;
|
||||
import com.kalyshev.yan.cabinet.model.Cabinet;
|
||||
|
||||
import jakarta.persistence.EntityManager;
|
||||
import jakarta.persistence.EntityNotFoundException;
|
||||
import jakarta.persistence.PersistenceContext;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
|
||||
@Service
|
||||
public class CabinetService {
|
||||
private final CabinetRepository cabinetRepository;
|
||||
private final ComputerRepository computerRepository;
|
||||
private final ComputerService computerService;
|
||||
private final ValidatorUtil validatorUtil;
|
||||
public CabinetService(CabinetRepository cabinetRepository,
|
||||
ComputerRepository computerRepository,
|
||||
ComputerService computerService,
|
||||
ValidatorUtil validatorUtil) {
|
||||
this.cabinetRepository = cabinetRepository;
|
||||
this.computerRepository = computerRepository;
|
||||
this.computerService = computerService;
|
||||
this.validatorUtil = validatorUtil;
|
||||
}
|
||||
@Transactional
|
||||
@ -45,21 +43,7 @@ public class CabinetService {
|
||||
}
|
||||
@Transactional(readOnly = true)
|
||||
public List<Cabinet> findFilteredCabinets(Long id, String number) {
|
||||
List<Cabinet> allCabinets = cabinetRepository.findAll();
|
||||
List<Cabinet> result = new ArrayList<>();
|
||||
for (Cabinet cabinet : allCabinets) {
|
||||
boolean flag = true;
|
||||
if (id != null && !Objects.equals(cabinet.getId(), id)) {
|
||||
flag = false;
|
||||
}
|
||||
if (number != null && !Objects.equals(cabinet.getNumber(), number)) {
|
||||
flag = false;
|
||||
}
|
||||
if (flag) {
|
||||
result.add(cabinet);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
return cabinetRepository.findFilteredCabinets(id, number);
|
||||
}
|
||||
@Transactional(readOnly = true)
|
||||
public List<Cabinet> findAllCabinets() {
|
||||
@ -78,13 +62,7 @@ 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 computer = currentCabinet.getComputers().get(i);
|
||||
computer.setCabinet(null);
|
||||
currentCabinet.removeComputer(computer);
|
||||
computerRepository.delete(computer);
|
||||
}
|
||||
computerService.deleteRelationsWithCabinets(currentCabinet.getComputers());
|
||||
cabinetRepository.delete(currentCabinet);
|
||||
return currentCabinet;
|
||||
}
|
||||
@ -93,38 +71,34 @@ public class CabinetService {
|
||||
cabinetRepository.deleteAll();
|
||||
}
|
||||
@Transactional
|
||||
public List<Computer> listComputers(Long id) {
|
||||
public List<Computer> listComputersFromCabinet(Long id) {
|
||||
if ((Object)id == null) {
|
||||
throw new IllegalArgumentException("Cabinet id is null or empty");
|
||||
}
|
||||
return findCabinet(id).getComputers();
|
||||
}
|
||||
@Transactional
|
||||
public Computer addComputer(Long computerId, Long cabinetId) {
|
||||
public void addComputerToCabinet(Long computerId, Long cabinetId) {
|
||||
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 = computerRepository.findById(computerId)
|
||||
.orElseThrow(() -> new ComputerNotFoundException(computerId));
|
||||
final Computer computer = computerService.findComputer(computerId);
|
||||
final Cabinet cabinet = findCabinet(cabinetId);
|
||||
cabinet.addComputer(computer);
|
||||
return computer;
|
||||
}
|
||||
@Transactional
|
||||
public Computer deleteComputer(Long computerId, Long cabinetId) {
|
||||
public void deleteComputerFromCabinet(Long computerId, Long cabinetId) {
|
||||
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 = computerRepository.findById(computerId)
|
||||
.orElseThrow(() -> new ComputerNotFoundException(computerId));
|
||||
final Computer computer = computerService.findComputer(computerId);
|
||||
final Cabinet cabinet = findCabinet(cabinetId);
|
||||
cabinet.removeComputer(computer);
|
||||
return computer;
|
||||
}
|
||||
}
|
@ -1,13 +1,10 @@
|
||||
package com.kalyshev.yan.computer.controller;
|
||||
|
||||
import com.kalyshev.yan.WebConfiguration;
|
||||
import com.kalyshev.yan.cabinet.controller.CabinetDto;
|
||||
import com.kalyshev.yan.cabinet.model.Cabinet;
|
||||
import com.kalyshev.yan.computer.service.ComputerService;
|
||||
import com.kalyshev.yan.monitor.controller.MonitorDto;
|
||||
import jakarta.validation.Valid;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import com.kalyshev.yan.computer.model.Computer;
|
||||
import com.kalyshev.yan.computer.service.ComputerService;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@ -38,18 +35,9 @@ public class ComputerController {
|
||||
.map(ComputerDto::new)
|
||||
.toList();
|
||||
}
|
||||
@GetMapping("/{id}/computers")
|
||||
public CabinetDto getComputersCabinet(@PathVariable Long id) {
|
||||
return new CabinetDto(computerService.getCabinet(id));
|
||||
}
|
||||
@PostMapping("/")
|
||||
public ComputerDto createComputer(@RequestBody @Valid ComputerDto computerDto) {
|
||||
return new ComputerDto(computerService.addComputer(computerDto.getModelName(), computerDto.getSerialNum(), computerDto.getMonitorId(), computerDto.getCabinetId()));
|
||||
}
|
||||
@PostMapping("/{id}/cabinet")
|
||||
public CabinetDto setCabinetComputer(@PathVariable Long id,
|
||||
@RequestParam("cabinetId") Long cabinetId) {
|
||||
return new CabinetDto(computerService.setCabinet(cabinetId, id));
|
||||
return new ComputerDto(computerService.addComputer(computerDto.getModelName(), computerDto.getSerialNum(), computerDto.getMonitorId()));
|
||||
}
|
||||
@PostMapping("/{id}/monitor")
|
||||
public MonitorDto setMonitorComputer(@PathVariable Long id,
|
||||
|
@ -2,12 +2,8 @@ package com.kalyshev.yan.computer.model;
|
||||
|
||||
import com.kalyshev.yan.cabinet.model.Cabinet;
|
||||
import com.kalyshev.yan.monitor.model.Monitor;
|
||||
import jakarta.annotation.Nullable;
|
||||
import jakarta.persistence.*;
|
||||
import jakarta.validation.constraints.Null;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
@Entity
|
||||
@ -21,7 +17,7 @@ public class Computer {
|
||||
@ManyToOne( cascade = {CascadeType.MERGE}, fetch = FetchType.EAGER)
|
||||
@JoinColumn(name = "cabinet", nullable = true)
|
||||
private Cabinet cabinet;
|
||||
@OneToOne()
|
||||
@OneToOne(cascade = {CascadeType.MERGE})
|
||||
@JoinColumn(name = "monitor_id")
|
||||
private Monitor monitor;
|
||||
|
||||
@ -61,13 +57,14 @@ public class Computer {
|
||||
this.monitor = null;
|
||||
return temp;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
if (this == o)
|
||||
return true;
|
||||
if (!(o instanceof Computer))
|
||||
return false;
|
||||
Computer computer = (Computer) o;
|
||||
return Objects.equals(id, computer.id);
|
||||
return Objects.equals(id, computer.id) && Objects.equals(this.modelName, computer.modelName) && Objects.equals(this.monitor, computer.monitor) && Objects.equals(this.cabinet, computer.cabinet);
|
||||
}
|
||||
@Override
|
||||
public int hashCode() {
|
||||
|
@ -1,7 +1,34 @@
|
||||
package com.kalyshev.yan.computer.repository;
|
||||
|
||||
import com.kalyshev.yan.computer.model.Computer;
|
||||
import com.kalyshev.yan.monitor.model.Monitor;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Modifying;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
public interface ComputerRepository extends JpaRepository<Computer, Long> {
|
||||
@Query(value = "select * from Computer where (\"ID\" = :id or :id is Null) and " +
|
||||
"(\"MODEL_NAME\" = :modelName or :modelName is Null) and " +
|
||||
"(\"SERIAL_NUM\" = :serialNum or :serialNum is Null) and " +
|
||||
"(\"MONITOR_ID\" = :monitorId or :monitorId is Null) and " +
|
||||
"(\"CABINET\" = :cabinetId or :cabinetId is Null)", nativeQuery = true)
|
||||
public List<Computer> findFilteredComputers(@Param("id") Long id,
|
||||
@Param("modelName") String modelName,
|
||||
@Param("serialNum") String serialNum,
|
||||
@Param("monitorId") Long monitorId,
|
||||
@Param("cabinetId") Long cabinetId);
|
||||
|
||||
@Query(value = "select * from Computer where \"MONITOR_ID\" = :monitorId", nativeQuery = true)
|
||||
public Computer findComputerByMonitor(@Param("monitorId") Long monitorId);
|
||||
|
||||
@Modifying
|
||||
@Query(value = "update Computer set \"MONITOR_ID\" = null", nativeQuery = true)
|
||||
public void removeAllComputerMonitorRelations();
|
||||
|
||||
// @Query(value = "update Computer where id in :computerIds set cabinet_id = null")
|
||||
// public void deleteRelationsWithCabinets(@Param("computerIds") Collection<Long> computerIds);
|
||||
}
|
||||
|
@ -1,44 +1,33 @@
|
||||
package com.kalyshev.yan.computer.service;
|
||||
|
||||
import com.kalyshev.yan.cabinet.model.Cabinet;
|
||||
import com.kalyshev.yan.cabinet.repository.CabinetNotFoundException;
|
||||
import com.kalyshev.yan.cabinet.repository.CabinetRepository;
|
||||
import com.kalyshev.yan.computer.model.Computer;
|
||||
import com.kalyshev.yan.computer.repository.ComputerNotFoundException;
|
||||
import com.kalyshev.yan.computer.repository.ComputerRepository;
|
||||
import com.kalyshev.yan.monitor.model.Monitor;
|
||||
import com.kalyshev.yan.monitor.repository.MonitorNotFoundException;
|
||||
import com.kalyshev.yan.monitor.repository.MonitorRepository;
|
||||
import com.kalyshev.yan.monitor.service.MonitorService;
|
||||
import com.kalyshev.yan.util.validation.ValidatorUtil;
|
||||
import jakarta.annotation.Nullable;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.StringUtils;
|
||||
import com.kalyshev.yan.computer.model.Computer;
|
||||
|
||||
import jakarta.persistence.EntityNotFoundException;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.*;
|
||||
|
||||
@Service
|
||||
public class ComputerService {
|
||||
private final CabinetRepository cabinetRepository;
|
||||
private final ComputerRepository computerRepository;
|
||||
private final MonitorRepository monitorRepository;
|
||||
private final MonitorService monitorService;
|
||||
private final ValidatorUtil validatorUtil;
|
||||
public ComputerService(CabinetRepository cabinetRepository,
|
||||
ComputerRepository computerRepository,
|
||||
MonitorRepository monitorRepository,
|
||||
public ComputerService(ComputerRepository computerRepository,
|
||||
MonitorService monitorService,
|
||||
ValidatorUtil validatorUtil) {
|
||||
this.cabinetRepository = cabinetRepository;
|
||||
this.computerRepository = computerRepository;
|
||||
this.monitorRepository = monitorRepository;
|
||||
this.monitorService = monitorService;
|
||||
this.validatorUtil = validatorUtil;
|
||||
}
|
||||
@Transactional
|
||||
public Computer addComputer(String modelName, String serialNum, @Nullable Long monitorId, @Nullable Long cabinetId) {
|
||||
public Computer addComputer(String modelName, String serialNum, @Nullable Long monitorId) {
|
||||
if (!StringUtils.hasText(modelName)) {
|
||||
throw new IllegalArgumentException("Computer model name is null or empty");
|
||||
}
|
||||
@ -47,15 +36,9 @@ public class ComputerService {
|
||||
}
|
||||
final Computer computer = new Computer(modelName, serialNum);
|
||||
if (monitorId != null) {
|
||||
final Monitor monitor = monitorRepository.findById(monitorId)
|
||||
.orElseThrow(() -> new ComputerNotFoundException(monitorId));
|
||||
final Monitor monitor = monitorService.findMonitor(monitorId);
|
||||
computer.setMonitor(monitor);
|
||||
}
|
||||
if (cabinetId != null) {
|
||||
final Cabinet cabinet = cabinetRepository.findById(cabinetId)
|
||||
.orElseThrow(() -> new CabinetNotFoundException(cabinetId));
|
||||
computer.setCabinet(cabinet);
|
||||
}
|
||||
validatorUtil.validate(computer);
|
||||
return computerRepository.save(computer);
|
||||
}
|
||||
@ -70,30 +53,7 @@ public class ComputerService {
|
||||
}
|
||||
@Transactional(readOnly = true)
|
||||
public List<Computer> findFilteredComputers(Long id, String modelName, String serialNum, Long monitorId, Long cabinetId) {
|
||||
List<Computer> allComputers = computerRepository.findAll();
|
||||
List<Computer> result = new ArrayList<>();
|
||||
for (Computer computer : allComputers) {
|
||||
boolean flag = true;
|
||||
if (id != null && !Objects.equals(computer.getId(), id)) {
|
||||
flag = false;
|
||||
}
|
||||
if (modelName != null && !Objects.equals(computer.getModelName(), modelName)) {
|
||||
flag = false;
|
||||
}
|
||||
if (serialNum != null && !Objects.equals(computer.getSerialNum(), serialNum)) {
|
||||
flag = false;
|
||||
}
|
||||
if (monitorId != null && computer.getMonitor() != null && !Objects.equals(computer.getMonitor().getId(), monitorId)) {
|
||||
flag = false;
|
||||
}
|
||||
if (cabinetId != null && computer.getCabinet() != null && !Objects.equals(computer.getCabinet().getId(), cabinetId)) {
|
||||
flag = false;
|
||||
}
|
||||
if (flag) {
|
||||
result.add(computer);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
return computerRepository.findFilteredComputers(id, modelName, serialNum, monitorId, cabinetId);
|
||||
}
|
||||
@Transactional
|
||||
public Computer updateComputer(Long id, String modelName, String serialNum, Long monitorId, Long cabinetId) {
|
||||
@ -108,15 +68,9 @@ public class ComputerService {
|
||||
currentComputer.setSerialNum(serialNum);
|
||||
}
|
||||
if (monitorId != null) {
|
||||
Monitor monitor = monitorRepository.findById(monitorId)
|
||||
.orElseThrow(() -> new MonitorNotFoundException(id));
|
||||
final Monitor monitor = monitorService.findMonitor(monitorId);
|
||||
currentComputer.setMonitor(monitor);
|
||||
}
|
||||
if (cabinetId != null) {
|
||||
Cabinet cabinet = cabinetRepository.findById(cabinetId)
|
||||
.orElseThrow(() -> new CabinetNotFoundException(cabinetId));
|
||||
currentComputer.setCabinet(cabinet);
|
||||
}
|
||||
validatorUtil.validate(currentComputer);
|
||||
return computerRepository.save(currentComputer);
|
||||
}
|
||||
@ -138,28 +92,6 @@ public class ComputerService {
|
||||
}
|
||||
}
|
||||
@Transactional
|
||||
public Cabinet getCabinet(Long id) {
|
||||
if ((Object)id == null) {
|
||||
throw new IllegalArgumentException("Computer id is null or empty");
|
||||
}
|
||||
final Computer computer = findComputer(id);
|
||||
return computer.getCabinet();
|
||||
}
|
||||
@Transactional
|
||||
public Cabinet setCabinet(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 = findComputer(computerId);
|
||||
final Cabinet cabinet = cabinetRepository.findById(cabinetId)
|
||||
.orElseThrow(() -> new CabinetNotFoundException(cabinetId));
|
||||
computer.setCabinet(cabinet);
|
||||
return cabinet;
|
||||
}
|
||||
@Transactional
|
||||
public Monitor setMonitor(Long monitorId, Long computerId) {
|
||||
if ((Object)computerId == null) {
|
||||
throw new IllegalArgumentException("Computer id is null or empty");
|
||||
@ -168,9 +100,36 @@ public class ComputerService {
|
||||
throw new IllegalArgumentException("Monitor id is null or empty");
|
||||
}
|
||||
final Computer computer = findComputer(computerId);
|
||||
final Monitor monitor = monitorRepository.findById(monitorId)
|
||||
.orElseThrow(() -> new MonitorNotFoundException(monitorId));
|
||||
final Monitor monitor = monitorService.findMonitor(monitorId);
|
||||
computer.setMonitor(monitor);
|
||||
return monitor;
|
||||
}
|
||||
@Transactional
|
||||
public void deleteRelationsWithCabinets(List<Computer> computers) {
|
||||
for (Computer computer: computers) {
|
||||
computer.setCabinet(null);
|
||||
}
|
||||
}
|
||||
@Transactional
|
||||
public Computer findComputerByMonitor(Monitor monitor) {
|
||||
if (monitor.getId() == null) {
|
||||
return null;
|
||||
}
|
||||
return computerRepository.findComputerByMonitor(monitor.getId());
|
||||
}
|
||||
@Transactional
|
||||
public Monitor deleteMonitorWithRelation(Long id) {
|
||||
final Monitor currentMonitor = monitorService.findMonitor(id);
|
||||
Computer computer = findComputerByMonitor(currentMonitor);
|
||||
if (computer != null) {
|
||||
computer.removeMonitor();
|
||||
}
|
||||
monitorService.deleteMonitor(currentMonitor);
|
||||
return currentMonitor;
|
||||
}
|
||||
@Transactional
|
||||
public void deleteAllMonitorsWithRelations() {
|
||||
computerRepository.removeAllComputerMonitorRelations();
|
||||
monitorService.deleteAllMonitors();
|
||||
}
|
||||
}
|
@ -1,8 +1,7 @@
|
||||
package com.kalyshev.yan.monitor.controller;
|
||||
|
||||
import com.kalyshev.yan.WebConfiguration;
|
||||
import com.kalyshev.yan.computer.controller.ComputerDto;
|
||||
import com.kalyshev.yan.monitor.model.Monitor;
|
||||
import com.kalyshev.yan.computer.service.ComputerService;
|
||||
import com.kalyshev.yan.monitor.service.MonitorService;
|
||||
import jakarta.validation.Valid;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
@ -13,8 +12,11 @@ import java.util.List;
|
||||
@RequestMapping(WebConfiguration.REST_API + "/monitor")
|
||||
public class MonitorController {
|
||||
private final MonitorService monitorService;
|
||||
public MonitorController(MonitorService monitorService) {
|
||||
private final ComputerService computerService;
|
||||
public MonitorController(MonitorService monitorService,
|
||||
ComputerService computerService) {
|
||||
this.monitorService = monitorService;
|
||||
this.computerService = computerService;
|
||||
}
|
||||
@GetMapping("/{id}")
|
||||
public MonitorDto getMonitor(@PathVariable Long id) {
|
||||
@ -44,6 +46,6 @@ public class MonitorController {
|
||||
}
|
||||
@DeleteMapping("/{id}")
|
||||
public MonitorDto deleteMonitor(@PathVariable Long id) {
|
||||
return new MonitorDto(monitorService.deleteMonitor(id));
|
||||
return new MonitorDto(computerService.deleteMonitorWithRelation(id));
|
||||
}
|
||||
}
|
@ -1,9 +1,10 @@
|
||||
package com.kalyshev.yan.monitor.model;
|
||||
|
||||
import com.kalyshev.yan.computer.model.Computer;
|
||||
import jakarta.persistence.*;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.Table;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
@Entity
|
||||
@ -29,10 +30,12 @@ public class Monitor {
|
||||
}
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
Monitor computer = (Monitor) o;
|
||||
return Objects.equals(id, computer.id);
|
||||
if (this == o)
|
||||
return true;
|
||||
if (!(o instanceof Monitor))
|
||||
return false;
|
||||
Monitor monitor = (Monitor) o;
|
||||
return Objects.equals(id, monitor.id) && Objects.equals(this.modelName, monitor.modelName);
|
||||
}
|
||||
@Override
|
||||
public int hashCode() {
|
||||
|
@ -2,6 +2,14 @@ package com.kalyshev.yan.monitor.repository;
|
||||
|
||||
import com.kalyshev.yan.monitor.model.Monitor;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface MonitorRepository extends JpaRepository<Monitor, Long> {
|
||||
@Query(value = "select * from Monitor where (\"ID\" = :id or :id is Null) and " +
|
||||
"(\"MODEL_NAME\" = :modelName or :modelName is Null)", nativeQuery = true)
|
||||
public List<Monitor> findFilteredMonitors(@Param("id") Long id,
|
||||
@Param("modelName") String modelName);
|
||||
}
|
||||
|
@ -1,25 +1,12 @@
|
||||
package com.kalyshev.yan.monitor.service;
|
||||
package com.kalyshev.yan.monitor.service;
|
||||
|
||||
import com.kalyshev.yan.cabinet.model.Cabinet;
|
||||
import com.kalyshev.yan.cabinet.repository.CabinetNotFoundException;
|
||||
import com.kalyshev.yan.cabinet.repository.CabinetRepository;
|
||||
import com.kalyshev.yan.computer.model.Computer;
|
||||
import com.kalyshev.yan.computer.repository.ComputerRepository;
|
||||
import com.kalyshev.yan.monitor.controller.MonitorController;
|
||||
import com.kalyshev.yan.monitor.model.Monitor;
|
||||
import com.kalyshev.yan.monitor.repository.MonitorNotFoundException;
|
||||
import com.kalyshev.yan.monitor.repository.MonitorRepository;
|
||||
import com.kalyshev.yan.util.validation.ValidatorUtil;
|
||||
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;
|
||||
import com.kalyshev.yan.monitor.model.Monitor;
|
||||
|
||||
import jakarta.persistence.EntityManager;
|
||||
import jakarta.persistence.EntityNotFoundException;
|
||||
import jakarta.persistence.PersistenceContext;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
@ -29,13 +16,10 @@ import java.util.Optional;
|
||||
@Service
|
||||
public class MonitorService {
|
||||
private final MonitorRepository monitorRepository;
|
||||
private final ComputerRepository computerRepository;
|
||||
private final ValidatorUtil validatorUtil;
|
||||
public MonitorService(MonitorRepository monitorRepository,
|
||||
ComputerRepository computerRepository,
|
||||
ValidatorUtil validatorUtil) {
|
||||
this.monitorRepository = monitorRepository;
|
||||
this.computerRepository = computerRepository;
|
||||
this.validatorUtil = validatorUtil;
|
||||
}
|
||||
@Transactional
|
||||
@ -58,21 +42,7 @@ public class MonitorService {
|
||||
}
|
||||
@Transactional(readOnly = true)
|
||||
public List<Monitor> findFilteredMonitors(Long id, String modelName) {
|
||||
List<Monitor> allMonitors = monitorRepository.findAll();
|
||||
List<Monitor> result = new ArrayList<>();
|
||||
for (Monitor monitor : allMonitors) {
|
||||
boolean flag = true;
|
||||
if (id != null && !Objects.equals(monitor.getId(), id)) {
|
||||
flag = false;
|
||||
}
|
||||
if (modelName != null && !Objects.equals(monitor.getModelName(), modelName)) {
|
||||
flag = false;
|
||||
}
|
||||
if (flag) {
|
||||
result.add(monitor);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
return monitorRepository.findFilteredMonitors(id, modelName);
|
||||
}
|
||||
@Transactional
|
||||
public Monitor updateMonitor(Long id, String modelName) {
|
||||
@ -85,33 +55,9 @@ public class MonitorService {
|
||||
return monitorRepository.save(currentMonitor);
|
||||
}
|
||||
@Transactional
|
||||
public Monitor deleteMonitor(Long id) {
|
||||
final Monitor currentMonitor = findMonitor(id);
|
||||
Computer computer = getComputer(id);
|
||||
if (computer != null) {
|
||||
computer.removeMonitor();
|
||||
}
|
||||
monitorRepository.delete(currentMonitor);
|
||||
return currentMonitor;
|
||||
public void deleteMonitor(Monitor monitor) {
|
||||
monitorRepository.delete(monitor);
|
||||
}
|
||||
@Transactional
|
||||
public void deleteAllMonitors() {
|
||||
List<Computer> computers = computerRepository.findAll();
|
||||
for (Computer computer : computers) {
|
||||
computer.removeMonitor();
|
||||
}
|
||||
monitorRepository.deleteAll();
|
||||
}
|
||||
@Transactional
|
||||
public Computer getComputer(Long id) {
|
||||
List<Computer> computers = computerRepository.findAll();
|
||||
for (Computer computer : computers) {
|
||||
if (computer.getMonitor() != null) {
|
||||
if (Objects.equals(computer.getMonitor().getId(), id)) {
|
||||
return computer;
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
public void deleteAllMonitors() { monitorRepository.deleteAll(); }
|
||||
}
|
@ -5,7 +5,6 @@ import com.kalyshev.yan.cabinet.repository.CabinetNotFoundException;
|
||||
import com.kalyshev.yan.cabinet.service.CabinetService;
|
||||
import com.kalyshev.yan.computer.service.ComputerService;
|
||||
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;
|
||||
|
@ -27,10 +27,10 @@ public class JpaComputerCabinetTests {
|
||||
void testCabinetAddOneToMany() {
|
||||
cabinetService.deleteAllCabinets();
|
||||
computerService.deleteAllComputers();
|
||||
monitorService.deleteAllMonitors();
|
||||
computerService.deleteAllMonitorsWithRelations();
|
||||
final Cabinet cabinet = cabinetService.addCabinet("18");
|
||||
final Monitor monitor = monitorService.addMonitor("Asus");
|
||||
final Computer computer = computerService.addComputer("PC", "78457h", monitor.getId(), null);
|
||||
final Computer computer = computerService.addComputer("PC", "78457h", monitor.getId());
|
||||
cabinet.addComputer(computer);
|
||||
log.info(cabinet.toString());
|
||||
log.info(computer.toString());
|
||||
@ -41,10 +41,10 @@ public class JpaComputerCabinetTests {
|
||||
void testCabinetDeleteOneToMany() {
|
||||
cabinetService.deleteAllCabinets();
|
||||
computerService.deleteAllComputers();
|
||||
monitorService.deleteAllMonitors();
|
||||
computerService.deleteAllMonitorsWithRelations();
|
||||
final Cabinet cabinet = cabinetService.addCabinet("18");
|
||||
final Monitor monitor = monitorService.addMonitor("Asus");
|
||||
final Computer computer = computerService.addComputer("PC", "78457h", monitor.getId(), null);
|
||||
final Computer computer = computerService.addComputer("PC", "78457h", monitor.getId());
|
||||
cabinet.addComputer(computer);
|
||||
log.info(cabinet.toString());
|
||||
log.info(computer.toString());
|
||||
@ -57,10 +57,10 @@ public class JpaComputerCabinetTests {
|
||||
void testComputerAddManyToMany() {
|
||||
cabinetService.deleteAllCabinets();
|
||||
computerService.deleteAllComputers();
|
||||
monitorService.deleteAllMonitors();
|
||||
computerService.deleteAllMonitorsWithRelations();
|
||||
final Cabinet cabinet = cabinetService.addCabinet("18");
|
||||
final Monitor monitor = monitorService.addMonitor("Asus");
|
||||
final Computer computer = computerService.addComputer("PC", "78457h", monitor.getId(), null);
|
||||
final Computer computer = computerService.addComputer("PC", "78457h", monitor.getId());
|
||||
computer.setCabinet(cabinet);
|
||||
log.info(cabinet.toString());
|
||||
log.info(computer.toString());
|
||||
@ -71,10 +71,10 @@ public class JpaComputerCabinetTests {
|
||||
void testComputerDeleteManyToMany() {
|
||||
cabinetService.deleteAllCabinets();
|
||||
computerService.deleteAllComputers();
|
||||
monitorService.deleteAllMonitors();
|
||||
computerService.deleteAllMonitorsWithRelations();
|
||||
final Cabinet cabinet = cabinetService.addCabinet("18");
|
||||
final Monitor monitor = monitorService.addMonitor("Asus");
|
||||
final Computer computer = computerService.addComputer("PC", "78457h", monitor.getId(), null);
|
||||
final Computer computer = computerService.addComputer("PC", "78457h", monitor.getId());
|
||||
computer.setCabinet(cabinet);
|
||||
log.info(cabinet.toString());
|
||||
log.info(computer.toString());
|
||||
|
@ -7,7 +7,6 @@ import com.kalyshev.yan.computer.repository.ComputerNotFoundException;
|
||||
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;
|
||||
@ -31,7 +30,7 @@ public class JpaComputerTests {
|
||||
void testComputerPartialCreate() {
|
||||
computerService.deleteAllComputers();
|
||||
final Monitor monitor = monitorService.addMonitor("Asus");
|
||||
final Computer computer = computerService.addComputer("Computer", "w7894572", monitor.getId(), null);
|
||||
final Computer computer = computerService.addComputer("Computer", "w7894572", monitor.getId());
|
||||
log.info(computer.toString());
|
||||
Assertions.assertNotNull(computer.getId());
|
||||
Assertions.assertNull(computer.getCabinet());
|
||||
@ -41,7 +40,8 @@ public class JpaComputerTests {
|
||||
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());
|
||||
final Computer computer = computerService.addComputer("Computer", "w7894572", monitor.getId());
|
||||
computer.setCabinet(cabinet);
|
||||
log.info(computer.toString());
|
||||
Assertions.assertNotNull(computer.getId());
|
||||
Assertions.assertEquals(cabinet, computer.getCabinet());
|
||||
@ -50,7 +50,7 @@ public class JpaComputerTests {
|
||||
void testComputerDelete() {
|
||||
computerService.deleteAllComputers();
|
||||
final Monitor monitor = monitorService.addMonitor("Asus");
|
||||
final Computer computer = computerService.addComputer("Computer", "w7894572", null, null);
|
||||
final Computer computer = computerService.addComputer("Computer", "w7894572", null);
|
||||
log.info(computer.toString());
|
||||
computerService.deleteComputer(computer.getId());
|
||||
Assertions.assertThrows(ComputerNotFoundException.class, () -> computerService.findComputer(computer.getId()));
|
||||
@ -58,7 +58,7 @@ public class JpaComputerTests {
|
||||
@Test
|
||||
void testComputerRead() {
|
||||
computerService.deleteAllComputers();
|
||||
final Computer computer = computerService.addComputer("Computer", "w7894572", null, null);
|
||||
final Computer computer = computerService.addComputer("Computer", "w7894572", null);
|
||||
log.info(computer.toString());
|
||||
final Computer findComputer = computerService.findComputer(computer.getId());
|
||||
log.info(findComputer.toString());
|
||||
@ -73,8 +73,8 @@ public class JpaComputerTests {
|
||||
void testComputerReadAll() {
|
||||
computerService.deleteAllComputers();
|
||||
final Monitor monitor = monitorService.addMonitor("Asus");
|
||||
computerService.addComputer("Computer", "w7894572", null, null);
|
||||
computerService.addComputer("Another comp", "3453s", null, null);
|
||||
computerService.addComputer("Computer", "w7894572", null);
|
||||
computerService.addComputer("Another comp", "3453s", null);
|
||||
final List<Computer> computers = computerService.findAllComputers();
|
||||
log.info(computers.toString());
|
||||
Assertions.assertEquals(computers.size(), 2);
|
||||
|
@ -1,15 +1,12 @@
|
||||
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.repository.MonitorNotFoundException;
|
||||
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;
|
||||
@ -25,14 +22,14 @@ public class JpaMonitorTests {
|
||||
private ComputerService computerService;
|
||||
@Test
|
||||
void testMonitorCreate() {
|
||||
monitorService.deleteAllMonitors();
|
||||
computerService.deleteAllMonitorsWithRelations();
|
||||
final Monitor monitor = monitorService.addMonitor("Asus");
|
||||
log.info(monitor.toString());
|
||||
Assertions.assertNotNull(monitor.getId());
|
||||
}
|
||||
@Test
|
||||
void testMonitorRead() {
|
||||
monitorService.deleteAllMonitors();
|
||||
computerService.deleteAllMonitorsWithRelations();
|
||||
final Monitor monitor = monitorService.addMonitor("Asus");
|
||||
log.info(monitor.toString());
|
||||
final Monitor findMonitor = monitorService.findMonitor(monitor.getId());
|
||||
@ -41,20 +38,20 @@ public class JpaMonitorTests {
|
||||
}
|
||||
@Test
|
||||
void testMonitorDelete() {
|
||||
monitorService.deleteAllMonitors();
|
||||
computerService.deleteAllMonitorsWithRelations();
|
||||
final Monitor monitor = monitorService.addMonitor("Asus");
|
||||
log.info(monitor.toString());
|
||||
monitorService.deleteMonitor(monitor.getId());
|
||||
computerService.deleteMonitorWithRelation(monitor.getId());
|
||||
Assertions.assertThrows(MonitorNotFoundException.class, () -> monitorService.findMonitor(monitor.getId()));
|
||||
}
|
||||
@Test
|
||||
void testMonitorReadNotFound() {
|
||||
monitorService.deleteAllMonitors();
|
||||
computerService.deleteAllMonitorsWithRelations();
|
||||
Assertions.assertThrows(MonitorNotFoundException.class, () -> monitorService.findMonitor(-1L));
|
||||
}
|
||||
@Test
|
||||
void testMonitorReadAll() {
|
||||
monitorService.deleteAllMonitors();
|
||||
computerService.deleteAllMonitorsWithRelations();
|
||||
monitorService.addMonitor("Asus");
|
||||
monitorService.addMonitor("HP");
|
||||
final List<Monitor> monitors = monitorService.findAllMonitors();
|
||||
@ -63,19 +60,19 @@ public class JpaMonitorTests {
|
||||
}
|
||||
@Test
|
||||
void testMonitorReadAllEmpty() {
|
||||
monitorService.deleteAllMonitors();
|
||||
computerService.deleteAllMonitorsWithRelations();
|
||||
final List<Monitor> monitors = monitorService.findAllMonitors();
|
||||
log.info(monitors.toString());
|
||||
Assertions.assertEquals(monitors.size(), 0);
|
||||
}
|
||||
@Test
|
||||
void testMonitorGetComputer() {
|
||||
monitorService.deleteAllMonitors();
|
||||
computerService.deleteAllMonitorsWithRelations();
|
||||
computerService.deleteAllComputers();
|
||||
Monitor monitor = monitorService.addMonitor("Asus");
|
||||
Computer computer = computerService.addComputer("Model", "6sfv4", monitor.getId(), null);
|
||||
Computer computer = computerService.addComputer("Model", "6sfv4", monitor.getId());
|
||||
log.info(computer.toString());
|
||||
Computer fetchedComputer = monitorService.getComputer(monitor.getId());
|
||||
Computer fetchedComputer = computerService.findComputerByMonitor(monitor);
|
||||
Assertions.assertEquals(computer, fetchedComputer);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user