-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPost.go
127 lines (113 loc) · 2.99 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
package models
import (
"errors"
"html"
"strings"
"time"
"github.com/jinzhu/gorm"
)
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("Required Title")
}
if p.Content == "" {
return errors.New("Required Content")
}
if p.AuthorID < 1 {
return errors.New("Required Author")
}
return nil
}
func (p *Post) SavePost(db *gorm.DB) (*Post, error) {
var err error
err = db.Debug().Model(&Post{}).Create(&p).Error
if err != nil {
return &Post{}, err
}
if p.ID != 0 {
err = db.Debug().Model(&User{}).Where("id = ?", p.AuthorID).Take(&p.Author).Error
if err != nil {
return &Post{}, err
}
}
return p, nil
}
func (p *Post) FindAllPosts(db *gorm.DB) (*[]Post, error) {
var err error
posts := []Post{}
err = db.Debug().Model(&Post{}).Limit(100).Find(&posts).Error
if err != nil {
return &[]Post{}, err
}
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 {
return &[]Post{}, err
}
}
}
return &posts, nil
}
func (p *Post) FindPostByID(db *gorm.DB, pid uint64) (*Post, error) {
var err error
err = db.Debug().Model(&Post{}).Where("id = ?", pid).Take(&p).Error
if err != nil {
return &Post{}, err
}
if p.ID != 0 {
err = db.Debug().Model(&User{}).Where("id = ?", p.AuthorID).Take(&p.Author).Error
if err != nil {
return &Post{}, err
}
}
return p, nil
}
func (p *Post) UpdateAPost(db *gorm.DB, pid uint64) (*Post, error) {
var err error
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 {
return &Post{}, err
}
if p.ID != 0 {
err = db.Debug().Model(&User{}).Where("id = ?", p.AuthorID).Take(&p.Author).Error
if err != nil {
return &Post{}, err
}
}
return p, nil
}
func (p *Post) DeleteAPost(db *gorm.DB, pid uint64, uid uint32) (int64, error) {
db = db.Debug().Model(&Post{}).Where("id = ? and author_id = ?", pid, uid).Take(&Post{}).Delete(&Post{})
if db.Error != nil {
if gorm.IsRecordNotFoundError(db.Error) {
return 0, errors.New("Post not found")
}
return 0, db.Error
}
return db.RowsAffected, nil
}