This repository has been archived by the owner on Jun 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
post.go
151 lines (110 loc) · 2.56 KB
/
post.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
package main
import (
"errors"
"github.com/jinzhu/gorm"
)
type Post struct {
gorm.Model
UserID uint // used to find the original poster
Title string `validate:"required" gorm:"unique"`
Description string `validate:"required"`
Content string `validate:"required"`
Public bool
ImgPath string // not required
}
// Creates a new post
func NewPost(p *Post) (err error) {
err = p.Validate()
if err != nil {
return
}
return db.Create(p).Error
}
// Returns a post using the title as key
func GetPost(title string) *Post {
p := &Post{}
db.Where("title = ?", title).First(p)
if *p == (Post{}) {
return nil
}
return p
}
func GetPostById(id uint) *Post {
p := &Post{}
db.Where("id = ?", id).First(p)
if *p == (Post{}) {
return nil
}
return p
}
// Returns a number of posts, ordered by date of creation
func GetPosts(number int) (ps []*Post) {
order := "created_at DESC"
if number > 0 {
db.Order(order).Limit(number).Find(&ps)
} else {
db.Order(order).Find(&ps)
}
return
}
func GetPostsCount(number int) (count int) {
order := "created_at DESC"
if number > 0 {
db.Model(&Post{}).Order(order).Limit(number).Count(&count)
} else {
db.Model(&Post{}).Order(order).Count(&count)
}
return
}
func GetPublicPosts(number int) (ps []*Post) {
order := "created_at DESC"
if number > 0 {
db.Order(order).Limit(number).Where("public = ?", true).Find(&ps)
} else {
db.Order(order).Where("public = ?", true).Find(&ps)
}
return
}
func GetPublicPostsCount(number int) int64 {
var count int64
order := "created_at DESC"
if number > 0 {
db.Model(&Post{}).Order(order).Limit(number).Where("public = ?", true).Count(&count)
} else {
db.Model(&Post{}).Order(order).Where("public = ?", true).Count(&count)
}
return count
}
func GetPostsByUserId(uid uint) (ps []*Post) {
order := "created_at DESC"
db.Order(order).Where("user_id = ?", uid).Find(&ps)
return
}
func PostSearch(field string) (ps []*Post) {
field = "%" + field + "%"
db.Where("public = ? AND title LIKE ? OR description LIKE ?", true, field, field).Find(&ps)
return
}
func (p *Post) GetOP() *User {
u := &User{}
db.Model(p).Related(&u, "UserID")
return u
}
// Deletes a post
func (p *Post) Delete() error {
return db.Unscoped().Delete(p).Error
}
// Updates a post
func (p *Post) Update() error {
if err := p.Validate(); err != nil {
return err
}
return db.Save(p).Error
}
// Validates the post's structure
func (p *Post) Validate() error {
if p.GetOP() == nil {
return errors.New("Original poster doesn't exist")
}
return validate.Struct(p)
}