49 lines
927 B
Go
49 lines
927 B
Go
|
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 {
|
||
|
Body string
|
||
|
Recipient string
|
||
|
}
|
||
|
|
||
|
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
|
||
|
|
||
|
if err := db.Save(sms); err != nil {
|
||
|
return errors.Wrap(err, "could not save email")
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|