package route import ( "net/http" "forge.cadoles.com/wpetit/fake-smtp/internal/command" "forge.cadoles.com/wpetit/fake-smtp/internal/query" "github.com/pkg/errors" "gitlab.com/wpetit/goweb/cqrs" "gitlab.com/wpetit/goweb/logger" "gitlab.com/wpetit/goweb/middleware/container" "gitlab.com/wpetit/goweb/service/template" ) func serveInboxPage(w http.ResponseWriter, r *http.Request) { ctn := container.Must(r.Context()) tmpl := template.Must(ctn) bus := cqrs.Must(ctn) ctx := r.Context() getInbox, err := createInboxQueryFromRequest(r) if err != nil { logger.Error(ctx, "bad request", logger.E(err)) http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest) return } result, err := bus.Query(ctx, getInbox) if err != nil { panic(errors.Wrap(err, "could not retrieve inbox")) } inboxData, ok := result.Data().(*query.InboxData) if !ok { panic(errors.New("unexpected data")) } data := extendTemplateData(w, r, template.Data{ "Emails": inboxData.Emails, }) if err := tmpl.RenderPage(w, "inbox.html.tmpl", data); err != nil { panic(errors.Wrapf(err, "could not render '%s' page", r.URL.Path)) } } func handleClearInbox(w http.ResponseWriter, r *http.Request) { ctn := container.Must(r.Context()) bus := cqrs.Must(ctn) clearInbox := &command.ClearInboxRequest{} ctx := r.Context() if _, err := bus.Exec(ctx, clearInbox); err != nil { panic(errors.Wrap(err, "could not clear inbox")) } http.Error(w, http.StatusText(http.StatusNoContent), http.StatusNoContent) }