Loading core/src/main/java/cz/muni/fi/pa165/core/common/DomainService.java +10 −0 Original line number Diff line number Diff line Loading @@ -95,6 +95,16 @@ public abstract class DomainService<T extends DomainObject> { getRepository().saveAll(entities); } /** * Deletes all entities by array of id's */ @Transactional public void deleteAllById(String[] ids) { for (String id: ids) { deleteById(id); } } /** * Deletes the entity with the given ID by setting its deletion datetime. * Loading core/src/main/java/cz/muni/fi/pa165/core/device/DeviceController.java +8 −2 Original line number Diff line number Diff line package cz.muni.fi.pa165.core.device; import cz.muni.fi.pa165.core.helpers.exceptions.EntityDeletionException; import cz.muni.fi.pa165.model.dto.device.DeviceCreateDto; import cz.muni.fi.pa165.model.dto.device.DeviceDto; import cz.muni.fi.pa165.model.dto.device.DeviceUpdateDto; Loading @@ -8,6 +9,9 @@ import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.tags.Tag; import jakarta.validation.Valid; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatusCode; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; Loading @@ -19,6 +23,8 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.data.domain.Pageable; import javax.swing.*; import java.util.List; @RestController Loading Loading @@ -80,8 +86,8 @@ public class DeviceController { summary = "Delete device", description = "Deletes the device with the specified id.") @DeleteMapping("/{id}") public DeviceDto deleteById( public ResponseEntity<DeviceDto> deleteById( @PathVariable @Parameter(description = "The id of the device.") String id) { return deviceFacade.deleteById(id); return new ResponseEntity<>(deviceFacade.deleteById(id), HttpStatus.OK); } } core/src/main/java/cz/muni/fi/pa165/core/device/DeviceFacade.java +12 −0 Original line number Diff line number Diff line package cz.muni.fi.pa165.core.device; import cz.muni.fi.pa165.core.common.DomainFacade; import cz.muni.fi.pa165.core.helpers.exceptions.EntityDeletionException; import cz.muni.fi.pa165.model.dto.device.DeviceCreateDto; import cz.muni.fi.pa165.model.dto.device.DeviceDto; import cz.muni.fi.pa165.model.dto.device.DeviceUpdateDto; import lombok.SneakyThrows; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; Loading @@ -17,4 +19,14 @@ public class DeviceFacade extends DomainFacade<Device, DeviceDto, DeviceCreateDt super(deviceService, deviceMapper); this.deviceService = deviceService; } @SneakyThrows @Override public DeviceDto deleteById(String id) { Device device = deviceService.findById(id); if (!device.getSmartMeterList().isEmpty()){ throw new EntityDeletionException("Entity has some data"); } return new DeviceDto(); } } core/src/main/java/cz/muni/fi/pa165/core/helpers/exceptions/EntityDeletionException.java 0 → 100644 +7 −0 Original line number Diff line number Diff line package cz.muni.fi.pa165.core.helpers.exceptions; public class EntityDeletionException extends Exception{ public EntityDeletionException(String message) { super(message); } } core/src/main/java/cz/muni/fi/pa165/core/metrics/MetricsFacade.java +15 −1 Original line number Diff line number Diff line package cz.muni.fi.pa165.core.metrics; import cz.muni.fi.pa165.core.common.DomainFacade; import cz.muni.fi.pa165.core.smartmeter.SmartMeter; import cz.muni.fi.pa165.core.smartmeter.SmartMeterService; import cz.muni.fi.pa165.model.dto.metrics.MetricsCreateDto; import cz.muni.fi.pa165.model.dto.metrics.MetricsDto; import cz.muni.fi.pa165.model.dto.metrics.MetricsUpdateDto; Loading @@ -13,10 +15,22 @@ public class MetricsFacade extends DomainFacade<Metrics, MetricsCreateDto, MetricsUpdateDto> { private MetricsService metricsService; // For the "MetricsService" specific methods private final SmartMeterService smartMeterService; @Autowired public MetricsFacade(MetricsService metricsService, MetricsMapper metricsMapper) { MetricsMapper metricsMapper, SmartMeterService smartMeterService) { super(metricsService, metricsMapper); this.metricsService = metricsService; this.smartMeterService = smartMeterService; } @Override public MetricsDto create(MetricsCreateDto metricsCreateDto){ Metrics metrics = mapper.fromCreateDto(metricsCreateDto); SmartMeter smartMeter = smartMeterService.findById(metricsCreateDto.getSmartMeterId()); metrics.setSmartMeter(smartMeter); Metrics metricsCreated = metricsService.create(metrics); return mapper.toDto(metricsCreated); } } Loading
core/src/main/java/cz/muni/fi/pa165/core/common/DomainService.java +10 −0 Original line number Diff line number Diff line Loading @@ -95,6 +95,16 @@ public abstract class DomainService<T extends DomainObject> { getRepository().saveAll(entities); } /** * Deletes all entities by array of id's */ @Transactional public void deleteAllById(String[] ids) { for (String id: ids) { deleteById(id); } } /** * Deletes the entity with the given ID by setting its deletion datetime. * Loading
core/src/main/java/cz/muni/fi/pa165/core/device/DeviceController.java +8 −2 Original line number Diff line number Diff line package cz.muni.fi.pa165.core.device; import cz.muni.fi.pa165.core.helpers.exceptions.EntityDeletionException; import cz.muni.fi.pa165.model.dto.device.DeviceCreateDto; import cz.muni.fi.pa165.model.dto.device.DeviceDto; import cz.muni.fi.pa165.model.dto.device.DeviceUpdateDto; Loading @@ -8,6 +9,9 @@ import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.tags.Tag; import jakarta.validation.Valid; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatusCode; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; Loading @@ -19,6 +23,8 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.data.domain.Pageable; import javax.swing.*; import java.util.List; @RestController Loading Loading @@ -80,8 +86,8 @@ public class DeviceController { summary = "Delete device", description = "Deletes the device with the specified id.") @DeleteMapping("/{id}") public DeviceDto deleteById( public ResponseEntity<DeviceDto> deleteById( @PathVariable @Parameter(description = "The id of the device.") String id) { return deviceFacade.deleteById(id); return new ResponseEntity<>(deviceFacade.deleteById(id), HttpStatus.OK); } }
core/src/main/java/cz/muni/fi/pa165/core/device/DeviceFacade.java +12 −0 Original line number Diff line number Diff line package cz.muni.fi.pa165.core.device; import cz.muni.fi.pa165.core.common.DomainFacade; import cz.muni.fi.pa165.core.helpers.exceptions.EntityDeletionException; import cz.muni.fi.pa165.model.dto.device.DeviceCreateDto; import cz.muni.fi.pa165.model.dto.device.DeviceDto; import cz.muni.fi.pa165.model.dto.device.DeviceUpdateDto; import lombok.SneakyThrows; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; Loading @@ -17,4 +19,14 @@ public class DeviceFacade extends DomainFacade<Device, DeviceDto, DeviceCreateDt super(deviceService, deviceMapper); this.deviceService = deviceService; } @SneakyThrows @Override public DeviceDto deleteById(String id) { Device device = deviceService.findById(id); if (!device.getSmartMeterList().isEmpty()){ throw new EntityDeletionException("Entity has some data"); } return new DeviceDto(); } }
core/src/main/java/cz/muni/fi/pa165/core/helpers/exceptions/EntityDeletionException.java 0 → 100644 +7 −0 Original line number Diff line number Diff line package cz.muni.fi.pa165.core.helpers.exceptions; public class EntityDeletionException extends Exception{ public EntityDeletionException(String message) { super(message); } }
core/src/main/java/cz/muni/fi/pa165/core/metrics/MetricsFacade.java +15 −1 Original line number Diff line number Diff line package cz.muni.fi.pa165.core.metrics; import cz.muni.fi.pa165.core.common.DomainFacade; import cz.muni.fi.pa165.core.smartmeter.SmartMeter; import cz.muni.fi.pa165.core.smartmeter.SmartMeterService; import cz.muni.fi.pa165.model.dto.metrics.MetricsCreateDto; import cz.muni.fi.pa165.model.dto.metrics.MetricsDto; import cz.muni.fi.pa165.model.dto.metrics.MetricsUpdateDto; Loading @@ -13,10 +15,22 @@ public class MetricsFacade extends DomainFacade<Metrics, MetricsCreateDto, MetricsUpdateDto> { private MetricsService metricsService; // For the "MetricsService" specific methods private final SmartMeterService smartMeterService; @Autowired public MetricsFacade(MetricsService metricsService, MetricsMapper metricsMapper) { MetricsMapper metricsMapper, SmartMeterService smartMeterService) { super(metricsService, metricsMapper); this.metricsService = metricsService; this.smartMeterService = smartMeterService; } @Override public MetricsDto create(MetricsCreateDto metricsCreateDto){ Metrics metrics = mapper.fromCreateDto(metricsCreateDto); SmartMeter smartMeter = smartMeterService.findById(metricsCreateDto.getSmartMeterId()); metrics.setSmartMeter(smartMeter); Metrics metricsCreated = metricsService.create(metrics); return mapper.toDto(metricsCreated); } }