fake-smtp/internal/route/inbox.go
2020-04-18 23:15:21 +02:00

56 lines
1.3 KiB
Go

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/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)
getInbox := &query.GetInboxRequest{}
ctx := r.Context()
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)
}