Skip to content
Snippets Groups Projects
Commit 3d2e9b89 authored by Filip Bugoš's avatar Filip Bugoš
Browse files

Merge branch 'Facades_dtos' into 'milestone-2'

Create entities with dependencies 08/04/23

See merge request !33
parents 3e60f409 095a5506
No related branches found
No related tags found
4 merge requests!79Final merge to main,!43Testing user,!35Deleted manufacturer,!33Create entities with dependencies 08/04/23
Pipeline #
Showing
with 87 additions and 7 deletions
......@@ -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.
*
......
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;
......@@ -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;
......@@ -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
......@@ -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);
}
}
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;
......@@ -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();
}
}
package cz.muni.fi.pa165.core.helpers.exceptions;
public class EntityDeletionException extends Exception{
public EntityDeletionException(String message) {
super(message);
}
}
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;
......@@ -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);
}
}
package cz.muni.fi.pa165.core.smartmeter;
import cz.muni.fi.pa165.core.common.DomainFacade;
import cz.muni.fi.pa165.core.device.DeviceService;
import cz.muni.fi.pa165.core.house.HouseService;
import cz.muni.fi.pa165.core.metrics.MetricsService;
import cz.muni.fi.pa165.model.dto.smartDevice.SmartMeterCreateDto;
import cz.muni.fi.pa165.model.dto.smartDevice.SmartMeterDto;
import cz.muni.fi.pa165.model.dto.smartDevice.SmartMeterUpdateDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
@Component
public class SmartMeterFacade extends DomainFacade<SmartMeter,
......@@ -13,12 +17,36 @@ public class SmartMeterFacade extends DomainFacade<SmartMeter,
SmartMeterCreateDto,
SmartMeterUpdateDto> {
private final SmartMeterService smartMeterService; // For the "SmartMeterService" specific methods
private final DeviceService deviceService;
private final HouseService houseService;
private final MetricsService metricsService;
@Autowired
public SmartMeterFacade(SmartMeterService smartMeterService,
SmartMeterMapper smartMeterMapper
) {
SmartMeterMapper smartMeterMapper,
DeviceService deviceService, HouseService houseService, MetricsService metricsService) {
super(smartMeterService, smartMeterMapper);
this.smartMeterService = smartMeterService;
this.deviceService = deviceService;
this.houseService = houseService;
this.metricsService = metricsService;
}
@Override
public SmartMeterDto create(SmartMeterCreateDto smartMeterCreateDto){
SmartMeter smartMeter = mapper.fromCreateDto(smartMeterCreateDto);
smartMeter.setDevice(deviceService.findById(smartMeterCreateDto.getDeviceId()));
smartMeter.setHouse(houseService.findById(smartMeterCreateDto.getHouseId()));
return mapper.toDto(smartMeterService.create(smartMeter));
}
@Override
@Transactional
public SmartMeterDto deleteById(String id){
// soft delete Smart Devices
SmartMeter smartMeter = smartMeterService.findById(id);
metricsService.deleteAllById((String[]) smartMeter.getMetricsList().stream().map(sm -> smartMeter.getId()).toArray());
SmartMeter smartMeterDeleted = smartMeterService.deleteById(smartMeter.getId());
return mapper.toDto(smartMeterDeleted);
}
}
......@@ -131,6 +131,7 @@ public class UserController {
})
public UserDto deleteById(
@Parameter(description = "ID of the user to be deleted") @PathVariable String id) {
return userFacade.deleteById(id);
}
......
......@@ -72,7 +72,6 @@ public class RoleController {
public CompanyRoleDto createCompanyRole(
@Parameter(description = "Role to be created") @RequestBody CompanyRoleDto roleDto) {
return roleFacade.createCompanyRole(roleDto);
//return new CompanyRole();
}
@Operation(
......
......@@ -8,4 +8,5 @@ import lombok.Setter;
@Setter
public class MetricsCreateDto extends DomainObjectDto {
private double consumptionKWH;
private String smartMeterId;
}
......@@ -10,4 +10,6 @@ import lombok.Setter;
@Setter
public class SmartMeterCreateDto extends DomainObjectDto {
private String name;
private String houseId;
private String deviceId;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment