forked from kabukky/journey
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.go
125 lines (118 loc) · 4.01 KB
/
update.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
package database
import (
"time"
)
const stmtUpdatePost = "UPDATE posts SET title = ?, slug = ?, markdown = ?, html = ?, featured = ?, page = ?, status = ?, image = ?, updated_at = ?, updated_by = ? WHERE id = ?"
const stmtUpdatePostPublished = "UPDATE posts SET title = ?, slug = ?, markdown = ?, html = ?, featured = ?, page = ?, status = ?, image = ?, updated_at = ?, updated_by = ?, published_at = ?, published_by = ? WHERE id = ?"
const stmtUpdateSettings = "UPDATE settings SET value = ?, updated_at = ?, updated_by = ? WHERE key = ?"
const stmtUpdateUser = "UPDATE users SET email = ?, image = ?, cover = ?, bio = ?, website = ?, location = ?, updated_at = ?, updated_by = ? WHERE id = ?"
const stmtUpdateUserPassword = "UPDATE users SET password = ?, updated_at = ?, updated_by = ? WHERE id = ?"
func UpdatePost(id int64, title []byte, slug string, markdown []byte, html []byte, featured bool, isPage bool, published bool, image []byte, updated_at time.Time, updated_by int64) error {
currentPost, err := RetrievePostById(id)
if err != nil {
return err
}
status := "draft"
if published {
status = "published"
}
writeDB, err := readDB.Begin()
if err != nil {
writeDB.Rollback()
return err
}
// If the updated post is published for the first time, add publication date and user
if published && !currentPost.IsPublished {
_, err = writeDB.Exec(stmtUpdatePostPublished, title, slug, markdown, html, featured, isPage, status, image, updated_at, updated_by, updated_at, updated_by, id)
} else {
_, err = writeDB.Exec(stmtUpdatePost, title, slug, markdown, html, featured, isPage, status, image, updated_at, updated_by, id)
}
if err != nil {
writeDB.Rollback()
return err
}
return writeDB.Commit()
}
func UpdateSettings(title []byte, description []byte, logo []byte, cover []byte, postsPerPage int64, activeTheme string, updated_at time.Time, updated_by int64) error {
writeDB, err := readDB.Begin()
if err != nil {
writeDB.Rollback()
return err
}
// Title
_, err = writeDB.Exec(stmtUpdateSettings, title, updated_at, updated_by, "title")
if err != nil {
writeDB.Rollback()
return err
}
// Description
_, err = writeDB.Exec(stmtUpdateSettings, description, updated_at, updated_by, "description")
if err != nil {
writeDB.Rollback()
return err
}
// Logo
_, err = writeDB.Exec(stmtUpdateSettings, logo, updated_at, updated_by, "logo")
if err != nil {
writeDB.Rollback()
return err
}
// Cover
_, err = writeDB.Exec(stmtUpdateSettings, cover, updated_at, updated_by, "cover")
if err != nil {
writeDB.Rollback()
return err
}
// PostsPerPage
_, err = writeDB.Exec(stmtUpdateSettings, postsPerPage, updated_at, updated_by, "postsPerPage")
if err != nil {
writeDB.Rollback()
return err
}
// ActiveTheme
_, err = writeDB.Exec(stmtUpdateSettings, activeTheme, updated_at, updated_by, "activeTheme")
if err != nil {
writeDB.Rollback()
return err
}
return writeDB.Commit()
}
func UpdateActiveTheme(activeTheme string, updated_at time.Time, updated_by int64) error {
writeDB, err := readDB.Begin()
if err != nil {
writeDB.Rollback()
return err
}
_, err = writeDB.Exec(stmtUpdateSettings, activeTheme, updated_at, updated_by, "activeTheme")
if err != nil {
writeDB.Rollback()
return err
}
return writeDB.Commit()
}
func UpdateUser(id int64, email []byte, image []byte, cover []byte, bio []byte, website []byte, location []byte, updated_at time.Time, updated_by int64) error {
writeDB, err := readDB.Begin()
if err != nil {
writeDB.Rollback()
return err
}
_, err = writeDB.Exec(stmtUpdateUser, email, image, cover, bio, website, location, updated_at, updated_by, id)
if err != nil {
writeDB.Rollback()
return err
}
return writeDB.Commit()
}
func UpdateUserPassword(id int64, password string, updated_at time.Time, updated_by int64) error {
writeDB, err := readDB.Begin()
if err != nil {
writeDB.Rollback()
return err
}
_, err = writeDB.Exec(stmtUpdateUserPassword, password, updated_at, updated_by, id)
if err != nil {
writeDB.Rollback()
return err
}
return writeDB.Commit()
}