room controller updated

This commit is contained in:
Tommaso Rodolfo Masera 2020-02-28 17:06:42 +01:00
parent 2ae0ab5b15
commit 5d1549cfa2
5 changed files with 81 additions and 20 deletions

View file

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

@ -21,7 +21,7 @@ public abstract class Device {
/** Device identifier */ /** Device identifier */
@Id @Id
@GeneratedValue(strategy = GenerationType.AUTO) @GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", updatable = false, nullable = false) @Column(name = "id", updatable = false, nullable = false, unique = true)
private long id; private long id;
/** The room this device belongs in */ /** The room this device belongs in */

View file

@ -8,22 +8,28 @@ import javax.persistence.Entity;
public class Switch extends InputDevice { public class Switch extends InputDevice {
/** The state of this switch */ /** The state of this switch */
@Column private boolean switchState = false; @Column(nullable = false, name = "switch_on")
private boolean on;
public Switch() { public Switch() {
super("switch"); super("switch");
} }
/** /**
* Setter method for Switch * Setter method for this Switch
* *
* @param state The state to be set * @param state The state to be set
*/ */
void setState(boolean state) { void setState(boolean state) {
switchState = state; on = state;
} }
/**
* Getter method for this Switch
*
* @return This Switch on state
*/
boolean getState() { boolean getState() {
return switchState; return on;
} }
} }

View file

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