Alter 2 files
Update `db.py` Update `endpoints.py`
This commit is contained in:
parent
6fa3a29264
commit
d684d3c12b
2 changed files with 21 additions and 3 deletions
|
@ -13,7 +13,7 @@ class Entry:
|
|||
image_url: Optional[str]
|
||||
|
||||
def validate(self, only_populated_fields=False) -> Tuple[bool, str]:
|
||||
if self.name == "" and not only_populated_fields:
|
||||
if (self.name == "" or self.name is None) and not only_populated_fields:
|
||||
return False, "name cannot be empty"
|
||||
|
||||
if self.votes < 0:
|
||||
|
@ -25,6 +25,9 @@ class Entry:
|
|||
except Exception:
|
||||
return False, "invalid URL"
|
||||
|
||||
if self.location_lat is None or self.location_long is None:
|
||||
return False, "missing locations"
|
||||
|
||||
return True, ""
|
||||
|
||||
def as_dict(self) -> Dict:
|
||||
|
|
|
@ -21,7 +21,7 @@ class Endpoints:
|
|||
app.add_url_rule(paths.CREATE_ENTRY, view_func=self.create_entry, methods=["POST"])
|
||||
|
||||
def list_entries(self):
|
||||
# TODO: populate from databaase
|
||||
# TODO: populate from databaase
|
||||
|
||||
a = db.Entry("203fc6a0-9587-41a4-9862-e1b72039b98b", "Birmingham Duck Pond", -1.2345, 33.4567, 0, None)
|
||||
b = db.Entry("b140e048-ea2c-4827-b670-ef41ba48c56d", "Northwich Duck Pond", -3.2345, 25.4567, 0, None)
|
||||
|
@ -44,7 +44,22 @@ class Endpoints:
|
|||
if body is None:
|
||||
return "no JSON body", 400
|
||||
|
||||
# TODO: validate inputs
|
||||
coordinates = body.get("location", None)
|
||||
if coordinates is None:
|
||||
return "missing location", 400
|
||||
|
||||
new_entry = db.Entry(
|
||||
uuid.uuid4(),
|
||||
body.get("name"),
|
||||
coordinates.get("lat"),
|
||||
coordinates.get("long"),
|
||||
0,
|
||||
body.get("imageURL"))
|
||||
|
||||
validation_result, error_text = new_entry.validate()
|
||||
|
||||
if not validation_result:
|
||||
return flask.abort(400, error_text)
|
||||
|
||||
# TODO: store in database
|
||||
|
||||
|
|
Reference in a new issue