Merge branch 'controllers-feature' into 'dev'

Controllers feature

See merge request sa4-2020/the-sanmarinoes/backend!6
This commit is contained in:
Matteo Omenetti 2020-02-26 14:52:24 +01:00
commit 4b9944ab0d
17 changed files with 461 additions and 9 deletions

2
.gitignore vendored
View file

@ -1,3 +1,5 @@
**/.DS_Store
# Compiled class file
*.class

View file

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="ProjectRootManager" version="2" languageLevel="JDK_11" default="false" project-jdk-name="12.0.1" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_11" default="false" project-jdk-name="11.0.3" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

View file

@ -13,9 +13,8 @@ if [ "$NO_VERIFY" ]; then
fi
# list all added/copied/modified/renamed java files
files="$(git diff --staged --name-only --diff-filter=ACMR | egrep -a '.java$' | tr '\n' ' ')"
files="`git diff --staged --name-only --diff-filter=ACMR | egrep -a '.java$' | tr \"\\n\" \" \"`"
for f in $files; do
# run google-java-format on each file and re-stage any new changes
$format_cmd --aosp -i "$f"
git add -f "$f"
$format_cmd --aosp -i "$f"
git add -f "$f"
done

View file

@ -0,0 +1,47 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ButtonDimmer;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ButtonDimmerRepository;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@EnableAutoConfiguration
@RequestMapping("/buttonDimmer")
public class ButtonDimmerController {
@Autowired private ButtonDimmerRepository buttonDimmerService;
@GetMapping
public Iterable<ButtonDimmer> findAll() {
return buttonDimmerService.findAll();
}
@GetMapping("/{id}")
public Optional<ButtonDimmer> findById(@PathVariable("id") long id) {
return buttonDimmerService.findById(id);
}
@PostMapping
public ButtonDimmer save(@RequestBody ButtonDimmer bd) {
return buttonDimmerService.save(bd);
}
@PutMapping
public ButtonDimmer update(@RequestBody ButtonDimmer bd) {
return buttonDimmerService.save(bd);
}
@DeleteMapping("/{id}")
public void delete(@PathVariable("id") long id) {
buttonDimmerService.deleteById(id);
}
}

View file

@ -0,0 +1,48 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.DimmableLight;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.DimmableLightRepository;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@EnableAutoConfiguration
@RequestMapping("/dimmableLight")
public class DimmableLightController {
@Autowired private DimmableLightRepository dimmableLightService;
@GetMapping
public List<DimmableLight> getAll() {
return dimmableLightService.getList();
}
@GetMapping("/{id}")
public DimmableLight getById(@PathVariable("id") long id) {
return dimmableLightService.getById();
}
@PostMapping
public DimmableLight create(@RequestBody DimmableLight dl) {
return dimmableLightService.create(dl);
}
@PutMapping("/{id}")
public DimmableLight update(@PathVariable("id") long id, @RequestBody DimmableLight dl) {
return dimmableLightService.update(id, dl);
}
@DeleteMapping("/{id}")
public void delete(@PathVariable("id") long id) {
dimmableLightService.delete(id);
}
}

View file

@ -0,0 +1,48 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.KnobDimmer;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.KnobDimmerRepository;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@EnableAutoConfiguration
@RequestMapping("/knobDimmer")
public class KnobDimmerController {
@Autowired private KnobDimmerRepository knobDimmerService;
@GetMapping
public Iterable<KnobDimmer> findAll() {
return knobDimmerService.findAll();
}
@GetMapping("/{id}")
public Optional<KnobDimmer> findById(@PathVariable("id") long id) {
return knobDimmerService.findById(id);
}
@PostMapping
public KnobDimmer save(@RequestBody KnobDimmer kd) {
return knobDimmerService.save(kd);
}
@PutMapping
public KnobDimmer update(@RequestBody KnobDimmer kd) {
return knobDimmerService.save(kd);
}
@DeleteMapping("/{id}")
public void delete(@PathVariable("id") long id) {
knobDimmerService.deleteById(id);
}
}

View file

@ -0,0 +1,48 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.MotionSensor;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.MotionSensorRepository;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@EnableAutoConfiguration
@RequestMapping("/motionSensor")
public class MotionSensorController {
@Autowired private MotionSensorRepository motionSensorService;
@GetMapping
public Iterable<MotionSensor> findAll() {
return motionSensorService.findAll();
}
@GetMapping("/{id}")
public Optional<MotionSensor> findById(@PathVariable("id") long id) {
return motionSensorService.findById(id);
}
@PostMapping
public MotionSensor save(@RequestBody MotionSensor ms) {
return motionSensorService.save(ms);
}
@PutMapping
public MotionSensor update(@RequestBody MotionSensor ms) {
return motionSensorService.save(ms);
}
@DeleteMapping("/{id}")
public void delete(@PathVariable("id") long id) {
motionSensorService.deleteById(id);
}
}

View file

@ -0,0 +1,48 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.RegularLight;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.RegularLightRepository;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@EnableAutoConfiguration
@RequestMapping("/regularLight")
public class RegularLightController {
@Autowired private RegularLightRepository regularLightService;
@GetMapping
public Iterable<RegularLight> findAll() {
return regularLightService.findAll();
}
@GetMapping("/{id}")
public Optional<RegularLight> findById(@PathVariable("id") long id) {
return regularLightService.findById(id);
}
@PostMapping
public RegularLight save(@RequestBody RegularLight rl) {
return regularLightService.save(rl);
}
@PutMapping
public RegularLight update(@RequestBody RegularLight rl) {
return regularLightService.save(rl);
}
@DeleteMapping("/{id}")
public void delete(@PathVariable("id") long id) {
regularLightService.deleteById(id);
}
}

View file

@ -0,0 +1,40 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*;
import java.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.*;
import org.springframework.web.bind.annotation.*;
@RestController
@EnableAutoConfiguration
@RequestMapping("/room")
public class RoomController {
@Autowired private RoomRepository roomRepository;
@GetMapping
public Iterable<Room> findAll() {
return roomRepository.findAll();
}
@GetMapping("/{id}")
public Optional<Room> findById(@PathVariable("id") long id) {
return roomRepository.findById(id);
}
@PostMapping
public Room save(@RequestBody Room r) {
return roomRepository.save(r);
}
@PutMapping
public Room update(@RequestBody Room r) {
return roomRepository.save(r);
}
@DeleteMapping("/{id}")
public void deleteById(@PathVariable("id") long id) {
roomRepository.deleteById(id);
}
}

View file

@ -0,0 +1,40 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*;
import java.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.*;
import org.springframework.web.bind.annotation.*;
@RestController
@EnableAutoConfiguration
@RequestMapping("/sensor")
public class SensorController {
@Autowired private SensorRepository sensorRepository;
@GetMapping
public Iterable<Sensor> findAll() {
return sensorRepository.findAll();
}
@GetMapping("/{id}")
public Optional<Sensor> findById(@PathVariable("id") long id) {
return sensorRepository.findById(id);
}
@PostMapping
public Sensor save(@RequestBody Sensor s) {
return sensorRepository.save(s);
}
@PutMapping
public Sensor update(@RequestBody Sensor s) {
return sensorRepository.save(s);
}
@DeleteMapping("/{id}")
public void deleteById(@PathVariable("id") long id) {
sensorRepository.deleteById(id);
}
}

View file

@ -0,0 +1,40 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*;
import java.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.*;
import org.springframework.web.bind.annotation.*;
@RestController
@EnableAutoConfiguration
@RequestMapping("/smartplug")
public class SmartPlugController {
@Autowired private SmartPlugRepository smartPlugRepository;
@GetMapping
public Iterable<SmartPlug> findAll() {
return smartPlugRepository.findAll();
}
@GetMapping("/{id}")
public Optional<SmartPlug> findById(@PathVariable("id") long id) {
return smartPlugRepository.findById(id);
}
@PostMapping
public SmartPlug save(@RequestBody SmartPlug sp) {
return smartPlugRepository.save(sp);
}
@PutMapping
public SmartPlug update(@RequestBody SmartPlug sp) {
return smartPlugRepository.save(sp);
}
@DeleteMapping("/{id}")
public void deleteById(@PathVariable("id") long id) {
smartPlugRepository.deleteById(id);
}
}

View file

@ -0,0 +1,40 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*;
import java.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.*;
import org.springframework.web.bind.annotation.*;
@RestController
@EnableAutoConfiguration
@RequestMapping("/switch")
public class SwitchController {
@Autowired private SwitchRepository switchRepository;
@GetMapping
public Iterable<Switch> findAll() {
return switchRepository.findAll();
}
@GetMapping("/{id}")
public Optional<Switch> findById(@PathVariable("id") long id) {
return switchRepository.findById(id);
}
@PostMapping
public Switch save(@RequestBody Switch s) {
return switchRepository.save(s);
}
@PutMapping
public Switch update(@RequestBody Switch s) {
return switchRepository.save(s);
}
@DeleteMapping("/{id}")
public void deleteById(@PathVariable("id") long id) {
switchRepository.deleteById(id);
}
}

View file

@ -0,0 +1,40 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*;
import java.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.*;
import org.springframework.web.bind.annotation.*;
@RestController
@EnableAutoConfiguration
@RequestMapping("/user")
public class UserController {
@Autowired private UserRepository userRepository;
@GetMapping
public Iterable<User> findAll() {
return userRepository.findAll();
}
@GetMapping("/{id}")
public Optional<User> findById(@PathVariable("id") long id) {
return userRepository.findById(id);
}
@PostMapping
public User save(@RequestBody User u) {
return userRepository.save(u);
}
@PutMapping
public User update(@RequestBody User u) {
return userRepository.save(u);
}
@DeleteMapping("/{id}")
public void deleteById(@PathVariable("id") long id) {
userRepository.deleteById(id);
}
}

View file

@ -3,15 +3,14 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*;
import java.util.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
@RestController
@EnableAutoConfiguration
@Controller
// @Mapping("/light")
public class WelcomeController {
@GetMapping("/")
@GetMapping
List<Device> testDevices() {
return Arrays.asList(
new KnobDimmer(),

View file

@ -12,6 +12,18 @@ public class Room {
@Column(name = "id", updatable = false, nullable = false)
private Long id;
/**
* Icon and image are to be given as byte[]. In order to get an encoded string from it, the
* Base64.getEncoder().encodeToString(byte[] content) should be used. For further information:
* https://www.baeldung.com/java-base64-image-string
* https://docs.oracle.com/javase/8/docs/api/java/util/Base64.html
*/
@Column(name = "icon", updatable = true, nullable = false)
private byte[] icon;
@Column(name = "image", updatable = true, nullable = false)
private byte[] image;
/** User that owns the house this room is in */
@ManyToOne
@JoinColumn(name = "user_id")

View file

@ -2,4 +2,4 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import org.springframework.data.repository.CrudRepository;
public interface RoomRepository extends CrudRepository<Room, User> {}
public interface RoomRepository extends CrudRepository<Room, Long> {}

View file

@ -1,5 +1,6 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import java.util.*;
import org.springframework.data.repository.CrudRepository;
public interface UserRepository extends CrudRepository<User, Long> {