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
|
package feed
import (
"adammathes.com/neko/models"
"log"
)
type Feed struct {
Id int64 `json:"_id"`
Url string `json:"url"`
Title string `json:"title"`
// TODO: last_updated scan
}
func NewFeed(url string) error {
stmt, err := models.DB.Prepare("INSERT INTO feed(url) VALUES(?)")
if err != nil {
return err
}
_, err = stmt.Exec(url)
if err != nil {
return err
}
return nil
}
func All() ([]*Feed, error) {
return filter(" ORDER BY URL desc")
}
func filter(where string) ([]*Feed, error) {
// todo: add back in title
rows, err := models.DB.Query(`SELECT id, url, title
FROM feed ` + where)
if err != nil {
return nil, err
}
defer rows.Close()
feeds := make([]*Feed, 0)
for rows.Next() {
f := new(Feed)
err := rows.Scan(&f.Id, &f.Url, &f.Title)
if err != nil {
return nil, err
}
feeds = append(feeds, f)
}
if err = rows.Err(); err != nil {
return nil, err
}
return feeds, nil
}
func (f *Feed) Update() {
if len(f.Title) == 0 {
return
}
if f.Id == 0 {
return
}
if len(f.Url) == 0 {
return
}
models.DB.Query(`UPDATE feed
SET title=?, url=?
WHERE id=?`, f.Title, f.Url, f.Id)
}
func (f *Feed) Delete() {
log.Println("lets delete some shiteeee")
_, err := models.DB.Exec(`DELETE FROM feed
WHERE id=?`, f.Id)
if err != nil {
log.Println(err)
}
}
func (f *Feed) ByUrl(url string) error {
err := models.DB.QueryRow(`SELECT id, url, title
FROM feed
WHERE url = ?`, url).Scan(&f.Id, &f.Url, &f.Title)
if err != nil {
return err
}
return nil
}
func (f *Feed) Create() error {
res, err := models.DB.Exec(`INSERT INTO feed(url, title)
VALUES(?, ?)`, f.Url, f.Title)
if err != nil {
return err
}
id, _ := res.LastInsertId()
f.Id = id
return nil
}
|