/
article.go
71 lines (59 loc) · 1.64 KB
/
article.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
package biz
import (
"context"
"gorm.io/gorm"
"time"
"go.uber.org/zap"
)
type Article struct {
ID int64
Title string
Content string
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt gorm.DeletedAt
Like int64
}
type ArticleRepo interface {
ListArticle(ctx context.Context) ([]*Article, error)
GetArticle(ctx context.Context, id int64) (*Article, error)
CreateArticle(ctx context.Context, article *Article) error
UpdateArticle(ctx context.Context, id int64, article *Article) error
DeleteArticle(ctx context.Context, id int64) error
GetArticleLike(ctx context.Context, id int64) (rv int64, err error)
IncArticleLike(ctx context.Context, id int64) error
}
type ArticleUsecase struct {
repo ArticleRepo
}
func NewArticleUsecase(repo ArticleRepo, logger *zap.Logger) *ArticleUsecase {
return &ArticleUsecase{repo: repo}
}
func (uc *ArticleUsecase) List(ctx context.Context) (ps []*Article, err error) {
ps, err = uc.repo.ListArticle(ctx)
return
}
func (uc *ArticleUsecase) Get(ctx context.Context, id int64) (p *Article, err error) {
p, err = uc.repo.GetArticle(ctx, id)
if err != nil {
return
}
err = uc.repo.IncArticleLike(ctx, id)
if err != nil {
return
}
p.Like, err = uc.repo.GetArticleLike(ctx, id)
if err != nil {
return
}
return
}
func (uc *ArticleUsecase) Create(ctx context.Context, article *Article) error {
return uc.repo.CreateArticle(ctx, article)
}
func (uc *ArticleUsecase) Update(ctx context.Context, id int64, article *Article) error {
return uc.repo.UpdateArticle(ctx, id, article)
}
func (uc *ArticleUsecase) Delete(ctx context.Context, id int64) error {
return uc.repo.DeleteArticle(ctx, id)
}