summaryrefslogtreecommitdiff
path: root/opendc/api/v1/rooms
diff options
context:
space:
mode:
authorGeorgios Andreadis <info@gandreadis.com>2020-06-23 18:08:28 +0200
committerGeorgios Andreadis <info@gandreadis.com>2020-06-23 18:08:28 +0200
commit565ede0dc50c3b2df09c066ea3a28a4901cce547 (patch)
treee81a1bbed43d2eaf7e2be0bc61007963f807b62d /opendc/api/v1/rooms
parent6f41be7d9c244b67bfa5ff72f1e90d18fa45b590 (diff)
Add DB handlers and rename to v2
Diffstat (limited to 'opendc/api/v1/rooms')
-rw-r--r--opendc/api/v1/rooms/__init__.py0
-rw-r--r--opendc/api/v1/rooms/roomId/__init__.py0
-rw-r--r--opendc/api/v1/rooms/roomId/endpoint.py137
-rw-r--r--opendc/api/v1/rooms/roomId/tiles/__init__.py0
-rw-r--r--opendc/api/v1/rooms/roomId/tiles/endpoint.py121
5 files changed, 0 insertions, 258 deletions
diff --git a/opendc/api/v1/rooms/__init__.py b/opendc/api/v1/rooms/__init__.py
deleted file mode 100644
index e69de29b..00000000
--- a/opendc/api/v1/rooms/__init__.py
+++ /dev/null
diff --git a/opendc/api/v1/rooms/roomId/__init__.py b/opendc/api/v1/rooms/roomId/__init__.py
deleted file mode 100644
index e69de29b..00000000
--- a/opendc/api/v1/rooms/roomId/__init__.py
+++ /dev/null
diff --git a/opendc/api/v1/rooms/roomId/endpoint.py b/opendc/api/v1/rooms/roomId/endpoint.py
deleted file mode 100644
index 1dfc32cc..00000000
--- a/opendc/api/v1/rooms/roomId/endpoint.py
+++ /dev/null
@@ -1,137 +0,0 @@
-from opendc.models.room import Room
-from opendc.util import exceptions
-from opendc.util.rest import Response
-
-
-def GET(request):
- """Get this Room."""
-
- # Make sure required parameters are there
-
- try:
- request.check_required_parameters(
- path={
- 'roomId': 'int'
- }
- )
-
- except exceptions.ParameterError as e:
- return Response(400, e.message)
-
- # Instantiate a Room from the database
-
- room = Room.from_primary_key((request.params_path['roomId'],))
-
- # Make sure this Room exists
-
- if not room.exists():
- return Response(404, '{} not found.'.format(room))
-
- # Make sure this user is authorized to view this Room
-
- if not room.google_id_has_at_least(request.google_id, 'VIEW'):
- return Response(403, 'Forbidden from retrieving {}.'.format(room))
-
- # Return this Room
-
- room.read()
-
- return Response(
- 200,
- 'Successfully retrieved {}.'.format(room),
- room.to_JSON()
- )
-
-
-def PUT(request):
- """Update this Room's name and type."""
-
- # Make sure required parameters are there
-
- try:
- request.check_required_parameters(
- path={
- 'roomId': 'int'
- },
- body={
- 'room': {
- 'name': 'string',
- 'roomType': 'string'
- }
- }
- )
-
- except exceptions.ParameterError as e:
- return Response(400, e.message)
-
- # Instantiate a Room from the database
-
- room = Room.from_primary_key((request.params_path['roomId'],))
-
- # Make sure this Room exists
-
- if not room.exists():
- return Response(404, '{} not found.'.format(room))
-
- # Make sure this user is authorized to edit this Room
-
- if not room.google_id_has_at_least(request.google_id, 'EDIT'):
- return Response(403, 'Forbidden from updating {}.'.format(room))
-
- # Update this Room
-
- room.name = request.params_body['room']['name']
- room.type = request.params_body['room']['roomType']
-
- try:
- room.update()
- except exceptions.ForeignKeyError:
- return Response(400, 'Invalid `roomType` or existing `name`.')
-
- # Return this Room
-
- return Response(
- 200,
- 'Successfully updated {}.'.format(room),
- room.to_JSON()
- )
-
-
-def DELETE(request):
- """Delete this Room."""
-
- # Make sure required parameters are there
-
- try:
- request.check_required_parameters(
- path={
- 'roomId': 'int'
- }
- )
-
- except exceptions.ParameterError as e:
- return Response(400, e.message)
-
- # Instantiate a Room and make sure it exists
-
- room = Room.from_primary_key((request.params_path['roomId'],))
-
- if not room.exists():
- return Response(404, '{} not found.'.format(room))
-
- # Make sure this user is authorized to delete this Room
-
- if not room.google_id_has_at_least(request.google_id, 'EDIT'):
- return Response(403, 'Forbidden from deleting {}.'.format(room))
-
- # Delete this Room
-
- room.delete()
-
- # Return this Room
-
- return Response(
- 200,
- 'Successfully deleted {}.'.format(room),
- room.to_JSON()
- )
diff --git a/opendc/api/v1/rooms/roomId/tiles/__init__.py b/opendc/api/v1/rooms/roomId/tiles/__init__.py
deleted file mode 100644
index e69de29b..00000000
--- a/opendc/api/v1/rooms/roomId/tiles/__init__.py
+++ /dev/null
diff --git a/opendc/api/v1/rooms/roomId/tiles/endpoint.py b/opendc/api/v1/rooms/roomId/tiles/endpoint.py
deleted file mode 100644
index a4ef51e7..00000000
--- a/opendc/api/v1/rooms/roomId/tiles/endpoint.py
+++ /dev/null
@@ -1,121 +0,0 @@
-from opendc.models.room import Room
-from opendc.models.tile import Tile
-from opendc.util import exceptions
-from opendc.util.rest import Response
-
-
-def GET(request):
- """Get this Room's Tiles."""
-
- # Make sure required parameters are there
-
- try:
- request.check_required_parameters(
- path={
- 'roomId': 'int'
- }
- )
-
- except exceptions.ParameterError as e:
- return Response(400, e.message)
-
- # Instantiate a Room from the database
-
- room = Room.from_primary_key((request.params_path['roomId'],))
-
- # Make sure this Room exists
-
- if not room.exists():
- return Response(404, '{} not found.'.format(room))
-
- # Make sure this user is authorized to view this Room's Tiles
-
- if not room.google_id_has_at_least(request.google_id, 'VIEW'):
- return Response(403, 'Forbidden from viewing Tiles for {}.'.format(room))
-
- # Get and return the Tiles
-
- tiles = Tile.query('room_id', room.id)
-
- for tile in tiles:
- tile.read()
-
- return Response(
- 200,
- 'Successfully retrieved Tiles for {}.'.format(room),
- [x.to_JSON() for x in tiles]
- )
-
-
-def POST(request):
- """Add a Tile."""
-
- # Make sure required parameters are there
-
- try:
- request.check_required_parameters(
- path={
- 'roomId': 'int'
- },
- body={
- 'tile': {
- 'roomId': 'int',
- 'positionX': 'int',
- 'positionY': 'int'
- }
- }
- )
-
- except exceptions.ParameterError as e:
- return Response(400, e.message)
-
- if request.params_path['roomId'] != request.params_body['tile']['roomId']:
- return Response(400, 'ID mismatch')
-
- # Instantiate a Room from the database
-
- room = Room.from_primary_key((request.params_path['roomId'],))
-
- # Make sure this Room exists
-
- if not room.exists():
- return Response(404, '{} not found.'.format(room))
-
- # Make sure this user is authorized to edit this Room's Tiles
-
- if not room.google_id_has_at_least(request.google_id, 'EDIT'):
- return Response(403, 'Forbidden from adding Tiles to {}.'.format(room))
-
- # Clean the tile JSON
-
- tile_json = request.params_body['tile']
-
- tile_json['objectId'] = None
- tile_json['objectType'] = None
-
- # Instantiate a Tile
-
- tile = Tile.from_JSON(tile_json)
-
- # Try to insert this Tile
-
- try:
- tile.insert()
-
- except exceptions.ForeignKeyError as e:
-
- if e.message == 'OccupiedTilePosition':
- return Response(409, 'Tile position occupied.')
-
- elif e.message == 'InvalidTilePosition':
- return Response(400, 'Invalid Tile position (new Tiles must neighbor existing Tiles).')
-
- # Return this Tile
-
- tile.read()
-
- return Response(
- 200,
- 'Successfully added {}.'.format(tile),
- tile.to_JSON()
- )