mirror of
https://github.com/kevin-DL/services.git
synced 2026-01-15 04:24:44 +00:00
Add idempotent_id to chat and threads services (#64)
* Add idempotent_id to chat and threads services * Rename
This commit is contained in:
@@ -62,14 +62,13 @@ func assertMessagesMatch(t *testing.T, exp, act *pb.Message) {
|
||||
return
|
||||
}
|
||||
|
||||
// adapt this check so we can reuse the func in testing create, where we don't know the exact id
|
||||
// which will be generated
|
||||
// adapt these checks so we can reuse the func in testing create, where we don't know the exact id /
|
||||
// idempotent_id which will be generated
|
||||
if len(exp.Id) > 0 {
|
||||
assert.Equal(t, exp.Id, act.Id)
|
||||
} else {
|
||||
assert.NotEmpty(t, act.Id)
|
||||
}
|
||||
|
||||
assert.Equal(t, exp.Text, act.Text)
|
||||
assert.Equal(t, exp.AuthorId, act.AuthorId)
|
||||
assert.Equal(t, exp.ChatId, act.ChatId)
|
||||
|
||||
@@ -2,6 +2,7 @@ package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/micro/micro/v3/service/errors"
|
||||
@@ -23,31 +24,40 @@ func (c *Chats) CreateMessage(ctx context.Context, req *pb.CreateMessageRequest,
|
||||
return ErrMissingText
|
||||
}
|
||||
|
||||
return c.DB.Transaction(func(tx *gorm.DB) error {
|
||||
// lookup the chat
|
||||
var conv Chat
|
||||
if err := tx.Where(&Chat{ID: req.ChatId}).First(&conv).Error; err == gorm.ErrRecordNotFound {
|
||||
return ErrNotFound
|
||||
} else if err != nil {
|
||||
logger.Errorf("Error reading chat: %v", err)
|
||||
return errors.InternalServerError("DATABASE_ERROR", "Error connecting to database")
|
||||
}
|
||||
// lookup the chat
|
||||
var conv Chat
|
||||
if err := c.DB.Where(&Chat{ID: req.ChatId}).First(&conv).Error; err == gorm.ErrRecordNotFound {
|
||||
return ErrNotFound
|
||||
} else if err != nil {
|
||||
logger.Errorf("Error reading chat: %v", err)
|
||||
return errors.InternalServerError("DATABASE_ERROR", "Error connecting to database")
|
||||
}
|
||||
|
||||
// create the message
|
||||
msg := &Message{
|
||||
ID: uuid.New().String(),
|
||||
SentAt: c.Time(),
|
||||
Text: req.Text,
|
||||
AuthorID: req.AuthorId,
|
||||
ChatID: req.ChatId,
|
||||
}
|
||||
if err := tx.Create(msg).Error; err != nil {
|
||||
logger.Errorf("Error creating message: %v", err)
|
||||
return errors.InternalServerError("DATABASE_ERROR", "Error connecting to database")
|
||||
}
|
||||
|
||||
// serialize the response
|
||||
// create the message
|
||||
msg := &Message{
|
||||
ID: req.Id,
|
||||
SentAt: c.Time(),
|
||||
Text: req.Text,
|
||||
AuthorID: req.AuthorId,
|
||||
ChatID: req.ChatId,
|
||||
}
|
||||
if len(msg.ID) == 0 {
|
||||
msg.ID = uuid.New().String()
|
||||
}
|
||||
if err := c.DB.Create(msg).Error; err == nil {
|
||||
rsp.Message = msg.Serialize()
|
||||
return nil
|
||||
})
|
||||
} else if !strings.Contains(err.Error(), "messages_pkey") {
|
||||
logger.Errorf("Error creating message: %v", err)
|
||||
return errors.InternalServerError("DATABASE_ERROR", "Error connecting to database")
|
||||
}
|
||||
|
||||
// a message already exists with this id
|
||||
var existing Message
|
||||
if err := c.DB.Where(&Message{ID: msg.ID}).First(&existing).Error; err != nil {
|
||||
logger.Errorf("Error creating message: %v", err)
|
||||
return errors.InternalServerError("DATABASE_ERROR", "Error connecting to database")
|
||||
}
|
||||
rsp.Message = existing.Serialize()
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -25,12 +25,14 @@ func TestCreateMessage(t *testing.T) {
|
||||
return
|
||||
}
|
||||
|
||||
iid := uuid.New().String()
|
||||
tt := []struct {
|
||||
Name string
|
||||
AuthorID string
|
||||
ChatID string
|
||||
Text string
|
||||
Error error
|
||||
ID string
|
||||
}{
|
||||
{
|
||||
Name: "MissingChatID",
|
||||
@@ -58,18 +60,35 @@ func TestCreateMessage(t *testing.T) {
|
||||
Error: handler.ErrNotFound,
|
||||
},
|
||||
{
|
||||
Name: "Valid",
|
||||
Name: "WithoutID",
|
||||
ChatID: cRsp.Chat.Id,
|
||||
AuthorID: uuid.New().String(),
|
||||
Text: "HelloWorld",
|
||||
},
|
||||
{
|
||||
Name: "WithID",
|
||||
ChatID: cRsp.Chat.Id,
|
||||
AuthorID: "johndoe",
|
||||
Text: "HelloWorld",
|
||||
ID: iid,
|
||||
},
|
||||
{
|
||||
Name: "RepeatID",
|
||||
ChatID: cRsp.Chat.Id,
|
||||
AuthorID: "johndoe",
|
||||
Text: "HelloWorld",
|
||||
ID: iid,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tt {
|
||||
t.Run(tc.Name, func(t *testing.T) {
|
||||
var rsp pb.CreateMessageResponse
|
||||
err := h.CreateMessage(context.TODO(), &pb.CreateMessageRequest{
|
||||
Text: tc.Text, ChatId: tc.ChatID, AuthorId: tc.AuthorID,
|
||||
AuthorId: tc.AuthorID,
|
||||
ChatId: tc.ChatID,
|
||||
Text: tc.Text,
|
||||
Id: tc.ID,
|
||||
}, &rsp)
|
||||
|
||||
assert.Equal(t, tc.Error, err)
|
||||
@@ -83,6 +102,7 @@ func TestCreateMessage(t *testing.T) {
|
||||
ChatId: tc.ChatID,
|
||||
SentAt: timestamppb.New(h.Time()),
|
||||
Text: tc.Text,
|
||||
Id: tc.ID,
|
||||
}, rsp.Message)
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user