Skip to content

Commit 0c8a10d

Browse files
committed
Implement GetAllChats
Signed-off-by: Elis Lulja <[email protected]>
1 parent 99d7ab5 commit 0c8a10d

File tree

1 file changed

+38
-2
lines changed

1 file changed

+38
-2
lines changed

pkg/server/server.go

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,43 @@ func (b *backendServer) GetChat(ctx context.Context, r *pb.ChatRequest) (*pb.Cha
6767
}, nil
6868
}
6969

70+
// GetAllChats returns a list of chats according to the request
7071
func (b *backendServer) GetAllChats(ctx context.Context, r *pb.ChatRequest) (*pb.ChatResponse, error) {
71-
// TODO: implement me
72-
return nil, nil
72+
chats, err := b.backend.GetAllChats()
73+
if err != nil {
74+
return &pb.ChatResponse{
75+
Code: 500,
76+
Message: err.Error(),
77+
}, fmt.Errorf("error while getting chats list")
78+
}
79+
80+
// Has a filter?
81+
if len(r.Type) == 0 {
82+
return &pb.ChatResponse{
83+
Code: 200,
84+
Message: "ok",
85+
Chats: chats,
86+
}, nil
87+
}
88+
89+
if r.Type != "group" && r.Type != "supergroup" && r.Type != "private" || r.Type != "channel" {
90+
return &pb.ChatResponse{
91+
Code: 500,
92+
Message: "unrecognized chat type",
93+
Chats: chats,
94+
}, fmt.Errorf("unrecognized chat type (%s)", r.Type)
95+
}
96+
97+
_chats := []*pb.Chat{}
98+
for _, chat := range chats {
99+
if chat.Type == r.Type {
100+
_chats = append(_chats, chat)
101+
}
102+
}
103+
104+
return &pb.ChatResponse{
105+
Code: 200,
106+
Message: "ok",
107+
Chats: _chats,
108+
}, nil
73109
}

0 commit comments

Comments
 (0)