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

45 lines
950 B
Go

package query
import (
"context"
"forge.cadoles.com/wpetit/fake-smtp/internal/model"
"forge.cadoles.com/wpetit/fake-smtp/internal/storm"
"github.com/pkg/errors"
"gitlab.com/wpetit/goweb/cqrs"
"gitlab.com/wpetit/goweb/middleware/container"
)
type OpenEmailRequest struct {
EmailID int
}
type OpenEmailData struct {
Email *model.Email
}
func HandleOpenEmail(ctx context.Context, qry cqrs.Query) (interface{}, error) {
req, ok := qry.Request().(*OpenEmailRequest)
if !ok {
return nil, cqrs.ErrUnexpectedRequest
}
ctn, err := container.From(ctx)
if err != nil {
return nil, errors.Wrap(err, "could not retrieve service container")
}
db, err := storm.From(ctn)
if err != nil {
return nil, errors.Wrap(err, "could not retrieve storm service")
}
email := &model.Email{}
if err := db.One("ID", req.EmailID, email); err != nil {
return nil, errors.Wrap(err, "could not find email")
}
return &OpenEmailData{email}, nil
}