66 lines
1.3 KiB
Go
66 lines
1.3 KiB
Go
|
|
package queue
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"encoding/json"
|
||
|
|
"gotemplate/internal/name"
|
||
|
|
"log"
|
||
|
|
|
||
|
|
"github.com/aws/aws-sdk-go-v2/aws"
|
||
|
|
"github.com/aws/aws-sdk-go-v2/service/sqs"
|
||
|
|
"github.com/aws/aws-sdk-go-v2/service/sqs/types"
|
||
|
|
)
|
||
|
|
|
||
|
|
type NameController struct {
|
||
|
|
name name.Service
|
||
|
|
}
|
||
|
|
|
||
|
|
func NewNameController(nameSvc name.Service) *NameController {
|
||
|
|
return &NameController{
|
||
|
|
name: nameSvc,
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
type CreateNameBody struct {
|
||
|
|
Name string `json:"name"`
|
||
|
|
}
|
||
|
|
|
||
|
|
func (s *NameController) Create(ctx context.Context, config *QueueConfig, msg *types.Message) error {
|
||
|
|
var body CreateNameBody
|
||
|
|
err := json.Unmarshal([]byte(*msg.Body), &body)
|
||
|
|
if err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
|
||
|
|
err = s.name.Add(ctx, body.Name)
|
||
|
|
if err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
|
||
|
|
log.Print("Added: ", body.Name)
|
||
|
|
|
||
|
|
_, err = config.Client.SendMessage(ctx, &sqs.SendMessageInput{
|
||
|
|
MessageAttributes: map[string]types.MessageAttributeValue{
|
||
|
|
"type": {
|
||
|
|
DataType: aws.String("String"),
|
||
|
|
StringValue: aws.String("CREATE_NAME_SUCCESS"),
|
||
|
|
},
|
||
|
|
},
|
||
|
|
QueueUrl: aws.String(config.URL),
|
||
|
|
MessageBody: aws.String("{}"),
|
||
|
|
})
|
||
|
|
if err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
|
||
|
|
_, err = config.Client.DeleteMessage(ctx, &sqs.DeleteMessageInput{
|
||
|
|
QueueUrl: aws.String(config.URL),
|
||
|
|
ReceiptHandle: msg.ReceiptHandle,
|
||
|
|
})
|
||
|
|
if err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
|
||
|
|
return nil
|
||
|
|
}
|