Merge branch '32-add-get-device-call-to-get-all-devices-of-a-user' into 'dev'
Resolve "Add GET /device call to get all devices of a user" Closes #32 See merge request sa4-2020/the-sanmarinoes/backend!40
This commit is contained in:
commit
a79c5d8671
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.DeviceRepository;
|
||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.RoomRepository;
|
||||
import java.security.Principal;
|
||||
import java.util.List;
|
||||
import javax.validation.Valid;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.web.bind.annotation.PutMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@RestController
|
||||
@EnableAutoConfiguration
|
||||
|
@ -22,6 +21,11 @@ public class DeviceController {
|
|||
@Autowired private DeviceRepository<Device> deviceRepository;
|
||||
@Autowired private RoomRepository roomRepository;
|
||||
|
||||
@GetMapping
|
||||
public List<Device> getAll(final Principal user) {
|
||||
return deviceRepository.findAllByUsername(user.getName());
|
||||
}
|
||||
|
||||
@PutMapping
|
||||
public Device update(@Valid @RequestBody DeviceSaveRequest deviceSaveRequest)
|
||||
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")
|
||||
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
|
||||
*
|
||||
|
|
Loading…
Reference in a new issue