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

feat: create entities with dependencies 08/04/23

parent 3e60f409
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> { ...@@ -95,6 +95,16 @@ public abstract class DomainService<T extends DomainObject> {
getRepository().saveAll(entities); 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. * Deletes the entity with the given ID by setting its deletion datetime.
* *
......
package cz.muni.fi.pa165.core.device; 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.DeviceCreateDto;
import cz.muni.fi.pa165.model.dto.device.DeviceDto; import cz.muni.fi.pa165.model.dto.device.DeviceDto;
import cz.muni.fi.pa165.model.dto.device.DeviceUpdateDto; import cz.muni.fi.pa165.model.dto.device.DeviceUpdateDto;
...@@ -8,6 +9,9 @@ import io.swagger.v3.oas.annotations.Parameter; ...@@ -8,6 +9,9 @@ import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag; import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid; import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired; 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.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
...@@ -19,6 +23,8 @@ import org.springframework.web.bind.annotation.RequestMapping; ...@@ -19,6 +23,8 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Pageable;
import javax.swing.*;
import java.util.List; import java.util.List;
@RestController @RestController
...@@ -80,8 +86,8 @@ public class DeviceController { ...@@ -80,8 +86,8 @@ public class DeviceController {
summary = "Delete device", summary = "Delete device",
description = "Deletes the device with the specified id.") description = "Deletes the device with the specified id.")
@DeleteMapping("/{id}") @DeleteMapping("/{id}")
public DeviceDto deleteById( public ResponseEntity<DeviceDto> deleteById(
@PathVariable @Parameter(description = "The id of the device.") String id) { @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; package cz.muni.fi.pa165.core.device;
import cz.muni.fi.pa165.core.common.DomainFacade; 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.DeviceCreateDto;
import cz.muni.fi.pa165.model.dto.device.DeviceDto; import cz.muni.fi.pa165.model.dto.device.DeviceDto;
import cz.muni.fi.pa165.model.dto.device.DeviceUpdateDto; import cz.muni.fi.pa165.model.dto.device.DeviceUpdateDto;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
...@@ -17,4 +19,14 @@ public class DeviceFacade extends DomainFacade<Device, DeviceDto, DeviceCreateDt ...@@ -17,4 +19,14 @@ public class DeviceFacade extends DomainFacade<Device, DeviceDto, DeviceCreateDt
super(deviceService, deviceMapper); super(deviceService, deviceMapper);
this.deviceService = deviceService; 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; package cz.muni.fi.pa165.core.metrics;
import cz.muni.fi.pa165.core.common.DomainFacade; 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.MetricsCreateDto;
import cz.muni.fi.pa165.model.dto.metrics.MetricsDto; import cz.muni.fi.pa165.model.dto.metrics.MetricsDto;
import cz.muni.fi.pa165.model.dto.metrics.MetricsUpdateDto; import cz.muni.fi.pa165.model.dto.metrics.MetricsUpdateDto;
...@@ -13,10 +15,22 @@ public class MetricsFacade extends DomainFacade<Metrics, ...@@ -13,10 +15,22 @@ public class MetricsFacade extends DomainFacade<Metrics,
MetricsCreateDto, MetricsCreateDto,
MetricsUpdateDto> { MetricsUpdateDto> {
private MetricsService metricsService; // For the "MetricsService" specific methods private MetricsService metricsService; // For the "MetricsService" specific methods
private final SmartMeterService smartMeterService;
@Autowired @Autowired
public MetricsFacade(MetricsService metricsService, public MetricsFacade(MetricsService metricsService,
MetricsMapper metricsMapper) { MetricsMapper metricsMapper, SmartMeterService smartMeterService) {
super(metricsService, metricsMapper); super(metricsService, metricsMapper);
this.metricsService = metricsService; 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; package cz.muni.fi.pa165.core.smartmeter;
import cz.muni.fi.pa165.core.common.DomainFacade; 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.SmartMeterCreateDto;
import cz.muni.fi.pa165.model.dto.smartDevice.SmartMeterDto; import cz.muni.fi.pa165.model.dto.smartDevice.SmartMeterDto;
import cz.muni.fi.pa165.model.dto.smartDevice.SmartMeterUpdateDto; import cz.muni.fi.pa165.model.dto.smartDevice.SmartMeterUpdateDto;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
@Component @Component
public class SmartMeterFacade extends DomainFacade<SmartMeter, public class SmartMeterFacade extends DomainFacade<SmartMeter,
...@@ -13,12 +17,36 @@ public class SmartMeterFacade extends DomainFacade<SmartMeter, ...@@ -13,12 +17,36 @@ public class SmartMeterFacade extends DomainFacade<SmartMeter,
SmartMeterCreateDto, SmartMeterCreateDto,
SmartMeterUpdateDto> { SmartMeterUpdateDto> {
private final SmartMeterService smartMeterService; // For the "SmartMeterService" specific methods private final SmartMeterService smartMeterService; // For the "SmartMeterService" specific methods
private final DeviceService deviceService;
private final HouseService houseService;
private final MetricsService metricsService;
@Autowired @Autowired
public SmartMeterFacade(SmartMeterService smartMeterService, public SmartMeterFacade(SmartMeterService smartMeterService,
SmartMeterMapper smartMeterMapper SmartMeterMapper smartMeterMapper,
) { DeviceService deviceService, HouseService houseService, MetricsService metricsService) {
super(smartMeterService, smartMeterMapper); super(smartMeterService, smartMeterMapper);
this.smartMeterService = smartMeterService; 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 { ...@@ -131,6 +131,7 @@ public class UserController {
}) })
public UserDto deleteById( public UserDto deleteById(
@Parameter(description = "ID of the user to be deleted") @PathVariable String id) { @Parameter(description = "ID of the user to be deleted") @PathVariable String id) {
return userFacade.deleteById(id); return userFacade.deleteById(id);
} }
......
...@@ -72,7 +72,6 @@ public class RoleController { ...@@ -72,7 +72,6 @@ public class RoleController {
public CompanyRoleDto createCompanyRole( public CompanyRoleDto createCompanyRole(
@Parameter(description = "Role to be created") @RequestBody CompanyRoleDto roleDto) { @Parameter(description = "Role to be created") @RequestBody CompanyRoleDto roleDto) {
return roleFacade.createCompanyRole(roleDto); return roleFacade.createCompanyRole(roleDto);
//return new CompanyRole();
} }
@Operation( @Operation(
......
...@@ -8,4 +8,5 @@ import lombok.Setter; ...@@ -8,4 +8,5 @@ import lombok.Setter;
@Setter @Setter
public class MetricsCreateDto extends DomainObjectDto { public class MetricsCreateDto extends DomainObjectDto {
private double consumptionKWH; private double consumptionKWH;
private String smartMeterId;
} }
...@@ -10,4 +10,6 @@ import lombok.Setter; ...@@ -10,4 +10,6 @@ import lombok.Setter;
@Setter @Setter
public class SmartMeterCreateDto extends DomainObjectDto { public class SmartMeterCreateDto extends DomainObjectDto {
private String name; 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