mirror of
https://github.com/kevin-DL/services.git
synced 2026-01-15 04:24:44 +00:00
Refactor Chats Service (#48)
This commit is contained in:
53
chats/handler/create_message.go
Normal file
53
chats/handler/create_message.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/micro/micro/v3/service/errors"
|
||||
"github.com/micro/micro/v3/service/logger"
|
||||
pb "github.com/micro/services/chats/proto"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// Create a message within a chat
|
||||
func (c *Chats) CreateMessage(ctx context.Context, req *pb.CreateMessageRequest, rsp *pb.CreateMessageResponse) error {
|
||||
// validate the request
|
||||
if len(req.AuthorId) == 0 {
|
||||
return ErrMissingAuthorID
|
||||
}
|
||||
if len(req.ChatId) == 0 {
|
||||
return ErrMissingChatID
|
||||
}
|
||||
if len(req.Text) == 0 {
|
||||
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")
|
||||
}
|
||||
|
||||
// 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
|
||||
rsp.Message = msg.Serialize()
|
||||
return nil
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user