Merge branch '57-change-input-output-connection-routes' into 'dev'

Resolve "Change input/output connection routes"

Closes #57

See merge request sa4-2020/the-sanmarinoes/backend!71
This commit is contained in:
Claudio Maggioni 2020-04-11 17:28:39 +02:00
commit 9575018b2e
4 changed files with 39 additions and 25 deletions

View file

@ -77,14 +77,14 @@ public class ButtonDimmerController
@PostMapping("/{id}/lights")
public Set<? extends OutputDevice> addLight(
@PathVariable("id") long inputId, @RequestParam("lightId") Long lightId)
@PathVariable("id") long inputId, @RequestBody List<Long> lightId)
throws NotFoundException {
return addOutput(inputId, lightId);
}
@DeleteMapping("/{id}/lights")
public Set<? extends OutputDevice> removeLight(
@PathVariable("id") long inputId, @RequestParam("lightId") Long lightId)
@PathVariable("id") long inputId, @RequestBody List<Long> lightId)
throws NotFoundException {
return removeOutput(inputId, lightId);
}

View file

@ -2,6 +2,8 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
/**
@ -14,11 +16,11 @@ import java.util.Set;
public abstract class InputDeviceConnectionController<
I extends InputDevice, O extends OutputDevice> {
private class IOPair {
private class Connection {
private final I input;
private final O output;
private final List<O> output;
private IOPair(I input, O output) {
private Connection(I input, List<O> output) {
this.input = input;
this.output = output;
}
@ -46,31 +48,39 @@ public abstract class InputDeviceConnectionController<
this.connector = connector;
}
private IOPair checkConnectionIDs(Long inputId, Long outputId) throws NotFoundException {
private Connection checkConnectionIDs(Long inputId, List<Long> outputs)
throws NotFoundException {
final I input =
inputRepository
.findById(inputId)
.orElseThrow(() -> new NotFoundException("input device"));
final O output =
final List<O> outputDevices = new ArrayList<>();
for (final Long outputId : outputs) {
outputDevices.add(
outputReposiory
.findById(outputId)
.orElseThrow(() -> new NotFoundException("output device"));
return new IOPair(input, output);
.orElseThrow(() -> new NotFoundException("output device")));
}
return new Connection(input, outputDevices);
}
/**
* Implements the output device connection creation (add) route
*
* @param inputId input device id
* @param outputId output device id
* @param outputId output device id list
* @return the list of output devices attached to the input device of id inputId
* @throws NotFoundException if inputId or outputId are not valid
*/
protected Set<? extends OutputDevice> addOutput(Long inputId, Long outputId)
protected Set<? extends OutputDevice> addOutput(Long inputId, List<Long> outputId)
throws NotFoundException {
final IOPair pair = checkConnectionIDs(inputId, outputId);
connector.connect(pair.input, pair.output, true);
outputReposiory.save(pair.output);
final Connection pair = checkConnectionIDs(inputId, outputId);
for (final O o : pair.output) {
connector.connect(pair.input, o, true);
}
outputReposiory.saveAll(pair.output);
return pair.input.getOutputs();
}
@ -78,15 +88,19 @@ public abstract class InputDeviceConnectionController<
* Implements the output device connection destruction (remove) route
*
* @param inputId input device id
* @param outputId output device id
* @param outputId output device id list
* @return the list of output devices attached to the input device of id inputId
* @throws NotFoundException if inputId or outputId are not valid
*/
protected Set<? extends OutputDevice> removeOutput(Long inputId, Long outputId)
protected Set<? extends OutputDevice> removeOutput(Long inputId, List<Long> outputId)
throws NotFoundException {
final IOPair pair = checkConnectionIDs(inputId, outputId);
connector.connect(pair.input, pair.output, false);
outputReposiory.save(pair.output);
final Connection pair = checkConnectionIDs(inputId, outputId);
for (final O o : pair.output) {
connector.connect(pair.input, o, false);
}
outputReposiory.saveAll(pair.output);
return pair.input.getOutputs();
}
}

View file

@ -70,14 +70,14 @@ public class KnobDimmerController
@PostMapping("/{id}/lights")
public Set<? extends OutputDevice> addLight(
@PathVariable("id") long inputId, @RequestParam("lightId") Long lightId)
@PathVariable("id") long inputId, @RequestBody List<Long> lightId)
throws NotFoundException {
return addOutput(inputId, lightId);
}
@DeleteMapping("/{id}/lights")
public Set<? extends OutputDevice> removeLight(
@PathVariable("id") long inputId, @RequestParam("lightId") Long lightId)
@PathVariable("id") long inputId, @RequestBody List<Long> lightId)
throws NotFoundException {
return removeOutput(inputId, lightId);
}

View file

@ -83,14 +83,14 @@ public class SwitchController extends InputDeviceConnectionController<Switch, Sw
@PostMapping("/{id}/lights")
public Set<? extends OutputDevice> addSwitchable(
@PathVariable("id") long inputId, @RequestParam("switchableId") Long switchableId)
@PathVariable("id") long inputId, @RequestBody List<Long> switchableId)
throws NotFoundException {
return addOutput(inputId, switchableId);
}
@DeleteMapping("/{id}/lights")
public Set<? extends OutputDevice> removeSwitchable(
@PathVariable("id") long inputId, @RequestParam("switchableId") Long switchableId)
@PathVariable("id") long inputId, @RequestBody List<Long> switchableId)
throws NotFoundException {
return removeOutput(inputId, switchableId);
}