-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Class with definitions of routes for endpoints
- Loading branch information
Showing
11 changed files
with
425 additions
and
3 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
src/main/java/io/patriotframework/virtualsmarthomeplus/APIRoutes.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package io.patriotframework.virtualsmarthomeplus; | ||
|
||
import org.springframework.stereotype.Service; | ||
|
||
|
||
/** | ||
* This class serves as resource of routes to API endpoints. | ||
*/ | ||
@Service | ||
public class APIRoutes { | ||
|
||
/** | ||
* Beginning of the all api routes. All api endpoints share this prefix. | ||
*/ | ||
public static final String API_ROUTE = "/api/{apiVersion}/"; | ||
|
||
/** | ||
* Route of the house. | ||
*/ | ||
public static final String HOUSE_ROUTE = API_ROUTE + "house/"; | ||
|
||
/** | ||
* Route of the device. | ||
*/ | ||
public static final String DEVICE_ROUTE = HOUSE_ROUTE + "device/"; | ||
|
||
/** | ||
* Route of the final device fireplace. | ||
*/ | ||
public static final String FIREPLACE_ROUTE = DEVICE_ROUTE + "fireplace/"; | ||
|
||
/** | ||
* Route of the final device door. | ||
*/ | ||
public static final String DOOR_ROUTE = DEVICE_ROUTE + "door/";; | ||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/io/patriotframework/virtualsmarthomeplus/APIVersions.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package io.patriotframework.virtualsmarthomeplus; | ||
|
||
/** | ||
* This class serves as resource of routes to API endpoints. | ||
*/ | ||
public class APIVersions { | ||
/** | ||
* Api version. | ||
*/ | ||
public static final String V0_1 = "v0.1"; | ||
|
||
/** | ||
* Api version. This version is not implemented yet! | ||
*/ | ||
public static final String V1_0 = "v1.0"; | ||
} |
55 changes: 55 additions & 0 deletions
55
src/main/java/io/patriotframework/virtualsmarthomeplus/controllers/DeviceController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package io.patriotframework.virtualsmarthomeplus.controllers; | ||
|
||
import io.patriotframework.virtualsmarthomeplus.APIRoutes; | ||
import io.patriotframework.virtualsmarthomeplus.APIVersions; | ||
import io.patriotframework.virtualsmarthomeplus.house.House; | ||
import io.patriotframework.virtualsmarthomeplus.house.devices.Device; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import org.springframework.web.server.ResponseStatusException; | ||
import java.util.ArrayList; | ||
|
||
|
||
|
||
@RestController | ||
public class DeviceController { | ||
private final House house; | ||
private static final String DEVICE_ID_ROUTE = APIRoutes.DEVICE_ROUTE + "{label}"; | ||
|
||
@Autowired | ||
public DeviceController(House house) { | ||
this.house = house; | ||
} | ||
|
||
@GetMapping(APIRoutes.DEVICE_ROUTE) | ||
public ArrayList<Device> getDevices(@PathVariable String apiVersion) { | ||
if(apiVersion.equals(APIVersions.V0_1)) { | ||
return new ArrayList<>((house.getDevicesOfType(Device.class).values())); | ||
} | ||
|
||
throw new ResponseStatusException( | ||
HttpStatus.NOT_FOUND, String.format("Unknown api version: %s", apiVersion) // 404 | ||
); | ||
} | ||
|
||
@GetMapping(DEVICE_ID_ROUTE) | ||
public Device getDevice(@PathVariable String label, @PathVariable String apiVersion) { | ||
if(apiVersion.equals(APIVersions.V0_1)) { | ||
Device retrievedDevice = house.getDevice(label); | ||
if(retrievedDevice == null) { | ||
throw new ResponseStatusException( | ||
HttpStatus.NOT_FOUND, "device with given label not found" // 404 | ||
); | ||
} | ||
|
||
return retrievedDevice; | ||
} | ||
|
||
throw new ResponseStatusException( | ||
HttpStatus.NOT_FOUND, String.format("Unknown api version: %s", apiVersion) // 404 | ||
); | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
src/main/java/io/patriotframework/virtualsmarthomeplus/controllers/DoorController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package io.patriotframework.virtualsmarthomeplus.controllers; | ||
|
||
import io.patriotframework.virtualsmarthomeplus.APIRoutes; | ||
import io.patriotframework.virtualsmarthomeplus.house.House; | ||
import io.patriotframework.virtualsmarthomeplus.house.devices.Device; | ||
import io.patriotframework.virtualsmarthomeplus.house.devices.finalDevices.Door; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
|
||
/** | ||
* Handles the POST, GET, PUT and DELETE requests on Door route: {@link APIRoutes#DOOR_ROUTE} | ||
*/ | ||
@RestController | ||
public class DoorController extends FinalDeviceHandling { | ||
private static final String DOOR_ID_ROUTE = APIRoutes.DOOR_ROUTE + "{label}"; | ||
|
||
@Autowired | ||
DoorController(House house) { | ||
super(house); | ||
} | ||
|
||
/** | ||
* Returns the door | ||
* @param label label specified in route | ||
* @param apiVersion api version specified in route | ||
* @return door if present in the house | ||
*/ | ||
@GetMapping(DOOR_ID_ROUTE) | ||
public Device getDoor(@PathVariable String label, @PathVariable String apiVersion) { | ||
return handleGet(label, Door.class, apiVersion); | ||
} | ||
|
||
/** | ||
* Creates the door | ||
* @param device new door | ||
* @param apiVersion api version specified in route | ||
* @return door added to the house | ||
*/ | ||
@PostMapping(APIRoutes.DOOR_ROUTE) | ||
public Device postDoor(@RequestBody Door device, @PathVariable String apiVersion) { | ||
return handlePost(device, apiVersion); | ||
} | ||
|
||
/** | ||
* Updates or creates the door | ||
* @param device updated door | ||
* @param apiVersion api version specified in route | ||
* @return door updated or added to the house | ||
*/ | ||
@PutMapping(APIRoutes.DOOR_ROUTE) | ||
public Device putDoor(@RequestBody Door device, @PathVariable String apiVersion) { | ||
return handlePut(device, apiVersion); | ||
} | ||
|
||
/** | ||
* Deletes the door | ||
* @param label label of the door to be deleted | ||
* @param apiVersion api version specified in route | ||
* @return "OK" if door exists in the house and was deleted | ||
*/ | ||
@DeleteMapping(DOOR_ID_ROUTE) | ||
public String deleteDoor(@PathVariable String label, @PathVariable String apiVersion) { | ||
return handleDelete(label, Door.class, apiVersion); | ||
} | ||
} |
132 changes: 132 additions & 0 deletions
132
src/main/java/io/patriotframework/virtualsmarthomeplus/controllers/FinalDeviceHandling.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
package io.patriotframework.virtualsmarthomeplus.controllers; | ||
|
||
import io.patriotframework.virtualsmarthomeplus.APIVersions; | ||
import io.patriotframework.virtualsmarthomeplus.house.House; | ||
import io.patriotframework.virtualsmarthomeplus.house.devices.Device; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.server.ResponseStatusException; | ||
|
||
|
||
|
||
@Component | ||
public class FinalDeviceHandling { | ||
|
||
private final House house; | ||
|
||
/** | ||
* string that will be returned after successful deletion | ||
*/ | ||
public static final String DELETED_RESPONSE = "OK"; | ||
|
||
@Autowired | ||
FinalDeviceHandling(House house) { | ||
this.house = house; | ||
} | ||
|
||
/** | ||
* Serving method for get requests on the final device. | ||
* @param label label of the requested device | ||
* @param deviceClass class of the requested device | ||
* @param apiVersion used api version | ||
* @throws ResponseStatusException 404 if device is not present in the house or invalid API version is demanded | ||
* @return device of specified class with given label if present in the house | ||
*/ | ||
public Device handleGet(String label, Class<? extends Device> deviceClass, String apiVersion) { | ||
if(apiVersion.equals(APIVersions.V0_1)) { | ||
final Device retrievedDevice = house.getDevicesOfType(deviceClass).get(label); | ||
|
||
if (retrievedDevice == null) { | ||
throw new ResponseStatusException( | ||
HttpStatus.NOT_FOUND, "device with given label not found" // 404 | ||
); | ||
} | ||
return retrievedDevice; | ||
} | ||
|
||
throw new ResponseStatusException( | ||
HttpStatus.NOT_FOUND, String.format("Unknown api version: %s", apiVersion) // 404 | ||
); | ||
} | ||
|
||
/** | ||
* Serving method for post requests on the final device. | ||
* @param device device to add to the house | ||
* @param apiVersion used api version | ||
* @throws ResponseStatusException 409 if device already exists in the house, 404 if invalid API version is demanded | ||
* @return device of specified class with given label if present in the house | ||
*/ | ||
public Device handlePost(Device device, String apiVersion) { | ||
if(apiVersion.equals(APIVersions.V0_1)) { | ||
final Device checkForConflict = house | ||
.getDevicesOfType(device.getClass()) | ||
.get(device.getLabel()); | ||
if (checkForConflict != null) { | ||
throw new ResponseStatusException( | ||
HttpStatus.CONFLICT, "device with given label already exists" // 409 | ||
); | ||
} | ||
|
||
house.addDevice(device); | ||
return house.getDevice(device.getLabel()); | ||
} | ||
|
||
throw new ResponseStatusException( | ||
HttpStatus.NOT_FOUND, String.format("Unknown api version: %s", apiVersion) // 404 | ||
); | ||
} | ||
|
||
/** | ||
* Serving method for put requests on the final device. | ||
* @param device device to update or add to the house | ||
* @param apiVersion used api version | ||
* @throws ResponseStatusException 404 if invalid API version is demanded | ||
* @return updated device of specified class with given label if present in the house | ||
*/ | ||
public Device handlePut(Device device, String apiVersion) { | ||
if(apiVersion.equals(APIVersions.V0_1)) { | ||
final Device deviceInHouse = house | ||
.getDevicesOfType(device.getClass()) | ||
.get(device.getLabel()); | ||
if (deviceInHouse == null) { | ||
house.addDevice(device); | ||
} else { | ||
house.updateDevice(device); | ||
} | ||
|
||
return house.getDevice(device.getLabel()); | ||
} | ||
|
||
throw new ResponseStatusException( | ||
HttpStatus.NOT_FOUND, String.format("Unknown api version: %s", apiVersion) // 404 | ||
); | ||
} | ||
|
||
/** | ||
* Serving method for delete requests on the final device. | ||
* @param label label of the device to delete | ||
* @param deviceClass class of the device requested to delete | ||
* @param apiVersion used api version | ||
* @throws ResponseStatusException 404 if invalid API version is demanded | ||
* @return {@link FinalDeviceHandling#DELETED_RESPONSE} if device of specified class with given label was deleted | ||
* from the house | ||
*/ | ||
public String handleDelete(String label, Class<? extends Device> deviceClass, String apiVersion) { | ||
if(apiVersion.equals(APIVersions.V0_1)) { | ||
final Device retrievedDevice = house.getDevicesOfType(deviceClass).get(label); | ||
|
||
if (retrievedDevice == null) { | ||
throw new ResponseStatusException( | ||
HttpStatus.NOT_FOUND, "device with given label not found" // 404 | ||
); | ||
} | ||
house.removeDevice(label); | ||
return DELETED_RESPONSE; | ||
} | ||
|
||
throw new ResponseStatusException( | ||
HttpStatus.NOT_FOUND, String.format("Unknown api version: %s", apiVersion) // 404 | ||
); | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
src/main/java/io/patriotframework/virtualsmarthomeplus/controllers/FireplaceController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package io.patriotframework.virtualsmarthomeplus.controllers; | ||
|
||
import io.patriotframework.virtualsmarthomeplus.APIRoutes; | ||
import io.patriotframework.virtualsmarthomeplus.house.House; | ||
import io.patriotframework.virtualsmarthomeplus.house.devices.Device; | ||
import io.patriotframework.virtualsmarthomeplus.house.devices.finalDevices.Door; | ||
import io.patriotframework.virtualsmarthomeplus.house.devices.finalDevices.Fireplace; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
/** | ||
* Handles the POST, GET, PUT and DELETE requests on Fireplace route: {@link APIRoutes#FIREPLACE_ROUTE} | ||
*/ | ||
@RestController | ||
public class FireplaceController extends FinalDeviceHandling { | ||
private static final String FIREPLACE_ID_ROUTE = APIRoutes.FIREPLACE_ROUTE + "{label}"; | ||
|
||
@Autowired | ||
FireplaceController(House house) { | ||
super(house); | ||
} | ||
|
||
/** | ||
* Returns the fireplace | ||
* @param label label specified in route | ||
* @param apiVersion api version specified in route | ||
* @return Fireplace if present in the house | ||
*/ | ||
@GetMapping(FIREPLACE_ID_ROUTE) | ||
public Device getFireplace(@PathVariable String label, @PathVariable String apiVersion) { | ||
return handleGet(label, Fireplace.class, apiVersion); | ||
} | ||
|
||
/** | ||
* Creates the fireplace | ||
* @param device new fireplace | ||
* @param apiVersion api version specified in route | ||
* @return fireplace added to the house | ||
*/ | ||
@PostMapping(APIRoutes.FIREPLACE_ROUTE) | ||
public Device postFireplace(@RequestBody Door device, @PathVariable String apiVersion) { | ||
return handlePost(device, apiVersion); | ||
} | ||
|
||
/** | ||
* Updates or creates the fireplace | ||
* @param device updated fireplace | ||
* @param apiVersion api version specified in route | ||
* @return fireplace updated or added to the house | ||
*/ | ||
@PutMapping(APIRoutes.FIREPLACE_ROUTE) | ||
public Device putFireplace(@RequestBody Door device, @PathVariable String apiVersion) { | ||
return handlePut(device, apiVersion); | ||
} | ||
|
||
/** | ||
* Deletes the fireplace | ||
* @param label label of the fireplace to be deleted | ||
* @param apiVersion api version specified in route | ||
* @return "OK" if fireplace exists in the house and was deleted | ||
*/ | ||
@DeleteMapping(FIREPLACE_ID_ROUTE) | ||
public String deleteFireplace(@PathVariable String label, @PathVariable String apiVersion) { | ||
return handleDelete(label, Fireplace.class, apiVersion); | ||
} | ||
} |
Oops, something went wrong.