mirror of
https://github.com/kevin-DL/services.git
synced 2026-01-11 19:04:35 +00:00
42 lines
1.2 KiB
Go
42 lines
1.2 KiB
Go
package handler
|
|
|
|
import (
|
|
"context"
|
|
|
|
"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"
|
|
)
|
|
|
|
// List all the conversations for a group
|
|
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
|
|
if len(req.GroupId) == 0 {
|
|
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
|
|
var convs []Conversation
|
|
if err := db.Where(&Conversation{GroupID: req.GroupId}).Find(&convs).Error; err != nil {
|
|
logger.Errorf("Error reading conversation: %v", err)
|
|
return errors.InternalServerError("DATABASE_ERROR", "Error connecting to database")
|
|
}
|
|
|
|
// serialize the response
|
|
rsp.Conversations = make([]*pb.Conversation, len(convs))
|
|
for i, c := range convs {
|
|
rsp.Conversations[i] = c.Serialize()
|
|
}
|
|
return nil
|
|
}
|