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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
|
package graphql
// THIS CODE WILL BE UPDATED WITH SCHEMA CHANGES. PREVIOUS IMPLEMENTATION FOR SCHEMA CHANGES WILL BE KEPT IN THE COMMENT SECTION. IMPLEMENTATION FOR UNCHANGED SCHEMA WILL BE KEPT.
import (
"context"
"database/sql"
"fmt"
"strconv"
"time"
"github.com/mmcdole/gofeed"
"undef.ninja/x/feedaka/graphql/model"
)
type Resolver struct {
DB *sql.DB
}
// AddFeed is the resolver for the addFeed field.
func (r *mutationResolver) AddFeed(ctx context.Context, url string) (*model.Feed, error) {
// Fetch the feed to get its title
fp := gofeed.NewParser()
feed, err := fp.ParseURL(url)
if err != nil {
return nil, fmt.Errorf("failed to parse feed: %w", err)
}
// Insert the feed into the database
result, err := r.DB.Exec(
"INSERT INTO feeds (url, title, fetched_at) VALUES (?, ?, ?)",
url, feed.Title, time.Now().Unix(),
)
if err != nil {
return nil, fmt.Errorf("failed to insert feed: %w", err)
}
id, err := result.LastInsertId()
if err != nil {
return nil, fmt.Errorf("failed to get last insert id: %w", err)
}
// Insert articles from the feed
for _, item := range feed.Items {
_, err = r.DB.Exec(
"INSERT INTO articles (feed_id, guid, title, url, is_read) VALUES (?, ?, ?, ?, ?)",
id, item.GUID, item.Title, item.Link, 0,
)
if err != nil {
// Log but don't fail on individual article errors
fmt.Printf("Failed to insert article: %v\n", err)
}
}
return &model.Feed{
ID: strconv.FormatInt(id, 10),
URL: url,
Title: feed.Title,
FetchedAt: time.Now().Format(time.RFC3339),
}, nil
}
// RemoveFeed is the resolver for the removeFeed field.
func (r *mutationResolver) RemoveFeed(ctx context.Context, id string) (bool, error) {
feedID, err := strconv.ParseInt(id, 10, 64)
if err != nil {
return false, fmt.Errorf("invalid feed ID: %w", err)
}
// Start a transaction
tx, err := r.DB.Begin()
if err != nil {
return false, fmt.Errorf("failed to begin transaction: %w", err)
}
defer tx.Rollback()
// Delete articles first (foreign key constraint)
_, err = tx.Exec("DELETE FROM articles WHERE feed_id = ?", feedID)
if err != nil {
return false, fmt.Errorf("failed to delete articles: %w", err)
}
// Delete the feed
result, err := tx.Exec("DELETE FROM feeds WHERE id = ?", feedID)
if err != nil {
return false, fmt.Errorf("failed to delete feed: %w", err)
}
rowsAffected, err := result.RowsAffected()
if err != nil {
return false, fmt.Errorf("failed to get rows affected: %w", err)
}
if rowsAffected == 0 {
return false, fmt.Errorf("feed not found")
}
err = tx.Commit()
if err != nil {
return false, fmt.Errorf("failed to commit transaction: %w", err)
}
return true, nil
}
// MarkArticleRead is the resolver for the markArticleRead field.
func (r *mutationResolver) MarkArticleRead(ctx context.Context, id string) (*model.Article, error) {
articleID, err := strconv.ParseInt(id, 10, 64)
if err != nil {
return nil, fmt.Errorf("invalid article ID: %w", err)
}
// Update the article's read status
_, err = r.DB.Exec("UPDATE articles SET is_read = 1 WHERE id = ?", articleID)
if err != nil {
return nil, fmt.Errorf("failed to mark article as read: %w", err)
}
// Fetch the updated article
return r.Query().Article(ctx, id)
}
// MarkArticleUnread is the resolver for the markArticleUnread field.
func (r *mutationResolver) MarkArticleUnread(ctx context.Context, id string) (*model.Article, error) {
articleID, err := strconv.ParseInt(id, 10, 64)
if err != nil {
return nil, fmt.Errorf("invalid article ID: %w", err)
}
// Update the article's read status
_, err = r.DB.Exec("UPDATE articles SET is_read = 0 WHERE id = ?", articleID)
if err != nil {
return nil, fmt.Errorf("failed to mark article as unread: %w", err)
}
// Fetch the updated article
return r.Query().Article(ctx, id)
}
// MarkFeedRead is the resolver for the markFeedRead field.
func (r *mutationResolver) MarkFeedRead(ctx context.Context, id string) (*model.Feed, error) {
feedID, err := strconv.ParseInt(id, 10, 64)
if err != nil {
return nil, fmt.Errorf("invalid feed ID: %w", err)
}
// Update all articles in the feed to be read
_, err = r.DB.Exec("UPDATE articles SET is_read = 1 WHERE feed_id = ?", feedID)
if err != nil {
return nil, fmt.Errorf("failed to mark feed as read: %w", err)
}
// Fetch the updated feed
return r.Query().Feed(ctx, id)
}
// MarkFeedUnread is the resolver for the markFeedUnread field.
func (r *mutationResolver) MarkFeedUnread(ctx context.Context, id string) (*model.Feed, error) {
feedID, err := strconv.ParseInt(id, 10, 64)
if err != nil {
return nil, fmt.Errorf("invalid feed ID: %w", err)
}
// Update all articles in the feed to be unread
_, err = r.DB.Exec("UPDATE articles SET is_read = 0 WHERE feed_id = ?", feedID)
if err != nil {
return nil, fmt.Errorf("failed to mark feed as unread: %w", err)
}
// Fetch the updated feed
return r.Query().Feed(ctx, id)
}
// Feeds is the resolver for the feeds field.
func (r *queryResolver) Feeds(ctx context.Context) ([]*model.Feed, error) {
rows, err := r.DB.Query("SELECT id, url, title, fetched_at FROM feeds")
if err != nil {
return nil, fmt.Errorf("failed to query feeds: %w", err)
}
defer rows.Close()
var feeds []*model.Feed
for rows.Next() {
var feed model.Feed
var fetchedAt int64
err := rows.Scan(&feed.ID, &feed.URL, &feed.Title, &fetchedAt)
if err != nil {
return nil, fmt.Errorf("failed to scan feed: %w", err)
}
feed.FetchedAt = time.Unix(fetchedAt, 0).Format(time.RFC3339)
feeds = append(feeds, &feed)
}
if err = rows.Err(); err != nil {
return nil, fmt.Errorf("error iterating over feeds: %w", err)
}
return feeds, nil
}
// UnreadArticles is the resolver for the unreadArticles field.
func (r *queryResolver) UnreadArticles(ctx context.Context) ([]*model.Article, error) {
rows, err := r.DB.Query(`
SELECT a.id, a.feed_id, a.guid, a.title, a.url, a.is_read,
f.id, f.url, f.title
FROM articles AS a
INNER JOIN feeds AS f ON a.feed_id = f.id
WHERE a.is_read = 0
ORDER BY a.id DESC
LIMIT 100
`)
if err != nil {
return nil, fmt.Errorf("failed to query unread articles: %w", err)
}
defer rows.Close()
var articles []*model.Article
for rows.Next() {
var article model.Article
var feed model.Feed
var isRead int
err := rows.Scan(
&article.ID, &article.FeedID, &article.GUID, &article.Title, &article.URL, &isRead,
&feed.ID, &feed.URL, &feed.Title,
)
if err != nil {
return nil, fmt.Errorf("failed to scan article: %w", err)
}
article.IsRead = isRead == 1
article.Feed = &feed
articles = append(articles, &article)
}
if err = rows.Err(); err != nil {
return nil, fmt.Errorf("error iterating over articles: %w", err)
}
return articles, nil
}
// ReadArticles is the resolver for the readArticles field.
func (r *queryResolver) ReadArticles(ctx context.Context) ([]*model.Article, error) {
rows, err := r.DB.Query(`
SELECT a.id, a.feed_id, a.guid, a.title, a.url, a.is_read,
f.id, f.url, f.title
FROM articles AS a
INNER JOIN feeds AS f ON a.feed_id = f.id
WHERE a.is_read = 1
ORDER BY a.id DESC
LIMIT 100
`)
if err != nil {
return nil, fmt.Errorf("failed to query read articles: %w", err)
}
defer rows.Close()
var articles []*model.Article
for rows.Next() {
var article model.Article
var feed model.Feed
var isRead int
err := rows.Scan(
&article.ID, &article.FeedID, &article.GUID, &article.Title, &article.URL, &isRead,
&feed.ID, &feed.URL, &feed.Title,
)
if err != nil {
return nil, fmt.Errorf("failed to scan article: %w", err)
}
article.IsRead = isRead == 1
article.Feed = &feed
articles = append(articles, &article)
}
if err = rows.Err(); err != nil {
return nil, fmt.Errorf("error iterating over articles: %w", err)
}
return articles, nil
}
// Feed is the resolver for the feed field.
func (r *queryResolver) Feed(ctx context.Context, id string) (*model.Feed, error) {
feedID, err := strconv.ParseInt(id, 10, 64)
if err != nil {
return nil, fmt.Errorf("invalid feed ID: %w", err)
}
var feed model.Feed
var fetchedAt int64
err = r.DB.QueryRow(
"SELECT id, url, title, fetched_at FROM feeds WHERE id = ?",
feedID,
).Scan(&feed.ID, &feed.URL, &feed.Title, &fetchedAt)
if err != nil {
if err == sql.ErrNoRows {
return nil, fmt.Errorf("feed not found")
}
return nil, fmt.Errorf("failed to query feed: %w", err)
}
feed.FetchedAt = time.Unix(fetchedAt, 0).Format(time.RFC3339)
return &feed, nil
}
// Article is the resolver for the article field.
func (r *queryResolver) Article(ctx context.Context, id string) (*model.Article, error) {
articleID, err := strconv.ParseInt(id, 10, 64)
if err != nil {
return nil, fmt.Errorf("invalid article ID: %w", err)
}
var article model.Article
var feed model.Feed
var isRead int
err = r.DB.QueryRow(`
SELECT a.id, a.feed_id, a.guid, a.title, a.url, a.is_read,
f.id, f.url, f.title
FROM articles AS a
INNER JOIN feeds AS f ON a.feed_id = f.id
WHERE a.id = ?
`, articleID).Scan(
&article.ID, &article.FeedID, &article.GUID, &article.Title, &article.URL, &isRead,
&feed.ID, &feed.URL, &feed.Title,
)
if err != nil {
if err == sql.ErrNoRows {
return nil, fmt.Errorf("article not found")
}
return nil, fmt.Errorf("failed to query article: %w", err)
}
article.IsRead = isRead == 1
article.Feed = &feed
return &article, nil
}
// Mutation returns MutationResolver implementation.
func (r *Resolver) Mutation() MutationResolver { return &mutationResolver{r} }
// Query returns QueryResolver implementation.
func (r *Resolver) Query() QueryResolver { return &queryResolver{r} }
type mutationResolver struct{ *Resolver }
type queryResolver struct{ *Resolver }
|