forked from kabukky/journey
-
Notifications
You must be signed in to change notification settings - Fork 0
/
post.go
76 lines (73 loc) · 1.77 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
package methods
import (
"github.com/kabukky/journey/database"
"github.com/kabukky/journey/structure"
"time"
)
func SavePost(p *structure.Post) error {
tagIds := make([]int64, 0)
// Insert tags
for _, tag := range p.Tags {
// Tag slug might already be in database
tagId, err := database.RetrieveTagIdBySlug(tag.Slug)
if err != nil {
// Tag is probably not in database yet
tagId, err = database.InsertTag(tag.Name, tag.Slug, time.Now(), p.Author.Id)
if err != nil {
return err
}
}
if tagId != 0 {
tagIds = append(tagIds, tagId)
}
}
// Insert post
postId, err := database.InsertPost(p.Title, p.Slug, p.Markdown, p.Html, p.IsFeatured, p.IsPage, p.IsPublished, p.Image, *p.Date, p.Author.Id)
if err != nil {
return err
}
// Insert postTags
for _, tagId := range tagIds {
err = database.InsertPostTag(postId, tagId)
if err != nil {
return err
}
}
return nil
}
func UpdatePost(p *structure.Post) error {
tagIds := make([]int64, 0)
// Insert tags
for _, tag := range p.Tags {
// Tag slug might already be in database
tagId, err := database.RetrieveTagIdBySlug(tag.Slug)
if err != nil {
// Tag is probably not in database yet
tagId, err = database.InsertTag(tag.Name, tag.Slug, time.Now(), p.Author.Id)
if err != nil {
return err
}
}
if tagId != 0 {
tagIds = append(tagIds, tagId)
}
}
// Update post
err := database.UpdatePost(p.Id, p.Title, p.Slug, p.Markdown, p.Html, p.IsFeatured, p.IsPage, p.IsPublished, p.Image, *p.Date, p.Author.Id)
if err != nil {
return err
}
// Delete old postTags
err = database.DeletePostTagsForPostId(p.Id)
// Insert postTags
if err != nil {
return err
}
for _, tagId := range tagIds {
err = database.InsertPostTag(p.Id, tagId)
if err != nil {
return err
}
}
return nil
}