-
Notifications
You must be signed in to change notification settings - Fork 0
/
posts.go
195 lines (175 loc) · 4.2 KB
/
posts.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package db
import (
"errors"
"github.com/lib/pq"
"gorm.io/gorm"
)
type Post struct {
gorm.Model
ProductID uint `json:"productID"`
Product Product `json:"product"`
User User `json:"user"`
UserID uint `json:"userID"`
Title string `json:"title"`
Description string `json:"description"`
Likes int64 `json:"likes" gorm:"default:0"`
Dislikes int64 `json:"dislikes" gorm:"default:0"`
UserLikes []User `json:"userLikes" gorm:"many2many:liked_posts;"`
UserDislikes []User `json:"userDislikes" gorm:"many2many:disliked_posts;"`
Comments []Comment `json:"comments"`
CommentsCount int64 `json:"commentsCount" gorm:"default:0"`
Type pq.StringArray `json:"type" gorm:"type:text[]"`
Images pq.StringArray `json:"images" gorm:"type:text[]"`
}
func ValidType(t string) bool {
switch t {
case "suggestions", "bugs", "announcements", "done", "working-on", "under-review", "changelogs":
return true
}
return false
}
func checkTypes(t []string) bool {
for _, s := range t {
if !ValidType(s) {
return false
}
}
return true
}
func (p *Post) BeforeCreate(db *gorm.DB) error {
if !checkTypes(p.Type) {
return errors.New("invalid type")
}
return nil
}
func (p *Post) AddType(t string) error {
if !ValidType(t) {
return errors.New("invalid type")
}
p.Type = append(p.Type, t)
DB.Save(&p)
return nil
}
func (p *Post) RemoveType(t string) error {
if !ValidType(t) {
return errors.New("invalid type")
}
var newTypes pq.StringArray
for _, field := range p.Type {
if field != t {
newTypes = append(newTypes, field)
}
}
p.Type = newTypes
DB.Save(&p)
return nil
}
type Comment struct {
gorm.Model
PostID uint `json:"postID"`
User User `json:"user"`
UserID uint `json:"userID"`
Comment string `json:"comment"`
}
type Poster interface {
GetID() uint
Update(title, description string)
AddComment(comment string, userID uint)
}
type IDGetter interface {
GetID() uint
}
type PostLiker interface {
Like(user User) error
Dislike(user User) error
RemoveDislike(user User) error
RemoveLike(user User) error
}
func (p *Post) AddComment(comment string, userID uint) {
var User User
DB.First(&User, userID)
p.Comments = append(p.Comments, Comment{
Comment: comment,
User: User,
UserID: userID,
PostID: p.ID,
})
DB.Save(&p)
}
type AdminPost interface {
Update(title, description, version string)
GetID() uint
}
type DeletePost interface {
Delete()
GetID() uint
}
func (p *Post) Update(newTitle, newDescription string) {
if newTitle != "" {
p.Title = newTitle
}
if newDescription != "" {
p.Description = newDescription
}
DB.Save(&p)
}
func (p *Post) Delete() {
DB.Delete(&p)
}
func (p *Post) GetID() uint {
return p.ID
}
func (c *Comment) BeforeCreate(tx *gorm.DB) error {
var post Post
err := DB.First(&post, c.PostID).Error
if errors.Is(err, gorm.ErrRecordNotFound) {
return errors.New("post not found")
}
post.CommentsCount++
DB.Save(&post)
return nil
}
func (p *Post) Like(user User) error {
err := p.RemoveDislike(user)
if err != nil {
return err
}
err = DB.Model(&p).Association("UserLikes").Append(&user)
DB.Table("liked_posts").Where("post_id = ? AND user_id = ?", p.ID, user.ID).Count(&p.Likes)
DB.Save(&p)
if err != nil {
return err
}
return nil
}
func (p *Post) Dislike(user User) error {
err := p.RemoveLike(user)
if err != nil {
return err
}
err = DB.Model(&p).Association("UserDislikes").Append(&user)
DB.Table("disliked_posts").Where("post_id = ? AND user_id = ?", p.ID, user.ID).Count(&p.Dislikes)
DB.Save(&p)
if err != nil {
return err
}
return nil
}
func (p *Post) RemoveLike(user User) error {
err := DB.Model(&p).Association("UserLikes").Delete(&user)
DB.Table("liked_posts").Where("post_id = ? AND user_id = ?", p.ID, user.ID).Count(&p.Likes)
DB.Save(&p)
if err != nil {
return err
}
return nil
}
func (p *Post) RemoveDislike(user User) error {
err := DB.Model(&p).Association("UserDislikes").Delete(&user)
DB.Table("disliked_posts").Where("post_id = ? AND user_id = ?", p.ID, user.ID).Count(&p.Dislikes)
DB.Save(&p)
if err != nil {
return err
}
return nil
}