-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrecord.go
96 lines (78 loc) · 1.68 KB
/
record.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
package main
import (
"log"
"time"
)
type Record struct {
Id int `json:"id"`
DomainId int `json:"domain_id"`
Name string `json:"name"`
Type string `json:"type"`
Content string `json:"content"`
Ttl int `json:"ttl"`
Priority *int `json:"prio"`
CreatedAt *time.Time `json:"created_at"`
UpdatedAt *time.Time `json:"updated_at"`
}
func FindRecord(id int) (*Record, error) {
row := db.QueryRow("SELECT id, domain_id, name, type, content, ttl, prio FROM records WHERE id = ?", id)
r := &Record{}
err := row.Scan(
&r.Id,
&r.DomainId,
&r.Name,
&r.Type,
&r.Content,
&r.Ttl,
&r.Priority,
)
return r, err
}
func AllRecords() []*Record {
rows, err := db.Query("SELECT id, domain_id, name, type, content, ttl, prio FROM records")
if err != nil {
log.Fatal(err)
}
records := []*Record{}
for rows.Next() {
r := &Record{}
err := rows.Scan(
&r.Id,
&r.DomainId,
&r.Name,
&r.Type,
&r.Content,
&r.Ttl,
&r.Priority,
)
if err != nil {
log.Fatal(err)
}
records = append(records, r)
}
return records
}
func (r *Record) Create() error {
sql := "INSERT INTO records (id, domain_id, name, type, content, ttl, prio) VALUES (?, ?, ?, ?, ?, ?, ?)"
_, err := db.Exec(
sql,
r.Id,
r.DomainId,
r.Name,
r.Type,
r.Content,
r.Ttl,
r.Priority,
)
return err
}
func (r *Record) Update() error {
sql := "UPDATE records SET name=?, type=?, content=?, ttl=?, prio=? WHERE id=?"
_, err := db.Exec(sql, r.Name, r.Type, r.Content, r.Ttl, r.Priority, r.Id)
return err
}
func (r *Record) Delete() error {
sql := "DELETE FROM records WHERE id = ?"
_, err := db.Exec(sql, r.Id)
return err
}