fake-sms/internal/command/store_sms.go

53 lines
1.0 KiB
Go
Raw Permalink Normal View History

2020-12-21 15:56:56 +01:00
package command
import (
"context"
"time"
"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 StoreSMSRequest struct {
From string
2020-12-21 15:56:56 +01:00
Body string
Recipient string
Metadata map[string]interface{}
2020-12-21 15:56:56 +01:00
}
func HandleStoreSMS(ctx context.Context, cmd cqrs.Command) error {
req, ok := cmd.Request().(*StoreSMSRequest)
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")
}
sms := &model.SMS{
SentAt: time.Now(),
}
sms.Body = req.Body
sms.Recipient = req.Recipient
sms.Metadata = req.Metadata
sms.From = req.From
2020-12-21 15:56:56 +01:00
if err := db.Save(sms); err != nil {
return errors.Wrap(err, "could not save email")
}
return nil
}