-
Notifications
You must be signed in to change notification settings - Fork 174
/
Copy pathPost.go
176 lines (162 loc) · 3.8 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package models
import (
"errors"
"html"
"strings"
"time"
"github.com/jinzhu/gorm"
"github.com/victorsteven/fullstack/api/utils/channels"
)
type Post struct {
ID uint64 `gorm:"primary_key;auto_increment" json:"id"`
Title string `gorm:"size:255;not null;unique" json:"title"`
Content string `gorm:"size:255;not null;" json:"content"`
Author User `json:"author"`
AuthorID uint32 `gorm:"not null" json:"author_id"`
CreatedAt time.Time `gorm:"default:current_timestamp()" json:"created_at"`
UpdatedAt time.Time `gorm:"default:current_timestamp()" json:"updated_at"`
}
func (p *Post) Prepare() {
p.ID = 0
p.Title = html.EscapeString(strings.TrimSpace(p.Title))
p.Content = html.EscapeString(strings.TrimSpace(p.Content))
p.Author = User{}
p.CreatedAt = time.Now()
p.UpdatedAt = time.Now()
}
func (p *Post) Validate() error {
if p.Title == "" {
return errors.New("Title required")
}
if p.Content == "" {
return errors.New("Content required")
}
if p.AuthorID < 1 {
return errors.New("Author required")
}
return nil
}
func (p *Post) SavePost(db *gorm.DB) (*Post, error) {
var err error
done := make(chan bool)
go func(ch chan<- bool) {
defer close(ch)
err = db.Debug().Model(&Post{}).Create(&p).Error
if err != nil {
ch <- false
return
}
if p.ID != 0 {
err = db.Debug().Model(&User{}).Where("id = ?", p.AuthorID).Take(&p.Author).Error
if err != nil {
ch <- false
return
}
}
ch <- true
}(done)
if channels.OK(done) {
return p, nil
}
return &Post{}, err
}
func (p *Post) FindAllPosts(db *gorm.DB) ([]Post, error) {
var err error
posts := []Post{}
done := make(chan bool)
go func(ch chan<- bool) {
defer close(ch)
err = db.Debug().Model(&Post{}).Limit(100).Find(&posts).Error
if err != nil {
ch <- false
return
}
if len(posts) > 0 {
for i, _ := range posts {
err := db.Debug().Model(&User{}).Where("id = ?", posts[i].AuthorID).Take(&posts[i].Author).Error
if err != nil {
ch <- false
return
}
}
}
ch <- true
}(done)
if channels.OK(done) {
return posts, nil
}
return nil, err
}
func (p *Post) FindPostByID(db *gorm.DB, pid uint64) (*Post, error) {
var err error
done := make(chan bool)
go func(ch chan<- bool) {
// defer close(ch)
err = db.Debug().Model(&Post{}).Where("id = ?", pid).Take(&p).Error
if err != nil {
ch <- false
return
}
if p.ID != 0 {
err = db.Debug().Model(&User{}).Where("id = ?", p.AuthorID).Take(&p.Author).Error
if err != nil {
ch <- false
return
}
}
ch <- true
}(done)
if channels.OK(done) {
return p, nil
}
return &Post{}, err
}
func (p *Post) UpdateAPost(db *gorm.DB, pid uint64) (*Post, error) {
var err error
done := make(chan bool)
go func(ch chan<- bool) {
defer close(ch)
db = db.Debug().Model(&Post{}).Where("id = ?", pid).Take(&Post{}).UpdateColumns(
map[string]interface{}{
"title": p.Title,
"content": p.Content,
"updated_at": time.Now(),
},
)
err = db.Debug().Model(&Post{}).Where("id = ?", pid).Take(&p).Error
if err != nil {
ch <- false
return
}
if p.ID != 0 {
err = db.Debug().Model(&User{}).Where("id = ?", p.AuthorID).Take(&p.Author).Error
if err != nil {
ch <- false
return
}
}
ch <- true
}(done)
if channels.OK(done) {
return p, nil
}
return &Post{}, err
}
func (p *Post) DeleteAPost(db *gorm.DB, pid uint64, uid uint32) (int64, error) {
done := make(chan bool)
go func(ch chan<- bool) {
defer close(ch)
db = db.Debug().Model(&Post{}).Where("id = ? and author_id = ?", pid, uid).Take(&Post{}).Delete(&Post{})
ch <- true
}(done)
if channels.OK(done) {
if db.Error != nil {
if gorm.IsRecordNotFoundError(db.Error) {
return 0, errors.New("Post not found")
}
return 0, db.Error
}
return db.RowsAffected, nil
}
return 0, db.Error
}