Skip to content
This repository was archived by the owner on Feb 4, 2021. It is now read-only.

Commit 5e7ca1b

Browse files
authored
Merge pull request #1 from gedorinku/github+use_sqlutil
use sqlutil
2 parents 6c4e57e + d0f3850 commit 5e7ca1b

File tree

11 files changed

+134
-209
lines changed

11 files changed

+134
-209
lines changed

app/di/store_component.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
sessionstore "github.com/ProgrammingLab/prolab-accounts/infra/store/session"
2323
userstore "github.com/ProgrammingLab/prolab-accounts/infra/store/user"
2424
userblogstore "github.com/ProgrammingLab/prolab-accounts/infra/store/user_blog"
25+
"github.com/ProgrammingLab/prolab-accounts/sqlutil"
2526
)
2627

2728
// StoreComponent is an interface of stores
@@ -57,7 +58,7 @@ func NewStoreComponent(cfg *config.Config) (StoreComponent, error) {
5758
}
5859

5960
return &storeComponentImpl{
60-
db: db,
61+
db: sqlutil.New(db),
6162
client: cli,
6263
minioCli: min,
6364
cfg: cfg,
@@ -132,7 +133,7 @@ func connectMinio(cfg *config.Config) (*minio.Client, error) {
132133
}
133134

134135
type storeComponentImpl struct {
135-
db *sql.DB
136+
db *sqlutil.DB
136137
client *redis.Client
137138
minioCli *minio.Client
138139
cfg *config.Config

app/di/test_store_component.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
"github.com/ProgrammingLab/prolab-accounts/app/config"
1212
"github.com/ProgrammingLab/prolab-accounts/infra/record"
13+
"github.com/ProgrammingLab/prolab-accounts/sqlutil"
1314
)
1415

1516
// MustCreateTestStoreComponent creates test store component or exits
@@ -20,7 +21,7 @@ func MustCreateTestStoreComponent(cfg *config.Config) *TestStoreComponent {
2021

2122
return &TestStoreComponent{
2223
storeComponentImpl: &storeComponentImpl{
23-
db: db,
24+
db: sqlutil.New(db),
2425
cfg: cfg,
2526
},
2627
}

infra/store/department/department_store.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@ package departmentstore
22

33
import (
44
"context"
5-
"database/sql"
65

76
"github.com/pkg/errors"
87

98
"github.com/ProgrammingLab/prolab-accounts/infra/record"
109
"github.com/ProgrammingLab/prolab-accounts/infra/store"
10+
"github.com/ProgrammingLab/prolab-accounts/sqlutil"
1111
)
1212

1313
type departmentStoreImpl struct {
1414
ctx context.Context
15-
db *sql.DB
15+
db *sqlutil.DB
1616
}
1717

1818
// NewDepartmentStore returns new entry department store
19-
func NewDepartmentStore(ctx context.Context, db *sql.DB) store.DepartmentStore {
19+
func NewDepartmentStore(ctx context.Context, db *sqlutil.DB) store.DepartmentStore {
2020
return &departmentStoreImpl{
2121
ctx: ctx,
2222
db: db,

infra/store/entry/entry_store.go

Lines changed: 52 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,19 @@ import (
1313
"github.com/volatiletech/sqlboiler/boil"
1414
"github.com/volatiletech/sqlboiler/queries/qm"
1515

16-
"github.com/ProgrammingLab/prolab-accounts/app/util"
1716
"github.com/ProgrammingLab/prolab-accounts/infra/record"
1817
"github.com/ProgrammingLab/prolab-accounts/infra/store"
1918
"github.com/ProgrammingLab/prolab-accounts/model"
19+
"github.com/ProgrammingLab/prolab-accounts/sqlutil"
2020
)
2121

2222
type entryStoreImpl struct {
2323
ctx context.Context
24-
db *sql.DB
24+
db *sqlutil.DB
2525
}
2626

2727
// NewEntryStore returns new entry blog store
28-
func NewEntryStore(ctx context.Context, db *sql.DB) store.EntryStore {
28+
func NewEntryStore(ctx context.Context, db *sqlutil.DB) store.EntryStore {
2929
return &entryStoreImpl{
3030
ctx: ctx,
3131
db: db,
@@ -55,82 +55,71 @@ func (s *entryStoreImpl) ListPublicEntries(before time.Time, limit int) ([]*reco
5555
return e[:limit], e[limit].PublishedAt, nil
5656
}
5757

58-
func (s *entryStoreImpl) CreateEntries(blog *record.Blog, feed *gofeed.Feed) (n int64, err error) {
58+
func (s *entryStoreImpl) CreateEntries(blog *record.Blog, feed *gofeed.Feed) (int64, error) {
5959
rev := make([]*gofeed.Item, len(feed.Items))
6060
for i, item := range feed.Items {
6161
rev[len(rev)-1-i] = item
6262
}
6363
feed.Items = rev
6464

65-
tx, err := s.db.Begin()
66-
if err != nil {
67-
return 0, errors.WithStack(err)
68-
}
69-
defer func() {
70-
if e := util.ErrorFromRecover(recover()); e != nil {
71-
_ = tx.Rollback()
72-
err = e
65+
var n int64
66+
err := s.db.Watch(s.ctx, func(ctx context.Context, tx *sql.Tx) error {
67+
mods := []qm.QueryMod{
68+
qm.Select(record.EntryColumns.ID, record.EntryColumns.GUID),
69+
qm.Where("blog_id = ?", blog.ID),
7370
}
74-
}()
75-
76-
mods := []qm.QueryMod{
77-
qm.Select(record.EntryColumns.ID, record.EntryColumns.GUID),
78-
qm.Where("blog_id = ?", blog.ID),
79-
}
80-
entries, err := record.Entries(mods...).All(s.ctx, tx)
81-
if err != nil {
82-
_ = tx.Rollback()
83-
return 0, errors.WithStack(err)
84-
}
85-
86-
exists := make(map[string]struct{})
87-
for _, e := range entries {
88-
exists[e.GUID] = struct{}{}
89-
}
90-
91-
n = 0
92-
for _, item := range feed.Items {
93-
guid, err := getGUID(blog.ID, item.GUID)
71+
entries, err := record.Entries(mods...).All(s.ctx, tx)
9472
if err != nil {
95-
_ = tx.Rollback()
96-
return 0, err
73+
return errors.WithStack(err)
9774
}
9875

99-
_, ok := exists[guid]
100-
if ok {
101-
continue
76+
exists := make(map[string]struct{})
77+
for _, e := range entries {
78+
exists[e.GUID] = struct{}{}
10279
}
10380

104-
e := &record.Entry{
105-
Title: item.Title,
106-
Description: item.Description,
107-
Content: item.Content,
108-
Link: item.Link,
109-
AuthorID: blog.UserID,
110-
GUID: guid,
111-
BlogID: blog.ID,
112-
}
113-
if i := item.Image; i != nil {
114-
e.ImageURL = i.URL
115-
}
116-
if t := item.PublishedParsed; t == nil {
117-
e.PublishedAt = time.Now().In(boil.GetLocation())
118-
} else {
119-
e.PublishedAt = t.In(boil.GetLocation())
81+
n = 0
82+
for _, item := range feed.Items {
83+
guid, err := getGUID(blog.ID, item.GUID)
84+
if err != nil {
85+
return err
86+
}
87+
88+
_, ok := exists[guid]
89+
if ok {
90+
continue
91+
}
92+
93+
e := &record.Entry{
94+
Title: item.Title,
95+
Description: item.Description,
96+
Content: item.Content,
97+
Link: item.Link,
98+
AuthorID: blog.UserID,
99+
GUID: guid,
100+
BlogID: blog.ID,
101+
}
102+
if i := item.Image; i != nil {
103+
e.ImageURL = i.URL
104+
}
105+
if t := item.PublishedParsed; t == nil {
106+
e.PublishedAt = time.Now().In(boil.GetLocation())
107+
} else {
108+
e.PublishedAt = t.In(boil.GetLocation())
109+
}
110+
111+
err = e.Insert(s.ctx, tx, boil.Infer())
112+
if err != nil {
113+
return errors.WithStack(err)
114+
}
115+
n++
120116
}
121117

122-
err = e.Insert(s.ctx, tx, boil.Infer())
123-
if err != nil {
124-
_ = tx.Rollback()
125-
return 0, errors.WithStack(err)
126-
}
127-
n++
128-
}
118+
return nil
119+
})
129120

130-
err = tx.Commit()
131121
if err != nil {
132-
_ = tx.Rollback()
133-
return 0, errors.WithStack(err)
122+
return 0, err
134123
}
135124
return n, nil
136125
}

infra/store/github/github_store.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ type githubStoreImpl struct {
2424
}
2525

2626
// NewGitHubStore returns new github store
27-
func NewGitHubStore(ctx context.Context, db *sql.DB, cli *redis.Client) store.GitHubStore {
27+
func NewGitHubStore(ctx context.Context, db *sqlutil.DB, cli *redis.Client) store.GitHubStore {
2828
return &githubStoreImpl{
2929
ctx: ctx,
30-
db: sqlutil.New(db),
30+
db: db,
3131
cli: cli,
3232
}
3333
}

infra/store/invitation/invitation_store.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,16 @@ import (
1414
"github.com/ProgrammingLab/prolab-accounts/infra/record"
1515
"github.com/ProgrammingLab/prolab-accounts/infra/store"
1616
"github.com/ProgrammingLab/prolab-accounts/model"
17+
"github.com/ProgrammingLab/prolab-accounts/sqlutil"
1718
)
1819

1920
type invitationStoreImpl struct {
2021
ctx context.Context
21-
db *sql.DB
22+
db *sqlutil.DB
2223
}
2324

2425
// NewInvitationStore returns new invitation store
25-
func NewInvitationStore(ctx context.Context, db *sql.DB) store.InvitationStore {
26+
func NewInvitationStore(ctx context.Context, db *sqlutil.DB) store.InvitationStore {
2627
return &invitationStoreImpl{
2728
ctx: ctx,
2829
db: db,

infra/store/profile/profile_store.go

Lines changed: 26 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -8,64 +8,49 @@ import (
88
"github.com/volatiletech/null"
99
"github.com/volatiletech/sqlboiler/boil"
1010

11-
"github.com/ProgrammingLab/prolab-accounts/app/util"
1211
"github.com/ProgrammingLab/prolab-accounts/infra/record"
1312
"github.com/ProgrammingLab/prolab-accounts/infra/store"
1413
"github.com/ProgrammingLab/prolab-accounts/model"
14+
"github.com/ProgrammingLab/prolab-accounts/sqlutil"
1515
)
1616

1717
type profileStoreImpl struct {
1818
ctx context.Context
19-
db *sql.DB
19+
db *sqlutil.DB
2020
}
2121

2222
// NewProfileStore returns new profile store
23-
func NewProfileStore(ctx context.Context, db *sql.DB) store.ProfileStore {
23+
func NewProfileStore(ctx context.Context, db *sqlutil.DB) store.ProfileStore {
2424
return &profileStoreImpl{
2525
ctx: ctx,
2626
db: db,
2727
}
2828
}
2929

30-
func (s *profileStoreImpl) CreateOrUpdateProfile(userID model.UserID, profile *record.Profile) (err error) {
31-
tx, err := s.db.Begin()
32-
if err != nil {
33-
return errors.WithStack(err)
34-
}
35-
defer func() {
36-
if e := util.ErrorFromRecover(recover()); e != nil {
37-
_ = tx.Rollback()
38-
err = e
30+
func (s *profileStoreImpl) CreateOrUpdateProfile(userID model.UserID, profile *record.Profile) error {
31+
err := s.db.Watch(s.ctx, func(ctx context.Context, tx *sql.Tx) error {
32+
if profile.ID == 0 {
33+
err := profile.Insert(s.ctx, tx, boil.Infer())
34+
if err != nil {
35+
return errors.WithStack(err)
36+
}
37+
u := record.User{
38+
ID: int64(userID),
39+
ProfileID: null.Int64From(profile.ID),
40+
}
41+
_, err = u.Update(s.ctx, tx, boil.Whitelist("profile_id", "updated_at"))
42+
if err != nil {
43+
return errors.WithStack(err)
44+
}
45+
} else {
46+
_, err := profile.Update(s.ctx, tx, boil.Infer())
47+
if err != nil {
48+
return errors.WithStack(err)
49+
}
3950
}
40-
}()
4151

42-
if profile.ID == 0 {
43-
err = profile.Insert(s.ctx, tx, boil.Infer())
44-
if err != nil {
45-
_ = tx.Rollback()
46-
return errors.WithStack(err)
47-
}
48-
u := record.User{
49-
ID: int64(userID),
50-
ProfileID: null.Int64From(profile.ID),
51-
}
52-
_, err = u.Update(s.ctx, tx, boil.Whitelist("profile_id", "updated_at"))
53-
if err != nil {
54-
_ = tx.Rollback()
55-
return errors.WithStack(err)
56-
}
57-
} else {
58-
_, err = profile.Update(s.ctx, tx, boil.Infer())
59-
if err != nil {
60-
_ = tx.Rollback()
61-
return errors.WithStack(err)
62-
}
63-
}
52+
return nil
53+
})
6454

65-
err = tx.Commit()
66-
if err != nil {
67-
_ = tx.Rollback()
68-
return errors.WithStack(err)
69-
}
70-
return nil
55+
return err
7156
}

infra/store/role/role_store.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@ package rolestore
22

33
import (
44
"context"
5-
"database/sql"
65

76
"github.com/pkg/errors"
87

98
"github.com/ProgrammingLab/prolab-accounts/infra/record"
109
"github.com/ProgrammingLab/prolab-accounts/infra/store"
10+
"github.com/ProgrammingLab/prolab-accounts/sqlutil"
1111
)
1212

1313
type roleStoreImpl struct {
1414
ctx context.Context
15-
db *sql.DB
15+
db *sqlutil.DB
1616
}
1717

1818
// NewRoleStore returns new role store
19-
func NewRoleStore(ctx context.Context, db *sql.DB) store.RoleStore {
19+
func NewRoleStore(ctx context.Context, db *sqlutil.DB) store.RoleStore {
2020
return &roleStoreImpl{
2121
ctx: ctx,
2222
db: db,

infra/store/session/session_store.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@ import (
1515
"github.com/ProgrammingLab/prolab-accounts/infra/record"
1616
"github.com/ProgrammingLab/prolab-accounts/infra/store"
1717
"github.com/ProgrammingLab/prolab-accounts/model"
18+
"github.com/ProgrammingLab/prolab-accounts/sqlutil"
1819
)
1920

2021
type sessionStoreImpl struct {
2122
ctx context.Context
2223
client *redis.Client
23-
db *sql.DB
24+
db *sqlutil.DB
2425
}
2526

2627
const (
@@ -29,7 +30,7 @@ const (
2930
)
3031

3132
// NewSessionStore returns new session store
32-
func NewSessionStore(ctx context.Context, cli *redis.Client, db *sql.DB) store.SessionStore {
33+
func NewSessionStore(ctx context.Context, cli *redis.Client, db *sqlutil.DB) store.SessionStore {
3334
return &sessionStoreImpl{
3435
ctx: ctx,
3536
client: cli,

0 commit comments

Comments
 (0)