started guest controllers (WIP)
This commit is contained in:
parent
566a2e72e3
commit
36a7a649d5
6 changed files with 103 additions and 9 deletions
|
@ -0,0 +1,39 @@
|
|||
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.GuestUserResponse;
|
||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException;
|
||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.User;
|
||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.UserRepository;
|
||||
import java.security.Principal;
|
||||
import java.util.List;
|
||||
import javax.validation.Valid;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
public class GuestController {
|
||||
|
||||
@Autowired private UserRepository userRepository;
|
||||
|
||||
@GetMapping
|
||||
public List<User> findAll() {
|
||||
return toList(userRepository.findAll());
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
public void addUserAsGuest(long id, final Principal principal) throws NotFoundException {
|
||||
User guest = userRepository.findById(id).orElseThrow(NotFoundException::new);
|
||||
User host = userRepository.findByUsername(principal.getName());
|
||||
|
||||
host.addGuest(guest);
|
||||
userRepository.save(guest);
|
||||
userRepository.save(host);
|
||||
}
|
||||
|
||||
@PutMapping
|
||||
public void updatePermissions(@Valid @RequestBody GuestUserResponse g, Principal principal) {}
|
||||
|
||||
@DeleteMapping
|
||||
public void removeUserAsGuest(long id) {}
|
||||
}
|
|
@ -26,10 +26,10 @@ import org.springframework.web.bind.annotation.RestController;
|
|||
@RequestMapping("/scene")
|
||||
public class SceneController {
|
||||
|
||||
@Autowired SceneRepository sceneService;
|
||||
@Autowired UserRepository userService;
|
||||
@Autowired StateRepository<State<?>> stateService;
|
||||
@Autowired DeviceRepository<Device> deviceRepository;
|
||||
@Autowired private SceneRepository sceneService;
|
||||
@Autowired private UserRepository userService;
|
||||
@Autowired private StateRepository<State<?>> stateService;
|
||||
@Autowired private DeviceRepository<Device> deviceRepository;
|
||||
|
||||
@GetMapping
|
||||
public List<Scene> findAll(Principal principal) {
|
||||
|
@ -91,6 +91,8 @@ public class SceneController {
|
|||
newScene.setName(s.getName());
|
||||
}
|
||||
|
||||
newScene.setGuestAccessEnabled(s.isGuestAccessEnabled());
|
||||
|
||||
return sceneService.save(newScene);
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,14 @@
|
|||
package ch.usi.inf.sa4.sanmarinoes.smarthut.dto;
|
||||
|
||||
|
||||
public class GuestUserResponse {
|
||||
private boolean cameraEnabled;
|
||||
|
||||
public boolean isCameraEnabled() {
|
||||
return cameraEnabled;
|
||||
}
|
||||
|
||||
public void setCameraEnabled(boolean cameraEnabled) {
|
||||
this.cameraEnabled = cameraEnabled;
|
||||
}
|
||||
}
|
|
@ -1,6 +1,7 @@
|
|||
package ch.usi.inf.sa4.sanmarinoes.smarthut.dto;
|
||||
|
||||
import com.sun.istack.NotNull;
|
||||
import javax.persistence.Column;
|
||||
|
||||
public class SceneSaveRequest {
|
||||
|
||||
|
@ -10,6 +11,17 @@ public class SceneSaveRequest {
|
|||
/** The user given name of this room (e.g. 'Master bedroom') */
|
||||
@NotNull private String name;
|
||||
|
||||
/** Determines whether a guest can access this scene */
|
||||
@Column @NotNull private boolean guestAccessEnabled;
|
||||
|
||||
public boolean isGuestAccessEnabled() {
|
||||
return guestAccessEnabled;
|
||||
}
|
||||
|
||||
public void setGuestAccessEnabled(boolean guestAccessEnabled) {
|
||||
this.guestAccessEnabled = guestAccessEnabled;
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
|
|
@ -39,6 +39,17 @@ public class Scene {
|
|||
@Column(nullable = false)
|
||||
private String name;
|
||||
|
||||
/** Determines whether a guest can access this scene */
|
||||
@Column private boolean guestAccessEnabled;
|
||||
|
||||
public boolean isGuestAccessEnabled() {
|
||||
return guestAccessEnabled;
|
||||
}
|
||||
|
||||
public void setGuestAccessEnabled(boolean guestAccessEnabled) {
|
||||
this.guestAccessEnabled = guestAccessEnabled;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
|
|
@ -6,7 +6,6 @@ import java.util.HashSet;
|
|||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import javax.persistence.*;
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
/** A user of the Smarthut application */
|
||||
@Entity(name = "smarthutuser")
|
||||
|
@ -56,10 +55,7 @@ public class User {
|
|||
private Set<User> hosts = new HashSet<>();
|
||||
|
||||
/** Determines whether a guest can access security cameras */
|
||||
@Column @NotNull private boolean cameraEnabled;
|
||||
|
||||
/** Determines whether a guest can access scenes */
|
||||
@Column @NotNull private boolean sceneEnabled;
|
||||
@Column private boolean cameraEnabled;
|
||||
|
||||
@Column(nullable = false)
|
||||
@GsonExclude
|
||||
|
@ -113,6 +109,26 @@ public class User {
|
|||
isEnabled = enabled;
|
||||
}
|
||||
|
||||
public Set<User> getGuests() {
|
||||
return guests;
|
||||
}
|
||||
|
||||
public Set<User> getHosts() {
|
||||
return hosts;
|
||||
}
|
||||
|
||||
public boolean isCameraEnabled() {
|
||||
return cameraEnabled;
|
||||
}
|
||||
|
||||
public void addGuest(User guest) {
|
||||
this.guests.add(guest);
|
||||
}
|
||||
|
||||
public void setCameraEnabled(boolean cameraEnabled) {
|
||||
this.cameraEnabled = cameraEnabled;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "User{"
|
||||
|
|
Loading…
Reference in a new issue