Initial commit
This commit is contained in:
41
internal/command/clear_outbox.go
Normal file
41
internal/command/clear_outbox.go
Normal file
@ -0,0 +1,41 @@
|
||||
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
|
||||
}
|
43
internal/command/delete_sms.go
Normal file
43
internal/command/delete_sms.go
Normal file
@ -0,0 +1,43 @@
|
||||
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 DeleteSMSRequest struct {
|
||||
SMSID int
|
||||
}
|
||||
|
||||
func HandleDeleteSMS(ctx context.Context, cmd cqrs.Command) error {
|
||||
req, ok := cmd.Request().(*DeleteSMSRequest)
|
||||
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.SMS{ID: req.SMSID}); err != nil {
|
||||
if err == storm.ErrNotFound {
|
||||
return nil
|
||||
}
|
||||
|
||||
return errors.Wrap(err, "could not delete email")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
48
internal/command/store_sms.go
Normal file
48
internal/command/store_sms.go
Normal file
@ -0,0 +1,48 @@
|
||||
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
|
||||
}
|
Reference in New Issue
Block a user