-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathmysql_article.go
187 lines (153 loc) · 4.05 KB
/
mysql_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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package mysql
import (
"context"
"database/sql"
"fmt"
"github.com/sirupsen/logrus"
"github.com/bxcodec/go-clean-arch/article/repository"
"github.com/bxcodec/go-clean-arch/domain"
)
var _ domain.ArticleRepository = &mysqlArticleRepository{}
type mysqlArticleRepository struct {
Conn *sql.DB
}
// NewMysqlArticleRepository will create an object that represent the article.Repository interface
func NewMysqlArticleRepository(Conn *sql.DB) domain.ArticleRepository {
return &mysqlArticleRepository{Conn}
}
func (m *mysqlArticleRepository) Fetch(ctx context.Context, cursor string, num int64) (res []domain.Article, nextCursor string, err error) {
query := `SELECT id,title,content, author_id, updated_at, created_at
FROM article WHERE created_at > ? ORDER BY created_at LIMIT ? `
decodedCursor, err := repository.DecodeCursor(cursor)
if err != nil && cursor != "" {
return nil, "", domain.ErrBadParamInput
}
res, err = m.fetch(ctx, query, decodedCursor, num)
if err != nil {
return nil, "", err
}
if len(res) == int(num) {
nextCursor = repository.EncodeCursor(res[len(res)-1].CreatedAt)
}
return
}
func (m *mysqlArticleRepository) GetByID(ctx context.Context, id int64) (res domain.Article, err error) {
query := `SELECT id,title,content, author_id, updated_at, created_at
FROM article WHERE ID = ?`
list, err := m.fetch(ctx, query, id)
if err != nil {
return domain.Article{}, err
}
if len(list) > 0 {
res = list[0]
} else {
return res, domain.ErrNotFound
}
return
}
func (m *mysqlArticleRepository) GetByTitle(ctx context.Context, title string) (res domain.Article, err error) {
query := `SELECT id,title,content, author_id, updated_at, created_at
FROM article WHERE title = ?`
list, err := m.fetch(ctx, query, title)
if err != nil {
return
}
if len(list) > 0 {
res = list[0]
} else {
return res, domain.ErrNotFound
}
return
}
func (m *mysqlArticleRepository) Store(ctx context.Context, a *domain.Article) (err error) {
query := `INSERT article SET title=? , content=? , author_id=?, updated_at=? , created_at=?`
stmt, err := m.Conn.PrepareContext(ctx, query)
if err != nil {
return
}
res, err := stmt.ExecContext(ctx, a.Title, a.Content, a.Author.ID, a.UpdatedAt, a.CreatedAt)
if err != nil {
return
}
lastID, err := res.LastInsertId()
if err != nil {
return
}
a.ID = lastID
return
}
func (m *mysqlArticleRepository) Delete(ctx context.Context, id int64) (err error) {
query := "DELETE FROM article WHERE id = ?"
stmt, err := m.Conn.PrepareContext(ctx, query)
if err != nil {
return
}
res, err := stmt.ExecContext(ctx, id)
if err != nil {
return
}
rowsAfected, err := res.RowsAffected()
if err != nil {
return
}
if rowsAfected != 1 {
err = fmt.Errorf("Weird Behavior. Total Affected: %d", rowsAfected)
return
}
return
}
func (m *mysqlArticleRepository) Update(ctx context.Context, ar *domain.Article) (err error) {
query := `UPDATE article set title=?, content=?, author_id=?, updated_at=? WHERE ID = ?`
stmt, err := m.Conn.PrepareContext(ctx, query)
if err != nil {
return
}
res, err := stmt.ExecContext(ctx, ar.Title, ar.Content, ar.Author.ID, ar.UpdatedAt, ar.ID)
if err != nil {
return
}
affect, err := res.RowsAffected()
if err != nil {
return
}
if affect != 1 {
err = fmt.Errorf("Weird Behavior. Total Affected: %d", affect)
return
}
return
}
func (m *mysqlArticleRepository) fetch(ctx context.Context, query string, args ...interface{}) (result []domain.Article, err error) {
rows, err := m.Conn.QueryContext(ctx, query, args...)
if err != nil {
logrus.Error(err)
return nil, err
}
defer func() {
errRow := rows.Close()
if errRow != nil {
logrus.Error(errRow)
}
}()
result = make([]domain.Article, 0)
for rows.Next() {
t := domain.Article{}
authorID := int64(0)
err = rows.Scan(
&t.ID,
&t.Title,
&t.Content,
&authorID,
&t.UpdatedAt,
&t.CreatedAt,
)
if err != nil {
logrus.Error(err)
return nil, err
}
t.Author = domain.Author{
ID: authorID,
}
result = append(result, t)
}
return result, nil
}