forked from kabukky/journey
-
Notifications
You must be signed in to change notification settings - Fork 0
/
insertion.go
137 lines (127 loc) · 4.6 KB
/
insertion.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
package database
import (
"database/sql"
"github.com/twinj/uuid"
"time"
)
const stmtInsertPost = "INSERT INTO posts (id, uuid, title, slug, markdown, html, featured, page, status, image, author_id, created_at, created_by, updated_at, updated_by, published_at, published_by) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
const stmtInsertUser = "INSERT INTO users (id, uuid, name, slug, password, email, image, cover, created_at, created_by, updated_at, updated_by) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
const stmtInsertRoleUser = "INSERT INTO roles_users (id, role_id, user_id) VALUES (?, ?, ?)"
const stmtInsertTag = "INSERT INTO tags (id, uuid, name, slug, created_at, created_by, updated_at, updated_by) VALUES (?, ?, ?, ?, ?, ?, ?, ?)"
const stmtInsertPostTag = "INSERT INTO posts_tags (id, post_id, tag_id) VALUES (?, ?, ?)"
const stmtInsertSetting = "INSERT INTO settings (id, uuid, key, value, type, created_at, created_by, updated_at, updated_by) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)"
func InsertPost(title []byte, slug string, markdown []byte, html []byte, featured bool, isPage bool, published bool, image []byte, created_at time.Time, created_by int64) (int64, error) {
status := "draft"
if published {
status = "published"
}
writeDB, err := readDB.Begin()
if err != nil {
writeDB.Rollback()
return 0, err
}
var result sql.Result
if published {
result, err = writeDB.Exec(stmtInsertPost, nil, uuid.Formatter(uuid.NewV4(), uuid.CleanHyphen), title, slug, markdown, html, featured, isPage, status, image, created_by, created_at, created_by, created_at, created_by, created_at, created_by)
} else {
result, err = writeDB.Exec(stmtInsertPost, nil, uuid.Formatter(uuid.NewV4(), uuid.CleanHyphen), title, slug, markdown, html, featured, isPage, status, image, created_by, created_at, created_by, created_at, created_by, nil, nil)
}
if err != nil {
writeDB.Rollback()
return 0, err
}
postId, err := result.LastInsertId()
if err != nil {
writeDB.Rollback()
return 0, err
}
return postId, writeDB.Commit()
}
func InsertUser(name []byte, slug string, password string, email []byte, image []byte, cover []byte, created_at time.Time, created_by int64) (int64, error) {
writeDB, err := readDB.Begin()
if err != nil {
writeDB.Rollback()
return 0, err
}
result, err := writeDB.Exec(stmtInsertUser, nil, uuid.Formatter(uuid.NewV4(), uuid.CleanHyphen), name, slug, password, email, image, cover, created_at, created_by, created_at, created_by)
if err != nil {
writeDB.Rollback()
return 0, err
}
userId, err := result.LastInsertId()
if err != nil {
writeDB.Rollback()
return 0, err
}
return userId, writeDB.Commit()
}
func InsertRoleUser(role_id int, user_id int64) error {
writeDB, err := readDB.Begin()
if err != nil {
writeDB.Rollback()
return err
}
_, err = writeDB.Exec(stmtInsertRoleUser, nil, role_id, user_id)
if err != nil {
writeDB.Rollback()
return err
}
return writeDB.Commit()
}
func InsertTag(name []byte, slug string, created_at time.Time, created_by int64) (int64, error) {
writeDB, err := readDB.Begin()
if err != nil {
writeDB.Rollback()
return 0, err
}
result, err := writeDB.Exec(stmtInsertTag, nil, uuid.Formatter(uuid.NewV4(), uuid.CleanHyphen), name, slug, created_at, created_by, created_at, created_by)
if err != nil {
writeDB.Rollback()
return 0, err
}
tagId, err := result.LastInsertId()
if err != nil {
writeDB.Rollback()
return 0, err
}
return tagId, writeDB.Commit()
}
func InsertPostTag(post_id int64, tag_id int64) error {
writeDB, err := readDB.Begin()
if err != nil {
writeDB.Rollback()
return err
}
_, err = writeDB.Exec(stmtInsertPostTag, nil, post_id, tag_id)
if err != nil {
writeDB.Rollback()
return err
}
return writeDB.Commit()
}
func insertSettingString(key string, value string, setting_type string, created_at time.Time, created_by int64) error {
writeDB, err := readDB.Begin()
if err != nil {
writeDB.Rollback()
return err
}
_, err = writeDB.Exec(stmtInsertSetting, nil, uuid.Formatter(uuid.NewV4(), uuid.CleanHyphen), key, value, setting_type, created_at, created_by, created_at, created_by)
if err != nil {
writeDB.Rollback()
return err
}
return writeDB.Commit()
}
func insertSettingInt64(key string, value int64, setting_type string, created_at time.Time, created_by int64) error {
writeDB, err := readDB.Begin()
if err != nil {
writeDB.Rollback()
return err
}
_, err = writeDB.Exec(stmtInsertSetting, nil, uuid.Formatter(uuid.NewV4(), uuid.CleanHyphen), key, value, setting_type, created_at, created_by, created_at, created_by)
if err != nil {
writeDB.Rollback()
return err
}
return writeDB.Commit()
}