-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy patharticle.go
72 lines (56 loc) · 1.54 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
72
package blog
import (
"context"
"time"
"github.com/uptrace/go-realworld-example-app/org"
"github.com/uptrace/go-realworld-example-app/rwe"
)
type Article struct {
tableName struct{} `pg:"articles,alias:a"`
ID uint64 `json:"-"`
Slug string `json:"slug"`
Title string `json:"title"`
Description string `json:"description"`
Body string `json:"body"`
Author *org.Profile `json:"author" pg:"rel:has-one"`
AuthorID uint64 `json:"-"`
Tags []ArticleTag `json:"-" pg:"rel:has-many"`
TagList []string `json:"tagList" pg:"-,array"`
Favorited bool `json:"favorited" pg:"-"`
FavoritesCount int `json:"favoritesCount" pg:"-"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
}
type ArticleTag struct {
tableName struct{} `pg:"alias:t"`
ArticleID uint64
Tag string
}
type FavoriteArticle struct {
tableName struct{} `pg:"alias:fa"`
UserID uint64
ArticleID uint64
}
func SelectArticle(c context.Context, slug string) (*Article, error) {
article := new(Article)
if err := rwe.PGMain().ModelContext(c, article).
Where("slug = ?", slug).
Select(); err != nil {
return nil, err
}
return article, nil
}
func selectArticleByFilter(ctx context.Context, f *ArticleFilter) (*Article, error) {
article := new(Article)
if err := rwe.PGMain().
ModelContext(ctx, article).
ColumnExpr("?TableColumns").
Apply(f.query).
Select(); err != nil {
return nil, err
}
if article.TagList == nil {
article.TagList = make([]string, 0)
}
return article, nil
}