fake-sms/internal/command/clear_outbox.go

42 lines
865 B
Go
Raw Normal View History

2020-12-21 15:56:56 +01:00
package command
import (
"context"
"github.com/pkg/errors"
"forge.cadoles.com/Cadoles/fake-sms/internal/model"
"forge.cadoles.com/Cadoles/fake-sms/internal/storm"
"gitlab.com/wpetit/goweb/cqrs"
"gitlab.com/wpetit/goweb/middleware/container"
)
type ClearOutboxRequest struct{}
func HandleClearOutbox(ctx context.Context, cmd cqrs.Command) error {
_, ok := cmd.Request().(*ClearOutboxRequest)
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.SMS{}); err != nil {
if err == storm.ErrNotFound {
return nil
}
return errors.Wrap(err, "could not delete messages")
}
return nil
}