-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.go
125 lines (93 loc) · 2.77 KB
/
db.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
package main
import (
"errors"
"log"
"github.com/emaele/rss-telegram-notifier/entities"
"github.com/mmcdole/gofeed"
"gorm.io/gorm"
)
func retrieveFeedByID(db *gorm.DB, ID string) (entities.RssFeed, error) {
var feed entities.RssFeed
// search for the requested feed
res := db.Where("ID = ?", ID).Find(&feed)
if res.RowsAffected == 0 {
return feed, errors.New("no feed found")
}
if res.Error != nil {
return feed, res.Error
}
return feed, nil
}
func retrieveFeeds(db *gorm.DB) ([]entities.RssFeed, error) {
var feeds []entities.RssFeed
res := db.Find(&feeds)
if res.RowsAffected == 0 {
return feeds, errors.New("no record found")
}
if res.Error != nil {
return feeds, res.Error
}
return feeds, nil
}
func retriveItemsByFeedID(db *gorm.DB, ID string) ([]entities.RssItem, error) {
var items []entities.RssItem
res := db.Where("Feed = ?", ID).Find(&items)
if res.RowsAffected == 0 {
return items, errors.New("no items found")
}
if res.Error != nil {
return items, res.Error
}
return items, nil
}
func addItems(db *gorm.DB, feedID int64, items []*gofeed.Item, markAsSent bool) {
log.Printf("adding %d feed elements\n", len(items))
for _, feedelement := range items {
element := entities.RssItem{
Title: feedelement.Title,
Description: feedelement.Description,
URL: feedelement.Link,
Sent: markAsSent,
Feed: feedID,
}
if feedelement.Image != nil {
element.ImageURL = feedelement.Image.URL
}
err := db.Where(entities.RssItem{URL: element.URL}).FirstOrCreate(&element).Error
if err != nil {
log.Printf("there was an error adding %s, %v", element.Title, err)
}
}
}
func retrieveFeedTitle(db *gorm.DB, feedID int64) string {
var feedTitle string
db.Table("rss_feeds").Where("ID = ?", feedID).Pluck("Title", &feedTitle)
return feedTitle
}
func retrieveItemsToSend(db *gorm.DB) ([]entities.RssItem, error) {
var elements []entities.RssItem
err := db.Where("sent = ?", false).Find(&elements).Error
return elements, err
}
func setItemAsSent(db *gorm.DB, element *entities.RssItem) error {
return db.Model(&element).Update("sent", true).Error
}
func deleteItemFromDB(db *gorm.DB, element *entities.RssItem) error {
return db.Delete(element).Error
}
func deleteFeedFromDB(db *gorm.DB, element *entities.RssFeed) error {
return db.Delete(element).Error
}
func feedExists(db *gorm.DB, URL string) bool {
// checking if the feed is duplicate
var f entities.RssFeed
rows := db.Where(entities.RssFeed{URL: URL}).Find(&f).RowsAffected
return rows != 0
}
func createFeed(db *gorm.DB, rssfeed *entities.RssFeed) error {
return db.Create(rssfeed).Error
}
func retrieveFeedID(db *gorm.DB, URL string) (feedID int64) {
db.Table("rss_feeds").Where(entities.RssFeed{URL: URL}).Pluck("id", &feedID)
return
}