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

44 lines
893 B
Go

package command
import (
"context"
"github.com/pkg/errors"
"forge.cadoles.com/wpetit/fake-smtp/internal/model"
"forge.cadoles.com/wpetit/fake-smtp/internal/storm"
"gitlab.com/wpetit/goweb/cqrs"
"gitlab.com/wpetit/goweb/middleware/container"
)
type DeleteEmailRequest struct {
EmailID int
}
func HandleDeleteEmail(ctx context.Context, cmd cqrs.Command) error {
req, ok := cmd.Request().(*DeleteEmailRequest)
if !ok {
return cqrs.ErrUnexpectedRequest
}
ctn, err := container.From(ctx)
if err != nil {
return errors.Wrap(err, "could not retrieve service container")
}
db, err := storm.From(ctn)
if err != nil {
return errors.Wrap(err, "could not retrieve storm service")
}
if err := db.DeleteStruct(&model.Email{ID: req.EmailID}); err != nil {
if err == storm.ErrNotFound {
return nil
}
return errors.Wrap(err, "could not delete email")
}
return nil
}