package web import ( "errors" "fmt" "net/http" "expenses/internal/expense" "expenses/internal/store" ) // maxBodyBytes is the largest request body the API reads. const maxBodyBytes = 4096 // expenseInput is what a client sends to create an expense. It has no ID, // because the store chooses the ID. type expenseInput struct { Date string `json:"date"` Category string `json:"category"` Note string `json:"note"` Cents int64 `json:"cents"` } // readMonth reads ?month= from the URL. An empty month means every month. On // failure it has already replied. func readMonth(w http.ResponseWriter, r *http.Request) (string, bool) { month := r.URL.Query().Get("month") if month == "" { return "", true } err := expense.CheckMonth(month) if err != nil { writeError(w, http.StatusBadRequest, err.Error()) return "", false } return month, true } // GET /api/expenses?month=2026-11 func (a *App) handleList(w http.ResponseWriter, r *http.Request) { month, ok := readMonth(w, r) if !ok { return } writeJSON(w, http.StatusOK, expense.InMonth(a.store.List(), month)) } // POST /api/expenses func (a *App) handleCreate(w http.ResponseWriter, r *http.Request) { var input expenseInput err := readJSON(w, r, &input) var tooBig *http.MaxBytesError if errors.As(err, &tooBig) { writeError(w, http.StatusRequestEntityTooLarge, fmt.Sprintf("the body can be at most %d bytes", tooBig.Limit)) return } if err != nil { writeError(w, http.StatusBadRequest, "invalid JSON: "+err.Error()) return } e, err := expense.New(input.Date, input.Category, input.Note, input.Cents) if err != nil { writeError(w, http.StatusBadRequest, err.Error()) return } added, err := a.store.Add(e) if err != nil { a.logger.Error("could not save", "err", err) writeError(w, http.StatusInternalServerError, "the expense could not be saved") return } a.logger.Info("expense added", "id", added.ID, "category", added.Category, "cents", added.Cents) w.Header().Set("Location", fmt.Sprintf("/api/expenses/%d", added.ID)) writeJSON(w, http.StatusCreated, added) } // GET /api/expenses/{id} func (a *App) handleGet(w http.ResponseWriter, r *http.Request) { id, ok := readID(w, r) if !ok { return } e, err := a.store.Get(id) if errors.Is(err, store.ErrNotFound) { writeError(w, http.StatusNotFound, fmt.Sprintf("no expense has the id %d", id)) return } if err != nil { a.logger.Error("could not read", "id", id, "err", err) writeError(w, http.StatusInternalServerError, "the expense could not be read") return } writeJSON(w, http.StatusOK, e) } // DELETE /api/expenses/{id} func (a *App) handleDelete(w http.ResponseWriter, r *http.Request) { id, ok := readID(w, r) if !ok { return } err := a.store.Delete(id) if errors.Is(err, store.ErrNotFound) { writeError(w, http.StatusNotFound, fmt.Sprintf("no expense has the id %d", id)) return } if err != nil { a.logger.Error("could not save", "id", id, "err", err) writeError(w, http.StatusInternalServerError, "the expense could not be deleted") return } a.logger.Info("expense deleted", "id", id) w.WriteHeader(http.StatusNoContent) } // GET /api/report?month=2026-11 func (a *App) handleReport(w http.ResponseWriter, r *http.Request) { month, ok := readMonth(w, r) if !ok { return } report := expense.Summarize(expense.InMonth(a.store.List(), month)) writeJSON(w, http.StatusOK, report) }