@@ -2,6 +2,7 @@ package firestore
2
2
3
3
import (
4
4
"context"
5
+ "errors"
5
6
"fmt"
6
7
"os"
7
8
"path"
@@ -13,6 +14,7 @@ import (
13
14
"github.com/SunSince90/kube-scraper-backend/pkg/backend"
14
15
"github.com/SunSince90/kube-scraper-backend/pkg/pb"
15
16
"github.com/rs/zerolog"
17
+ "google.golang.org/api/iterator"
16
18
"google.golang.org/api/option"
17
19
"google.golang.org/grpc/codes"
18
20
"google.golang.org/grpc/status"
@@ -123,8 +125,39 @@ func (f *fsBackend) GetChatByID(id int64) (*pb.Chat, error) {
123
125
124
126
// GetChatByUsername gets a chat from firestore by username
125
127
func (f * fsBackend ) GetChatByUsername (username string ) (* pb.Chat , error ) {
126
- // TODO: implement me
127
- return nil , nil
128
+ // -- Init
129
+ if len (username ) == 0 {
130
+ return nil , fmt .Errorf ("chat username cannot be 0" )
131
+ }
132
+
133
+ l := log .With ().Str ("func" , "GetChatByUsername" ).Str ("username" , username ).Logger ()
134
+ ctx , canc := context .WithTimeout (context .Background (), timeout )
135
+ defer canc ()
136
+
137
+ // -- Get the chat
138
+ docIter := f .client .Collection (f .ChatsCollection ).Where ("username" , "==" , username ).Limit (1 ).Documents (ctx )
139
+ doc , err := docIter .Next ()
140
+ if err != nil {
141
+ if errors .Is (err , iterator .Done ) {
142
+ return nil , backend .ErrNotFound
143
+ }
144
+
145
+ return nil , err
146
+ }
147
+ l .Debug ().Msg ("pulled from firestore" )
148
+
149
+ // -- Cast and return
150
+ var _chat chat
151
+ if err := doc .DataTo (& _chat ); err != nil {
152
+ return nil , err
153
+ }
154
+ c := convertToProto (& _chat )
155
+
156
+ if f .UseCache {
157
+ // TODO: implement cache
158
+ }
159
+
160
+ return c , nil
128
161
}
129
162
130
163
// StoreChats inserts a chat into firestore
0 commit comments