-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.go
149 lines (126 loc) · 3.3 KB
/
data.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
package rtfblog
import (
"fmt"
"html/template"
"strings"
)
const (
MaxFileSize = 50 * 1024 * 1024 // bytes
PostsPerPage = 5
NumFeedItems = 3
NumRecentPosts = 10
)
type Tag struct {
ID int64
Name string `gorm:"column:tag"`
}
type TagMap struct {
ID int64
TagID int64 `gorm:"column:tag_id"`
EntryID int64 `gorm:"column:post_id"`
}
type Author struct {
ID int64
UserName string `gorm:"column:disp_name"`
Passwd string `gorm:"column:passwd"`
FullName string `gorm:"column:full_name"`
Email string `gorm:"column:email"`
Www string `gorm:"column:www"`
}
// Commenter and Comment tables have been split up a bit to avoid a couple of
// problems:
// 1. If Comment contains Commenter substruct, gorm complains about duplicate
// 'id' columns and rightfully so. Thus, Commenter's ID is moved to
// CommenterTable.
// 2. If Comment contains Commenter and I try to insert it into a table, gorm
// tries to map Commenter's fields to 'comment' table and fails. Thus,
// CommentTable contains only the fields that map to 'comment' table.
type Commenter struct {
Name string
Email string
EmailHash string `sql:"-"`
Website string `gorm:"column:www"`
IP string `gorm:"column:ip"`
}
type CommenterTable struct {
ID int64
Commenter
}
func (t CommenterTable) TableName() string {
return "commenter"
}
type CommentTable struct {
CommenterID int64 `gorm:"column:commenter_id"`
PostID int64 `gorm:"column:post_id"`
Body template.HTML `sql:"-"`
RawBody string `gorm:"column:body"`
Time string `sql:"-"`
Timestamp int64 `gorm:"column:timestamp"`
CommentID int64 `gorm:"column:id; primary_key:yes"`
}
func (t CommentTable) TableName() string {
return "comment"
}
type Comment struct {
Commenter
CommentTable
}
type CommentWithPostTitle struct {
Comment
EntryLink
}
// Note: URL mapping is required, gorm won't be able to map to all-caps URL
// without it. Others are only for consistency.
type EntryLink struct {
Title string `gorm:"column:title"`
URL string `gorm:"column:url"`
Hidden bool `gorm:"column:hidden"`
}
type EntryTable struct {
EntryLink
ID int64
AuthorID int64 `gorm:"column:author_id"`
Date string `sql:"-"`
UnixDate int64 `gorm:"column:date"`
Body template.HTML `sql:"-"`
RawBody string `gorm:"column:body"`
}
func (e EntryTable) TableName() string {
return "post"
}
type Entry struct {
EntryTable
Author string `gorm:"column:disp_name"`
Tags []*Tag `sql:"-"`
Comments []*Comment `sql:"-"`
}
func (e Entry) HasTags() bool {
return len(e.Tags) > 0
}
func (e Entry) HasComments() bool {
return len(e.Comments) > 0
}
func (e Entry) NumCommentsStr() string {
return L10n("{{.Count}} comments", len(e.Comments))
}
func (e Entry) TagsStr() template.HTML {
var parts []string
for _, t := range e.Tags {
format := `<a href="/tag/%s">%s</a>`
url := t.Name
title := capitalize(t.Name)
part := fmt.Sprintf(format, url, title)
parts = append(parts, part)
}
return template.HTML(strings.Join(parts, ", "))
}
func (e Entry) TagsList() string {
var parts []string
for _, t := range e.Tags {
parts = append(parts, t.Name)
}
return strings.Join(parts, ", ")
}
func (t TagMap) TableName() string {
return "tagmap"
}