/
url.go
54 lines (46 loc) 路 1.42 KB
/
url.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package repository
import (
"context"
"github.com/Xanonymous-GitHub/sxcctw/pkg/proto/pb"
"github.com/sirupsen/logrus"
"google.golang.org/protobuf/types/known/timestamppb"
"time"
)
type UrlRepository interface {
GetOriginUrlWith(ctx context.Context, shortenedId string) (*pb.GetOriginUrlResponse, error)
CreateRecordWith(ctx context.Context, originUrl string, expireAt time.Time) (*pb.CreateRecordResponse, error)
}
type urlRepository struct {
logger *logrus.Logger
recordServiceClient pb.RecordServiceClient
}
func CreateUrlRepositoryWith(
recordServiceClient pb.RecordServiceClient,
logger *logrus.Logger,
) UrlRepository {
return &urlRepository{
recordServiceClient: recordServiceClient,
logger: logger,
}
}
func (r *urlRepository) GetOriginUrlWith(ctx context.Context, shortenedId string) (*pb.GetOriginUrlResponse, error) {
req := &pb.GetOriginUrlRequest{ShortenedId: shortenedId}
resp, err := r.recordServiceClient.GetOriginUrl(ctx, req)
if err != nil {
r.logger.Errorln(err)
return nil, err
}
return resp, nil
}
func (r *urlRepository) CreateRecordWith(ctx context.Context, originUrl string, expireAt time.Time) (*pb.CreateRecordResponse, error) {
req := &pb.CreateRecordRequest{
OriginUrl: originUrl,
ExpireAt: timestamppb.New(expireAt),
}
resp, err := r.recordServiceClient.CreateRecord(ctx, req)
if err != nil {
r.logger.Errorln(err)
return nil, err
}
return resp, nil
}