Added GET /device route
This commit is contained in:
parent
7e53ccd608
commit
9c60475e92
2 changed files with 17 additions and 4 deletions
|
@ -6,13 +6,12 @@ import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException;
|
||||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Device;
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Device;
|
||||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.DeviceRepository;
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.DeviceRepository;
|
||||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.RoomRepository;
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.RoomRepository;
|
||||||
|
import java.security.Principal;
|
||||||
|
import java.util.List;
|
||||||
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.EnableAutoConfiguration;
|
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||||
import org.springframework.web.bind.annotation.PutMapping;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import org.springframework.web.bind.annotation.RequestBody;
|
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@EnableAutoConfiguration
|
@EnableAutoConfiguration
|
||||||
|
@ -22,6 +21,11 @@ public class DeviceController {
|
||||||
@Autowired private DeviceRepository<Device> deviceRepository;
|
@Autowired private DeviceRepository<Device> deviceRepository;
|
||||||
@Autowired private RoomRepository roomRepository;
|
@Autowired private RoomRepository roomRepository;
|
||||||
|
|
||||||
|
@GetMapping
|
||||||
|
public List<Device> getAll(final Principal user) {
|
||||||
|
return deviceRepository.findAllByUsername(user.getName());
|
||||||
|
}
|
||||||
|
|
||||||
@PutMapping
|
@PutMapping
|
||||||
public Device update(@Valid @RequestBody DeviceSaveRequest deviceSaveRequest)
|
public Device update(@Valid @RequestBody DeviceSaveRequest deviceSaveRequest)
|
||||||
throws NotFoundException, BadDataException {
|
throws NotFoundException, BadDataException {
|
||||||
|
|
|
@ -23,6 +23,15 @@ public interface DeviceRepository<T extends Device> extends CrudRepository<T, Lo
|
||||||
@Query("SELECT d FROM Device d JOIN d.room r JOIN r.user u WHERE d.id = ?1 AND u.username = ?2")
|
@Query("SELECT d FROM Device d JOIN d.room r JOIN r.user u WHERE d.id = ?1 AND u.username = ?2")
|
||||||
Optional<T> findByIdAndUsername(Long id, String username);
|
Optional<T> findByIdAndUsername(Long id, String username);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Finds all devices belonging to a user
|
||||||
|
*
|
||||||
|
* @param username the User's username
|
||||||
|
* @return all devices of that user
|
||||||
|
*/
|
||||||
|
@Query("SELECT d FROM Device d JOIN d.room r JOIN r.user u WHERE u.username = ?1")
|
||||||
|
List<T> findAllByUsername(String username);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Find the user associated with a device through a room
|
* Find the user associated with a device through a room
|
||||||
*
|
*
|
||||||
|
|
Loading…
Reference in a new issue