-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodels.go
169 lines (147 loc) · 5.5 KB
/
models.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
161
162
163
164
165
166
167
168
169
// oerc, alias oer-collector
// Copyright (C) 2021-2024 emschu[aet]mailbox.org
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public
// License along with this program.
// If not, see <https://www.gnu.org/licenses/>.
package main
// database struct(ure) models
import (
"log"
"strings"
"time"
)
const materializedStatusView = `SELECT min(program_entries.start_date_time) AS data_start_time,
max(program_entries.end_date_time) AS data_end_time,
count(*) as program_entry_count,
(SELECT count(*) from channel_families) as channel_family_count,
(SELECT count(*) from channels) as channel_count,
(SELECT count(*) from image_links) as image_link_count,
(SELECT count(*) from tv_shows) as tv_show_count,
(SELECT count(*) from log_entries) as log_count,
(SELECT count(*) from recommendations) as recommendation_count,
now() as created_at
FROM program_entries`
// BaseModel base model for all entities
type BaseModel struct {
ID uint `gorm:"primary_key" json:"id"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
// ChannelFamily entity
type ChannelFamily struct {
ID uint `gorm:"primary_key" json:"id"`
Title string `gorm:"size:32" json:"title"`
}
// ManagedRecord base entity part, meta-struct used by several other db related structs
type ManagedRecord struct {
Title string `gorm:"size:500" json:"title"`
URL string `gorm:"size:1500" json:"url"`
Hash string `gorm:"type:varchar(32);unique_index;not null" json:"hash"`
TechnicalID string `gorm:"index;type:varchar(64);null" json:"technical_id"`
Homepage string `gorm:"size:1500" json:"homepage"`
ChannelFamily ChannelFamily `json:"-"`
ChannelFamilyID uint `gorm:"index" json:"channel_family_id"`
}
// Channel entity
type Channel struct {
BaseModel
ManagedRecord
IsDeprecated bool `gorm:"default:false;not null" json:"is_deprecated"`
}
// TvShow entity
type TvShow struct {
BaseModel
ManagedRecord
RelatedProgramEntries []ProgramEntry `gorm:"many2many:tv_show_program_entries" json:"-"`
}
// ProgramEntry entity
type ProgramEntry struct {
BaseModel
ManagedRecord
StartDateTime *time.Time `gorm:"index;not null" json:"start_date_time"`
EndDateTime *time.Time `gorm:"index;not null" json:"end_date_time"`
LastCheck *time.Time `json:"last_check"`
DurationMinutes int16 `json:"duration_in_minutes"`
Description string `gorm:"size:30000" json:"description"`
Channel Channel `json:"-"`
ChannelID uint `gorm:"index;not null" json:"channel_id"`
Tags string `gorm:"size:256" json:"tags"`
IsDeprecated bool `gorm:"default:false;not null" json:"is_deprecated"`
ImageLinks []ImageLink `gorm:"foreignKey:ProgramEntryID" json:"image_links"`
CollisionEntries []ProgramEntry `gorm:"many2many:collision_entries;null" json:"collision_entries"`
LastCollisionCheck *time.Time `gorm:"nullable" json:"-"`
}
// ImageLink entity
type ImageLink struct {
BaseModel
URL string `gorm:"size:2048" json:"url"`
ProgramEntryID uint `gorm:"index" json:"program_entry_id"`
}
// LogEntry used to store parsing errors
type LogEntry struct {
BaseModel
Message string `gorm:"size:1536" json:"message"`
}
// Settings used to store values
type Settings struct {
ID uint `gorm:"primary_key" json:"id"`
SettingKey string `gorm:"index,type:varchar(64)" json:"key"`
Value string `gorm:"type:varchar(1024)" json:"value"`
}
// Recommendation store program recommendations
type Recommendation struct {
ID uint `gorm:"primary_key" json:"id"`
CreatedAt *time.Time `json:"created_at"`
ProgramEntry ProgramEntry `json:"program_entry"`
ProgramEntryID uint `gorm:"unique_index;not null" json:"program_entry_id"`
ChannelID uint `gorm:"index;not null" json:"channel_id"`
StartDateTime *time.Time `gorm:"index;not null" json:"start_date_time"`
Keywords string `gorm:"size:1024" json:"keywords"`
}
func (p *ProgramEntry) doesImageLinkExist(url string) bool {
for _, entryDetails := range p.ImageLinks {
if entryDetails.URL == url {
return true
}
}
return false
}
func (p *ProgramEntry) considerImageLinkExists(url string) {
for _, entryDetails := range p.ImageLinks {
if entryDetails.URL == url {
return
}
}
p.ImageLinks = append(p.ImageLinks, ImageLink{URL: url})
return
}
// considerTagExists: adds a tag to the program entry.
func (p *ProgramEntry) considerTagExists(mainTagName *string) {
var existingTags []string
if len(p.Tags) > 0 {
existingTags = strings.Split(p.Tags, ";")
} else {
existingTags = []string{}
}
for _, tag := range existingTags {
if tag == *mainTagName {
return
}
}
existingTags = append(existingTags, trimAndSanitizeString(*mainTagName))
if verboseGlobal {
log.Printf("Save new tag '%s' to program entry #%d\n", *mainTagName, p.ID)
}
p.Tags = strings.Join(existingTags, ";")
}