Solved recursive JSON problem in RoomController.getAllDevices()
This commit is contained in:
parent
8bbcc0304f
commit
4f0351cab9
14 changed files with 78 additions and 76 deletions
|
@ -50,6 +50,16 @@ public class SpringFoxConfig {
|
|||
return List.of(new ApiKey("Bearer", "Authorization", "header"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a Java functional API predicate for regex matches
|
||||
*
|
||||
* @param regex the regex to match on
|
||||
* @return a Java functional API predicate
|
||||
*/
|
||||
private Predicate<String> regexPredicate(final String regex) {
|
||||
return regex(regex)::apply;
|
||||
}
|
||||
|
||||
/**
|
||||
* Configures the paths the documentation must be generated for. Add a path here only when the
|
||||
* spec has been totally defined.
|
||||
|
@ -57,7 +67,7 @@ public class SpringFoxConfig {
|
|||
* @return A predicate that tests whether a path must be included or not
|
||||
*/
|
||||
private Predicate<String> paths() {
|
||||
return regex("/auth.*")::apply;
|
||||
return regexPredicate("/auth.*").or(regexPredicate("/room.*"));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -1,7 +1,10 @@
|
|||
package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
|
||||
|
||||
import static ch.usi.inf.sa4.sanmarinoes.smarthut.utils.Utils.toList;
|
||||
|
||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ButtonDimmer;
|
||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ButtonDimmerRepository;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import javax.validation.Valid;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
@ -22,8 +25,8 @@ public class ButtonDimmerController {
|
|||
@Autowired private ButtonDimmerRepository buttonDimmerService;
|
||||
|
||||
@GetMapping
|
||||
public Iterable<ButtonDimmer> findAll() {
|
||||
return buttonDimmerService.findAll();
|
||||
public List<ButtonDimmer> findAll() {
|
||||
return toList(buttonDimmerService.findAll());
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
|
|
|
@ -1,7 +1,10 @@
|
|||
package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
|
||||
|
||||
import static ch.usi.inf.sa4.sanmarinoes.smarthut.utils.Utils.toList;
|
||||
|
||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.DimmableLight;
|
||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.DimmableLightRepository;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import javax.validation.Valid;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
@ -23,8 +26,8 @@ public class DimmableLightController {
|
|||
@Autowired private DimmableLightRepository dimmableLightService;
|
||||
|
||||
@GetMapping
|
||||
public Iterable<DimmableLight> findAll() {
|
||||
return dimmableLightService.findAll();
|
||||
public List<DimmableLight> findAll() {
|
||||
return toList(dimmableLightService.findAll());
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
|
|
|
@ -1,7 +1,10 @@
|
|||
package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
|
||||
|
||||
import static ch.usi.inf.sa4.sanmarinoes.smarthut.utils.Utils.toList;
|
||||
|
||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.KnobDimmer;
|
||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.KnobDimmerRepository;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import javax.validation.Valid;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
@ -23,8 +26,8 @@ public class KnobDimmerController {
|
|||
@Autowired private KnobDimmerRepository knobDimmerService;
|
||||
|
||||
@GetMapping
|
||||
public Iterable<KnobDimmer> findAll() {
|
||||
return knobDimmerService.findAll();
|
||||
public List<KnobDimmer> findAll() {
|
||||
return toList(knobDimmerService.findAll());
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
|
|
|
@ -1,7 +1,10 @@
|
|||
package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
|
||||
|
||||
import static ch.usi.inf.sa4.sanmarinoes.smarthut.utils.Utils.toList;
|
||||
|
||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.MotionSensor;
|
||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.MotionSensorRepository;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import javax.validation.Valid;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
@ -23,8 +26,8 @@ public class MotionSensorController {
|
|||
@Autowired private MotionSensorRepository motionSensorService;
|
||||
|
||||
@GetMapping
|
||||
public Iterable<MotionSensor> findAll() {
|
||||
return motionSensorService.findAll();
|
||||
public List<MotionSensor> findAll() {
|
||||
return toList(motionSensorService.findAll());
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
|
|
|
@ -1,7 +1,10 @@
|
|||
package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
|
||||
|
||||
import static ch.usi.inf.sa4.sanmarinoes.smarthut.utils.Utils.toList;
|
||||
|
||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.RegularLight;
|
||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.RegularLightRepository;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import javax.validation.Valid;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
@ -23,8 +26,8 @@ public class RegularLightController {
|
|||
@Autowired private RegularLightRepository regularLightService;
|
||||
|
||||
@GetMapping
|
||||
public Iterable<RegularLight> findAll() {
|
||||
return regularLightService.findAll();
|
||||
public List<RegularLight> findAll() {
|
||||
return toList(regularLightService.findAll());
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
|
||||
|
||||
import static ch.usi.inf.sa4.sanmarinoes.smarthut.utils.Utils.toList;
|
||||
|
||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.RoomSaveRequest;
|
||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*;
|
||||
import java.security.Principal;
|
||||
|
@ -19,15 +21,15 @@ public class RoomController {
|
|||
|
||||
@Autowired private UserRepository userRepository;
|
||||
|
||||
@Autowired private DeviceRepository deviceRepository;
|
||||
@Autowired private DeviceRepository<Device> deviceRepository;
|
||||
|
||||
@GetMapping
|
||||
public Iterable<Room> findAll() {
|
||||
return roomRepository.findAll();
|
||||
public List<Room> findAll() {
|
||||
return toList(roomRepository.findAll());
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public Optional<Room> findById(@PathVariable("id") long id) {
|
||||
public @ResponseBody Optional<Room> findById(@PathVariable("id") long id) {
|
||||
return roomRepository.findById(id);
|
||||
}
|
||||
|
||||
|
@ -40,7 +42,6 @@ public class RoomController {
|
|||
final String icon = r.getIcon();
|
||||
|
||||
newRoom.setUserId(userId);
|
||||
newRoom.setUser(null);
|
||||
if (img != null) {
|
||||
newRoom.setImage(img.getBytes());
|
||||
} else if (setWhenNull) {
|
||||
|
@ -56,12 +57,14 @@ public class RoomController {
|
|||
}
|
||||
|
||||
@PostMapping
|
||||
public Room create(@Valid @RequestBody RoomSaveRequest r, final Principal principal) {
|
||||
public @ResponseBody Room create(
|
||||
@Valid @RequestBody RoomSaveRequest r, final Principal principal) {
|
||||
return this.save(r, principal, true);
|
||||
}
|
||||
|
||||
@PutMapping
|
||||
public Room update(@Valid @RequestBody RoomSaveRequest r, final Principal principal) {
|
||||
public @ResponseBody Room update(
|
||||
@Valid @RequestBody RoomSaveRequest r, final Principal principal) {
|
||||
return this.save(r, principal, false);
|
||||
}
|
||||
|
||||
|
@ -74,9 +77,8 @@ public class RoomController {
|
|||
* Returns a List<Device> of all Devices that are present in a given room (identified by its
|
||||
* id).
|
||||
*/
|
||||
@GetMapping(path = "/room/{roomid}/devices")
|
||||
@ResponseBody
|
||||
public List<Device> getDevices(@PathVariable("roomid") long roomid) {
|
||||
@GetMapping(path = "/{roomId}/devices")
|
||||
public @ResponseBody List<? extends Device> getDevices(@PathVariable("roomId") long roomid) {
|
||||
return deviceRepository.findByRoomId(roomid);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,10 @@
|
|||
package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
|
||||
|
||||
import static ch.usi.inf.sa4.sanmarinoes.smarthut.utils.Utils.toList;
|
||||
|
||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*;
|
||||
import java.util.*;
|
||||
import java.util.List;
|
||||
import javax.validation.Valid;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.*;
|
||||
|
@ -15,8 +18,8 @@ public class SensorController {
|
|||
@Autowired private SensorRepository sensorRepository;
|
||||
|
||||
@GetMapping
|
||||
public Iterable<Sensor> findAll() {
|
||||
return sensorRepository.findAll();
|
||||
public List<Sensor> findAll() {
|
||||
return toList(sensorRepository.findAll());
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
|
|
|
@ -1,7 +1,10 @@
|
|||
package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
|
||||
|
||||
import static ch.usi.inf.sa4.sanmarinoes.smarthut.utils.Utils.toList;
|
||||
|
||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*;
|
||||
import java.util.*;
|
||||
import java.util.List;
|
||||
import javax.validation.Valid;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.*;
|
||||
|
@ -15,8 +18,8 @@ public class SmartPlugController {
|
|||
@Autowired private SmartPlugRepository smartPlugRepository;
|
||||
|
||||
@GetMapping
|
||||
public Iterable<SmartPlug> findAll() {
|
||||
return smartPlugRepository.findAll();
|
||||
public List<SmartPlug> findAll() {
|
||||
return toList(smartPlugRepository.findAll());
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
|
|
|
@ -1,7 +1,10 @@
|
|||
package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
|
||||
|
||||
import static ch.usi.inf.sa4.sanmarinoes.smarthut.utils.Utils.toList;
|
||||
|
||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*;
|
||||
import java.util.*;
|
||||
import java.util.List;
|
||||
import javax.validation.Valid;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.*;
|
||||
|
@ -15,8 +18,8 @@ public class SwitchController {
|
|||
@Autowired private SwitchRepository switchRepository;
|
||||
|
||||
@GetMapping
|
||||
public Iterable<Switch> findAll() {
|
||||
return switchRepository.findAll();
|
||||
public List<Switch> findAll() {
|
||||
return toList(switchRepository.findAll());
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
|
|
|
@ -26,12 +26,6 @@ public abstract class Device {
|
|||
@ApiModelProperty(hidden = true)
|
||||
private long id;
|
||||
|
||||
/** The room this device belongs in */
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "room_id", nullable = false, updatable = false, insertable = false)
|
||||
@ApiModelProperty(hidden = true)
|
||||
private Room room;
|
||||
|
||||
/**
|
||||
* The room this device belongs in, as a foreign key id. To use when updating and inserting from
|
||||
* a REST call.
|
||||
|
@ -69,14 +63,6 @@ public abstract class Device {
|
|||
this.id = id;
|
||||
}
|
||||
|
||||
public Room getRoom() {
|
||||
return room;
|
||||
}
|
||||
|
||||
public void setRoom(Room room) {
|
||||
this.room = room;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
|
||||
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import java.util.Set;
|
||||
import javax.persistence.*;
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
|
@ -42,17 +41,6 @@ public class Room {
|
|||
@Column(nullable = false)
|
||||
private String name;
|
||||
|
||||
/** Collection of devices present in this room */
|
||||
@ApiModelProperty(hidden = true)
|
||||
@OneToMany(mappedBy = "room")
|
||||
private Set<Device> devices;
|
||||
|
||||
/** User that owns the house this room is in */
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "user_id", nullable = false, updatable = false, insertable = false)
|
||||
@ApiModelProperty(hidden = true)
|
||||
private User user;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
@ -69,14 +57,6 @@ public class Room {
|
|||
this.userId = userId;
|
||||
}
|
||||
|
||||
public User getUser() {
|
||||
return user;
|
||||
}
|
||||
|
||||
public void setUser(User user) {
|
||||
this.user = user;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
@ -85,10 +65,6 @@ public class Room {
|
|||
this.name = name;
|
||||
}
|
||||
|
||||
public Set<Device> getDevices() {
|
||||
return devices;
|
||||
}
|
||||
|
||||
public byte[] getIcon() {
|
||||
return icon;
|
||||
}
|
||||
|
@ -107,6 +83,6 @@ public class Room {
|
|||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Room{" + "id=" + id + ", user=" + user + ", name='" + name + "\'}";
|
||||
return "Room{" + "id=" + id + ", name='" + name + "\'}";
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
|
||||
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import java.util.Set;
|
||||
import javax.persistence.*;
|
||||
import javax.validation.constraints.*;
|
||||
|
||||
|
@ -49,11 +48,6 @@ public class User {
|
|||
@Pattern(regexp = ".+@.+\\..+", message = "Please provide a valid email address")
|
||||
private String email;
|
||||
|
||||
/** All rooms in the user's house */
|
||||
@OneToMany(mappedBy = "user")
|
||||
@ApiModelProperty(hidden = true)
|
||||
private Set<Room> rooms;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
@ -94,10 +88,6 @@ public class User {
|
|||
this.password = password;
|
||||
}
|
||||
|
||||
public Set<Room> getRooms() {
|
||||
return rooms;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "User{"
|
||||
|
|
|
@ -0,0 +1,14 @@
|
|||
package ch.usi.inf.sa4.sanmarinoes.smarthut.utils;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.StreamSupport;
|
||||
|
||||
/** A class with a bunch of useful static methods */
|
||||
public class Utils {
|
||||
private Utils() {}
|
||||
|
||||
public static <T> List<T> toList(Iterable<T> iterable) {
|
||||
return StreamSupport.stream(iterable.spliterator(), false).collect(Collectors.toList());
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue