-
Notifications
You must be signed in to change notification settings - Fork 6
/
db.go
134 lines (111 loc) · 3.13 KB
/
db.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
128
129
130
131
132
133
134
package account
import (
"context"
"github.com/Southclaws/dt"
"github.com/rs/xid"
"github.com/Southclaws/storyden/internal/errctx"
"github.com/Southclaws/storyden/internal/errtag"
"github.com/Southclaws/storyden/internal/infrastructure/db/model"
"github.com/Southclaws/storyden/internal/infrastructure/db/model/account"
"github.com/Southclaws/storyden/internal/utils"
)
type database struct {
db *model.Client
}
func New(db *model.Client) Repository {
return &database{db}
}
func (d *database) Create(ctx context.Context, email string, username string, opts ...option) (*Account, error) {
withrequired := Account{
Email: email,
Name: username,
}
for _, v := range opts {
v(&withrequired)
}
u, err := d.db.Account.
Create().
SetEmail(withrequired.Email).
SetName(withrequired.Name).
SetNillableBio(utils.OptionalToPointer(withrequired.Bio)).
SetNillableID(utils.OptionalID(xid.ID(withrequired.ID))).
Save(ctx)
if err != nil {
if model.IsConstraintError(err) {
return nil, errtag.Wrap(errctx.Wrap(err, ctx), errtag.AlreadyExists{})
}
return nil, errtag.Wrap(errctx.Wrap(err, ctx), errtag.Internal{})
}
return utils.Ref(FromModel(*u)), nil
}
func (d *database) GetByID(ctx context.Context, userId AccountID) (*Account, error) {
account, err := d.db.Account.Get(ctx, xid.ID(userId))
if err != nil {
if model.IsNotFound(err) {
return nil, errtag.Wrap(errctx.Wrap(err, ctx), errtag.NotFound{})
}
return nil, errtag.Wrap(errctx.Wrap(err, ctx), errtag.Internal{})
}
// threads, posts, err := d.getPostCounts(ctx, account.ID)
// if err != nil {
// return nil, err
// }
u := FromModel(*account)
// u.ThreadCount = threads
// u.PostCount = posts
return &u, nil
}
// func (d *database) getPostCounts(ctx context.Context, id string) (int, int, error) {
// type R struct {
// Threads int `json:"threads"`
// Posts int `json:"posts"`
// }
// var count []R
// err := d.db.Prisma.
// QueryRaw(`
// select
// count(*) filter (where "first") as threads,
// count(*) filter (where not "first") as posts
// from (
// select p.first
// from "Account" u
// inner join "Post" p on p."userId" = u.id
// where u.id = $1
// ) t`, id).
// Exec(ctx, &count)
// if err != nil {
// return 0, 0, err
// }
// if len(count) == 0 {
// return 0, 0, nil
// }
// return count[0].Threads, count[0].Posts, nil
// }
func (d *database) LookupByEmail(ctx context.Context, email string) (*Account, bool, error) {
account, err := d.db.Account.
Query().
Where(account.Email(email)).
Only(ctx)
if err != nil {
if model.IsNotFound(err) {
return nil, false, nil
}
return nil, false, errtag.Wrap(errctx.Wrap(err, ctx), errtag.Internal{})
}
return utils.Ref(FromModel(*account)), true, nil
}
func (d *database) List(ctx context.Context, sort string, limit, offset int) ([]Account, error) {
users, err := d.db.Account.
Query().
Limit(limit).
Offset(offset).
Order(model.Asc(account.FieldCreatedAt)).
All(ctx)
if err != nil {
return nil, errtag.Wrap(errctx.Wrap(err, ctx), errtag.Internal{})
}
return dt.Map(
dt.Map(users, utils.Deref[model.Account]),
utils.ToMap(FromModel),
), nil
}