Skip to content

Commit

Permalink
Update list of meallogs as they are added
Browse files Browse the repository at this point in the history
  • Loading branch information
szabolcs-horvath committed Dec 31, 2024
1 parent f9b3f00 commit febc500
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 38 deletions.
42 changes: 37 additions & 5 deletions http_server/routes/htmx/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ const Prefix = "/htmx"

func Routes() map[string]http.HandlerFunc {
return map[string]http.HandlerFunc{
"GET /": rootHandler,
"GET /today": todayHandler,
"GET /notifications": notificationsHandler,
"GET /items": itemsHandler,
"POST /items/search": itemSearchHandler,
"GET /": rootHandler,
"GET /today": todayHandler,
"GET /notifications": notificationsHandler,
"GET /items": itemsHandler,
"POST /items/search": itemSearchHandler,
"POST /meallogs/meal/{mealId}": addMealLogForMealHandler,
}
}
func rootHandler(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -175,3 +176,34 @@ func itemSearchHandler(w http.ResponseWriter, r *http.Request) {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}

func addMealLogForMealHandler(w http.ResponseWriter, r *http.Request) {
mealIdParam := r.PathValue("mealId")
mealId, err := strconv.ParseInt(mealIdParam, 10, 64)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}

var requestMealLog repository.CreateMealLogRequest
if err = util.ReadJson(r, &requestMealLog); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
_, err = repository.CreateMealLog(r.Context(), requestMealLog)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

meallogs, err := repository.FindMealLogsForMealAndCurrentDay(r.Context(), mealId)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}

err = repository.Render(w, "meallogs_simple", meallogs)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
22 changes: 22 additions & 0 deletions repository/meallogs.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,28 @@ func FindMealLogsForUserAndDate(ctx context.Context, ownerId int64, date time.Ti
return result, nil
}

func FindMealLogsForMealAndCurrentDay(ctx context.Context, mealId int64) ([]*MealLog, error) {
queries, err := GetQueries()
if err != nil {
return nil, err
}
list, err := queries.FindMealLogsForMealAndDate(ctx, sqlc.FindMealLogsForMealAndDateParams{
MealID: mealId,
Date: time.Now(),
})
if err != nil {
return nil, err
}
result := make([]*MealLog, len(list))
for i, m := range list {
result[i] = convertMealLog(&m.MealLogSqlc)
result[i].Meal = convertMeal(&m.MealSqlc)
result[i].Item = convertItem(&m.ItemSqlc)
result[i].Portion = convertPortion(&m.PortionSqlc)
}
return result, nil
}

type CreateMealLogRequest struct {
MealID int64 `json:"meal_id"`
ItemID int64 `json:"item_id"`
Expand Down
2 changes: 1 addition & 1 deletion repository/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func init() {
templates = template.Must(template.New("templates").Funcs(util.TemplateFuncs()).Funcs(TemplateFuncs()).ParseGlob("web/templates/*.gohtml"))
}

func Render(w io.Writer, name string, data map[string]any) error {
func Render(w io.Writer, name string, data any) error {
return templates.ExecuteTemplate(w, name, data)
}

Expand Down
9 changes: 9 additions & 0 deletions sqlite/queries/meallog_queries.sql
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ JOIN portions ON meallogs.portion_id = portions.id
WHERE meals.owner_id = ?
AND date(meallogs.datetime) IS date(sqlc.arg(date));

-- name: FindMealLogsForMealAndDate :many
SELECT sqlc.embed(meallogs), sqlc.embed(meals), sqlc.embed(items), sqlc.embed(portions)
FROM meallogs
JOIN meals ON meallogs.meal_id = meals.id
JOIN items ON meallogs.item_id = items.id
JOIN portions ON meallogs.portion_id = portions.id
WHERE meals.id = sqlc.arg(meal_id)
AND date(meallogs.datetime) IS date(sqlc.arg(date));

-- name: CreateMealLog :one
INSERT INTO meallogs(meal_id,
item_id,
Expand Down
6 changes: 4 additions & 2 deletions web/templates/item_search.gohtml
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,10 @@
</table>
</div>
<hr/>
{{/* TODO this form should target an htmx endpoint that returns all meallogs for this meal and oob swaps the list at the top*/}}
<form hx-swap="none" hx-post="/api/v1/meallogs/" hx-ext="json-enc">
<form hx-swap="innerHtml"
hx-target="#meallogs_for_meal_{{ $.Data.Meal.ID }}"
hx-post="meallogs/meal/{{ $.Data.Meal.ID }}"
hx-ext="json-enc">
<div class="row" hidden>
<div class="col input-group mb-2">
<span class="input-group-text">Meal ID</span>
Expand Down
30 changes: 30 additions & 0 deletions web/templates/meallogs_simple.gohtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{{ define "meallogs_simple" }}
{{ range . }}
<div class="card">
<div class="card-body">
<div class="row">
<div class="col">
<h6 class="card-title">
{{ .Item.Name }} - {{ .PortionMultiplier }} {{ .Portion.Name }}
</h6>
<h6 class="card-subtitle text-muted">
{{ .DateTime.Format "15:04" }}
</h6>
</div>
<div class="col text-end">
{{/* TODO: Add the hx attributes and endpoints for these buttons*/}}
<button type="button" class="btn btn-outline-warning">
<i class="bi bi-pencil-square"></i>
</button>
<button type="button" class="btn btn-outline-danger">
<i class="bi bi-trash"></i>
</button>
</div>
</div>
<div class="row">
{{/* TODO: This where the form to update meallog should appear if you press the edit button*/}}
</div>
</div>
</div>
{{ end }}
{{ end }}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{{ define "meallogs_by_meal" }}
{{ define "meals" }}
<div class="accordion" id="mealsAccordion">
{{ range $meal, $meallogs := .MealLogsByMeal }}
<div class="accordion-item">
Expand All @@ -12,34 +12,7 @@
<div id="collapse{{ $meal.ID }}" class="accordion-collapse collapse" data-bs-parent="#mealsAccordion">
<div class="accordion-body">
<div id="meallogs_for_meal_{{ $meal.ID }}">
{{ range $meallogs }}
<div class="card">
<div class="card-body">
<div class="row">
<div class="col">
<h6 class="card-title">
{{ .Item.Name }} - {{ .PortionMultiplier }} {{ .Portion.Name }}
</h6>
<h6 class="card-subtitle text-muted">
{{ .DateTime.Format "15:04" }}
</h6>
</div>
<div class="col text-end">
{{/* TODO: Add the hx attributes and endpoints for these buttons*/}}
<button type="button" class="btn btn-outline-warning">
<i class="bi bi-pencil-square"></i>
</button>
<button type="button" class="btn btn-outline-danger">
<i class="bi bi-trash"></i>
</button>
</div>
</div>
<div class="row">
{{/* TODO: This where the form to update meallog should appear if you press the edit button*/}}
</div>
</div>
</div>
{{ end }}
{{ template "meallogs_simple" $meallogs }}
</div>
<hr/>
{{ range $quotaType, $quotaValue := $meal.Quotas }}
Expand Down
2 changes: 1 addition & 1 deletion web/templates/today_tab.gohtml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@
{{ template "daily_quota" .Data }}
<hr/>
{{ end }}
{{ template "meallogs_by_meal" .Data }}
{{ template "meals" .Data }}
</div>
{{ end }}

0 comments on commit febc500

Please sign in to comment.