Merge branch 'model-update' of lab.si.usi.ch:sa4-2020/the-sanmarinoes/backend into swagger-feature

This commit is contained in:
Claudio Maggioni 2020-03-01 13:37:27 +01:00
commit 970949f635
14 changed files with 255 additions and 16 deletions

View file

@ -0,0 +1,5 @@
<component name="ProjectCodeStyleConfiguration">
<state>
<option name="PREFERRED_PROJECT_CODE_STYLE" value="Default" />
</state>
</component>

9
.idea/compiler.xml Normal file
View file

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<bytecodeTargetLevel>
<module name="smarthut.main" target="11" />
<module name="smarthut.test" target="11" />
</bytecodeTargetLevel>
</component>
</project>

View file

@ -1,7 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<<<<<<< HEAD
<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.5" project-jdk-type="JavaSDK">
>>>>>>> 5d1549cfa20b7d50b7f27f915d1169ace5253c70
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

10
.idea/modules.xml Normal file
View file

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/modules/smarthut.iml" filepath="$PROJECT_DIR$/.idea/modules/smarthut.iml" />
<module fileurl="file://$PROJECT_DIR$/.idea/modules/smarthut.main.iml" filepath="$PROJECT_DIR$/.idea/modules/smarthut.main.iml" />
<module fileurl="file://$PROJECT_DIR$/.idea/modules/smarthut.test.iml" filepath="$PROJECT_DIR$/.idea/modules/smarthut.test.iml" />
</modules>
</component>
</project>

View file

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<module version="4">
<component name="ExternalSystem" externalSystem="GRADLE" externalSystemModuleGroup="ch.usi.inf.sa4.sanmarinoes." externalSystemModuleVersion="0.0.1-SNAPSHOT" linkedProjectId="smarthut" linkedProjectPath="$MODULE_DIR$/../.." rootProjectPath="$MODULE_DIR$/../.." />
</module>

View file

@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<module version="4">
<component name="ExternalSystem" externalSystem="GRADLE" externalSystemModuleGroup="ch.usi.inf.sa4.sanmarinoes." externalSystemModuleType="sourceSet" externalSystemModuleVersion="0.0.1-SNAPSHOT" linkedProjectId="smarthut:main" linkedProjectPath="$MODULE_DIR$/../.." rootProjectPath="$MODULE_DIR$/../.." />
<component name="FacetManager">
<facet type="Spring" name="Spring">
<configuration>
<fileset id="fileset" name="SecurityConfig" removed="false">
<file>file://$MODULE_DIR$/../../src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/config/SecurityConfig.java</file>
<file>file://$MODULE_DIR$/../../src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/SmarthutApplication.java</file>
</fileset>
</configuration>
</facet>
<facet type="web" name="Web">
<configuration>
<webroots />
</configuration>
</facet>
</component>
</module>

View file

@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<module version="4">
<component name="ExternalSystem" externalSystem="GRADLE" externalSystemModuleGroup="ch.usi.inf.sa4.sanmarinoes." externalSystemModuleType="sourceSet" externalSystemModuleVersion="0.0.1-SNAPSHOT" linkedProjectId="smarthut:test" linkedProjectPath="$MODULE_DIR$/../.." rootProjectPath="$MODULE_DIR$/../.." />
<component name="FacetManager">
<facet type="Spring" name="Spring">
<configuration />
</facet>
<facet type="web" name="Web">
<configuration>
<webroots />
<sourceRoots />
</configuration>
</facet>
</component>
<component name="TestModuleProperties" production-module="smarthut.main" />
</module>

View file

@ -1,11 +1,12 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.RoomSaveRequest;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*;
import java.security.Principal;
import java.util.*;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.*;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.*;
@ -28,27 +29,38 @@ public class RoomController {
return roomRepository.findById(id);
}
@PostMapping
public Room save(@Valid @RequestBody Room r) {
final Object principal =
SecurityContextHolder.getContext().getAuthentication().getPrincipal();
if (!(principal instanceof UserDetails)) {
throw new IllegalStateException("User is not logged in");
}
private Room save(final RoomSaveRequest r, final Principal principal, boolean setWhenNull) {
Room newRoom = new Room();
final String username = ((UserDetails) principal).getUsername();
final Long userId = userRepository.findByUsername(username).getId();
final String img = r.getImage();
final String icon = r.getIcon();
r.setUserId(userId);
r.setUser(null);
newRoom.setUserId(userId);
newRoom.setUser(null);
if (img != null) {
newRoom.setImage(img.getBytes());
} else if (setWhenNull) {
newRoom.setImage(null);
}
if (icon != null) {
newRoom.setIcon(icon.getBytes());
} else if (setWhenNull) {
newRoom.setIcon(null);
}
return roomRepository.save(r);
return roomRepository.save(newRoom);
}
@PostMapping
public Room create(@Valid @RequestBody RoomSaveRequest r, final Principal principal) {
return this.save(r, principal, true);
}
@PutMapping
public Room update(@Valid @RequestBody Room r) {
return roomRepository.save(r);
public Room update(@Valid @RequestBody RoomSaveRequest r, final Principal principal) {
return this.save(r, principal, false);
}
@DeleteMapping("/{id}")

View file

@ -0,0 +1,43 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.dto;
import javax.persistence.Lob;
import javax.validation.constraints.NotNull;
public class RoomSaveRequest {
/**
* 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
*/
@Lob private String icon;
@Lob private String image;
/** The user given name of this room (e.g. 'Master bedroom') */
@NotNull private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getIcon() {
return icon;
}
public void setIcon(String icon) {
this.icon = icon;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
}

View file

@ -1,6 +1,9 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Entity;
import javax.persistence.OneToMany;
/**
* Represents a dimmer that can only instruct an increase or decrease of intensity (i.e. like a
@ -8,7 +11,47 @@ import javax.persistence.Entity;
*/
@Entity
public class ButtonDimmer extends Dimmer {
@OneToMany(mappedBy = "button_dimmer")
private Set<DimmableLight> lights = new HashSet<DimmableLight>();
public ButtonDimmer() {
super("button-dimmer");
}
/** Increases the current intensity level of the dimmable light by 1 */
public void increaseIntensity() {
for (DimmableLight dl : lights) {
dl.setIntensity(dl.getIntensity() + 1);
}
}
/** Decreases the current intensity level of the dimmable light by 1 */
public void decreaseIntensity() {
for (DimmableLight dl : lights) {
dl.setIntensity(dl.getIntensity() - 1);
}
}
/**
* Adds a DimmableLight to this set of DimmableLights
*
* @param dl The DimmableLight to be added
*/
public void addLight(DimmableLight dl) {
lights.add(dl);
}
/**
* Removes the given DimmableLight
*
* @param dl The DimmableLight to be removed
*/
public void removeLight(DimmableLight dl) {
lights.remove(dl);
}
/** Clears this set */
public void clearSet() {
lights.clear();
}
}

View file

@ -22,7 +22,7 @@ public abstract class Device {
/** Device identifier */
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", updatable = false, nullable = false)
@Column(name = "id", updatable = false, nullable = false, unique = true)
@ApiModelProperty(hidden = true)
private long id;

View file

@ -1,6 +1,9 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Entity;
import javax.persistence.OneToMany;
/**
* Represents a dimmer able to set absolute intensity values (i.e. knowing the absolute intensity
@ -8,7 +11,54 @@ import javax.persistence.Entity;
*/
@Entity
public class KnobDimmer extends Dimmer {
@OneToMany(mappedBy = "knob_dimmer")
private Set<DimmableLight> lights = new HashSet<DimmableLight>();
public KnobDimmer() {
super("knob-dimmer");
}
/**
* Increases or decreases the current intensity level by 5, moving between absolute multiples of
* 5 between 0 and 100, of all dimmable lights mapped to this knob
*
* @param inc The direction the knob is turned with
*/
public void modifyIntensity(boolean inc) {
for (DimmableLight dl : lights) {
int remainder = dl.getIntensity() / 5;
if (inc) {
dl.setIntensity(dl.getIntensity() - remainder);
dl.setIntensity((dl.getIntensity() + 5) % 105);
} else {
dl.setIntensity(dl.getIntensity() + (5 - remainder));
dl.setIntensity((dl.getIntensity() - 5) % 105);
}
}
}
/**
* Adds a DimmableLight to this set of DimmableLights
*
* @param dl The DimmableLight to be added
*/
public void addLight(DimmableLight dl) {
lights.add(dl);
}
/**
* Removes the given DimmableLight
*
* @param dl The DimmableLight to be removed
*/
public void removeLight(DimmableLight dl) {
lights.remove(dl);
}
/** Clears this set */
public void clearSet() {
lights.clear();
}
}

View file

@ -1,11 +1,35 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import javax.persistence.Column;
import javax.persistence.Entity;
/** A switch input device TODO: define switch behaviour (push button vs on/off state) */
@Entity
public class Switch extends InputDevice {
/** The state of this switch */
@Column(nullable = false, name = "switch_on")
private boolean on;
public Switch() {
super("switch");
}
/**
* Setter method for this Switch
*
* @param state The state to be set
*/
void setState(boolean state) {
on = state;
}
/**
* Getter method for this Switch
*
* @return This Switch on state
*/
boolean getState() {
return on;
}
}

View file

@ -42,7 +42,7 @@ public class User {
* The user's email (validated according to criteria used in <code>&gt;input type="email"&lt;>
* </code>, technically not RFC 5322 compliant
*/
@Column(nullable = false)
@Column(nullable = false, unique = true)
@NotNull
@NotEmpty(message = "Please provide an email")
@Email(message = "Please provide a valid email address")