fake-smtp/internal/command/clear_inbox.go

42 lines
864 B
Go
Raw Normal View History

2020-04-17 17:53:01 +02:00
package command
import (
"context"
"github.com/pkg/errors"
2024-03-21 09:09:57 +01:00
"forge.cadoles.com/Cadoles/fake-smtp/internal/model"
"forge.cadoles.com/Cadoles/fake-smtp/internal/storm"
2020-04-17 17:53:01 +02:00
"gitlab.com/wpetit/goweb/cqrs"
"gitlab.com/wpetit/goweb/middleware/container"
)
type ClearInboxRequest struct{}
func HandleClearInbox(ctx context.Context, cmd cqrs.Command) error {
_, ok := cmd.Request().(*ClearInboxRequest)
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.Select().Delete(&model.Email{}); err != nil {
if err == storm.ErrNotFound {
return nil
}
return errors.Wrap(err, "could not delete emails")
}
return nil
}