mirror of
https://github.com/kevin-DL/services.git
synced 2026-01-22 15:25:19 +00:00
@@ -8,6 +8,8 @@ import (
|
|||||||
"github.com/micro/micro/v3/service/logger"
|
"github.com/micro/micro/v3/service/logger"
|
||||||
"github.com/micro/services/groups/handler"
|
"github.com/micro/services/groups/handler"
|
||||||
pb "github.com/micro/services/groups/proto"
|
pb "github.com/micro/services/groups/proto"
|
||||||
|
|
||||||
|
_ "github.com/jackc/pgx/v4/stdlib"
|
||||||
)
|
)
|
||||||
|
|
||||||
var dbAddress = "postgresql://postgres:postgres@localhost:5432/groups?sslmode=disable"
|
var dbAddress = "postgresql://postgres:postgres@localhost:5432/groups?sslmode=disable"
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
|
"github.com/micro/micro/v3/service/auth"
|
||||||
"github.com/micro/micro/v3/service/errors"
|
"github.com/micro/micro/v3/service/errors"
|
||||||
"github.com/micro/micro/v3/service/logger"
|
"github.com/micro/micro/v3/service/logger"
|
||||||
pb "github.com/micro/services/threads/proto"
|
pb "github.com/micro/services/threads/proto"
|
||||||
@@ -11,6 +12,10 @@ import (
|
|||||||
|
|
||||||
// Create a conversation
|
// Create a conversation
|
||||||
func (s *Threads) CreateConversation(ctx context.Context, req *pb.CreateConversationRequest, rsp *pb.CreateConversationResponse) error {
|
func (s *Threads) CreateConversation(ctx context.Context, req *pb.CreateConversationRequest, rsp *pb.CreateConversationResponse) error {
|
||||||
|
_, ok := auth.AccountFromContext(ctx)
|
||||||
|
if !ok {
|
||||||
|
errors.Unauthorized("UNAUTHORIZED", "Unauthorized")
|
||||||
|
}
|
||||||
// validate the request
|
// validate the request
|
||||||
if len(req.GroupId) == 0 {
|
if len(req.GroupId) == 0 {
|
||||||
return ErrMissingGroupID
|
return ErrMissingGroupID
|
||||||
@@ -26,7 +31,12 @@ func (s *Threads) CreateConversation(ctx context.Context, req *pb.CreateConversa
|
|||||||
GroupID: req.GroupId,
|
GroupID: req.GroupId,
|
||||||
CreatedAt: s.Time(),
|
CreatedAt: s.Time(),
|
||||||
}
|
}
|
||||||
if err := s.DB.Create(conv).Error; err != nil {
|
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")
|
||||||
|
}
|
||||||
|
if err := db.Create(conv).Error; err != nil {
|
||||||
logger.Errorf("Error creating conversation: %v", err)
|
logger.Errorf("Error creating conversation: %v", err)
|
||||||
return errors.InternalServerError("DATABASE_ERROR", "Error connecting to database")
|
return errors.InternalServerError("DATABASE_ERROR", "Error connecting to database")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package handler_test
|
package handler_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/micro/services/threads/handler"
|
"github.com/micro/services/threads/handler"
|
||||||
@@ -40,7 +39,7 @@ func TestCreateConversation(t *testing.T) {
|
|||||||
for _, tc := range tt {
|
for _, tc := range tt {
|
||||||
t.Run(tc.Name, func(t *testing.T) {
|
t.Run(tc.Name, func(t *testing.T) {
|
||||||
var rsp pb.CreateConversationResponse
|
var rsp pb.CreateConversationResponse
|
||||||
err := h.CreateConversation(context.TODO(), &pb.CreateConversationRequest{
|
err := h.CreateConversation(microAccountCtx(), &pb.CreateConversationRequest{
|
||||||
Topic: tc.Topic, GroupId: tc.GroupID,
|
Topic: tc.Topic, GroupId: tc.GroupID,
|
||||||
}, &rsp)
|
}, &rsp)
|
||||||
|
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
|
"github.com/micro/micro/v3/service/auth"
|
||||||
"github.com/micro/micro/v3/service/errors"
|
"github.com/micro/micro/v3/service/errors"
|
||||||
"github.com/micro/micro/v3/service/logger"
|
"github.com/micro/micro/v3/service/logger"
|
||||||
pb "github.com/micro/services/threads/proto"
|
pb "github.com/micro/services/threads/proto"
|
||||||
@@ -13,6 +14,10 @@ import (
|
|||||||
|
|
||||||
// Create a message within a conversation
|
// Create a message within a conversation
|
||||||
func (s *Threads) CreateMessage(ctx context.Context, req *pb.CreateMessageRequest, rsp *pb.CreateMessageResponse) error {
|
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
|
// validate the request
|
||||||
if len(req.AuthorId) == 0 {
|
if len(req.AuthorId) == 0 {
|
||||||
return ErrMissingAuthorID
|
return ErrMissingAuthorID
|
||||||
@@ -24,9 +29,14 @@ func (s *Threads) CreateMessage(ctx context.Context, req *pb.CreateMessageReques
|
|||||||
return ErrMissingText
|
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
|
// lookup the conversation
|
||||||
var conv 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
|
return ErrNotFound
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
logger.Errorf("Error reading conversation: %v", err)
|
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 {
|
if len(msg.ID) == 0 {
|
||||||
msg.ID = uuid.New().String()
|
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()
|
rsp.Message = msg.Serialize()
|
||||||
return nil
|
return nil
|
||||||
} else if !strings.Contains(err.Error(), "messages_pkey") {
|
} 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
|
// a message already exists with this id
|
||||||
var existing Message
|
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)
|
logger.Errorf("Error creating message: %v", err)
|
||||||
return errors.InternalServerError("DATABASE_ERROR", "Error connecting to database")
|
return errors.InternalServerError("DATABASE_ERROR", "Error connecting to database")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package handler_test
|
package handler_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/micro/services/threads/handler"
|
"github.com/micro/services/threads/handler"
|
||||||
@@ -17,7 +16,7 @@ func TestCreateMessage(t *testing.T) {
|
|||||||
|
|
||||||
// seed some data
|
// seed some data
|
||||||
var cRsp pb.CreateConversationResponse
|
var cRsp pb.CreateConversationResponse
|
||||||
err := h.CreateConversation(context.TODO(), &pb.CreateConversationRequest{
|
err := h.CreateConversation(microAccountCtx(), &pb.CreateConversationRequest{
|
||||||
Topic: "HelloWorld", GroupId: uuid.New().String(),
|
Topic: "HelloWorld", GroupId: uuid.New().String(),
|
||||||
}, &cRsp)
|
}, &cRsp)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -84,7 +83,7 @@ func TestCreateMessage(t *testing.T) {
|
|||||||
for _, tc := range tt {
|
for _, tc := range tt {
|
||||||
t.Run(tc.Name, func(t *testing.T) {
|
t.Run(tc.Name, func(t *testing.T) {
|
||||||
var rsp pb.CreateMessageResponse
|
var rsp pb.CreateMessageResponse
|
||||||
err := h.CreateMessage(context.TODO(), &pb.CreateMessageRequest{
|
err := h.CreateMessage(microAccountCtx(), &pb.CreateMessageRequest{
|
||||||
AuthorId: tc.AuthorID,
|
AuthorId: tc.AuthorID,
|
||||||
ConversationId: tc.ConversationID,
|
ConversationId: tc.ConversationID,
|
||||||
Text: tc.Text,
|
Text: tc.Text,
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package handler
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
|
"github.com/micro/micro/v3/service/auth"
|
||||||
"github.com/micro/micro/v3/service/errors"
|
"github.com/micro/micro/v3/service/errors"
|
||||||
"github.com/micro/micro/v3/service/logger"
|
"github.com/micro/micro/v3/service/logger"
|
||||||
pb "github.com/micro/services/threads/proto"
|
pb "github.com/micro/services/threads/proto"
|
||||||
@@ -11,12 +12,21 @@ import (
|
|||||||
|
|
||||||
// Delete a conversation and all the messages within
|
// Delete a conversation and all the messages within
|
||||||
func (s *Threads) DeleteConversation(ctx context.Context, req *pb.DeleteConversationRequest, rsp *pb.DeleteConversationResponse) error {
|
func (s *Threads) DeleteConversation(ctx context.Context, req *pb.DeleteConversationRequest, rsp *pb.DeleteConversationResponse) error {
|
||||||
|
_, ok := auth.AccountFromContext(ctx)
|
||||||
|
if !ok {
|
||||||
|
errors.Unauthorized("UNAUTHORIZED", "Unauthorized")
|
||||||
|
}
|
||||||
// validate the request
|
// validate the request
|
||||||
if len(req.Id) == 0 {
|
if len(req.Id) == 0 {
|
||||||
return ErrMissingID
|
return ErrMissingID
|
||||||
}
|
}
|
||||||
|
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")
|
||||||
|
}
|
||||||
|
|
||||||
return s.DB.Transaction(func(tx *gorm.DB) error {
|
return db.Transaction(func(tx *gorm.DB) error {
|
||||||
// delete all the messages
|
// delete all the messages
|
||||||
if err := tx.Where(&Message{ConversationID: req.Id}).Delete(&Message{}).Error; err != nil {
|
if err := tx.Where(&Message{ConversationID: req.Id}).Delete(&Message{}).Error; err != nil {
|
||||||
logger.Errorf("Error deleting messages: %v", err)
|
logger.Errorf("Error deleting messages: %v", err)
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package handler_test
|
package handler_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/micro/services/threads/handler"
|
"github.com/micro/services/threads/handler"
|
||||||
@@ -16,7 +15,7 @@ func TestDeleteConversation(t *testing.T) {
|
|||||||
|
|
||||||
// seed some data
|
// seed some data
|
||||||
var cRsp pb.CreateConversationResponse
|
var cRsp pb.CreateConversationResponse
|
||||||
err := h.CreateConversation(context.TODO(), &pb.CreateConversationRequest{
|
err := h.CreateConversation(microAccountCtx(), &pb.CreateConversationRequest{
|
||||||
Topic: "HelloWorld", GroupId: uuid.New().String(),
|
Topic: "HelloWorld", GroupId: uuid.New().String(),
|
||||||
}, &cRsp)
|
}, &cRsp)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -25,24 +24,24 @@ func TestDeleteConversation(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
t.Run("MissingID", func(t *testing.T) {
|
t.Run("MissingID", func(t *testing.T) {
|
||||||
err := h.DeleteConversation(context.TODO(), &pb.DeleteConversationRequest{}, &pb.DeleteConversationResponse{})
|
err := h.DeleteConversation(microAccountCtx(), &pb.DeleteConversationRequest{}, &pb.DeleteConversationResponse{})
|
||||||
assert.Equal(t, handler.ErrMissingID, err)
|
assert.Equal(t, handler.ErrMissingID, err)
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("Valid", func(t *testing.T) {
|
t.Run("Valid", func(t *testing.T) {
|
||||||
err := h.DeleteConversation(context.TODO(), &pb.DeleteConversationRequest{
|
err := h.DeleteConversation(microAccountCtx(), &pb.DeleteConversationRequest{
|
||||||
Id: cRsp.Conversation.Id,
|
Id: cRsp.Conversation.Id,
|
||||||
}, &pb.DeleteConversationResponse{})
|
}, &pb.DeleteConversationResponse{})
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
|
||||||
err = h.ReadConversation(context.TODO(), &pb.ReadConversationRequest{
|
err = h.ReadConversation(microAccountCtx(), &pb.ReadConversationRequest{
|
||||||
Id: cRsp.Conversation.Id,
|
Id: cRsp.Conversation.Id,
|
||||||
}, &pb.ReadConversationResponse{})
|
}, &pb.ReadConversationResponse{})
|
||||||
assert.Equal(t, handler.ErrNotFound, err)
|
assert.Equal(t, handler.ErrNotFound, err)
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("Retry", func(t *testing.T) {
|
t.Run("Retry", func(t *testing.T) {
|
||||||
err := h.DeleteConversation(context.TODO(), &pb.DeleteConversationRequest{
|
err := h.DeleteConversation(microAccountCtx(), &pb.DeleteConversationRequest{
|
||||||
Id: cRsp.Conversation.Id,
|
Id: cRsp.Conversation.Id,
|
||||||
}, &pb.DeleteConversationResponse{})
|
}, &pb.DeleteConversationResponse{})
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package handler
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
|
"github.com/micro/micro/v3/service/auth"
|
||||||
"github.com/micro/micro/v3/service/errors"
|
"github.com/micro/micro/v3/service/errors"
|
||||||
"github.com/micro/micro/v3/service/logger"
|
"github.com/micro/micro/v3/service/logger"
|
||||||
pb "github.com/micro/services/threads/proto"
|
pb "github.com/micro/services/threads/proto"
|
||||||
@@ -10,14 +11,23 @@ import (
|
|||||||
|
|
||||||
// List all the conversations for a group
|
// List all the conversations for a group
|
||||||
func (s *Threads) ListConversations(ctx context.Context, req *pb.ListConversationsRequest, rsp *pb.ListConversationsResponse) error {
|
func (s *Threads) ListConversations(ctx context.Context, req *pb.ListConversationsRequest, rsp *pb.ListConversationsResponse) error {
|
||||||
|
_, ok := auth.AccountFromContext(ctx)
|
||||||
|
if !ok {
|
||||||
|
errors.Unauthorized("UNAUTHORIZED", "Unauthorized")
|
||||||
|
}
|
||||||
// validate the request
|
// validate the request
|
||||||
if len(req.GroupId) == 0 {
|
if len(req.GroupId) == 0 {
|
||||||
return ErrMissingGroupID
|
return ErrMissingGroupID
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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")
|
||||||
|
}
|
||||||
// query the database
|
// query the database
|
||||||
var convs []Conversation
|
var convs []Conversation
|
||||||
if err := s.DB.Where(&Conversation{GroupID: req.GroupId}).Find(&convs).Error; err != nil {
|
if err := db.Where(&Conversation{GroupID: req.GroupId}).Find(&convs).Error; err != nil {
|
||||||
logger.Errorf("Error reading conversation: %v", err)
|
logger.Errorf("Error reading conversation: %v", err)
|
||||||
return errors.InternalServerError("DATABASE_ERROR", "Error connecting to database")
|
return errors.InternalServerError("DATABASE_ERROR", "Error connecting to database")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package handler_test
|
package handler_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
@@ -15,7 +14,7 @@ func TestListConversations(t *testing.T) {
|
|||||||
|
|
||||||
// seed some data
|
// seed some data
|
||||||
var cRsp1 pb.CreateConversationResponse
|
var cRsp1 pb.CreateConversationResponse
|
||||||
err := h.CreateConversation(context.TODO(), &pb.CreateConversationRequest{
|
err := h.CreateConversation(microAccountCtx(), &pb.CreateConversationRequest{
|
||||||
Topic: "HelloWorld", GroupId: uuid.New().String(),
|
Topic: "HelloWorld", GroupId: uuid.New().String(),
|
||||||
}, &cRsp1)
|
}, &cRsp1)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -23,7 +22,7 @@ func TestListConversations(t *testing.T) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
var cRsp2 pb.CreateConversationResponse
|
var cRsp2 pb.CreateConversationResponse
|
||||||
err = h.CreateConversation(context.TODO(), &pb.CreateConversationRequest{
|
err = h.CreateConversation(microAccountCtx(), &pb.CreateConversationRequest{
|
||||||
Topic: "FooBar", GroupId: uuid.New().String(),
|
Topic: "FooBar", GroupId: uuid.New().String(),
|
||||||
}, &cRsp2)
|
}, &cRsp2)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -33,14 +32,14 @@ func TestListConversations(t *testing.T) {
|
|||||||
|
|
||||||
t.Run("MissingGroupID", func(t *testing.T) {
|
t.Run("MissingGroupID", func(t *testing.T) {
|
||||||
var rsp pb.ListConversationsResponse
|
var rsp pb.ListConversationsResponse
|
||||||
err := h.ListConversations(context.TODO(), &pb.ListConversationsRequest{}, &rsp)
|
err := h.ListConversations(microAccountCtx(), &pb.ListConversationsRequest{}, &rsp)
|
||||||
assert.Equal(t, handler.ErrMissingGroupID, err)
|
assert.Equal(t, handler.ErrMissingGroupID, err)
|
||||||
assert.Nil(t, rsp.Conversations)
|
assert.Nil(t, rsp.Conversations)
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("Valid", func(t *testing.T) {
|
t.Run("Valid", func(t *testing.T) {
|
||||||
var rsp pb.ListConversationsResponse
|
var rsp pb.ListConversationsResponse
|
||||||
err := h.ListConversations(context.TODO(), &pb.ListConversationsRequest{
|
err := h.ListConversations(microAccountCtx(), &pb.ListConversationsRequest{
|
||||||
GroupId: cRsp1.Conversation.GroupId,
|
GroupId: cRsp1.Conversation.GroupId,
|
||||||
}, &rsp)
|
}, &rsp)
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package handler
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
|
"github.com/micro/micro/v3/service/auth"
|
||||||
"github.com/micro/micro/v3/service/errors"
|
"github.com/micro/micro/v3/service/errors"
|
||||||
"github.com/micro/micro/v3/service/logger"
|
"github.com/micro/micro/v3/service/logger"
|
||||||
pb "github.com/micro/services/threads/proto"
|
pb "github.com/micro/services/threads/proto"
|
||||||
@@ -13,13 +14,22 @@ const DefaultLimit = 25
|
|||||||
// List the messages within a conversation in reverse chronological order, using sent_before to
|
// List the messages within a conversation in reverse chronological order, using sent_before to
|
||||||
// offset as older messages need to be loaded
|
// offset as older messages need to be loaded
|
||||||
func (s *Threads) ListMessages(ctx context.Context, req *pb.ListMessagesRequest, rsp *pb.ListMessagesResponse) error {
|
func (s *Threads) ListMessages(ctx context.Context, req *pb.ListMessagesRequest, rsp *pb.ListMessagesResponse) error {
|
||||||
|
_, ok := auth.AccountFromContext(ctx)
|
||||||
|
if !ok {
|
||||||
|
errors.Unauthorized("UNAUTHORIZED", "Unauthorized")
|
||||||
|
}
|
||||||
// validate the request
|
// validate the request
|
||||||
if len(req.ConversationId) == 0 {
|
if len(req.ConversationId) == 0 {
|
||||||
return ErrMissingConversationID
|
return ErrMissingConversationID
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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")
|
||||||
|
}
|
||||||
// construct the query
|
// construct the query
|
||||||
q := s.DB.Where(&Message{ConversationID: req.ConversationId}).Order("sent_at DESC")
|
q := db.Where(&Message{ConversationID: req.ConversationId}).Order("sent_at DESC")
|
||||||
if req.SentBefore != nil {
|
if req.SentBefore != nil {
|
||||||
q = q.Where("sent_at < ?", req.SentBefore.AsTime())
|
q = q.Where("sent_at < ?", req.SentBefore.AsTime())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package handler_test
|
package handler_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"sort"
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
"testing"
|
"testing"
|
||||||
@@ -20,7 +19,7 @@ func TestListMessages(t *testing.T) {
|
|||||||
|
|
||||||
// seed some data
|
// seed some data
|
||||||
var convRsp pb.CreateConversationResponse
|
var convRsp pb.CreateConversationResponse
|
||||||
err := h.CreateConversation(context.TODO(), &pb.CreateConversationRequest{
|
err := h.CreateConversation(microAccountCtx(), &pb.CreateConversationRequest{
|
||||||
Topic: "TestListMessages", GroupId: uuid.New().String(),
|
Topic: "TestListMessages", GroupId: uuid.New().String(),
|
||||||
}, &convRsp)
|
}, &convRsp)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
@@ -31,7 +30,7 @@ func TestListMessages(t *testing.T) {
|
|||||||
msgs := make([]*pb.Message, 50)
|
msgs := make([]*pb.Message, 50)
|
||||||
for i := 0; i < len(msgs); i++ {
|
for i := 0; i < len(msgs); i++ {
|
||||||
var rsp pb.CreateMessageResponse
|
var rsp pb.CreateMessageResponse
|
||||||
err := h.CreateMessage(context.TODO(), &pb.CreateMessageRequest{
|
err := h.CreateMessage(microAccountCtx(), &pb.CreateMessageRequest{
|
||||||
ConversationId: convRsp.Conversation.Id,
|
ConversationId: convRsp.Conversation.Id,
|
||||||
AuthorId: uuid.New().String(),
|
AuthorId: uuid.New().String(),
|
||||||
Text: strconv.Itoa(i),
|
Text: strconv.Itoa(i),
|
||||||
@@ -42,14 +41,14 @@ func TestListMessages(t *testing.T) {
|
|||||||
|
|
||||||
t.Run("MissingConversationID", func(t *testing.T) {
|
t.Run("MissingConversationID", func(t *testing.T) {
|
||||||
var rsp pb.ListMessagesResponse
|
var rsp pb.ListMessagesResponse
|
||||||
err := h.ListMessages(context.TODO(), &pb.ListMessagesRequest{}, &rsp)
|
err := h.ListMessages(microAccountCtx(), &pb.ListMessagesRequest{}, &rsp)
|
||||||
assert.Equal(t, handler.ErrMissingConversationID, err)
|
assert.Equal(t, handler.ErrMissingConversationID, err)
|
||||||
assert.Nil(t, rsp.Messages)
|
assert.Nil(t, rsp.Messages)
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("NoOffset", func(t *testing.T) {
|
t.Run("NoOffset", func(t *testing.T) {
|
||||||
var rsp pb.ListMessagesResponse
|
var rsp pb.ListMessagesResponse
|
||||||
err := h.ListMessages(context.TODO(), &pb.ListMessagesRequest{
|
err := h.ListMessages(microAccountCtx(), &pb.ListMessagesRequest{
|
||||||
ConversationId: convRsp.Conversation.Id,
|
ConversationId: convRsp.Conversation.Id,
|
||||||
}, &rsp)
|
}, &rsp)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
@@ -67,7 +66,7 @@ func TestListMessages(t *testing.T) {
|
|||||||
|
|
||||||
t.Run("LimitSet", func(t *testing.T) {
|
t.Run("LimitSet", func(t *testing.T) {
|
||||||
var rsp pb.ListMessagesResponse
|
var rsp pb.ListMessagesResponse
|
||||||
err := h.ListMessages(context.TODO(), &pb.ListMessagesRequest{
|
err := h.ListMessages(microAccountCtx(), &pb.ListMessagesRequest{
|
||||||
ConversationId: convRsp.Conversation.Id,
|
ConversationId: convRsp.Conversation.Id,
|
||||||
Limit: &wrapperspb.Int32Value{Value: 10},
|
Limit: &wrapperspb.Int32Value{Value: 10},
|
||||||
}, &rsp)
|
}, &rsp)
|
||||||
@@ -86,7 +85,7 @@ func TestListMessages(t *testing.T) {
|
|||||||
|
|
||||||
t.Run("OffsetAndLimit", func(t *testing.T) {
|
t.Run("OffsetAndLimit", func(t *testing.T) {
|
||||||
var rsp pb.ListMessagesResponse
|
var rsp pb.ListMessagesResponse
|
||||||
err := h.ListMessages(context.TODO(), &pb.ListMessagesRequest{
|
err := h.ListMessages(microAccountCtx(), &pb.ListMessagesRequest{
|
||||||
ConversationId: convRsp.Conversation.Id,
|
ConversationId: convRsp.Conversation.Id,
|
||||||
Limit: &wrapperspb.Int32Value{Value: 5},
|
Limit: &wrapperspb.Int32Value{Value: 5},
|
||||||
SentBefore: msgs[20].SentAt,
|
SentBefore: msgs[20].SentAt,
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package handler
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
|
"github.com/micro/micro/v3/service/auth"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
|
|
||||||
"github.com/micro/micro/v3/service/errors"
|
"github.com/micro/micro/v3/service/errors"
|
||||||
@@ -12,6 +13,10 @@ import (
|
|||||||
|
|
||||||
// Read a conversation using its ID, can filter using group ID if provided
|
// Read a conversation using its ID, can filter using group ID if provided
|
||||||
func (s *Threads) ReadConversation(ctx context.Context, req *pb.ReadConversationRequest, rsp *pb.ReadConversationResponse) error {
|
func (s *Threads) ReadConversation(ctx context.Context, req *pb.ReadConversationRequest, rsp *pb.ReadConversationResponse) error {
|
||||||
|
_, ok := auth.AccountFromContext(ctx)
|
||||||
|
if !ok {
|
||||||
|
errors.Unauthorized("UNAUTHORIZED", "Unauthorized")
|
||||||
|
}
|
||||||
// validate the request
|
// validate the request
|
||||||
if len(req.Id) == 0 {
|
if len(req.Id) == 0 {
|
||||||
return ErrMissingID
|
return ErrMissingID
|
||||||
@@ -23,9 +28,14 @@ func (s *Threads) ReadConversation(ctx context.Context, req *pb.ReadConversation
|
|||||||
q.GroupID = req.GroupId.Value
|
q.GroupID = req.GroupId.Value
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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")
|
||||||
|
}
|
||||||
// execute the query
|
// execute the query
|
||||||
var conv Conversation
|
var conv Conversation
|
||||||
if err := s.DB.Where(&q).First(&conv).Error; err == gorm.ErrRecordNotFound {
|
if err := db.Where(&q).First(&conv).Error; err == gorm.ErrRecordNotFound {
|
||||||
return ErrNotFound
|
return ErrNotFound
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
logger.Errorf("Error reading conversation: %v", err)
|
logger.Errorf("Error reading conversation: %v", err)
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package handler_test
|
package handler_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
@@ -16,7 +15,7 @@ func TestReadConversation(t *testing.T) {
|
|||||||
|
|
||||||
// seed some data
|
// seed some data
|
||||||
var cRsp pb.CreateConversationResponse
|
var cRsp pb.CreateConversationResponse
|
||||||
err := h.CreateConversation(context.TODO(), &pb.CreateConversationRequest{
|
err := h.CreateConversation(microAccountCtx(), &pb.CreateConversationRequest{
|
||||||
Topic: "HelloWorld", GroupId: uuid.New().String(),
|
Topic: "HelloWorld", GroupId: uuid.New().String(),
|
||||||
}, &cRsp)
|
}, &cRsp)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -55,7 +54,7 @@ func TestReadConversation(t *testing.T) {
|
|||||||
for _, tc := range tt {
|
for _, tc := range tt {
|
||||||
t.Run(tc.Name, func(t *testing.T) {
|
t.Run(tc.Name, func(t *testing.T) {
|
||||||
var rsp pb.ReadConversationResponse
|
var rsp pb.ReadConversationResponse
|
||||||
err := h.ReadConversation(context.TODO(), &pb.ReadConversationRequest{
|
err := h.ReadConversation(microAccountCtx(), &pb.ReadConversationRequest{
|
||||||
Id: tc.ID, GroupId: tc.GroupID,
|
Id: tc.ID, GroupId: tc.GroupID,
|
||||||
}, &rsp)
|
}, &rsp)
|
||||||
assert.Equal(t, tc.Error, err)
|
assert.Equal(t, tc.Error, err)
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package handler
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
|
"github.com/micro/micro/v3/service/auth"
|
||||||
"github.com/micro/micro/v3/service/errors"
|
"github.com/micro/micro/v3/service/errors"
|
||||||
"github.com/micro/micro/v3/service/logger"
|
"github.com/micro/micro/v3/service/logger"
|
||||||
pb "github.com/micro/services/threads/proto"
|
pb "github.com/micro/services/threads/proto"
|
||||||
@@ -13,6 +14,10 @@ import (
|
|||||||
// most messages retrieved per conversation is 25, however this can be overriden using the
|
// most messages retrieved per conversation is 25, however this can be overriden using the
|
||||||
// limit_per_conversation option
|
// limit_per_conversation option
|
||||||
func (s *Threads) RecentMessages(ctx context.Context, req *pb.RecentMessagesRequest, rsp *pb.RecentMessagesResponse) error {
|
func (s *Threads) RecentMessages(ctx context.Context, req *pb.RecentMessagesRequest, rsp *pb.RecentMessagesResponse) error {
|
||||||
|
_, ok := auth.AccountFromContext(ctx)
|
||||||
|
if !ok {
|
||||||
|
errors.Unauthorized("UNAUTHORIZED", "Unauthorized")
|
||||||
|
}
|
||||||
// validate the request
|
// validate the request
|
||||||
if len(req.ConversationIds) == 0 {
|
if len(req.ConversationIds) == 0 {
|
||||||
return ErrMissingConversationIDs
|
return ErrMissingConversationIDs
|
||||||
@@ -23,9 +28,14 @@ func (s *Threads) RecentMessages(ctx context.Context, req *pb.RecentMessagesRequ
|
|||||||
limit = int(req.LimitPerConversation.Value)
|
limit = int(req.LimitPerConversation.Value)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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")
|
||||||
|
}
|
||||||
// query the database
|
// query the database
|
||||||
var msgs []Message
|
var msgs []Message
|
||||||
err := s.DB.Transaction(func(tx *gorm.DB) error {
|
err = db.Transaction(func(tx *gorm.DB) error {
|
||||||
for _, id := range req.ConversationIds {
|
for _, id := range req.ConversationIds {
|
||||||
var cms []Message
|
var cms []Message
|
||||||
if err := tx.Where(&Message{ConversationID: id}).Order("sent_at DESC").Limit(limit).Find(&cms).Error; err != nil {
|
if err := tx.Where(&Message{ConversationID: id}).Order("sent_at DESC").Limit(limit).Find(&cms).Error; err != nil {
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package handler_test
|
package handler_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
@@ -22,7 +21,7 @@ func TestRecentMessages(t *testing.T) {
|
|||||||
convos := make(map[string][]*pb.Message, 3)
|
convos := make(map[string][]*pb.Message, 3)
|
||||||
for i := 0; i < 3; i++ {
|
for i := 0; i < 3; i++ {
|
||||||
var convRsp pb.CreateConversationResponse
|
var convRsp pb.CreateConversationResponse
|
||||||
err := h.CreateConversation(context.TODO(), &pb.CreateConversationRequest{
|
err := h.CreateConversation(microAccountCtx(), &pb.CreateConversationRequest{
|
||||||
Topic: "TestRecentMessages", GroupId: uuid.New().String(),
|
Topic: "TestRecentMessages", GroupId: uuid.New().String(),
|
||||||
}, &convRsp)
|
}, &convRsp)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
@@ -35,7 +34,7 @@ func TestRecentMessages(t *testing.T) {
|
|||||||
|
|
||||||
for j := 0; j < 50; j++ {
|
for j := 0; j < 50; j++ {
|
||||||
var rsp pb.CreateMessageResponse
|
var rsp pb.CreateMessageResponse
|
||||||
err := h.CreateMessage(context.TODO(), &pb.CreateMessageRequest{
|
err := h.CreateMessage(microAccountCtx(), &pb.CreateMessageRequest{
|
||||||
ConversationId: convRsp.Conversation.Id,
|
ConversationId: convRsp.Conversation.Id,
|
||||||
AuthorId: uuid.New().String(),
|
AuthorId: uuid.New().String(),
|
||||||
Text: fmt.Sprintf("Conversation %v, Message %v", i, j),
|
Text: fmt.Sprintf("Conversation %v, Message %v", i, j),
|
||||||
@@ -47,14 +46,14 @@ func TestRecentMessages(t *testing.T) {
|
|||||||
|
|
||||||
t.Run("MissingConversationIDs", func(t *testing.T) {
|
t.Run("MissingConversationIDs", func(t *testing.T) {
|
||||||
var rsp pb.RecentMessagesResponse
|
var rsp pb.RecentMessagesResponse
|
||||||
err := h.RecentMessages(context.TODO(), &pb.RecentMessagesRequest{}, &rsp)
|
err := h.RecentMessages(microAccountCtx(), &pb.RecentMessagesRequest{}, &rsp)
|
||||||
assert.Equal(t, handler.ErrMissingConversationIDs, err)
|
assert.Equal(t, handler.ErrMissingConversationIDs, err)
|
||||||
assert.Nil(t, rsp.Messages)
|
assert.Nil(t, rsp.Messages)
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("LimitSet", func(t *testing.T) {
|
t.Run("LimitSet", func(t *testing.T) {
|
||||||
var rsp pb.RecentMessagesResponse
|
var rsp pb.RecentMessagesResponse
|
||||||
err := h.RecentMessages(context.TODO(), &pb.RecentMessagesRequest{
|
err := h.RecentMessages(microAccountCtx(), &pb.RecentMessagesRequest{
|
||||||
ConversationIds: ids,
|
ConversationIds: ids,
|
||||||
LimitPerConversation: &wrapperspb.Int32Value{Value: 10},
|
LimitPerConversation: &wrapperspb.Int32Value{Value: 10},
|
||||||
}, &rsp)
|
}, &rsp)
|
||||||
@@ -79,7 +78,7 @@ func TestRecentMessages(t *testing.T) {
|
|||||||
reducedIDs := ids[:2]
|
reducedIDs := ids[:2]
|
||||||
|
|
||||||
var rsp pb.RecentMessagesResponse
|
var rsp pb.RecentMessagesResponse
|
||||||
err := h.RecentMessages(context.TODO(), &pb.RecentMessagesRequest{
|
err := h.RecentMessages(microAccountCtx(), &pb.RecentMessagesRequest{
|
||||||
ConversationIds: reducedIDs,
|
ConversationIds: reducedIDs,
|
||||||
}, &rsp)
|
}, &rsp)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
|||||||
@@ -4,9 +4,9 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/micro/micro/v3/service/errors"
|
"github.com/micro/micro/v3/service/errors"
|
||||||
|
gorm2 "github.com/micro/services/pkg/gorm"
|
||||||
pb "github.com/micro/services/threads/proto"
|
pb "github.com/micro/services/threads/proto"
|
||||||
"google.golang.org/protobuf/types/known/timestamppb"
|
"google.golang.org/protobuf/types/known/timestamppb"
|
||||||
"gorm.io/gorm"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -21,7 +21,7 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Threads struct {
|
type Threads struct {
|
||||||
DB *gorm.DB
|
gorm2.Helper
|
||||||
Time func() time.Time
|
Time func() time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,17 +1,17 @@
|
|||||||
package handler_test
|
package handler_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
|
"database/sql"
|
||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/golang/protobuf/ptypes/timestamp"
|
"github.com/golang/protobuf/ptypes/timestamp"
|
||||||
|
"github.com/micro/micro/v3/service/auth"
|
||||||
"github.com/micro/services/threads/handler"
|
"github.com/micro/services/threads/handler"
|
||||||
pb "github.com/micro/services/threads/proto"
|
pb "github.com/micro/services/threads/proto"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
|
||||||
"gorm.io/driver/postgres"
|
|
||||||
"gorm.io/gorm"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func testHandler(t *testing.T) *handler.Threads {
|
func testHandler(t *testing.T) *handler.Threads {
|
||||||
@@ -20,27 +20,20 @@ func testHandler(t *testing.T) *handler.Threads {
|
|||||||
if len(addr) == 0 {
|
if len(addr) == 0 {
|
||||||
addr = "postgresql://postgres@localhost:5432/postgres?sslmode=disable"
|
addr = "postgresql://postgres@localhost:5432/postgres?sslmode=disable"
|
||||||
}
|
}
|
||||||
db, err := gorm.Open(postgres.Open(addr), &gorm.Config{})
|
sqlDB, err := sql.Open("pgx", addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Error connecting to database: %v", err)
|
t.Fatalf("Failed to open connection to DB %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// clean any data from a previous run
|
// clean any data from a previous run
|
||||||
if err := db.Exec("DROP TABLE IF EXISTS conversations, messages CASCADE").Error; err != nil {
|
if _, err := sqlDB.Exec("DROP TABLE IF EXISTS micro_conversations, micro_messages CASCADE"); err != nil {
|
||||||
t.Fatalf("Error cleaning database: %v", err)
|
t.Fatalf("Error cleaning database: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// migrate the database
|
h := &handler.Threads{Time: func() time.Time { return time.Unix(1611327673, 0) }}
|
||||||
if err := db.AutoMigrate(&handler.Conversation{}, &handler.Message{}); err != nil {
|
h.DBConn(sqlDB).Migrations(&handler.Conversation{}, &handler.Message{})
|
||||||
t.Fatalf("Error migrating database: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// clean any data from a previous run
|
return h
|
||||||
if err := db.Exec("TRUNCATE TABLE conversations, messages CASCADE").Error; err != nil {
|
|
||||||
t.Fatalf("Error cleaning database: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return &handler.Threads{DB: db, Time: func() time.Time { return time.Unix(1611327673, 0) }}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func assertConversationsMatch(t *testing.T, exp, act *pb.Conversation) {
|
func assertConversationsMatch(t *testing.T, exp, act *pb.Conversation) {
|
||||||
@@ -99,3 +92,9 @@ func microSecondTime(t *timestamp.Timestamp) time.Time {
|
|||||||
tt := t.AsTime()
|
tt := t.AsTime()
|
||||||
return time.Unix(tt.Unix(), int64(tt.Nanosecond()-tt.Nanosecond()%1000))
|
return time.Unix(tt.Unix(), int64(tt.Nanosecond()-tt.Nanosecond()%1000))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func microAccountCtx() context.Context {
|
||||||
|
return auth.ContextWithAccount(context.TODO(), &auth.Account{
|
||||||
|
Issuer: "micro",
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package handler
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
|
"github.com/micro/micro/v3/service/auth"
|
||||||
"github.com/micro/micro/v3/service/errors"
|
"github.com/micro/micro/v3/service/errors"
|
||||||
"github.com/micro/micro/v3/service/logger"
|
"github.com/micro/micro/v3/service/logger"
|
||||||
pb "github.com/micro/services/threads/proto"
|
pb "github.com/micro/services/threads/proto"
|
||||||
@@ -11,6 +12,10 @@ import (
|
|||||||
|
|
||||||
// Update a conversations topic
|
// Update a conversations topic
|
||||||
func (s *Threads) UpdateConversation(ctx context.Context, req *pb.UpdateConversationRequest, rsp *pb.UpdateConversationResponse) error {
|
func (s *Threads) UpdateConversation(ctx context.Context, req *pb.UpdateConversationRequest, rsp *pb.UpdateConversationResponse) error {
|
||||||
|
_, ok := auth.AccountFromContext(ctx)
|
||||||
|
if !ok {
|
||||||
|
errors.Unauthorized("UNAUTHORIZED", "Unauthorized")
|
||||||
|
}
|
||||||
// validate the request
|
// validate the request
|
||||||
if len(req.Id) == 0 {
|
if len(req.Id) == 0 {
|
||||||
return ErrMissingID
|
return ErrMissingID
|
||||||
@@ -19,9 +24,14 @@ func (s *Threads) UpdateConversation(ctx context.Context, req *pb.UpdateConversa
|
|||||||
return ErrMissingTopic
|
return ErrMissingTopic
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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
|
// lookup the conversation
|
||||||
var conv Conversation
|
var conv Conversation
|
||||||
if err := s.DB.Where(&Conversation{ID: req.Id}).First(&conv).Error; err == gorm.ErrRecordNotFound {
|
if err := db.Where(&Conversation{ID: req.Id}).First(&conv).Error; err == gorm.ErrRecordNotFound {
|
||||||
return ErrNotFound
|
return ErrNotFound
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
logger.Errorf("Error reading conversation: %v", err)
|
logger.Errorf("Error reading conversation: %v", err)
|
||||||
@@ -30,7 +40,7 @@ func (s *Threads) UpdateConversation(ctx context.Context, req *pb.UpdateConversa
|
|||||||
|
|
||||||
// update the conversation
|
// update the conversation
|
||||||
conv.Topic = req.Topic
|
conv.Topic = req.Topic
|
||||||
if err := s.DB.Save(&conv).Error; err != nil {
|
if err := db.Save(&conv).Error; err != nil {
|
||||||
logger.Errorf("Error updating conversation: %v", err)
|
logger.Errorf("Error updating conversation: %v", err)
|
||||||
return errors.InternalServerError("DATABASE_ERROR", "Error connecting to database")
|
return errors.InternalServerError("DATABASE_ERROR", "Error connecting to database")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package handler_test
|
package handler_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
@@ -15,7 +14,7 @@ func TestUpdateConversation(t *testing.T) {
|
|||||||
|
|
||||||
// seed some data
|
// seed some data
|
||||||
var cRsp pb.CreateConversationResponse
|
var cRsp pb.CreateConversationResponse
|
||||||
err := h.CreateConversation(context.TODO(), &pb.CreateConversationRequest{
|
err := h.CreateConversation(microAccountCtx(), &pb.CreateConversationRequest{
|
||||||
Topic: "HelloWorld", GroupId: uuid.New().String(),
|
Topic: "HelloWorld", GroupId: uuid.New().String(),
|
||||||
}, &cRsp)
|
}, &cRsp)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -24,21 +23,21 @@ func TestUpdateConversation(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
t.Run("MissingID", func(t *testing.T) {
|
t.Run("MissingID", func(t *testing.T) {
|
||||||
err := h.UpdateConversation(context.TODO(), &pb.UpdateConversationRequest{
|
err := h.UpdateConversation(microAccountCtx(), &pb.UpdateConversationRequest{
|
||||||
Topic: "NewTopic",
|
Topic: "NewTopic",
|
||||||
}, &pb.UpdateConversationResponse{})
|
}, &pb.UpdateConversationResponse{})
|
||||||
assert.Equal(t, handler.ErrMissingID, err)
|
assert.Equal(t, handler.ErrMissingID, err)
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("MissingTopic", func(t *testing.T) {
|
t.Run("MissingTopic", func(t *testing.T) {
|
||||||
err := h.UpdateConversation(context.TODO(), &pb.UpdateConversationRequest{
|
err := h.UpdateConversation(microAccountCtx(), &pb.UpdateConversationRequest{
|
||||||
Id: uuid.New().String(),
|
Id: uuid.New().String(),
|
||||||
}, &pb.UpdateConversationResponse{})
|
}, &pb.UpdateConversationResponse{})
|
||||||
assert.Equal(t, handler.ErrMissingTopic, err)
|
assert.Equal(t, handler.ErrMissingTopic, err)
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("InvalidID", func(t *testing.T) {
|
t.Run("InvalidID", func(t *testing.T) {
|
||||||
err := h.UpdateConversation(context.TODO(), &pb.UpdateConversationRequest{
|
err := h.UpdateConversation(microAccountCtx(), &pb.UpdateConversationRequest{
|
||||||
Id: uuid.New().String(),
|
Id: uuid.New().String(),
|
||||||
Topic: "NewTopic",
|
Topic: "NewTopic",
|
||||||
}, &pb.UpdateConversationResponse{})
|
}, &pb.UpdateConversationResponse{})
|
||||||
@@ -46,14 +45,14 @@ func TestUpdateConversation(t *testing.T) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
t.Run("Valid", func(t *testing.T) {
|
t.Run("Valid", func(t *testing.T) {
|
||||||
err := h.UpdateConversation(context.TODO(), &pb.UpdateConversationRequest{
|
err := h.UpdateConversation(microAccountCtx(), &pb.UpdateConversationRequest{
|
||||||
Id: cRsp.Conversation.Id,
|
Id: cRsp.Conversation.Id,
|
||||||
Topic: "NewTopic",
|
Topic: "NewTopic",
|
||||||
}, &pb.UpdateConversationResponse{})
|
}, &pb.UpdateConversationResponse{})
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
|
||||||
var rsp pb.ReadConversationResponse
|
var rsp pb.ReadConversationResponse
|
||||||
err = h.ReadConversation(context.TODO(), &pb.ReadConversationRequest{
|
err = h.ReadConversation(microAccountCtx(), &pb.ReadConversationRequest{
|
||||||
Id: cRsp.Conversation.Id,
|
Id: cRsp.Conversation.Id,
|
||||||
}, &rsp)
|
}, &rsp)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"database/sql"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/micro/services/threads/handler"
|
"github.com/micro/services/threads/handler"
|
||||||
@@ -9,8 +10,8 @@ import (
|
|||||||
"github.com/micro/micro/v3/service"
|
"github.com/micro/micro/v3/service"
|
||||||
"github.com/micro/micro/v3/service/config"
|
"github.com/micro/micro/v3/service/config"
|
||||||
"github.com/micro/micro/v3/service/logger"
|
"github.com/micro/micro/v3/service/logger"
|
||||||
"gorm.io/driver/postgres"
|
|
||||||
"gorm.io/gorm"
|
_ "github.com/jackc/pgx/v4/stdlib"
|
||||||
)
|
)
|
||||||
|
|
||||||
var dbAddress = "postgresql://postgres:postgres@localhost:5432/threads?sslmode=disable"
|
var dbAddress = "postgresql://postgres:postgres@localhost:5432/threads?sslmode=disable"
|
||||||
@@ -28,16 +29,15 @@ func main() {
|
|||||||
logger.Fatalf("Error loading config: %v", err)
|
logger.Fatalf("Error loading config: %v", err)
|
||||||
}
|
}
|
||||||
addr := cfg.String(dbAddress)
|
addr := cfg.String(dbAddress)
|
||||||
db, err := gorm.Open(postgres.Open(addr), &gorm.Config{})
|
sqlDB, err := sql.Open("pgx", addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Fatalf("Error connecting to database: %v", err)
|
logger.Fatalf("Failed to open connection to DB %s", err)
|
||||||
}
|
|
||||||
if err := db.AutoMigrate(&handler.Conversation{}, &handler.Message{}); err != nil {
|
|
||||||
logger.Fatalf("Error migrating database: %v", err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
h := &handler.Threads{Time: time.Now}
|
||||||
|
h.DBConn(sqlDB).Migrations(&handler.Conversation{}, &handler.Message{})
|
||||||
// Register handler
|
// Register handler
|
||||||
pb.RegisterThreadsHandler(srv.Server(), &handler.Threads{DB: db, Time: time.Now})
|
pb.RegisterThreadsHandler(srv.Server(), h)
|
||||||
|
|
||||||
// Run service
|
// Run service
|
||||||
if err := srv.Run(); err != nil {
|
if err := srv.Run(); err != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user