fake-smtp/internal/route/inbox.go

63 lines
1.5 KiB
Go
Raw Permalink Normal View History

2020-04-17 17:53:01 +02:00
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"
2020-11-03 11:49:31 +01:00
"gitlab.com/wpetit/goweb/logger"
2020-04-17 17:53:01 +02:00
"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()
2020-11-03 11:49:31 +01:00
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
}
2020-04-17 17:53:01 +02:00
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)
}