added repositories, dtos, validator, changed services and controllers
This commit is contained in:
parent
a73685f3b3
commit
019bcc6411
20
src/main/java/com/kalyshev/yan/WebConfiguration.java
Normal file
20
src/main/java/com/kalyshev/yan/WebConfiguration.java
Normal file
@ -0,0 +1,20 @@
|
||||
package com.kalyshev.yan;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.config.annotation.CorsRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
@Configuration
|
||||
public class WebConfiguration implements WebMvcConfigurer {
|
||||
public static final String REST_API = "/api";
|
||||
@Override
|
||||
public void addViewControllers(ViewControllerRegistry registry) {
|
||||
WebMvcConfigurer.super.addViewControllers(registry);
|
||||
registry.addViewController("rest-test");
|
||||
}
|
||||
@Override
|
||||
public void addCorsMappings(CorsRegistry registry) {
|
||||
registry.addMapping("/**").allowedMethods("*");
|
||||
}
|
||||
}
|
@ -1,68 +1,60 @@
|
||||
package com.kalyshev.yan.cabinet.controller;
|
||||
|
||||
import com.kalyshev.yan.WebConfiguration;
|
||||
import com.kalyshev.yan.computer.controller.ComputerDto;
|
||||
import com.kalyshev.yan.computer.model.Computer;
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PatchMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
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.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/cabinet")
|
||||
@RequestMapping(WebConfiguration.REST_API + "/cabinet")
|
||||
public class CabinetController {
|
||||
private final CabinetService cabinetService;
|
||||
|
||||
public CabinetController(CabinetService cabinetService) {
|
||||
this.cabinetService = cabinetService;
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public Cabinet getCabinet(@PathVariable Long id) {
|
||||
return cabinetService.findCabinet(id);
|
||||
public CabinetDto getCabinet(@PathVariable Long id) {
|
||||
return new CabinetDto(cabinetService.findCabinet(id));
|
||||
}
|
||||
|
||||
@GetMapping("/")
|
||||
public List<Cabinet> getCabinets() {
|
||||
return cabinetService.findAllCabinets();
|
||||
public List<CabinetDto> getCabinets() {
|
||||
return cabinetService.findAllCabinets().stream()
|
||||
.map(CabinetDto::new)
|
||||
.toList();
|
||||
}
|
||||
|
||||
@GetMapping("/{id}/computers")
|
||||
public List<Computer> getCabinetComputers(@PathVariable Long id) {
|
||||
return cabinetService.listComputers(id);
|
||||
public List<ComputerDto> getCabinetComputers(@PathVariable Long id) {
|
||||
return cabinetService.listComputers(id).stream()
|
||||
.map(ComputerDto::new)
|
||||
.toList();
|
||||
}
|
||||
|
||||
@PostMapping("/")
|
||||
public Cabinet createCabinet(@RequestParam("number") String number) {
|
||||
return cabinetService.addCabinet(number);
|
||||
public CabinetDto createCabinet(@RequestBody @Valid CabinetDto cabinetDto) {
|
||||
return new CabinetDto(cabinetService.addCabinet(cabinetDto.getNumber()));
|
||||
}
|
||||
|
||||
@PostMapping("/computer")
|
||||
public Computer createCabinetComputer(@RequestParam("computerId") Long computerId,
|
||||
public ComputerDto createCabinetComputer(@RequestParam("computerId") Long computerId,
|
||||
@RequestParam("cabinetId") Long cabinetId) {
|
||||
return cabinetService.addComputer(computerId, cabinetId);
|
||||
return new ComputerDto(cabinetService.addComputer(computerId, cabinetId));
|
||||
}
|
||||
|
||||
@PatchMapping("/{id}")
|
||||
public Cabinet updateCabinet(@PathVariable Long id,
|
||||
@RequestParam("number") String number) {
|
||||
return cabinetService.updateCabinet(id, number);
|
||||
@PutMapping("/{id}")
|
||||
public CabinetDto updateCabinet(@PathVariable Long id,
|
||||
@RequestBody @Valid CabinetDto cabinetDto) {
|
||||
return new CabinetDto(cabinetService.updateCabinet(id, cabinetDto.getNumber()));
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
public Cabinet deleteCabinet(@PathVariable Long id) {
|
||||
return cabinetService.deleteCabinet(id);
|
||||
public CabinetDto deleteCabinet(@PathVariable Long id) {
|
||||
return new CabinetDto(cabinetService.deleteCabinet(id));
|
||||
}
|
||||
|
||||
@DeleteMapping("/{cabinetId}/computer")
|
||||
public Computer deleteCabinetComputer(@PathVariable Long cabinetId,
|
||||
@RequestParam("computerId") Long computerId) {
|
||||
return cabinetService.deleteComputer(computerId, cabinetId);
|
||||
public ComputerDto deleteCabinetComputer(@PathVariable Long cabinetId,
|
||||
@RequestParam("computerId") Long computerId) {
|
||||
return new ComputerDto(cabinetService.deleteComputer(computerId, cabinetId));
|
||||
}
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
package com.kalyshev.yan.cabinet.controller;
|
||||
|
||||
import com.kalyshev.yan.cabinet.model.Cabinet;
|
||||
import com.kalyshev.yan.computer.model.Computer;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class CabinetDto {
|
||||
private Long id;
|
||||
private String number;
|
||||
private List<Computer> computers;
|
||||
public CabinetDto() {}
|
||||
public CabinetDto(Cabinet cabinet) {
|
||||
this.id = cabinet.getId();
|
||||
this.number = cabinet.getNumber();
|
||||
this.computers = List.copyOf(cabinet.getComputers());
|
||||
}
|
||||
public Long getId() { return this.id; }
|
||||
public void setId(Long id) { this.id = id; }
|
||||
public String getNumber() { return this.number; }
|
||||
public void setNumber(String number) { this.number = number; }
|
||||
public List<Computer> getComputers() { return this.computers; }
|
||||
public void setComputers(List<Computer> computers) { this.computers = computers; }
|
||||
}
|
@ -25,10 +25,10 @@ public class Cabinet {
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
public String getnumber() {
|
||||
public String getNumber() {
|
||||
return number;
|
||||
}
|
||||
public void setnumber(String number) {
|
||||
public void setNumber(String number) {
|
||||
this.number = number;
|
||||
}
|
||||
public List<Computer> getComputers() {
|
||||
|
@ -0,0 +1,7 @@
|
||||
package com.kalyshev.yan.cabinet.repository;
|
||||
|
||||
public class CabinetNotFoundException extends RuntimeException {
|
||||
public CabinetNotFoundException(Long id) {
|
||||
super(String.format("Cabinet with id [%s] is not found", id));
|
||||
}
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package com.kalyshev.yan.cabinet.repository;
|
||||
|
||||
import com.kalyshev.yan.cabinet.model.Cabinet;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
public interface CabinetRepository extends JpaRepository<Cabinet, Long> {
|
||||
}
|
@ -1,6 +1,11 @@
|
||||
package com.kalyshev.yan.cabinet.service;
|
||||
|
||||
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.util.validation.ValidatorUtil;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.StringUtils;
|
||||
@ -10,37 +15,38 @@ import jakarta.persistence.EntityManager;
|
||||
import jakarta.persistence.EntityNotFoundException;
|
||||
import jakarta.persistence.PersistenceContext;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@Service
|
||||
public class CabinetService {
|
||||
@PersistenceContext
|
||||
private EntityManager em;
|
||||
|
||||
private final CabinetRepository cabinetRepository;
|
||||
private final ComputerRepository computerRepository;
|
||||
private final ValidatorUtil validatorUtil;
|
||||
public CabinetService(CabinetRepository cabinetRepository,
|
||||
ComputerRepository computerRepository,
|
||||
ValidatorUtil validatorUtil) {
|
||||
this.cabinetRepository = cabinetRepository;
|
||||
this.computerRepository = computerRepository;
|
||||
this.validatorUtil = validatorUtil;
|
||||
}
|
||||
@Transactional
|
||||
public Cabinet addCabinet(String number) {
|
||||
if (!StringUtils.hasText(number)) {
|
||||
throw new IllegalArgumentException("Cabinet number is null or empty");
|
||||
}
|
||||
final Cabinet cabinet = new Cabinet(number);
|
||||
em.persist(cabinet);
|
||||
return cabinet;
|
||||
validatorUtil.validate(cabinet);
|
||||
return cabinetRepository.save(cabinet);
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public Cabinet findCabinet(Long id) {
|
||||
final Cabinet cabinet = em.find(Cabinet.class, id);
|
||||
if (cabinet == null) {
|
||||
throw new EntityNotFoundException(String.format("Cabinet with id [%s] is not found", id));
|
||||
}
|
||||
return cabinet;
|
||||
final Optional<Cabinet> cabinet = cabinetRepository.findById(id);
|
||||
return cabinet.orElseThrow(() -> new CabinetNotFoundException(id));
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<Cabinet> findAllCabinets() {
|
||||
return em.createQuery("select s from Cabinet s", Cabinet.class)
|
||||
.getResultList();
|
||||
return cabinetRepository.findAll();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Cabinet updateCabinet(Long id, String number) {
|
||||
if (!StringUtils.hasText(number)) {
|
||||
@ -48,38 +54,33 @@ public class CabinetService {
|
||||
}
|
||||
final Cabinet currentCabinet = findCabinet(id);
|
||||
currentCabinet.setnumber(number);
|
||||
return em.merge(currentCabinet);
|
||||
validatorUtil.validate(currentCabinet);
|
||||
return cabinetRepository.save(currentCabinet);
|
||||
}
|
||||
|
||||
@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.setCabinet(null);
|
||||
currentCabinet.removeComputer(temp);
|
||||
em.remove(temp);
|
||||
Computer computer = currentCabinet.getComputers().get(i);
|
||||
computer.setCabinet(null);
|
||||
currentCabinet.removeComputer(computer);
|
||||
computerRepository.delete(computer);
|
||||
}
|
||||
em.remove(currentCabinet);
|
||||
cabinetRepository.delete(currentCabinet);
|
||||
return currentCabinet;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void deleteAllCabinets() {
|
||||
em.createQuery("delete from Cabinet").executeUpdate();
|
||||
cabinetRepository.deleteAll();
|
||||
}
|
||||
|
||||
|
||||
@Transactional
|
||||
public List<Computer> listComputers(Long id) {
|
||||
if ((Object)id == null) {
|
||||
throw new IllegalArgumentException("Cabinet id is null or empty");
|
||||
}
|
||||
final Cabinet cabinet = em.find(Cabinet.class, id);
|
||||
return cabinet.getComputers();
|
||||
return findCabinet(id).getComputers();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Computer addComputer(Long computerId, Long cabinetId) {
|
||||
if ((Object)computerId == null) {
|
||||
@ -88,12 +89,12 @@ public class CabinetService {
|
||||
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);
|
||||
final Computer computer = computerRepository.findById(computerId)
|
||||
.orElseThrow(() -> new ComputerNotFoundException(computerId));
|
||||
final Cabinet cabinet = findCabinet(cabinetId);
|
||||
cabinet.addComputer(computer);
|
||||
return computer;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Computer deleteComputer(Long computerId, Long cabinetId) {
|
||||
if ((Object)computerId == null) {
|
||||
@ -102,8 +103,9 @@ public class CabinetService {
|
||||
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);
|
||||
final Computer computer = computerRepository.findById(computerId)
|
||||
.orElseThrow(() -> new ComputerNotFoundException(computerId));
|
||||
final Cabinet cabinet = findCabinet(cabinetId);
|
||||
cabinet.removeComputer(computer);
|
||||
return computer;
|
||||
}
|
||||
|
@ -1,68 +1,53 @@
|
||||
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 org.springframework.web.bind.annotation.DeleteMapping;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PatchMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
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;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/computer")
|
||||
@RequestMapping(WebConfiguration.REST_API + "/computer")
|
||||
public class ComputerController {
|
||||
private final ComputerService computerService;
|
||||
|
||||
public ComputerController(ComputerService computerService) {
|
||||
this.computerService = computerService;
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public Computer getStudent(@PathVariable Long id) {
|
||||
return computerService.findComputer(id);
|
||||
public ComputerDto getStudent(@PathVariable Long id) {
|
||||
return new ComputerDto(computerService.findComputer(id));
|
||||
}
|
||||
|
||||
@GetMapping("/")
|
||||
public List<Computer> getComputers() {
|
||||
return computerService.findAllComputers();
|
||||
public List<ComputerDto> getComputers() {
|
||||
return computerService.findAllComputers().stream()
|
||||
.map(ComputerDto::new)
|
||||
.toList();
|
||||
}
|
||||
|
||||
@GetMapping("/{id}/computers")
|
||||
public Cabinet getComputersCabinet(@PathVariable Long id) {
|
||||
return computerService.getCabinet(id);
|
||||
public CabinetDto getComputersCabinet(@PathVariable Long id) {
|
||||
return new CabinetDto(computerService.getCabinet(id));
|
||||
}
|
||||
|
||||
@PostMapping("/")
|
||||
public Computer createComputer(@RequestParam("modelName") String modelName,
|
||||
@RequestParam("serialNum") String serialNum,
|
||||
@RequestParam("monitorId") Long monitorId,
|
||||
@RequestParam("cabinetId") Long cabinetId) {
|
||||
return computerService.addComputer(modelName, serialNum, monitorId, cabinetId);
|
||||
public ComputerDto createComputer(@RequestBody @Valid ComputerDto computerDto) {
|
||||
return new ComputerDto(computerService.addComputer(computerDto.getModelName(), computerDto.getSerialNum(), computerDto.getMonitorId(), computerDto.getCabinetId()));
|
||||
}
|
||||
|
||||
@PostMapping("/cabinet")
|
||||
public Cabinet setCabinetComputer(@RequestParam("computerId") Long computerId,
|
||||
public CabinetDto setCabinetComputer(@RequestParam("computerId") Long computerId,
|
||||
@RequestParam("cabinetId") Long cabinetId) {
|
||||
return computerService.setCabinet(cabinetId, computerId);
|
||||
return new CabinetDto(computerService.setCabinet(cabinetId, computerId));
|
||||
}
|
||||
|
||||
@PatchMapping("/{id}")
|
||||
public Computer updateComputer(@PathVariable Long id,
|
||||
@RequestParam("modelName") String modelName,
|
||||
@RequestParam("serialNum") String serialNum,
|
||||
@RequestParam("monitorId") Long monitorId,
|
||||
@RequestParam("cabinetId") Long cabinetId) {
|
||||
return computerService.updateComputer(id, modelName, serialNum, monitorId, cabinetId);
|
||||
@PutMapping("/{id}")
|
||||
public ComputerDto updateComputer(@PathVariable Long id,
|
||||
@RequestBody @Valid ComputerDto computerDto) {
|
||||
return new ComputerDto(computerService.updateComputer(id, computerDto.getModelName(), computerDto.getSerialNum(), computerDto.getMonitorId(), computerDto.getCabinetId()));
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
public Computer deleteComputer(@PathVariable Long id) {
|
||||
return computerService.deleteComputer(id);
|
||||
public ComputerDto deleteComputer(@PathVariable Long id) {
|
||||
return new ComputerDto(computerService.deleteComputer(id));
|
||||
}
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
package com.kalyshev.yan.computer.controller;
|
||||
|
||||
import com.kalyshev.yan.computer.model.Computer;
|
||||
|
||||
public class ComputerDto {
|
||||
private Long id;
|
||||
private String modelName;
|
||||
private String serialNum;
|
||||
private Long monitorId;
|
||||
private Long cabinetId;
|
||||
public ComputerDto() {}
|
||||
public ComputerDto(Computer computer) {
|
||||
this.id = computer.getId();
|
||||
this.modelName = computer.getModelName();
|
||||
this.serialNum = computer.getSerialNum();
|
||||
this.monitorId = computer.getMonitor().getId();
|
||||
this.cabinetId = computer.getCabinet().getId();
|
||||
}
|
||||
public Long getId() { return this.id; }
|
||||
public String getModelName() { return this.modelName; }
|
||||
public void setModelName(String modelName) { this.modelName = modelName; }
|
||||
public String getSerialNum() { return this.serialNum; }
|
||||
public void setSerialNum(String serialNum) { this.serialNum = serialNum; }
|
||||
public Long getMonitorId() { return this.monitorId; }
|
||||
public void setMonitorId(Long monitorId) { this.monitorId = monitorId; }
|
||||
public Long getCabinetId() { return this.cabinetId; }
|
||||
public void setCabinetId(Long cabinetId) { this.cabinetId = cabinetId; }
|
||||
}
|
@ -33,16 +33,16 @@ public class Computer {
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
public String getmodelName() {
|
||||
public String getModelName() {
|
||||
return modelName;
|
||||
}
|
||||
public void setmodelName(String modelName) {
|
||||
public void setModelName(String modelName) {
|
||||
this.modelName = modelName;
|
||||
}
|
||||
public String getserialNum() {
|
||||
public String getSerialNum() {
|
||||
return serialNum;
|
||||
}
|
||||
public void setserialNum(String serialNum) { this.serialNum = serialNum; }
|
||||
public void setSerialNum(String serialNum) { this.serialNum = serialNum; }
|
||||
public Cabinet getCabinet() { return cabinet; }
|
||||
public void setCabinet(Cabinet cabinet) { this.cabinet = cabinet; }
|
||||
|
||||
|
@ -0,0 +1,7 @@
|
||||
package com.kalyshev.yan.computer.repository;
|
||||
|
||||
public class ComputerNotFoundException extends RuntimeException {
|
||||
public ComputerNotFoundException(Long id) {
|
||||
super(String.format("Computer with id [%s] is not found", id));
|
||||
}
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package com.kalyshev.yan.computer.repository;
|
||||
|
||||
import com.kalyshev.yan.computer.model.Computer;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
public interface ComputerRepository extends JpaRepository<Computer, Long> {
|
||||
}
|
@ -1,25 +1,39 @@
|
||||
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.repository.ComputerNotFoundException;
|
||||
import com.kalyshev.yan.computer.repository.ComputerRepository;
|
||||
import com.kalyshev.yan.monitor.model.Monitor;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import com.kalyshev.yan.monitor.repository.MonitorNotFoundException;
|
||||
import com.kalyshev.yan.monitor.repository.MonitorRepository;
|
||||
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.computer.model.Computer;
|
||||
|
||||
import jakarta.persistence.EntityManager;
|
||||
import jakarta.persistence.EntityNotFoundException;
|
||||
import jakarta.persistence.PersistenceContext;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@Service
|
||||
public class ComputerService {
|
||||
private static final Logger log = LoggerFactory.getLogger(ComputerService.class);
|
||||
@PersistenceContext
|
||||
private EntityManager em;
|
||||
|
||||
private final CabinetRepository cabinetRepository;
|
||||
private final ComputerRepository computerRepository;
|
||||
private final MonitorRepository monitorRepository;
|
||||
private final ValidatorUtil validatorUtil;
|
||||
public ComputerService(CabinetRepository cabinetRepository,
|
||||
ComputerRepository computerRepository,
|
||||
MonitorRepository monitorRepository,
|
||||
ValidatorUtil validatorUtil) {
|
||||
this.cabinetRepository = cabinetRepository;
|
||||
this.computerRepository = computerRepository;
|
||||
this.monitorRepository = monitorRepository;
|
||||
this.validatorUtil = validatorUtil;
|
||||
}
|
||||
@Transactional
|
||||
public Computer addComputer(String modelName, String serialNum, Long monitorId, Long cabinetId) {
|
||||
if (!StringUtils.hasText(modelName)) {
|
||||
@ -30,38 +44,27 @@ public class ComputerService {
|
||||
}
|
||||
final Computer computer = new Computer(modelName, serialNum);
|
||||
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));
|
||||
}
|
||||
final Monitor monitor = monitorRepository.findById(monitorId)
|
||||
.orElseThrow(() -> new ComputerNotFoundException(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));
|
||||
}
|
||||
final Cabinet cabinet = cabinetRepository.findById(cabinetId)
|
||||
.orElseThrow(() -> new CabinetNotFoundException(cabinetId));
|
||||
computer.setCabinet(cabinet);
|
||||
}
|
||||
em.persist(computer);
|
||||
return computer;
|
||||
validatorUtil.validate(computer);
|
||||
return computerRepository.save(computer);
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
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] not found", id));
|
||||
}
|
||||
return computer;
|
||||
final Optional<Computer> computer = computerRepository.findById(id);
|
||||
return computer.orElseThrow(() -> new ComputerNotFoundException(id));
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<Computer> findAllComputers() {
|
||||
return em.createQuery("select s from Computer s", Computer.class)
|
||||
.getResultList();
|
||||
return computerRepository.findAll();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Computer updateComputer(Long id, String modelName, String serialNum, Long monitorId, Long cabinetId) {
|
||||
if (!(StringUtils.hasText(modelName) || StringUtils.hasText(serialNum))) {
|
||||
@ -75,16 +78,18 @@ public class ComputerService {
|
||||
currentComputer.setserialNum(serialNum);
|
||||
}
|
||||
if (monitorId != null) {
|
||||
Monitor monitor = em.find(Monitor.class, monitorId);
|
||||
Monitor monitor = monitorRepository.findById(monitorId)
|
||||
.orElseThrow(() -> new MonitorNotFoundException(id));
|
||||
currentComputer.setMonitor(monitor);
|
||||
}
|
||||
if (cabinetId != null) {
|
||||
Cabinet cabinet = em.find(Cabinet.class, cabinetId);
|
||||
Cabinet cabinet = cabinetRepository.findById(cabinetId)
|
||||
.orElseThrow(() -> new CabinetNotFoundException(cabinetId));
|
||||
currentComputer.setCabinet(cabinet);
|
||||
}
|
||||
return em.merge(currentComputer);
|
||||
validatorUtil.validate(currentComputer);
|
||||
return computerRepository.save(currentComputer);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Computer deleteComputer(Long id) {
|
||||
final Computer currentComputer = findComputer(id);
|
||||
@ -92,24 +97,24 @@ public class ComputerService {
|
||||
if (currentComputerCabinet != null) {
|
||||
currentComputerCabinet.removeComputer(currentComputer);
|
||||
}
|
||||
em.remove(currentComputer);
|
||||
computerRepository.delete(currentComputer);
|
||||
return currentComputer;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void deleteAllComputers() {
|
||||
em.createQuery("delete from Computer").executeUpdate();
|
||||
List<Computer> computers = findAllComputers();
|
||||
for (Computer computer : computers) {
|
||||
deleteComputer(computer.getId());
|
||||
}
|
||||
}
|
||||
|
||||
@Transactional
|
||||
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);
|
||||
final Computer computer = findComputer(id);
|
||||
return computer.getCabinet();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Cabinet setCabinet(Long cabinetId, Long computerId) {
|
||||
if ((Object)computerId == null) {
|
||||
@ -118,8 +123,9 @@ public class ComputerService {
|
||||
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);
|
||||
final Computer computer = findComputer(computerId);
|
||||
final Cabinet cabinet = cabinetRepository.findById(cabinetId)
|
||||
.orElseThrow(() -> new CabinetNotFoundException(cabinetId));
|
||||
computer.setCabinet(cabinet);
|
||||
return cabinet;
|
||||
}
|
||||
|
@ -1,50 +1,41 @@
|
||||
package com.kalyshev.yan.monitor.controller;
|
||||
|
||||
import com.kalyshev.yan.WebConfiguration;
|
||||
import com.kalyshev.yan.monitor.model.Monitor;
|
||||
import com.kalyshev.yan.monitor.service.MonitorService;
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PatchMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import jakarta.validation.Valid;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/monitor")
|
||||
@RequestMapping(WebConfiguration.REST_API + "/monitor")
|
||||
public class MonitorController {
|
||||
private final MonitorService monitorService;
|
||||
|
||||
public MonitorController(MonitorService monitorService) {
|
||||
this.monitorService = monitorService;
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public Monitor getMonitor(@PathVariable Long id) {
|
||||
return monitorService.findMonitor(id);
|
||||
public MonitorDto getMonitor(@PathVariable Long id) {
|
||||
return new MonitorDto(monitorService.findMonitor(id));
|
||||
}
|
||||
|
||||
@GetMapping("/")
|
||||
public List<Monitor> getMonitors() {
|
||||
return monitorService.findAllMonitors();
|
||||
public List<MonitorDto> getMonitors() {
|
||||
return monitorService.findAllMonitors().stream()
|
||||
.map(MonitorDto::new)
|
||||
.toList();
|
||||
}
|
||||
|
||||
@PostMapping("/")
|
||||
public Monitor createMonitor(@RequestParam("modelName") String modelName) {
|
||||
return monitorService.addMonitor(modelName);
|
||||
public MonitorDto createMonitor(@RequestBody @Valid MonitorDto monitorDto) {
|
||||
return new MonitorDto(monitorService.addMonitor(monitorDto.getModelName()));
|
||||
}
|
||||
|
||||
@PatchMapping("/{id}")
|
||||
public Monitor updateMonitor(@PathVariable Long id,
|
||||
@RequestParam("modelName") String modelName) {
|
||||
return monitorService.updateMonitor(id, modelName);
|
||||
@PutMapping("/{id}")
|
||||
public MonitorDto updateMonitor(@PathVariable Long id,
|
||||
@RequestBody @Valid MonitorDto monitorDto) {
|
||||
return new MonitorDto(monitorService.updateMonitor(id, monitorDto.getModelName()));
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
public Monitor deleteMonitor(@PathVariable Long id) {
|
||||
return monitorService.deleteMonitor(id);
|
||||
public MonitorDto deleteMonitor(@PathVariable Long id) {
|
||||
return new MonitorDto(monitorService.deleteMonitor(id));
|
||||
}
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
package com.kalyshev.yan.monitor.controller;
|
||||
|
||||
import com.kalyshev.yan.monitor.model.Monitor;
|
||||
|
||||
public class MonitorDto {
|
||||
private Long id;
|
||||
private String modelName;
|
||||
public MonitorDto() {
|
||||
}
|
||||
public MonitorDto(Monitor monitor) {
|
||||
this.id = monitor.getId();
|
||||
this.modelName = monitor.getModelName();
|
||||
}
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
public String getModelName() {
|
||||
return modelName;
|
||||
}
|
||||
public void setModelName(String modelName) {
|
||||
this.modelName = modelName;
|
||||
}
|
||||
}
|
@ -13,23 +13,20 @@ public class Monitor {
|
||||
@GeneratedValue
|
||||
private Long id;
|
||||
private String modelName;
|
||||
|
||||
public Monitor() {
|
||||
}
|
||||
public Monitor(String modelName) {
|
||||
this.modelName = modelName;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
public String getmodelName() {
|
||||
public String getModelName() {
|
||||
return modelName;
|
||||
}
|
||||
public void setmodelName(String modelName) {
|
||||
public void setModelName(String modelName) {
|
||||
this.modelName = modelName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
@ -37,12 +34,10 @@ public class Monitor {
|
||||
Monitor computer = (Monitor) o;
|
||||
return Objects.equals(id, computer.id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Monitor{" +
|
||||
|
@ -0,0 +1,7 @@
|
||||
package com.kalyshev.yan.monitor.repository;
|
||||
|
||||
public class MonitorNotFoundException extends RuntimeException {
|
||||
public MonitorNotFoundException(Long id) {
|
||||
super(String.format("Monitor with id [%s] is not found", id));
|
||||
}
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package com.kalyshev.yan.monitor.repository;
|
||||
|
||||
import com.kalyshev.yan.monitor.model.Monitor;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
public interface MonitorRepository extends JpaRepository<Monitor, Long> {
|
||||
}
|
@ -1,6 +1,13 @@
|
||||
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.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;
|
||||
@ -14,72 +21,72 @@ import jakarta.persistence.EntityNotFoundException;
|
||||
import jakarta.persistence.PersistenceContext;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
|
||||
@Service
|
||||
public class MonitorService {
|
||||
private static final Logger log = LoggerFactory.getLogger(MonitorService.class);
|
||||
@PersistenceContext
|
||||
private EntityManager em;
|
||||
|
||||
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
|
||||
public Monitor addMonitor(String modelName) {
|
||||
if (!StringUtils.hasText(modelName)) {
|
||||
throw new IllegalArgumentException("Monitor model name is null or empty");
|
||||
}
|
||||
final Monitor monitor = new Monitor(modelName);
|
||||
em.persist(monitor);
|
||||
return monitor;
|
||||
validatorUtil.validate(monitor);
|
||||
return monitorRepository.save(monitor);
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public Monitor findMonitor(Long id) {
|
||||
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 monitor;
|
||||
final Optional<Monitor> monitor = monitorRepository.findById(id);
|
||||
return monitor.orElseThrow(() -> new MonitorNotFoundException(id));
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<Monitor> findAllMonitors() {
|
||||
return em.createQuery("select s from Monitor s", Monitor.class)
|
||||
.getResultList();
|
||||
return monitorRepository.findAll();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Monitor updateMonitor(Long id, String modelName) {
|
||||
if (!StringUtils.hasText(modelName)) {
|
||||
throw new IllegalArgumentException("Monitor model name is null or empty");
|
||||
}
|
||||
final Monitor currentMonitor = findMonitor(id);
|
||||
currentMonitor.setmodelName(modelName);
|
||||
return em.merge(currentMonitor);
|
||||
currentMonitor.setModelName(modelName);
|
||||
validatorUtil.validate(currentMonitor);
|
||||
return monitorRepository.save(currentMonitor);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Monitor deleteMonitor(Long id) {
|
||||
final Monitor currentMonitor = findMonitor(id);
|
||||
em.remove(currentMonitor);
|
||||
Computer computer = getComputer(id);
|
||||
computer.removeMonitor();
|
||||
monitorRepository.delete(currentMonitor);
|
||||
return currentMonitor;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void deleteAllMonitors() {
|
||||
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());
|
||||
List<Computer> computers = computerRepository.findAll();
|
||||
for (Computer computer : computers) {
|
||||
computer.removeMonitor();
|
||||
}
|
||||
monitorRepository.deleteAll();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Computer getComputer(Long id) {
|
||||
Computer computer = em.createQuery("select s from Computer s where s.monitor.id = :id", Computer.class)
|
||||
.setParameter("id", id)
|
||||
.getSingleResult();
|
||||
return computer;
|
||||
List<Computer> computers = computerRepository.findAll();
|
||||
for (Computer computer : computers) {
|
||||
if (Objects.equals(computer.getMonitor().getId(), id)) {
|
||||
return computer;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
package com.kalyshev.yan.util.error;
|
||||
|
||||
import com.kalyshev.yan.cabinet.repository.CabinetNotFoundException;
|
||||
import com.kalyshev.yan.util.validation.ValidationException;
|
||||
import org.springframework.context.support.DefaultMessageSourceResolvable;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.MethodArgumentNotValidException;
|
||||
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@ControllerAdvice(annotations = RestController.class)
|
||||
public class AdviceController {
|
||||
@ExceptionHandler({
|
||||
CabinetNotFoundException.class,
|
||||
ValidationException.class
|
||||
})
|
||||
public ResponseEntity<Object> handleException(Throwable e) {
|
||||
return new ResponseEntity<>(e.getMessage(), HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
@ExceptionHandler(MethodArgumentNotValidException.class)
|
||||
public ResponseEntity<Object> handleBindException(MethodArgumentNotValidException e) {
|
||||
final ValidationException validationException = new ValidationException(
|
||||
e.getBindingResult().getAllErrors().stream()
|
||||
.map(DefaultMessageSourceResolvable::getDefaultMessage)
|
||||
.collect(Collectors.toSet()));
|
||||
return handleException(validationException);
|
||||
}
|
||||
@ExceptionHandler(Exception.class)
|
||||
public ResponseEntity<Object> handleUnknownException(Throwable e) {
|
||||
e.printStackTrace();
|
||||
return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
package com.kalyshev.yan.util.validation;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
public class ValidationException extends RuntimeException {
|
||||
public <T> ValidationException(Set<String> errors) {
|
||||
super(String.join("\n", errors));
|
||||
}
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
package com.kalyshev.yan.util.validation;
|
||||
|
||||
import jakarta.validation.ConstraintViolation;
|
||||
import jakarta.validation.Validation;
|
||||
import jakarta.validation.Validator;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Component
|
||||
public class ValidatorUtil {
|
||||
private final Validator validator;
|
||||
public ValidatorUtil() {
|
||||
this.validator = Validation.buildDefaultValidatorFactory().getValidator();
|
||||
}
|
||||
public <T> void validate(T object) {
|
||||
final Set<ConstraintViolation<T>> errors = validator.validate(object);
|
||||
if (!errors.isEmpty()) {
|
||||
throw new ValidationException(errors.stream()
|
||||
.map(ConstraintViolation::getMessage)
|
||||
.collect(Collectors.toSet()));
|
||||
}
|
||||
}
|
||||
}
|
@ -34,7 +34,7 @@ public class JpaComputerCabinetTests {
|
||||
cabinet.addComputer(computer);
|
||||
log.info(cabinet.toString());
|
||||
log.info(computer.toString());
|
||||
Assertions.assertEquals(cabinet.getComputers().get(0).getserialNum(), computer.getserialNum());
|
||||
Assertions.assertEquals(cabinet.getComputers().get(0).getSerialNum(), computer.getSerialNum());
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -48,7 +48,7 @@ public class JpaComputerCabinetTests {
|
||||
cabinet.addComputer(computer);
|
||||
log.info(cabinet.toString());
|
||||
log.info(computer.toString());
|
||||
Assertions.assertEquals(cabinet.getComputers().get(0).getserialNum(), computer.getserialNum());
|
||||
Assertions.assertEquals(cabinet.getComputers().get(0).getSerialNum(), computer.getSerialNum());
|
||||
cabinet.removeComputer(computer);
|
||||
Assertions.assertEquals(cabinet.getComputers().size(), 0);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user