From e46ac02da563b4b1bc73c91b44fc355605902bcc Mon Sep 17 00:00:00 2001 From: Claudio Maggioni Date: Sat, 11 Apr 2020 17:24:23 +0200 Subject: [PATCH] Input output connection routes now accept array of outputs --- .../controller/ButtonDimmerController.java | 4 +- .../InputDeviceConnectionController.java | 52 ++++++++++++------- .../controller/KnobDimmerController.java | 4 +- .../smarthut/controller/SwitchController.java | 4 +- 4 files changed, 39 insertions(+), 25 deletions(-) diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ButtonDimmerController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ButtonDimmerController.java index 944bd8e..ed517f8 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ButtonDimmerController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ButtonDimmerController.java @@ -77,14 +77,14 @@ public class ButtonDimmerController @PostMapping("/{id}/lights") public Set addLight( - @PathVariable("id") long inputId, @RequestParam("lightId") Long lightId) + @PathVariable("id") long inputId, @RequestBody List lightId) throws NotFoundException { return addOutput(inputId, lightId); } @DeleteMapping("/{id}/lights") public Set removeLight( - @PathVariable("id") long inputId, @RequestParam("lightId") Long lightId) + @PathVariable("id") long inputId, @RequestBody List lightId) throws NotFoundException { return removeOutput(inputId, lightId); } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/InputDeviceConnectionController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/InputDeviceConnectionController.java index 52f3483..ca2997a 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/InputDeviceConnectionController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/InputDeviceConnectionController.java @@ -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 output; - private IOPair(I input, O output) { + private Connection(I input, List 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 outputs) + throws NotFoundException { final I input = inputRepository .findById(inputId) .orElseThrow(() -> new NotFoundException("input device")); - final O output = - outputReposiory - .findById(outputId) - .orElseThrow(() -> new NotFoundException("output device")); - return new IOPair(input, output); + final List outputDevices = new ArrayList<>(); + for (final Long outputId : outputs) { + outputDevices.add( + outputReposiory + .findById(outputId) + .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 addOutput(Long inputId, Long outputId) + protected Set addOutput(Long inputId, List 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 removeOutput(Long inputId, Long outputId) + protected Set removeOutput(Long inputId, List 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(); } } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/KnobDimmerController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/KnobDimmerController.java index c15d867..a259d98 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/KnobDimmerController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/KnobDimmerController.java @@ -70,14 +70,14 @@ public class KnobDimmerController @PostMapping("/{id}/lights") public Set addLight( - @PathVariable("id") long inputId, @RequestParam("lightId") Long lightId) + @PathVariable("id") long inputId, @RequestBody List lightId) throws NotFoundException { return addOutput(inputId, lightId); } @DeleteMapping("/{id}/lights") public Set removeLight( - @PathVariable("id") long inputId, @RequestParam("lightId") Long lightId) + @PathVariable("id") long inputId, @RequestBody List lightId) throws NotFoundException { return removeOutput(inputId, lightId); } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SwitchController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SwitchController.java index fc64ccb..5794a01 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SwitchController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SwitchController.java @@ -83,14 +83,14 @@ public class SwitchController extends InputDeviceConnectionController addSwitchable( - @PathVariable("id") long inputId, @RequestParam("switchableId") Long switchableId) + @PathVariable("id") long inputId, @RequestBody List switchableId) throws NotFoundException { return addOutput(inputId, switchableId); } @DeleteMapping("/{id}/lights") public Set removeSwitchable( - @PathVariable("id") long inputId, @RequestParam("switchableId") Long switchableId) + @PathVariable("id") long inputId, @RequestBody List switchableId) throws NotFoundException { return removeOutput(inputId, switchableId); }