/
keyword.go
160 lines (146 loc) · 4.42 KB
/
keyword.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
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
/*
Copyright © 2020 reeve0930 <reeve0930@gmail.com>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package db
import (
"fmt"
"path/filepath"
"time"
"github.com/jinzhu/gorm"
_ "github.com/mattn/go-sqlite3"
homedir "github.com/mitchellh/go-homedir"
)
//KeywordRecFile is a struct of video file
type KeywordRecFile struct {
gorm.Model
Keyword string
Title string
PID int
FileTS string
FileMP4HD string
FileMP4SD string
Station string
Time time.Time
Drop int
Scramble int
Copy bool
}
func (v KeywordRecFile) String() string {
return fmt.Sprintf("Keyword: %s, Title: %s, PID : %d, Time : %s, Staiton : %s, FileTS : %s, FileMP4HD : %s, FileMP4SD : %s, Drop : %d, Scramble : %d, Copy : %t\n", v.Keyword, v.Title, v.PID, v.Time, v.Station, v.FileTS, v.FileMP4HD, v.FileMP4SD, v.Drop, v.Scramble, v.Copy)
}
// InitKeywordRecFileDB : Initialize KeywordRecFile DB
func InitKeywordRecFileDB() error {
home, err := homedir.Dir()
if err != nil {
return err
}
dbPath := filepath.Join(home, ".config", "falko", "foltia_keyword.sqlite3")
db, err := gorm.Open("sqlite3", dbPath)
defer db.Close()
if err != nil {
return err
}
db.AutoMigrate(&KeywordRecFile{})
return nil
}
// InsertKeywordRecFile : Insert Data to KeywordRecFile DB
func InsertKeywordRecFile(keyword string, title string, pid int, filets string, filemp4hd string, filemp4sd string, station string, time time.Time, drop int, scramble int, cp bool) error {
home, err := homedir.Dir()
if err != nil {
return err
}
dbPath := filepath.Join(home, ".config", "falko", "foltia_keyword.sqlite3")
db, err := gorm.Open("sqlite3", dbPath)
defer db.Close()
if err != nil {
return err
}
db.Create(&KeywordRecFile{Keyword: keyword, Title: title, PID: pid, FileTS: filets, FileMP4HD: filemp4hd, FileMP4SD: filemp4sd, Station: station, Time: time, Drop: drop, Scramble: scramble, Copy: cp})
return nil
}
// UpdateKeywordRecFile : Update Data of KeywordRecFile DB
func UpdateKeywordRecFile(id uint, keyword string, title string, pid int, filets string, filemp4hd string, filemp4sd string, station string, time time.Time, drop int, scramble int, cp bool) error {
home, err := homedir.Dir()
if err != nil {
return err
}
dbPath := filepath.Join(home, ".config", "falko", "foltia_keyword.sqlite3")
db, err := gorm.Open("sqlite3", dbPath)
defer db.Close()
if err != nil {
return err
}
var krf KeywordRecFile
db.First(&krf, id)
krf.Keyword = keyword
krf.Title = title
krf.PID = pid
krf.FileTS = filets
krf.FileMP4HD = filemp4hd
krf.FileMP4SD = filemp4sd
krf.Station = station
krf.Time = time
krf.Drop = drop
krf.Scramble = scramble
krf.Copy = cp
db.Save(&krf)
return nil
}
// DeleteKeywordRecFile : Delete Data of KeywordRecFile DB
func DeleteKeywordRecFile(id uint) error {
home, err := homedir.Dir()
if err != nil {
return err
}
dbPath := filepath.Join(home, ".config", "falko", "foltia_keyword.sqlite3")
db, err := gorm.Open("sqlite3", dbPath)
defer db.Close()
if err != nil {
return err
}
var krf KeywordRecFile
db.First(&krf, id)
db.Delete(&krf)
return nil
}
// GetAllKeywordRecFile : Get All Data from KeywordRecFile DB
func GetAllKeywordRecFile() ([]KeywordRecFile, error) {
home, err := homedir.Dir()
if err != nil {
return []KeywordRecFile{}, err
}
dbPath := filepath.Join(home, ".config", "falko", "foltia_keyword.sqlite3")
db, err := gorm.Open("sqlite3", dbPath)
defer db.Close()
if err != nil {
return []KeywordRecFile{}, err
}
var krfl []KeywordRecFile
db.Order("created_at desc").Find(&krfl)
return krfl, nil
}
// GetOneKeywordRecFile : Get Data from KeywordRecFile DB
func GetOneKeywordRecFile(id uint) (KeywordRecFile, error) {
home, err := homedir.Dir()
if err != nil {
return KeywordRecFile{}, err
}
dbPath := filepath.Join(home, ".config", "falko", "foltia_keyword.sqlite3")
db, err := gorm.Open("sqlite3", dbPath)
defer db.Close()
if err != nil {
return KeywordRecFile{}, err
}
var krf KeywordRecFile
db.First(&krf, id)
return krf, nil
}