@@ -4,14 +4,22 @@ import (
4
4
"context"
5
5
"fmt"
6
6
"os"
7
+ "path"
7
8
"sync"
9
+ "time"
8
10
9
11
fs "cloud.google.com/go/firestore"
10
12
firebase "firebase.google.com/go/v4"
11
13
"github.com/SunSince90/kube-scraper-backend/pkg/backend"
12
14
"github.com/SunSince90/kube-scraper-backend/pkg/pb"
13
15
"github.com/rs/zerolog"
14
16
"google.golang.org/api/option"
17
+ "google.golang.org/grpc/codes"
18
+ "google.golang.org/grpc/status"
19
+ )
20
+
21
+ const (
22
+ timeout = time .Duration (15 ) * time .Second
15
23
)
16
24
17
25
var (
@@ -74,8 +82,43 @@ func (f *fsBackend) Close() {
74
82
75
83
// GetChatByID gets a chat from firestore
76
84
func (f * fsBackend ) GetChatByID (id int64 ) (* pb.Chat , error ) {
77
- // TODO: implement me
78
- return nil , nil
85
+ // -- Init
86
+ if id == 0 {
87
+ return nil , fmt .Errorf ("chat id cannot be 0" )
88
+ }
89
+
90
+ l := log .With ().Str ("func" , "GetChatByID" ).Int64 ("id" , id ).Logger ()
91
+ if f .UseCache {
92
+ // TODO: implement cache
93
+ }
94
+
95
+ // -- Get the chat
96
+ docPath := path .Join (f .ChatsCollection , fmt .Sprintf ("%d" , id ))
97
+ ctx , canc := context .WithTimeout (context .Background (), timeout )
98
+ defer canc ()
99
+
100
+ doc , err := f .client .Doc (docPath ).Get (ctx )
101
+ if err != nil {
102
+ if status .Code (err ) == codes .NotFound {
103
+ return nil , backend .ErrNotFound
104
+ }
105
+
106
+ return nil , err
107
+ }
108
+ l .Debug ().Msg ("pulled from firestore" )
109
+
110
+ // -- Cast and return
111
+ var _chat chat
112
+ if err := doc .DataTo (& _chat ); err != nil {
113
+ return nil , err
114
+ }
115
+ c := convertToProto (& _chat )
116
+
117
+ if f .UseCache {
118
+ // TODO: implement cache
119
+ }
120
+
121
+ return c , nil
79
122
}
80
123
81
124
// GetChatByUsername gets a chat from firestore by username
0 commit comments