Skip to content

Commit

Permalink
Merge pull request #1 from ZacHooper/dev/add-coffee-options
Browse files Browse the repository at this point in the history
Dev/add coffee options
  • Loading branch information
ZacHooper authored Feb 29, 2024
2 parents 78f3de5 + ff9e090 commit f808891
Show file tree
Hide file tree
Showing 7 changed files with 208 additions and 7 deletions.
55 changes: 53 additions & 2 deletions api/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from fastapi.templating import Jinja2Templates
from mangum import Mangum
from utils import s3
import uuid

app = FastAPI()

Expand All @@ -16,9 +17,9 @@

@app.get("/", response_class=HTMLResponse)
async def home(request: Request):
id = "123"
coffees = s3.get_active_coffees(BUCKET)
return templates.TemplateResponse(
request=request, name="index.html", context={"id": id}
request=request, name="index.html", context={"coffees": coffees}
)


Expand All @@ -38,4 +39,54 @@ async def review(request: Request):
return templates.TemplateResponse(request=request, name="form.html")


@app.get("/api/pantry", response_class=HTMLResponse)
async def show_pantry(request: Request):
coffees = s3.get_active_coffees(BUCKET)
return templates.TemplateResponse(
request=request, name="pantry.html", context={"coffees": coffees}
)


# no response
@app.post("/api/coffees", response_class=HTMLResponse)
async def show_pantry(request: Request):
form = await request.form()
active_coffees = s3.get_all_coffees(BUCKET)
# TODO: handle coffee already exists. Ask if they want to refill instead

form_data = dict(form)
form_data["active"] = "True"
form_data["id"] = str(uuid.uuid4())

# add the new coffee to the list of active coffees
active_coffees.append(form_data)

s3.update_coffees(active_coffees, BUCKET)
return templates.TemplateResponse(request=request, name="new_coffee_form.html")


@app.delete("/api/coffees/{coffee_id}", response_class=HTMLResponse)
async def show_pantry(request: Request, coffee_id: str):
all_coffees = s3.get_all_coffees(BUCKET)
# change the coffee to inactive
updated_coffee = [coffee for coffee in all_coffees if coffee["uuid"] == coffee_id][
0
]
updated_coffee["active"] = False

# update the coffee list
all_coffees = [
coffee if coffee["uuid"] != coffee_id else updated_coffee
for coffee in all_coffees
]

s3.update_coffees(all_coffees, BUCKET)

# get the active coffees
active_coffees = s3.get_active_coffees(BUCKET)
return templates.TemplateResponse(
request=request, name="pantry.html", context={"coffees": active_coffees}
)


handler = Mangum(app)
25 changes: 20 additions & 5 deletions api/templates/form.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,26 @@
<div class="label">
<span class="label-text-alt">Coffee</span>
</div>
<select id="coffee" name="coffee" class="select select-bordered">
<option value="Ocean Grind" selected>Ocean Grind</option>
<option value="Seven Seeds">Seven Seeds</option>
<option value="Inglewood">Inglewood</option>
</select>
<div class="join">
<select id="coffee" name="coffee" class="select select-bordered join-item w-full">
<!-- <option value="Ocean Grind" selected>Ocean Grind</option>
<option value="Seven Seeds">Seven Seeds</option>
<option value="Inglewood">Inglewood</option> -->

{% for coffee in coffees %}
<option value="{{ coffee.uuid }}">{{ coffee.name }} - {{coffee.roastery}}, {{ coffee.roast_date }}
</option>
{% endfor %}
</select>
<button hx-get="api/pantry" hx-target="body" hx-push-url="true" class="btn btn-primary join-item">
<svg width="100%" height="100%" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"
class="text-neutral w-1/2">
<path d="M4 12H20M12 4V20" stroke="currentColor" stroke-width="2" stroke-linecap="round"
stroke-linejoin="round" />
</svg>
</button>
</div>

</label>

<div id="left-col">
Expand Down
1 change: 1 addition & 0 deletions api/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<script src="https://unpkg.com/[email protected]"></script>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/full.min.css" rel="stylesheet" type="text/css" />
<script src="https://cdn.tailwindcss.com?plugins=typography,aspect-ratio,line-clamp"></script>

<script>
tailwind.config = {
plugins: [require("@tailwindcss/typography"), require("daisyui")],
Expand Down
43 changes: 43 additions & 0 deletions api/templates/new_coffee_form.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<form hx-post="/api/coffees" class="grid gap-y-2">
<label class="form-control w-full max-w" for="name">
<input id="name" name="name" type="text" class="input input-bordered w-full max-w" placeholder="name" />
</label>
<label class="form-control w-full max-w" for="roastery">
<input id="roastery" name="roastery" type="text" class="input input-bordered w-full max-w"
placeholder="Roastery" />
</label>
<label class="form-control w-full max-w" for="roast_date">
<input id="roast_date" name="roast_date" type="date" class="input input-bordered w-full max-w"
placeholder="Roast Date" />
</label>
<label class="form-control w-full max-w" for="roast_level">
<input id="roast_level" name="roast_level" type="text" class="input input-bordered w-full max-w"
placeholder="Roast Level (light, medium, dark)" />
</label>
<label class="form-control w-full max-w" for="origin">
<input id="origin" name="origin" type="text" class="input input-bordered w-full max-w" placeholder="Origin" />
</label>
<label class="form-control w-full max-w" for="region">
<input id="region" name="region" type="text" class="input input-bordered w-full max-w" placeholder="Region" />
</label>
<label class="form-control w-full max-w" for="farm">
<input id="farm" name="farm" type="text" class="input input-bordered w-full max-w" placeholder="Farm" />
</label>
<label class="form-control w-full max-w" for="altitude">
<input id="altitude" name="altitude" type="number" class="input input-bordered w-full max-w"
placeholder="Altitude" />
</label>
<label class="form-control w-full max-w" for="variety">
<input id="variety" name="variety" type="text" class="input input-bordered w-full max-w"
placeholder="Variety" />
</label>
<label class="form-control w-full max-w" for="processing-method">
<input id="processing-method" name="processing-method" type="text" class="input input-bordered w-full max-w"
placeholder="Processing Method" />
</label>
<label class="form-control w-full max-w" for="tasting-notes">
<input id="tasting-notes" name="tasting-notes" type="text" class="input input-bordered w-full max-w"
placeholder="Tasting Notes. Separate with commas" />
</label>
<button class="btn btn-primary w-full">Add</button>
</form>
52 changes: 52 additions & 0 deletions api/templates/pantry.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<div class="flex flex-col justify-center h-screen">
<div class="collapse collapse-plus">
<input type="radio" name="my-accordion-3" />
<div class="collapse-title text-xl font-medium">
Add new coffee to pantry
</div>
<div class="collapse-content overflow-y-auto">
{% include 'new_coffee_form.html' %}
</div>
</div>
<div class=" collapse collapse-plus">
<input type="radio" name="my-accordion-3" checked="checked" />
<div class="collapse-title text-xl font-medium">
Coffee's in the pantry
</div>
<div class="collapse-content p-0 overflow-y-auto">
<div class="overflow-x-auto max-w-sm">
<table class="table table-auto divide-red-500">
<!-- head -->
<thead>
<tr>
<th>Name</th>
<th>Roast Date</th>
<th></th>
</tr>
</thead>
<tbody>
<!-- row 1 -->
{% for coffee in coffees %}
<tr>
<td class="text-xs">{{ coffee.name }}, {{ coffee.roastery }}</td>
<td>{{ coffee.roast_date }}</td>
<td class="join">
<button class="btn btn-primary join-item">Refill</button>
<button class="btn btn-secondary join-item" hx-delete="/api/coffees/{{ coffee.uuid }}"
hx-target="body" hx-swap="true">Empty</button>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
</div>
<div class="flex flex-grow items-end m-2">
<button class="btn btn-primary w-full" hx-get="/" hx-target="body" hx-push-url="true">Make a
Review</button>
</div>



</div>
Empty file added api/utils/models/__init__.py
Empty file.
39 changes: 39 additions & 0 deletions api/utils/s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,42 @@ def upload_review(review: dict, bucket: str) -> bool:
except Exception as e:
print(e)
return False


def get_active_coffees(bucket: str) -> list[dict]:
"""Get the active coffees from S3"""
s3 = boto3.client("s3")
key = "coffees.json"
# Download the coffees.json file
all_coffees_raw = s3.get_object(Bucket=bucket, Key=key)

# Load the JSON
all_coffees = json.loads(all_coffees_raw["Body"].read())

# Filter for active coffees
active_coffees = [coffee for coffee in all_coffees if coffee["active"]]
return active_coffees


def get_all_coffees(bucket: str) -> list[dict]:
"""Get the active coffees from S3"""
s3 = boto3.client("s3")
key = "coffees.json"
# Download the coffees.json file
all_coffees_raw = s3.get_object(Bucket=bucket, Key=key)

# Load the JSON
all_coffees = json.loads(all_coffees_raw["Body"].read())
return all_coffees


def update_coffees(coffees: list[dict], bucket: str) -> bool:
"""Update the active coffees in S3"""
s3 = boto3.client("s3")
key = "coffees.json"
try:
s3.put_object(Bucket=bucket, Key=key, Body=json.dumps(coffees))
return True
except Exception as e:
print(e)
return False

0 comments on commit f808891

Please sign in to comment.