package web import ( "bytes" "errors" "net/http" "strconv" "time" "expenses/internal/expense" "expenses/internal/store" ) // formValues is what was typed into the form, kept as text so that it can be // shown again after a mistake. type formValues struct { Date string Category string Amount string Note string } // row is one expense, ready to show. The amount is already text. type row struct { ID int Date string Category string Amount string Note string } // sumRow is one line of the totals table. type sumRow struct { Name string Amount string } // homeData is everything the template needs. type homeData struct { Month string MonthError string // a bad month, shown next to the month box Error string // a bad form, shown next to the form Form formValues Rows []row ByCategory []sumRow Total string Version string } // showHome fills in the rows and totals, builds the page in memory, and sends // it only if that worked. func (a *App) showHome(w http.ResponseWriter, status int, data homeData) { list := expense.InMonth(a.store.List(), data.Month) for _, e := range list { data.Rows = append(data.Rows, row{ ID: e.ID, Date: e.Date, Category: e.Category, Amount: expense.FormatCents(e.Cents), Note: e.Note, }) } report := expense.Summarize(list) for _, sum := range report.ByCategory { data.ByCategory = append(data.ByCategory, sumRow{Name: sum.Name, Amount: expense.FormatCents(sum.Cents)}) } data.Total = expense.FormatCents(report.TotalCents) data.Version = a.version var page bytes.Buffer err := a.page.Execute(&page, data) if err != nil { a.logger.Error("could not build the page", "err", err) http.Error(w, "The page could not be built.", http.StatusInternalServerError) return } w.Header().Set("Content-Type", "text/html; charset=utf-8") w.WriteHeader(status) // As in writeJSON, an error here means the browser has gone away. _, _ = page.WriteTo(w) } // GET /?month=2026-11 func (a *App) handleHome(w http.ResponseWriter, r *http.Request) { data := homeData{Form: formValues{Date: time.Now().Format(expense.DateLayout)}} month := r.URL.Query().Get("month") if month != "" { err := expense.CheckMonth(month) if err != nil { data.MonthError = err.Error() a.showHome(w, http.StatusBadRequest, data) return } } data.Month = month a.showHome(w, http.StatusOK, data) } // POST /expenses func (a *App) handleAddForm(w http.ResponseWriter, r *http.Request) { form := formValues{ Date: r.FormValue("date"), Category: r.FormValue("category"), Amount: r.FormValue("amount"), Note: r.FormValue("note"), } cents, err := expense.ParseAmount(form.Amount) if err != nil { a.showHome(w, http.StatusBadRequest, homeData{Error: err.Error(), Form: form}) return } e, err := expense.New(form.Date, form.Category, form.Note, cents) if err != nil { a.showHome(w, http.StatusBadRequest, homeData{Error: err.Error(), Form: form}) return } added, err := a.store.Add(e) if err != nil { a.logger.Error("could not add an expense", "err", err) http.Error(w, "The expense could not be saved.", http.StatusInternalServerError) return } a.logger.Info("expense added", "id", added.ID, "category", added.Category, "cents", added.Cents) http.Redirect(w, r, "/", http.StatusSeeOther) } // POST /expenses/{id}/delete func (a *App) handleDeleteForm(w http.ResponseWriter, r *http.Request) { id, err := strconv.Atoi(r.PathValue("id")) if err != nil { http.Error(w, "The ID must be a whole number.", http.StatusBadRequest) return } err = a.store.Delete(id) if errors.Is(err, store.ErrNotFound) { http.Error(w, "There is no such expense.", http.StatusNotFound) return } if err != nil { a.logger.Error("could not delete an expense", "id", id, "err", err) http.Error(w, "The expense could not be deleted.", http.StatusInternalServerError) return } a.logger.Info("expense deleted", "id", id) http.Redirect(w, r, "/", http.StatusSeeOther) }