-
Notifications
You must be signed in to change notification settings - Fork 0
/
banfile.go
98 lines (75 loc) · 1.58 KB
/
banfile.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
package models
import (
"database/sql"
"errors"
"github.com/eirka/eirka-libs/db"
e "github.com/eirka/eirka-libs/errors"
)
// BanFileModel holds request input
type BanFileModel struct {
Ib uint
Thread uint
ID uint
User uint
Reason string
Hash string
}
// IsValid will check struct validity
func (m *BanFileModel) IsValid() bool {
if m.Ib == 0 {
return false
}
if m.Thread == 0 {
return false
}
if m.ID == 0 {
return false
}
if m.User == 0 || m.User == 1 {
return false
}
if m.Reason == "" {
return false
}
if m.Hash == "" {
return false
}
return true
}
// Status will return info
func (m *BanFileModel) Status() (err error) {
// Get Database handle
dbase, err := db.GetDb()
if err != nil {
return
}
// get thread ib and title
err = dbase.QueryRow(`SELECT image_hash FROM threads
INNER JOIN posts ON threads.thread_id = posts.thread_id
INNER JOIN images ON posts.post_id = images.post_id
WHERE ib_id = ? AND threads.thread_id = ? AND post_num = ? LIMIT 1`, m.Ib, m.Thread, m.ID).Scan(&m.Hash)
if err == sql.ErrNoRows {
return e.ErrNotFound
} else if err != nil {
return
}
return
}
// Post will add the file to the table
func (m *BanFileModel) Post() (err error) {
// check model validity
if !m.IsValid() {
return errors.New("BanFileModel is not valid")
}
// Get Database handle
dbase, err := db.GetDb()
if err != nil {
return
}
_, err = dbase.Exec("INSERT IGNORE INTO banned_files (user_id,ib_id,ban_hash,ban_reason) VALUES (?,?,?,?)",
m.User, m.Ib, m.Hash, m.Reason)
if err != nil {
return
}
return
}