Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,26 @@ def get_applicants():
data = db.get_all(table_name="zap-applications")
return data

@app.route("/create", methods=["POST"])
def create_listing():
"""Creates a new listing with given information
"""
data = app.current_request.json_body
listing_id = str(uuid.uuid4())
data["listingId"] = listing_id

db.put_data(table_name="zap-listings", data=data)

return {"msg": True}

@app.route("/listings", methods=["GET"])
def get_all_listings():
"""Gets all listings available
"""
data = db.get_all(table_name="zap-listings")
return data



"""
TODO:
Expand Down
37 changes: 31 additions & 6 deletions chalicelib/db.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,59 @@
import os
import boto3
from botocore import errorfactory


class DBResource:
def __init__(self):
self.is_prod = os.environ.get("ENV") == "prod"
self.db = boto3.resource("dynamodb")
self.resource = boto3.resource("dynamodb")
self.primary_keys = {"zap-listings": "listingId"}

# def create_table(self, table_name: str, primary_key: str):
# """Creates a table with a given primary_key"""
# table = self.resource.create_table(
# TableName=table_name,
# KeySchema=[
# {
# "AttributeName": primary_key,
# "KeyType": "HASH", # Assuming you have a hash key
# }
# ],
# AttributeDefinitions=[
# {
# "AttributeName": primary_key,
# "AttributeType": "S", # Assuming it's a string
# }
# ],
# ProvisionedThroughput={"ReadCapacityUnits": 5, "WriteCapacityUnits": 5},
# )
# # Wait until the table exists
# table.meta.client.get_waiter("table_exists").wait(TableName=table_name)
# print(table)
# return table

def put_data(self, table_name: str, data):
if self.is_prod:
table_name += "-prod"
else:
table_name += "-dev"

table = self.db.Table(table_name)
table = self.resource.Table(table_name)

table.put_item(Item=data)


def get_all(self, table_name: str):
if self.is_prod:
table_name += "-prod"
else:
table_name += "-dev"

table = self.db.Table(table_name)
table = self.resource.Table(table_name)

# Use the scan operation to retrieve all items from the table
response = table.scan()

# The response contains the items as well as other information like metadata
items = response.get("Items", [])

return items
return items