UI: Create + Modify Property

This commit is contained in:
Patrick Haßel 2021-11-11 23:20:51 +01:00
parent de95a6aed1
commit 644e6ed213
14 changed files with 58 additions and 22 deletions

View File

@ -36,4 +36,8 @@ export class PropertyService implements ISearchService {
this.api.postReturnItem("property/set/" + property.id + "/" + key, value, Property.fromJson, next, error);
}
create(next: (item: Property) => void, error: (error: any) => void = NO_OP): void {
this.api.getItem("property/create/", Property.fromJson, next, error);
}
}

View File

@ -69,8 +69,3 @@
.shutterUnknown {
background-color: gray;
}
.config {
clear: both;
margin-bottom: 5px;
}

View File

@ -2,6 +2,10 @@
<td class="empty">-</td>
</ng-template>
<div class="config">
<button (click)="create()">+ Hinzufügen</button>
</div>
<table>
<tr>
<th>Bezeichnung</th>

View File

@ -73,7 +73,11 @@ export class PropertyListComponent implements OnInit {
}
set(property: Property, key: string, value: any): void {
this.propertyService.set(property, key, value, property => this.updateProperty(property, true));
}
create(): void {
this.propertyService.create(property => this.updateProperty(property, true));
}
}

View File

@ -106,3 +106,7 @@ table.vertical {
}
}
.config {
clear: both;
margin-bottom: 5px;
}

View File

@ -4,4 +4,6 @@ import org.springframework.data.repository.CrudRepository;
public interface ChannelRepository extends CrudRepository<Channel, Long> {
Channel getById(long id);
}

View File

@ -60,4 +60,8 @@ public class ChannelService {
return channelRepository.findById(id).map(this::toDto);
}
public Channel getById(final long id) {
return channelRepository.getById(id);
}
}

View File

@ -8,7 +8,7 @@ import org.springframework.web.bind.annotation.*;
import java.util.List;
import static de.ph87.homeautomation.shared.Helpers.mapIfNotNull;
import static de.ph87.homeautomation.shared.Helpers.mapOrNull;
@RestController
@RequestMapping("device")
@ -53,22 +53,22 @@ public class DeviceController {
@PostMapping("set/{id}/DeviceSwitch/stateProperty")
public DeviceDto setDeviceSwitchStateProperty(@PathVariable final long id, @RequestBody(required = false) final Long propertyId) {
return deviceWriteService.setDeviceSwitch(id, (device, v) -> device.setStateProperty(mapIfNotNull(v, propertyReadService::getById)), propertyId);
return deviceWriteService.setDeviceSwitch(id, (device, v) -> device.setStateProperty(mapOrNull(v, propertyReadService::getById)), propertyId);
}
@PostMapping("set/{id}/DeviceStateScene/stateProperty")
public DeviceDto setDeviceStateSceneStateProperty(@PathVariable final long id, @RequestBody(required = false) final Long propertyId) {
return deviceWriteService.setDeviceStateScene(id, (device, v) -> device.setStateProperty(mapIfNotNull(v, propertyReadService::getById)), propertyId);
return deviceWriteService.setDeviceStateScene(id, (device, v) -> device.setStateProperty(mapOrNull(v, propertyReadService::getById)), propertyId);
}
@PostMapping("set/{id}/DeviceStateScene/sceneProperty")
public DeviceDto setDeviceStateSceneSceneProperty(@PathVariable final long id, @RequestBody(required = false) final Long propertyId) {
return deviceWriteService.setDeviceStateScene(id, (device, v) -> device.setSceneProperty(mapIfNotNull(v, propertyReadService::getById)), propertyId);
return deviceWriteService.setDeviceStateScene(id, (device, v) -> device.setSceneProperty(mapOrNull(v, propertyReadService::getById)), propertyId);
}
@PostMapping("set/{id}/DeviceShutter/positionProperty")
public DeviceDto setDeviceShutterPositionProperty(@PathVariable final long id, @RequestBody(required = false) final Long propertyId) {
return deviceWriteService.setDeviceShutter(id, (device, v) -> device.setPositionProperty(mapIfNotNull(v, propertyReadService::getById)), propertyId);
return deviceWriteService.setDeviceShutter(id, (device, v) -> device.setPositionProperty(mapOrNull(v, propertyReadService::getById)), propertyId);
}
}

View File

@ -11,7 +11,7 @@ import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.stream.Collectors;
import static de.ph87.homeautomation.shared.Helpers.mapIfNotNull;
import static de.ph87.homeautomation.shared.Helpers.mapOrNull;
@Slf4j
@Service
@ -30,13 +30,13 @@ public class DeviceReadService {
public DeviceDto toDto(final Device device) {
if (device instanceof DeviceSwitch) {
final DeviceSwitch deviceSwitch = (DeviceSwitch) device;
return new DeviceSwitchDto(deviceSwitch, mapIfNotNull(deviceSwitch.getStateProperty(), propertyMapper::toDto));
return new DeviceSwitchDto(deviceSwitch, mapOrNull(deviceSwitch.getStateProperty(), propertyMapper::toDto));
} else if (device instanceof DeviceStateScene) {
final DeviceStateScene deviceStateScene = (DeviceStateScene) device;
return new DeviceStateSceneDto(deviceStateScene, mapIfNotNull(deviceStateScene.getStateProperty(), propertyMapper::toDto), mapIfNotNull(deviceStateScene.getSceneProperty(), propertyMapper::toDto));
return new DeviceStateSceneDto(deviceStateScene, mapOrNull(deviceStateScene.getStateProperty(), propertyMapper::toDto), mapOrNull(deviceStateScene.getSceneProperty(), propertyMapper::toDto));
} else if (device instanceof DeviceShutter) {
final DeviceShutter deviceShutter = (DeviceShutter) device;
return new DeviceShutterDto(deviceShutter, mapIfNotNull(deviceShutter.getPositionProperty(), propertyMapper::toDto));
return new DeviceShutterDto(deviceShutter, mapOrNull(deviceShutter.getPositionProperty(), propertyMapper::toDto));
}
throw new RuntimeException();
}

View File

@ -1,5 +1,6 @@
package de.ph87.homeautomation.property;
import de.ph87.homeautomation.channel.ChannelService;
import de.ph87.homeautomation.shared.ISearchController;
import de.ph87.homeautomation.shared.SearchResult;
import lombok.RequiredArgsConstructor;
@ -17,6 +18,13 @@ public class PropertyController implements ISearchController {
private final PropertyReadService propertyReadService;
private final ChannelService channelService;
@GetMapping("create")
public PropertyDto create() {
return propertyWriteService.create();
}
@GetMapping("findAll")
public List<PropertyDto> findAll() {
return propertyReadService.findAllDto();
@ -37,6 +45,16 @@ public class PropertyController implements ISearchController {
return propertyWriteService.set(id, propertyWriteService::write, value);
}
@PostMapping("set/{id}/readChannel")
public PropertyDto setReadChannel(@PathVariable final long id, @RequestBody(required = false) final Long channelId) {
return propertyWriteService.set(id, Property::setReadChannel, channelService.getById(channelId));
}
@PostMapping("set/{id}/writeChannel")
public PropertyDto setWriteChannel(@PathVariable final long id, @RequestBody(required = false) final Long channelId) {
return propertyWriteService.set(id, Property::setWriteChannel, channelService.getById(channelId));
}
@Override
@GetMapping("getById/{id}")
public SearchResult getById(@PathVariable final long id) {

View File

@ -47,9 +47,10 @@ public class PropertyWriteService {
}
public PropertyDto create() {
final Property entry = new Property();
entry.setTitle(generateUnusedTitle());
return publish(propertyRepository.save(entry), true);
final Property property = new Property();
property.setTitle(generateUnusedTitle());
property.setType(PropertyType.BOOLEAN);
return publish(propertyRepository.save(property), true);
}
private String generateUnusedTitle() {

View File

@ -6,7 +6,7 @@ import org.springframework.web.bind.annotation.*;
import java.util.List;
import static de.ph87.homeautomation.shared.Helpers.mapIfNotNull;
import static de.ph87.homeautomation.shared.Helpers.mapOrNull;
@RestController
@RequestMapping("schedule")
@ -51,7 +51,7 @@ public class ScheduleController {
@PostMapping("set/{id}/property")
public ScheduleDto setPropertyName(@PathVariable final long id, @RequestBody(required = false) final Long propertyId) {
return scheduleWriteService.set(id, (schedule, v) -> schedule.setProperty(mapIfNotNull(v, propertyReadService::getById)), propertyId);
return scheduleWriteService.set(id, Schedule::setProperty, mapOrNull(propertyId, propertyReadService::getById));
}
}

View File

@ -6,7 +6,7 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import static de.ph87.homeautomation.shared.Helpers.mapIfNotNull;
import static de.ph87.homeautomation.shared.Helpers.mapOrNull;
@Slf4j
@Service
@ -17,7 +17,7 @@ public class ScheduleMapper {
private final PropertyMapper propertyMapper;
public ScheduleDto toDto(final Schedule schedule) {
return new ScheduleDto(schedule, mapIfNotNull(schedule.getProperty(), propertyMapper::toDto));
return new ScheduleDto(schedule, mapOrNull(schedule.getProperty(), propertyMapper::toDto));
}
}

View File

@ -21,7 +21,7 @@ public class Helpers {
return value;
}
public static <T, U> U mapIfNotNull(final T value, final Function<T, U> map) {
public static <T, U> U mapOrNull(final T value, final Function<T, U> map) {
if (value == null) {
return null;
}