-
Notifications
You must be signed in to change notification settings - Fork 0
/
users.go
127 lines (117 loc) · 3.3 KB
/
users.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package postgres
import (
"context"
"errors"
"time"
"github.com/daochanio/backend/common"
"github.com/daochanio/backend/domain/entities"
"github.com/daochanio/backend/gateways/postgres/bindings"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgtype"
)
func (p *postgresGateway) GetUserByAddress(ctx context.Context, address string) (entities.User, error) {
dbUser, err := p.queries.GetUser(ctx, address)
if errors.Is(err, pgx.ErrNoRows) {
return entities.User{}, common.ErrNotFound
}
if err != nil {
return entities.User{}, err
}
user := toUser(
dbUser.Address,
dbUser.EnsName,
dbUser.EnsAvatarFileName,
dbUser.EnsAvatarOriginalUrl,
dbUser.EnsAvatarOriginalContentType,
dbUser.EnsAvatarFormattedUrl,
dbUser.EnsAvatarFormattedContentType,
dbUser.Reputation,
dbUser.CreatedAt,
dbUser.UpdatedAt,
)
return user, nil
}
func (p *postgresGateway) UpsertUser(ctx context.Context, address string) error {
return p.queries.UpsertUser(ctx, address)
}
func (p *postgresGateway) UpdateUser(ctx context.Context, address string, name *string, avatar *entities.Image) error {
ensName := pgtype.Text{}
if name != nil {
ensName.String = *name
ensName.Valid = true
} else {
ensName.Valid = false
}
fileName := pgtype.Text{}
originalURL := pgtype.Text{}
originalContentType := pgtype.Text{}
formattedURL := pgtype.Text{}
formattedContentType := pgtype.Text{}
if avatar != nil {
fileName.String = avatar.FileName()
fileName.Valid = true
originalURL.String = avatar.OriginalURL()
originalURL.Valid = true
originalContentType.String = avatar.OriginalContentType()
originalContentType.Valid = true
formattedURL.String = avatar.FormattedURL()
formattedURL.Valid = true
formattedContentType.String = avatar.FormattedContentType()
formattedContentType.Valid = true
} else {
fileName.Valid = false
originalURL.Valid = false
originalContentType.Valid = false
formattedURL.Valid = false
formattedContentType.Valid = false
}
return p.queries.UpdateUser(ctx, bindings.UpdateUserParams{
Address: address,
EnsName: ensName,
EnsAvatarFileName: fileName,
EnsAvatarOriginalUrl: originalURL,
EnsAvatarOriginalContentType: originalContentType,
EnsAvatarFormattedUrl: formattedURL,
EnsAvatarFormattedContentType: formattedContentType,
})
}
func toUser(
address string,
name pgtype.Text,
avatarFileName pgtype.Text,
avatarOriginalUrl pgtype.Text,
avatarOriginalContentType pgtype.Text,
avatarFormattedUrl pgtype.Text,
avatarFormattedContentType pgtype.Text,
reputation pgtype.Numeric,
createdAt pgtype.Timestamp,
updatedAt pgtype.Timestamp,
) entities.User {
var ensName *string
if name.Valid {
ensName = &name.String
}
var ensAvatar *entities.Image
if avatarOriginalUrl.Valid {
avatar := entities.NewImage(
avatarFileName.String,
avatarOriginalUrl.String,
avatarOriginalContentType.String,
avatarFormattedUrl.String,
avatarFormattedContentType.String,
)
ensAvatar = &avatar
}
var updatedAtTime *time.Time
if updatedAt.Valid {
updatedAtTime = &updatedAt.Time
}
return entities.NewUser(entities.UserParams{
Address: address,
EnsName: ensName,
EnsAvatar: ensAvatar,
Reputation: numericToBigInt(reputation),
CreatedAt: createdAt.Time,
UpdatedAt: updatedAtTime,
})
}