Skip to content

Commit fb1eb67

Browse files
committed
refactor: repo should correspond to the domain, not the db table
1 parent 378425e commit fb1eb67

File tree

8 files changed

+43
-129
lines changed

8 files changed

+43
-129
lines changed

api/api.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ func Run() {
9595
})
9696

9797
feeds := authed.Group("/feeds")
98-
feedAPIHandler := newFeedAPI(server.NewFeed(repo.NewFeed(repo.DB), repo.NewItem(repo.DB)))
98+
feedAPIHandler := newFeedAPI(server.NewFeed(repo.NewFeed(repo.DB)))
9999
feeds.GET("", feedAPIHandler.All)
100100
feeds.GET("/:id", feedAPIHandler.Get)
101101
feeds.POST("", feedAPIHandler.Create)
@@ -105,7 +105,7 @@ func Run() {
105105
feeds.POST("/refresh", feedAPIHandler.Refresh)
106106

107107
groups := authed.Group("/groups")
108-
groupAPIHandler := newGroupAPI(server.NewGroup(repo.NewGroup(repo.DB), repo.NewFeed(repo.DB)))
108+
groupAPIHandler := newGroupAPI(server.NewGroup(repo.NewGroup(repo.DB)))
109109
groups.GET("", groupAPIHandler.All)
110110
groups.POST("", groupAPIHandler.Create)
111111
groups.PATCH("/:id", groupAPIHandler.Update)

repo/feed.go

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package repo
22

33
import (
4+
"errors"
5+
46
"github.com/0x2e/fusion/model"
57

68
"gorm.io/gorm"
@@ -37,9 +39,10 @@ func (f Feed) Update(id uint, feed *model.Feed) error {
3739
}
3840

3941
func (f Feed) Delete(id uint) error {
40-
return f.db.Delete(&model.Feed{}, id).Error
41-
}
42-
43-
func (f Feed) UpdateGroupID(from uint, to uint) error {
44-
return f.db.Model(&model.Feed{}).Where("group_id = ?", from).Update("group_id", to).Error
42+
return f.db.Transaction(func(tx *gorm.DB) error {
43+
if err := tx.Model(&model.Item{}).Where("feed_id = ?", id).Delete(&model.Item{}).Error; err != nil && !errors.Is(err, ErrNotFound) {
44+
return err
45+
}
46+
return tx.Delete(&model.Feed{}, id).Error
47+
})
4548
}

repo/group.go

+18-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package repo
22

33
import (
4+
"errors"
5+
46
"github.com/0x2e/fusion/model"
57

68
"gorm.io/gorm"
@@ -16,26 +18,32 @@ type Group struct {
1618
db *gorm.DB
1719
}
1820

19-
func (f Group) All() ([]*model.Group, error) {
21+
func (g Group) All() ([]*model.Group, error) {
2022
var res []*model.Group
21-
err := f.db.Find(&res).Error
23+
err := g.db.Find(&res).Error
2224
return res, err
2325
}
2426

25-
func (f Group) Get(id uint) (*model.Group, error) {
27+
func (g Group) Get(id uint) (*model.Group, error) {
2628
var res model.Group
27-
err := f.db.First(&res, id).Error
29+
err := g.db.First(&res, id).Error
2830
return &res, err
2931
}
3032

31-
func (f Group) Create(group *model.Group) error {
32-
return f.db.Create(group).Error
33+
func (g Group) Create(group *model.Group) error {
34+
return g.db.Create(group).Error
3335
}
3436

35-
func (f Group) Update(id uint, group *model.Group) error {
36-
return f.db.Model(&model.Group{}).Where("id = ?", id).Updates(group).Error
37+
func (g Group) Update(id uint, group *model.Group) error {
38+
return g.db.Model(&model.Group{}).Where("id = ?", id).Updates(group).Error
3739
}
3840

39-
func (f Group) Delete(id uint) error {
40-
return f.db.Delete(&model.Group{}, id).Error
41+
func (g Group) Delete(id uint) error {
42+
return g.db.Transaction(func(tx *gorm.DB) error {
43+
if err := tx.Model(&model.Feed{}).Where("group_id = ?", id).Update("group_id", 1).Error; err != nil && !errors.Is(err, ErrNotFound) {
44+
return err
45+
}
46+
47+
return tx.Delete(&model.Group{}, id).Error
48+
})
4149
}

repo/item.go

-4
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,6 @@ func (i Item) Delete(id uint) error {
7979
return i.db.Delete(&model.Item{}, id).Error
8080
}
8181

82-
func (i Item) DeleteByFeed(feedID uint) error {
83-
return i.db.Where("feed_id = ?", feedID).Delete(&model.Item{}).Error
84-
}
85-
8682
func (i Item) UpdateUnread(ids []uint, unread *bool) error {
8783
return i.db.Model(&model.Item{}).Where("id IN ?", ids).Update("unread", unread).Error
8884
}

server/feed.go

+8-18
Original file line numberDiff line numberDiff line change
@@ -23,24 +23,18 @@ type FeedRepo interface {
2323
Delete(id uint) error
2424
}
2525

26-
type ItemInFeedRepo interface {
27-
DeleteByFeed(id uint) error
28-
}
29-
3026
type Feed struct {
31-
feedRepo FeedRepo
32-
itemRepo ItemInFeedRepo
27+
repo FeedRepo
3328
}
3429

35-
func NewFeed(feedRepo FeedRepo, itemRepo ItemInFeedRepo) *Feed {
30+
func NewFeed(repo FeedRepo) *Feed {
3631
return &Feed{
37-
feedRepo: feedRepo,
38-
itemRepo: itemRepo,
32+
repo: repo,
3933
}
4034
}
4135

4236
func (f Feed) All(ctx context.Context) (*RespFeedAll, error) {
43-
data, err := f.feedRepo.All()
37+
data, err := f.repo.All()
4438
if err != nil {
4539
return nil, err
4640
}
@@ -64,7 +58,7 @@ func (f Feed) All(ctx context.Context) (*RespFeedAll, error) {
6458
}
6559

6660
func (f Feed) Get(ctx context.Context, req *ReqFeedGet) (*RespFeedGet, error) {
67-
data, err := f.feedRepo.Get(req.ID)
61+
data, err := f.repo.Get(req.ID)
6862
if err != nil {
6963
return nil, err
7064
}
@@ -94,7 +88,7 @@ func (f Feed) Create(ctx context.Context, req *ReqFeedCreate) error {
9488
return nil
9589
}
9690

97-
if err := f.feedRepo.Create(feeds); err != nil {
91+
if err := f.repo.Create(feeds); err != nil {
9892
if errors.Is(err, repo.ErrDuplicatedKey) {
9993
err = NewBizError(err, http.StatusBadRequest, "link is not allowed to be the same as other feeds")
10094
}
@@ -167,19 +161,15 @@ func (f Feed) Update(ctx context.Context, req *ReqFeedUpdate) error {
167161
if req.GroupID != nil {
168162
data.GroupID = *req.GroupID
169163
}
170-
err := f.feedRepo.Update(req.ID, data)
164+
err := f.repo.Update(req.ID, data)
171165
if errors.Is(err, repo.ErrDuplicatedKey) {
172166
err = NewBizError(err, http.StatusBadRequest, "link is not allowed to be the same as other feeds")
173167
}
174168
return err
175169
}
176170

177171
func (f Feed) Delete(ctx context.Context, req *ReqFeedDelete) error {
178-
// FIX: transaction
179-
if err := f.itemRepo.DeleteByFeed(req.ID); err != nil && !errors.Is(err, repo.ErrNotFound) {
180-
return err
181-
}
182-
return f.feedRepo.Delete(req.ID)
172+
return f.repo.Delete(req.ID)
183173
}
184174

185175
func (f Feed) Refresh(ctx context.Context, req *ReqFeedRefresh) error {

server/feed_mock.go

-37
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

server/group.go

+7-16
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,18 @@ type GroupRepo interface {
1818
Delete(id uint) error
1919
}
2020

21-
type FeedinGroupRepo interface {
22-
UpdateGroupID(from uint, to uint) error
23-
}
2421
type Group struct {
25-
groupRepo GroupRepo
26-
feedRepo FeedinGroupRepo
22+
repo GroupRepo
2723
}
2824

29-
func NewGroup(groupRepo GroupRepo, feedRepo FeedinGroupRepo) *Group {
25+
func NewGroup(repo GroupRepo) *Group {
3026
return &Group{
31-
groupRepo: groupRepo,
32-
feedRepo: feedRepo,
27+
repo: repo,
3328
}
3429
}
3530

3631
func (g Group) All(ctx context.Context) (*RespGroupAll, error) {
37-
data, err := g.groupRepo.All()
32+
data, err := g.repo.All()
3833
if err != nil {
3934
return nil, err
4035
}
@@ -55,15 +50,15 @@ func (g Group) Create(ctx context.Context, req *ReqGroupCreate) error {
5550
newGroup := &model.Group{
5651
Name: req.Name,
5752
}
58-
err := g.groupRepo.Create(newGroup)
53+
err := g.repo.Create(newGroup)
5954
if errors.Is(err, repo.ErrDuplicatedKey) {
6055
err = NewBizError(err, http.StatusBadRequest, "name is not allowed to be the same as other groups")
6156
}
6257
return err
6358
}
6459

6560
func (g Group) Update(ctx context.Context, req *ReqGroupUpdate) error {
66-
err := g.groupRepo.Update(req.ID, &model.Group{
61+
err := g.repo.Update(req.ID, &model.Group{
6762
Name: req.Name,
6863
})
6964
if errors.Is(err, repo.ErrDuplicatedKey) {
@@ -76,9 +71,5 @@ func (g Group) Delete(ctx context.Context, req *ReqGroupDelete) error {
7671
if req.ID == 1 {
7772
return errors.New("cannot delete the default group")
7873
}
79-
// FIX: transaction
80-
if err := g.feedRepo.UpdateGroupID(req.ID, 1); err != nil && !errors.Is(err, repo.ErrNotFound) {
81-
return err
82-
}
83-
return g.groupRepo.Delete(req.ID)
74+
return g.repo.Delete(req.ID)
8475
}

server/group_mock.go

-37
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)