mirror of
https://github.com/kevin-DL/services.git
synced 2026-01-14 12:04:41 +00:00
@@ -5,6 +5,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/micro/micro/v3/service/auth"
|
||||
"github.com/micro/micro/v3/service/errors"
|
||||
"github.com/micro/micro/v3/service/logger"
|
||||
pb "github.com/micro/services/threads/proto"
|
||||
@@ -13,6 +14,10 @@ import (
|
||||
|
||||
// Create a message within a conversation
|
||||
func (s *Threads) CreateMessage(ctx context.Context, req *pb.CreateMessageRequest, rsp *pb.CreateMessageResponse) error {
|
||||
_, ok := auth.AccountFromContext(ctx)
|
||||
if !ok {
|
||||
errors.Unauthorized("UNAUTHORIZED", "Unauthorized")
|
||||
}
|
||||
// validate the request
|
||||
if len(req.AuthorId) == 0 {
|
||||
return ErrMissingAuthorID
|
||||
@@ -24,9 +29,14 @@ func (s *Threads) CreateMessage(ctx context.Context, req *pb.CreateMessageReques
|
||||
return ErrMissingText
|
||||
}
|
||||
|
||||
db, err := s.GetDBConn(ctx)
|
||||
if err != nil {
|
||||
logger.Errorf("Error connecting to DB: %v", err)
|
||||
return errors.InternalServerError("DB_ERROR", "Error connecting to DB")
|
||||
}
|
||||
// lookup the conversation
|
||||
var conv Conversation
|
||||
if err := s.DB.Where(&Conversation{ID: req.ConversationId}).First(&conv).Error; err == gorm.ErrRecordNotFound {
|
||||
if err := db.Where(&Conversation{ID: req.ConversationId}).First(&conv).Error; err == gorm.ErrRecordNotFound {
|
||||
return ErrNotFound
|
||||
} else if err != nil {
|
||||
logger.Errorf("Error reading conversation: %v", err)
|
||||
@@ -44,7 +54,7 @@ func (s *Threads) CreateMessage(ctx context.Context, req *pb.CreateMessageReques
|
||||
if len(msg.ID) == 0 {
|
||||
msg.ID = uuid.New().String()
|
||||
}
|
||||
if err := s.DB.Create(msg).Error; err == nil {
|
||||
if err := db.Create(msg).Error; err == nil {
|
||||
rsp.Message = msg.Serialize()
|
||||
return nil
|
||||
} else if !strings.Contains(err.Error(), "messages_pkey") {
|
||||
@@ -54,7 +64,7 @@ func (s *Threads) CreateMessage(ctx context.Context, req *pb.CreateMessageReques
|
||||
|
||||
// a message already exists with this id
|
||||
var existing Message
|
||||
if err := s.DB.Where(&Message{ID: msg.ID}).First(&existing).Error; err != nil {
|
||||
if err := 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")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user