Small Changes
This commit is contained in:
parent
1ab4fee6ff
commit
b2353e9834
3 changed files with 45 additions and 16 deletions
|
@ -4,6 +4,8 @@ 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.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@RestController
|
||||
|
@ -13,6 +15,8 @@ public class RoomController {
|
|||
|
||||
@Autowired private RoomRepository roomRepository;
|
||||
|
||||
@Autowired private UserRepository userRepository;
|
||||
|
||||
@GetMapping
|
||||
public Iterable<Room> findAll() {
|
||||
return roomRepository.findAll();
|
||||
|
@ -25,6 +29,19 @@ public class RoomController {
|
|||
|
||||
@PostMapping
|
||||
public Room save(@RequestBody Room r) {
|
||||
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 Long userId = userRepository.findByUsername(username).getId();
|
||||
|
||||
r.setUserId(userId);
|
||||
r.setUser(null);
|
||||
|
||||
return roomRepository.save(r);
|
||||
}
|
||||
|
||||
|
|
|
@ -21,9 +21,16 @@ public abstract class Device {
|
|||
|
||||
/** The room this device belongs in */
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "room_id")
|
||||
@JoinColumn(name = "room_id", nullable = false, updatable = false, insertable = false)
|
||||
private Room room;
|
||||
|
||||
/**
|
||||
* The room this device belongs in, as a foreign key id. To use when updating and inserting from
|
||||
* a REST call.
|
||||
*/
|
||||
@Column(name = "room_id")
|
||||
private Long roomId;
|
||||
|
||||
/** The name of the device as assigned by the user (e.g. 'Master bedroom light') */
|
||||
@Column private String name;
|
||||
|
||||
|
|
|
@ -18,21 +18,28 @@ public class Room {
|
|||
* 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)
|
||||
@Column(name = "icon", nullable = false)
|
||||
private byte[] icon;
|
||||
|
||||
@Column(name = "image", updatable = true, nullable = false)
|
||||
@Column(name = "image", nullable = false)
|
||||
private byte[] image;
|
||||
|
||||
/** User that owns the house this room is in */
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "user_id")
|
||||
@JoinColumn(name = "user_id", nullable = false, updatable = false, insertable = false)
|
||||
private User user;
|
||||
|
||||
/**
|
||||
* User that owns the house this room is in as a foreign key id. To use when updating and
|
||||
* inserting from a REST call.
|
||||
*/
|
||||
@Column(name = "user_id")
|
||||
private Long userId;
|
||||
|
||||
/** The user given name of this room (e.g. 'Master bedroom') */
|
||||
@Column private String name;
|
||||
|
||||
/** Collectiion of devices present in this room */
|
||||
/** Collection of devices present in this room */
|
||||
@OneToMany(mappedBy = "room")
|
||||
private Set<Device> devices;
|
||||
|
||||
|
@ -44,6 +51,14 @@ public class Room {
|
|||
this.id = id;
|
||||
}
|
||||
|
||||
public Long getUserId() {
|
||||
return userId;
|
||||
}
|
||||
|
||||
public void setUserId(Long userId) {
|
||||
this.userId = userId;
|
||||
}
|
||||
|
||||
public User getUser() {
|
||||
return user;
|
||||
}
|
||||
|
@ -66,16 +81,6 @@ public class Room {
|
|||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Room{"
|
||||
+ "id="
|
||||
+ id
|
||||
+ ", user="
|
||||
+ user
|
||||
+ ", name='"
|
||||
+ name
|
||||
+ '\''
|
||||
+ ", devices="
|
||||
+ devices
|
||||
+ '}';
|
||||
return "Room{" + "id=" + id + ", user=" + user + ", name='" + name + "\'}";
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue